about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorRicky Kresslein <ricky@kressle.in>2022-02-23 22:54:50 +0100
committerRicky Kresslein <ricky@kressle.in>2022-02-23 22:54:50 +0100
commitde4ea78d38007a2efda4cbd03d0207fa27decbf4 (patch)
tree77f642be49be3656276628b8fc885a5f5d169c86 /src
parent2a8e1c75863bc84c55eadcff1f3adb42d409d180 (diff)
downloadFurtherance-de4ea78d38007a2efda4cbd03d0207fa27decbf4.tar.zst
- Clearly states idle notify works in Gnome only
- Fixed bug idle preference only updated on start
- Added dark theme switch
Diffstat (limited to 'src')
-rw-r--r--src/application.rs37
-rw-r--r--src/gtk/preferences_window.ui19
-rw-r--r--src/ui/preferences_window.rs14
-rw-r--r--src/ui/window.rs11
4 files changed, 70 insertions, 11 deletions
diff --git a/src/application.rs b/src/application.rs
index f8b5527..6d051ba 100644
--- a/src/application.rs
+++ b/src/application.rs
@@ -22,12 +22,15 @@ use gtk::{gdk, gio, glib};
 use crate::config;
 use crate::ui::{FurtheranceWindow, FurPreferencesWindow};
 use crate::database;
+use crate::settings_manager;
 
 mod imp {
     use super::*;
 
     #[derive(Debug, Default)]
-    pub struct FurtheranceApplication {}
+    pub struct FurtheranceApplication {
+        // pub settings: gio::Settings,
+    }
 
     #[glib::object_subclass]
     impl ObjectSubclass for FurtheranceApplication {
@@ -41,6 +44,7 @@ mod imp {
             self.parent_constructed(obj);
 
             obj.setup_gactions();
+            obj.setup_application();
             obj.set_accels_for_action("app.quit", &["<primary>Q", "<primary>W"]);
         }
     }
@@ -110,6 +114,20 @@ impl FurtheranceApplication {
         self.add_action(&about_action);
     }
 
+    fn setup_application(&self) {
+        let app_id = config::APP_ID.trim_end_matches(".Devel");
+        let settings = gio::Settings::new(app_id);
+
+        settings.connect_changed(
+            Some("dark-mode"),
+            clone!(@weak self as app => move |_, _| {
+                    app.update_light_dark();
+                }
+            ),
+        );
+        self.update_light_dark()
+    }
+
     fn show_about(&self) {
         let window = self.active_window().unwrap();
         let dialog = gtk::AboutDialog::builder()
@@ -120,9 +138,8 @@ impl FurtheranceApplication {
             .version(config::VERSION)
             .comments("Track your time without being tracked.")
             .copyright("© 2022 Ricky Kresslein")
-            .website("https://lakoliu.com")
             .authors(vec!["Ricky Kresslein <rk@lakoliu.com>".into()])
-            // .website("https://furtherance.app")
+            .website("https://furtherance.app")
             .license_type(gtk::License::Gpl30)
             .build();
 
@@ -179,6 +196,19 @@ impl FurtheranceApplication {
             self.remove_action("delete-history");
         }
     }
+
+    fn update_light_dark(&self) {
+        let manager = adw::StyleManager::default();
+
+        if !manager.system_supports_color_schemes() {
+            let color_scheme = if settings_manager::get_bool("dark-mode") {
+                adw::ColorScheme::PreferDark
+            } else {
+                adw::ColorScheme::PreferLight
+            };
+            manager.set_color_scheme(color_scheme);
+        }
+    }
 }
 
 impl Default for FurtheranceApplication {
@@ -189,3 +219,4 @@ impl Default for FurtheranceApplication {
             .unwrap()
     }
 }
+
diff --git a/src/gtk/preferences_window.ui b/src/gtk/preferences_window.ui
index a9866cb..0ac2337 100644
--- a/src/gtk/preferences_window.ui
+++ b/src/gtk/preferences_window.ui
@@ -6,12 +6,31 @@
         <property name="icon_name">emblem-system-symbolic</property>
         <property name="title" translatable="yes">General</property>
         <child>
+          <object class="AdwPreferencesGroup" id="appearance_group">
+            <property name="title" translatable="yes">Appearance</property>
+            <property name="visible">True</property>
+            <child>
+              <object class="AdwActionRow">
+                <property name="title" translatable="yes">_Dark Theme</property>
+                <property name="use_underline">True</property>
+                <property name="activatable_widget">dark_theme_switch</property>
+                <child>
+                  <object class="GtkSwitch" id="dark_theme_switch">
+                    <property name="valign">center</property>
+                  </object>
+                </child>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
           <object class="AdwPreferencesGroup" id="idle_group">
             <property name="title" translatable="yes">Idle</property>
             <property name="visible">True</property>
             <child>
               <object class="AdwExpanderRow" id="notify_of_idle_expander">
                 <property name="title" translatable="yes">Notify of Idle</property>
+                <property name="subtitle" translatable="yes">(Gnome only)</property>
                 <property name="show_enable_switch">True</property>
                 <property name="use_underline">True</property>
                 <child>
diff --git a/src/ui/preferences_window.rs b/src/ui/preferences_window.rs
index e9ef36e..b6088ec 100644
--- a/src/ui/preferences_window.rs
+++ b/src/ui/preferences_window.rs
@@ -31,6 +31,11 @@ mod imp {
     #[template(resource = "/com/lakoliu/Furtherance/gtk/preferences_window.ui")]
     pub struct FurPreferencesWindow {
         #[template_child]
+        pub appearance_group: TemplateChild<adw::PreferencesGroup>,
+        #[template_child]
+        pub dark_theme_switch: TemplateChild<gtk::Switch>,
+
+        #[template_child]
         pub idle_group: TemplateChild<adw::PreferencesGroup>,
         #[template_child]
         pub notify_of_idle_expander: TemplateChild<adw::ExpanderRow>,
@@ -88,13 +93,22 @@ impl FurPreferencesWindow {
     }
 
     fn setup_widgets(&self) {
+        let imp = imp::FurPreferencesWindow::from_instance(self);
 
+        let manager = adw::StyleManager::default();
+        let support_darkmode = manager.system_supports_color_schemes();
+        imp.appearance_group.set_visible(!support_darkmode);
     }
 
     fn setup_signals(&self) {
         let imp = imp::FurPreferencesWindow::from_instance(self);
 
         settings_manager::bind_property(
+            "dark-mode",
+            &*imp.dark_theme_switch,
+            "active");
+
+        settings_manager::bind_property(
             "notify-of-idle",
             &*imp.notify_of_idle_expander,
             "enable-expansion",
diff --git a/src/ui/window.rs b/src/ui/window.rs
index 92c8282..7c1e8cc 100644
--- a/src/ui/window.rs
+++ b/src/ui/window.rs
@@ -25,7 +25,6 @@ use std::rc::Rc;
 use std::cell::RefCell;
 use chrono::{DateTime, Local, Duration as ChronDur};
 use dbus::blocking::Connection;
-use once_cell::unsync::OnceCell;
 
 use crate::ui::FurHistoryBox;
 use crate::FurtheranceApplication;
@@ -53,7 +52,6 @@ mod imp {
         #[template_child]
         pub toast_overlay: TemplateChild<adw::ToastOverlay>,
 
-        pub notify_of_idle: OnceCell<u64>,
         pub stored_idle: Mutex<u64>,
         pub idle_notified: Mutex<bool>,
         pub idle_time_reached: Mutex<bool>,
@@ -220,9 +218,6 @@ impl FurtheranceWindow {
 
     fn setup_settings(&self) {
         let imp = imp::FurtheranceWindow::from_instance(self);
-        // Get user setting idle-time in minutes and convert it to seconds
-        imp.notify_of_idle.set((settings_manager::get_int("idle-time") * 60) as u64)
-            .expect("Failed to set notify_of_idle");
         self.reset_vars();
 
         // Enter starts timer
@@ -249,7 +244,7 @@ impl FurtheranceWindow {
         let idle_time = self.get_idle_time().unwrap();
 
         // If user was idle and has now returned...
-        if idle_time < *imp.notify_of_idle.get().unwrap()
+        if idle_time < (settings_manager::get_int("idle-time") * 60) as u64
             && *imp.idle_time_reached.lock().unwrap()
             && !*imp.idle_notified.lock().unwrap() {
 
@@ -259,12 +254,12 @@ impl FurtheranceWindow {
         *imp.stored_idle.lock().unwrap() = idle_time;
 
         // If user is idle but has not returned...
-        if *imp.stored_idle.lock().unwrap() >= *imp.notify_of_idle.get().unwrap()
+        if *imp.stored_idle.lock().unwrap() >= (settings_manager::get_int("idle-time") * 60) as u64
             && !*imp.idle_time_reached.lock().unwrap() {
 
             *imp.idle_time_reached.lock().unwrap() = true;
             let true_idle_start_time = Local::now() -
-                ChronDur::seconds(*imp.notify_of_idle.get().unwrap() as i64);
+                ChronDur::seconds((settings_manager::get_int("idle-time") * 60) as i64);
             *imp.idle_start_time.lock().unwrap() = true_idle_start_time.to_rfc3339();
         }
     }