From e55d5a7a55d3df8c031cef2629b9e3b27bb75230 Mon Sep 17 00:00:00 2001 From: MeexReay Date: Mon, 21 Apr 2025 20:01:31 +0300 Subject: [PATCH] notifications setting and now all gnotifications hide --- Makefile | 6 ++++-- src/chat/config.rs | 21 ++++++++++-------- src/chat/gui.rs | 54 +++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 0bdb9bb..ab5b857 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,8 @@ -.PHONY: clean build +.PHONY: clean build windows build build: build/windows-x86_64 build/linux-x86_64 +windows: build/windows-x86_64 +linux: build/linux-x86_64 build/windows-x86_64: mkdir -p build @@ -20,7 +22,7 @@ build/windows-x86_64: build/linux-x86_64: mkdir -p build mkdir -p $@ - cargo build -r -F libnotify --target x86_64-unknown-linux-gnu + cargo build -r --target x86_64-unknown-linux-gnu # patchbin target/x86_64-unknown-linux-gnu/release/bRAC cp target/x86_64-unknown-linux-gnu/release/bRAC $@ cp ru.themixray.bRAC.png $@ diff --git a/src/chat/config.rs b/src/chat/config.rs index ae6af99..6e338b1 100644 --- a/src/chat/config.rs +++ b/src/chat/config.rs @@ -27,6 +27,7 @@ pub struct Config { #[serde(default = "default_true")] pub formatting_enabled: bool, #[serde(default = "default_true")] pub commands_enabled: bool, #[serde(default)] pub proxy: Option, + #[serde(default = "default_true")] pub notifications_enabled: bool, } pub fn get_config_path() -> PathBuf { @@ -115,6 +116,7 @@ pub struct Args { #[arg(long)] pub chunked_enabled: Option, #[arg(long)] pub formatting_enabled: Option, #[arg(long)] pub commands_enabled: Option, + #[arg(long)] pub notifications_enabled: Option, #[arg(long)] pub proxy: Option, } @@ -124,14 +126,15 @@ impl Args { if let Some(v) = self.name.clone() { config.name = Some(v) } if let Some(v) = self.proxy.clone() { config.proxy = Some(v) } if let Some(v) = self.message_format.clone() { config.message_format = v } - if let Some(v) = self.update_time.clone() { config.update_time = v } - if let Some(v) = self.max_messages.clone() { config.max_messages = v } - if let Some(v) = self.hide_my_ip.clone() { config.hide_my_ip = v } - if let Some(v) = self.show_other_ip.clone() { config.show_other_ip = v } - if let Some(v) = self.auth_enabled.clone() { config.auth_enabled = v } - if let Some(v) = self.ssl_enabled.clone() { config.ssl_enabled = v } - if let Some(v) = self.chunked_enabled.clone() { config.chunked_enabled = v } - if let Some(v) = self.formatting_enabled.clone() { config.formatting_enabled = v } - if let Some(v) = self.commands_enabled.clone() { config.commands_enabled = v } + if let Some(v) = self.update_time { config.update_time = v } + if let Some(v) = self.max_messages { config.max_messages = v } + if let Some(v) = self.hide_my_ip { config.hide_my_ip = v } + if let Some(v) = self.show_other_ip { config.show_other_ip = v } + if let Some(v) = self.auth_enabled { config.auth_enabled = v } + if let Some(v) = self.ssl_enabled { config.ssl_enabled = v } + if let Some(v) = self.chunked_enabled { config.chunked_enabled = v } + if let Some(v) = self.formatting_enabled { config.formatting_enabled = v } + if let Some(v) = self.commands_enabled { config.commands_enabled = v } + if let Some(v) = self.notifications_enabled { config.notifications_enabled = v } } } \ No newline at end of file diff --git a/src/chat/gui.rs b/src/chat/gui.rs index 58bee08..e63f364 100644 --- a/src/chat/gui.rs +++ b/src/chat/gui.rs @@ -35,7 +35,9 @@ struct UiModel { app: Application, window: ApplicationWindow, #[cfg(feature = "libnotify")] - notifications: Arc>> + notifications: Arc>>, + #[cfg(not(feature = "libnotify"))] + notifications: Arc>> } thread_local!( @@ -166,6 +168,7 @@ fn open_settings(ctx: Arc, app: &Application) { let chunked_enabled_entry = gui_checkbox_setting!("Chunked Enabled", chunked_enabled, ctx, vbox); let formatting_enabled_entry = gui_checkbox_setting!("Formatting Enabled", formatting_enabled, ctx, vbox); let commands_enabled_entry = gui_checkbox_setting!("Commands Enabled", commands_enabled, ctx, vbox); + let notifications_enabled_entry = gui_checkbox_setting!("Notifications Enabled", notifications_enabled, ctx, vbox); let save_button = Button::builder() .label("Save") @@ -187,6 +190,7 @@ fn open_settings(ctx: Arc, app: &Application) { #[weak] chunked_enabled_entry, #[weak] formatting_enabled_entry, #[weak] commands_enabled_entry, + #[weak] notifications_enabled_entry, #[weak] proxy_entry, move |_| { let config = Config { @@ -230,6 +234,7 @@ fn open_settings(ctx: Arc, app: &Application) { chunked_enabled: chunked_enabled_entry.is_active(), formatting_enabled: formatting_enabled_entry.is_active(), commands_enabled: commands_enabled_entry.is_active(), + notifications_enabled: notifications_enabled_entry.is_active(), proxy: { let proxy = proxy_entry.text().to_string(); @@ -265,6 +270,7 @@ fn open_settings(ctx: Arc, app: &Application) { #[weak] chunked_enabled_entry, #[weak] formatting_enabled_entry, #[weak] commands_enabled_entry, + #[weak] notifications_enabled_entry, #[weak] proxy_entry, move |_| { let config = Config::default(); @@ -283,6 +289,7 @@ fn open_settings(ctx: Arc, app: &Application) { chunked_enabled_entry.set_active(config.chunked_enabled); formatting_enabled_entry.set_active(config.formatting_enabled); commands_enabled_entry.set_active(config.commands_enabled); + notifications_enabled_entry.set_active(config.notifications_enabled); } )); @@ -295,6 +302,22 @@ fn open_settings(ctx: Arc, app: &Application) { .child(&vbox) .build(); + let controller = gtk::EventControllerKey::new(); + controller.connect_key_pressed({ + let window = window.clone(); + + move |_, key, _, _| { + if key == gtk::gdk::Key::Escape { + window.close(); + gtk::glib::Propagation::Proceed + } else { + gtk::glib::Propagation::Stop + } + } + }); + + window.add_controller(controller); + window.present(); } @@ -592,7 +615,9 @@ fn build_ui(ctx: Arc, app: &Application) -> UiModel { app: app.clone(), window: window.clone(), #[cfg(feature = "libnotify")] - notifications: Arc::new(RwLock::new(Vec::::new())) + notifications: Arc::new(RwLock::new(Vec::::new())), + #[cfg(not(feature = "libnotify"))] + notifications: Arc::new(RwLock::new(Vec::::new())), } } @@ -618,6 +643,19 @@ fn setup(_: &Application, ctx: Arc, ui: UiModel) { } }); + #[cfg(not(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() { + ui.app.withdraw_notification(&i); + } + } + }); + } + }); + GLOBAL.with(|global| { *global.borrow_mut() = Some((ui, rx)); }); @@ -690,11 +728,21 @@ fn send_notification(_: Arc, ui: &UiModel, title: &str, message: &str) #[cfg(not(feature = "libnotify"))] fn send_notification(_: Arc, ui: &UiModel, title: &str, message: &str) { + use std::{hash::{DefaultHasher, Hasher}, time::UNIX_EPOCH}; + use gtk4::gio::Notification; + let mut hash = DefaultHasher::new(); + hash.write(title.as_bytes()); + hash.write(message.as_bytes()); + + let id = format!("bRAC-{}-{}", SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis(), hash.finish()); + let notif = Notification::new(title); notif.set_body(Some(&message)); - ui.app.send_notification(None, ¬if); + ui.app.send_notification(Some(&id), ¬if); + + ui.notifications.write().unwrap().push(id); } fn on_add_message(ctx: Arc, ui: &UiModel, message: String) {