about summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xCargo.lock10
-rwxr-xr-xCargo.toml1
-rwxr-xr-xsrc/application.rs52
-rwxr-xr-xsrc/ui/window.rs16
4 files changed, 71 insertions, 8 deletions
diff --git a/Cargo.lock b/Cargo.lock
index d1c476f..6d88198 100755
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -214,6 +214,7 @@ dependencies = [
  "gettext-rs",
  "gtk4",
  "libadwaita",
+ "log",
  "once_cell",
  "rusqlite",
 ]
@@ -666,6 +667,15 @@ dependencies = [
 ]
 
 [[package]]
+name = "log"
+version = "0.4.16"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6389c490849ff5bc16be905ae24bc913a9c8892e19b2341dbc175e14c341c2b8"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
 name = "malloc_buf"
 version = "0.0.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 53a4f02..3265b3b 100755
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,6 +12,7 @@ directories = "4.0"
 once_cell = "1.9.0"
 dbus = "0.9.5"
 dbus-codegen = "0.10.0"
+log = "0.4"
 
 [dependencies.gtk]
 package = "gtk4"
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, &notification);
+    }
 }
 
 impl Default for FurtheranceApplication {
diff --git a/src/ui/window.rs b/src/ui/window.rs
index a9b43bd..d252ef7 100755
--- a/src/ui/window.rs
+++ b/src/ui/window.rs
@@ -296,10 +296,9 @@ impl FurtheranceWindow {
         let h = idle_time / 60 / 60;
         let m = (idle_time / 60) - (h * 60);
         let s = idle_time - (m * 60);
-        let idle_time_str = format!(
-            "{}{:02}:{:02}:{:02}{}", gettext("You have been idle for "), h, m, s,
-            gettext(".\nWould you like to discard that time, or continue the clock?")
-            );
+        let idle_time_str = format!("{}{:02}:{:02}:{:02}", gettext("You have been idle for "), h, m, s);
+        let question_str = gettext("\nWould you like to discard that time, or continue the clock?");
+        let idle_time_msg = format!("{}{}", idle_time_str, question_str);
 
         let dialog = gtk::MessageDialog::with_markup(
             Some(self),
@@ -312,7 +311,10 @@ impl FurtheranceWindow {
             (&gettext("Discard"), gtk::ResponseType::Reject),
             (&gettext("Continue"), gtk::ResponseType::Accept)
         ]);
-        dialog.set_secondary_text(Some(&idle_time_str));
+        dialog.set_secondary_text(Some(&idle_time_msg));
+
+        let app = FurtheranceApplication::default();
+        app.system_notification(&idle_time_str, &question_str, dialog.clone());
 
         dialog.connect_response(clone!(
             @weak self as this,
@@ -331,7 +333,7 @@ impl FurtheranceWindow {
         dialog.show()
     }
 
-    fn reset_vars(&self) {
+    pub fn reset_vars(&self) {
         let imp = imp::FurtheranceWindow::from_instance(self);
         *imp.stored_idle.lock().unwrap() = 0;
         *imp.idle_notified.lock().unwrap() = false;
@@ -339,7 +341,7 @@ impl FurtheranceWindow {
         *imp.subtract_idle.lock().unwrap() = false;
     }
 
-    fn set_subtract_idle(&self, val: bool) {
+    pub fn set_subtract_idle(&self, val: bool) {
         let imp = imp::FurtheranceWindow::from_instance(self);
         *imp.subtract_idle.lock().unwrap() = val;
     }