From cc01dd1e49f5f85bb4c6d7a3bc4aa7bba0590201 Mon Sep 17 00:00:00 2001 From: MeexReay Date: Mon, 21 Apr 2025 01:14:13 +0300 Subject: [PATCH] close libnotify notifications on active --- src/chat/gui.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/chat/gui.rs b/src/chat/gui.rs index 2e89c4e..05d2e67 100644 --- a/src/chat/gui.rs +++ b/src/chat/gui.rs @@ -1,4 +1,4 @@ -use std::{sync::{mpsc::{channel, Receiver}, Arc}, time::UNIX_EPOCH}; +use std::{sync::{mpsc::{channel, Receiver}, Arc, RwLock}, time::UNIX_EPOCH}; use std::cell::RefCell; use std::time::{Duration, SystemTime}; use std::thread; @@ -33,7 +33,9 @@ struct UiModel { chat_box: GtkBox, chat_scrolled: ScrolledWindow, app: Application, - window: ApplicationWindow + window: ApplicationWindow, + #[cfg(feature = "libnotify")] + notifications: Arc>> } thread_local!( @@ -595,7 +597,9 @@ fn build_ui(ctx: Arc, app: &Application) -> UiModel { chat_scrolled, chat_box, app: app.clone(), - window: window.clone() + window: window.clone(), + #[cfg(feature = "libnotify")] + notifications: Arc::new(RwLock::new(Vec::::new())) } } @@ -608,6 +612,19 @@ fn setup(app: &Application, ctx: Arc, ui: UiModel) { let (tx, rx) = channel(); + #[cfg(feature = "libnotify")] + ui.window.connect_notify(Some("is-active"), move |a, _| { + if a.is_active() { + GLOBAL.with(|global| { + if let Some((ui, _)) = &*global.borrow() { + for i in ui.notifications.read().unwrap().clone() { + i.close().expect("libnotify close error"); + } + } + }); + } + }); + GLOBAL.with(|global| { *global.borrow_mut() = Some((ui, rx)); }); @@ -664,13 +681,15 @@ fn load_css() { } #[cfg(feature = "libnotify")] -fn send_notification(_: Arc, _: &UiModel, title: &str, message: &str) { +fn send_notification(_: Arc, ui: &UiModel, title: &str, message: &str) { use libnotify::Notification; let notification = Notification::new(title, message, None); notification.set_app_name("bRAC"); notification.set_image_from_pixbuf(&load_gdk_pixbuf(include_bytes!("images/icon.png"))); notification.show().expect("libnotify send error"); + + ui.notifications.write().unwrap().push(notification); } #[cfg(not(feature = "libnotify"))]