diff options
author | Ricky Kresslein <rk@lakoliu.com> | 2022-04-09 12:17:14 +0300 |
---|---|---|
committer | Ricky Kresslein <rk@lakoliu.com> | 2022-04-09 12:17:14 +0300 |
commit | e3a44b30e9d728d9129cfca5d656ae2e186b37f3 (patch) | |
tree | b90b93f417721762c93ebf265bd2786f5ac7be54 /src/application.rs | |
parent | 707e8ed72d248bcc80a19c9220c1cc6de4dba28d (diff) | |
download | Furtherance-e3a44b30e9d728d9129cfca5d656ae2e186b37f3.tar.zst |
Add system notifications for idle (Issue #15)
Diffstat (limited to 'src/application.rs')
-rwxr-xr-x | src/application.rs | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/src/application.rs b/src/application.rs index e5153da..4891c7d 100755 --- a/src/application.rs +++ b/src/application.rs @@ -19,6 +19,8 @@ use glib::clone; use gtk::prelude::*; use gtk::subclass::prelude::*; use gtk::{gdk, gio, glib}; +use log::debug; +use std::sync::Mutex; use crate::config; use crate::ui::{FurtheranceWindow, FurPreferencesWindow}; @@ -29,7 +31,9 @@ mod imp { use super::*; #[derive(Debug, Default)] - pub struct FurtheranceApplication {} + pub struct FurtheranceApplication { + pub idle_dialog: Mutex<gtk::MessageDialog>, + } #[glib::object_subclass] impl ObjectSubclass for FurtheranceApplication { @@ -111,6 +115,30 @@ impl FurtheranceApplication { app.show_about(); })); self.add_action(&about_action); + + let discard_idle_action = gio::SimpleAction::new("discard-idle-action", None); + discard_idle_action.connect_activate(clone!(@weak self as app => move |_, _| { + let window = FurtheranceWindow::default(); + let imp = window.imp(); + if *imp.running.lock().unwrap() && *imp.idle_time_reached.lock().unwrap() { + window.set_subtract_idle(true); + imp.start_button.emit_clicked(); + let imp_app = imp::FurtheranceApplication::from_instance(&app); + imp_app.idle_dialog.lock().unwrap().close(); + } + })); + self.add_action(&discard_idle_action); + + let continue_idle_action = gio::SimpleAction::new("continue-idle-action", None); + continue_idle_action.connect_activate(clone!(@weak self as app => move |_, _| { + let window = FurtheranceWindow::default(); + if *window.imp().running.lock().unwrap() { + window.reset_vars(); + let imp = imp::FurtheranceApplication::from_instance(&app); + imp.idle_dialog.lock().unwrap().close(); + } + })); + self.add_action(&continue_idle_action); } fn setup_application(&self) { @@ -198,6 +226,28 @@ impl FurtheranceApplication { manager.set_color_scheme(color_scheme); } } + + pub fn system_notification(&self, title: &str, subtitle: &str, dialog: gtk::MessageDialog) { + let imp = imp::FurtheranceApplication::from_instance(self); + *imp.idle_dialog.lock().unwrap() = dialog; + let icon = Some("appointment-missed-symbolic"); + let notification = gio::Notification::new(title.as_ref()); + notification.set_body(Some(subtitle.as_ref())); + + if let Some(icon) = icon { + match gio::Icon::for_string(icon) { + Ok(gicon) => notification.set_icon(&gicon), + Err(err) => debug!("Unable to display notification: {:?}", err), + } + } + + notification.add_button("Discard", "app.discard-idle-action"); + notification.add_button("Continue", "app.continue-idle-action"); + + gio::Application::default() + .unwrap() + .send_notification(None, ¬ification); + } } impl Default for FurtheranceApplication { |