remove deprecated MainContext channel

This commit is contained in:
ranfdev
2024-02-01 14:48:15 +01:00
parent f790748e0a
commit 410569689c
5 changed files with 81 additions and 79 deletions

View File

@ -265,9 +265,12 @@ impl NotifyApplication {
// `Invalid client serial` and it's broken.
// Until https://github.com/flatpak/xdg-dbus-proxy/issues/46 is solved, I have to handle these things
// in the main thread. Uff.
let (tx, rx) = glib::MainContext::channel(Default::default());
let (s, r) = async_channel::unbounded::<models::Notification>();
let app = self.clone();
rx.attach(None, move |n: models::Notification| {
glib::MainContext::ref_thread_default().spawn_local(async move {
while let Ok(n) = r.recv().await {
let gio_notif = gio::Notification::new(&n.title);
gio_notif.set_body(Some(&n.body));
@ -288,15 +291,14 @@ impl NotifyApplication {
}
app.send_notification(None, &gio_notif);
glib::ControlFlow::Continue
}
});
struct Proxies {
notification: glib::Sender<models::Notification>,
notification: async_channel::Sender<models::Notification>,
}
impl models::NotificationProxy for Proxies {
fn send(&self, n: models::Notification) -> anyhow::Result<()> {
self.notification.send(n)?;
self.notification.send_blocking(n)?;
Ok(())
}
}
@ -317,7 +319,7 @@ impl NotifyApplication {
Box::pin(rx)
}
}
let proxies = std::sync::Arc::new(Proxies { notification: tx });
let proxies = std::sync::Arc::new(Proxies { notification: s });
ntfy_daemon::system_client::start(
socket_path.to_owned(),
dbpath.to_str().unwrap(),

View File

@ -1,28 +1,28 @@
use std::cell::Cell;
use std::rc::Rc;
use glib::Receiver;
use glib::SourceId;
use gtk::glib;
pub fn debounce_channel<T: 'static>(
duration: std::time::Duration,
source: Receiver<T>,
) -> Receiver<T> {
let (tx, rx) = glib::MainContext::channel(Default::default());
let scheduled = Rc::new(Cell::new(None::<SourceId>));
source.attach(None, move |data| {
if let Some(scheduled) = scheduled.take() {
#[derive(Clone)]
pub struct Debouncer {
scheduled: Rc<Cell<Option<SourceId>>>,
}
impl Debouncer {
pub fn new() -> Self {
Self {
scheduled: Default::default(),
}
}
pub fn call(&self, duration: std::time::Duration, f: impl Fn() -> () + 'static) {
if let Some(scheduled) = self.scheduled.take() {
scheduled.remove();
}
let tx = tx.clone();
let scheduled_clone = scheduled.clone();
let scheduled_clone = self.scheduled.clone();
let source_id = glib::source::timeout_add_local_once(duration, move || {
tx.send(data).unwrap();
f();
scheduled_clone.take();
});
scheduled.set(Some(source_id));
glib::ControlFlow::Continue
});
rx
self.scheduled.set(Some(source_id));
}
}

View File

@ -145,24 +145,29 @@ impl AddSubscriptionDialog {
},
}
let (tx, rx) = glib::MainContext::channel(Default::default());
let txc = tx.clone();
topic_entry.delegate().unwrap().connect_changed(move |_| {
txc.send(()).unwrap();
});
let txc = tx.clone();
server_entry.delegate().unwrap().connect_changed(move |_| {
txc.send(()).unwrap();
});
server_expander.connect_enable_expansion_notify(move |_| {
tx.send(()).unwrap();
});
let rx = crate::async_utils::debounce_channel(std::time::Duration::from_millis(500), rx);
let debounced_error_check = {
let db = crate::async_utils::Debouncer::new();
let objc = obj.clone();
rx.attach(None, move |_| {
objc.check_errors();
glib::ControlFlow::Continue
move || {
db.call(std::time::Duration::from_millis(500), move || {
objc.check_errors()
});
}
};
let f = debounced_error_check.clone();
topic_entry
.delegate()
.unwrap()
.connect_changed(move |_| f.clone()());
let f = debounced_error_check.clone();
server_entry
.delegate()
.unwrap()
.connect_changed(move |_| f.clone()());
let f = debounced_error_check.clone();
server_expander.connect_enable_expansion_notify(move |_| f.clone()());
imp.widgets.replace(Widgets {
topic_entry,
server_expander,

View File

@ -9,6 +9,8 @@ use gtk::{gdk, gio, glib};
use ntfy_daemon::models;
use tracing::error;
use crate::widgets::window::SpawnWithToast;
mod imp {
use super::*;
@ -161,10 +163,10 @@ impl MessageRow {
Ok(bytes)
}
fn build_image(&self, url: String) -> gtk::Picture {
let (tx, rx) = glib::MainContext::channel(Default::default());
let (s, r) = async_channel::unbounded();
gio::spawn_blocking(move || {
if let Err(e) =
Self::fetch_image_bytes(&url).map(|bytes| tx.send(glib::Bytes::from_owned(bytes)))
if let Err(e) = Self::fetch_image_bytes(&url)
.map(|bytes| s.send_blocking(glib::Bytes::from_owned(bytes)))
{
error!(error = %e)
}
@ -174,18 +176,15 @@ impl MessageRow {
picture.set_can_shrink(true);
picture.set_height_request(350);
let picturec = picture.clone();
rx.attach(Default::default(), move |b| {
self.spawn_with_near_toast(async move {
let b = r.recv().await?;
let stream = gio::MemoryInputStream::from_bytes(&b);
let pixbuf = match Pixbuf::from_stream(&stream, gio::Cancellable::NONE) {
Ok(res) => res,
Err(e) => {
error!(error = %e, "parsing image contents");
return glib::ControlFlow::Break;
}
};
let pixbuf = Pixbuf::from_stream(&stream, gio::Cancellable::NONE)?;
picturec.set_paintable(Some(&gdk::Texture::for_pixbuf(&pixbuf)));
glib::ControlFlow::Break
Ok::<(), anyhow::Error>(())
});
picture
}
fn build_action_btn(&self, action: models::Action) -> gtk::Button {

View File

@ -49,23 +49,19 @@ mod imp {
self.parent_constructed();
let this = self.obj().clone();
let (tx, rx) = glib::MainContext::channel(glib::Priority::default());
let rx =
crate::async_utils::debounce_channel(std::time::Duration::from_millis(500), rx);
rx.attach(None, move |entry| {
this.update_display_name(&entry);
glib::ControlFlow::Continue
});
let this = self.obj().clone();
self.display_name_entry
.set_text(&this.subscription().unwrap().display_name());
self.muted_switch_row
.set_active(this.subscription().unwrap().muted());
let debouncer = crate::async_utils::Debouncer::new();
self.display_name_entry.connect_changed({
move |entry| {
tx.send(entry.clone()).unwrap();
let entry = entry.clone();
let this = this.clone();
debouncer.call(std::time::Duration::from_millis(500), move || {
this.update_display_name(&entry);
})
}
});
let this = self.obj().clone();