[gnome-break-timer] Move some operations outside of event handlers



commit 53c26a20fa73aa8a71620ac21fbb5726d70cafc4
Author: Dylan McCall <dylan dylanmccall ca>
Date:   Thu Nov 19 14:26:20 2020 -0800

    Move some operations outside of event handlers

 src/common/IBreakTimer.vala                 |  2 +-
 src/daemon/Application.vala                 | 26 +++++++++++---
 src/daemon/BreakManagerDBusObject.vala      |  7 ++--
 src/daemon/break/BreakView.vala             | 10 ++++--
 src/daemon/util/PausableTimeout.vala        |  2 +-
 src/settings/Application.vala               |  7 ++--
 src/settings/MainWindow.vala                | 56 +++++++++++++++++------------
 src/settings/timerbreak/TimerBreakType.vala |  2 +-
 src/settings/widgets/CircleCounter.vala     |  3 +-
 9 files changed, 76 insertions(+), 39 deletions(-)
---
diff --git a/src/common/IBreakTimer.vala b/src/common/IBreakTimer.vala
index 51a18a7..8f5a3a2 100644
--- a/src/common/IBreakTimer.vala
+++ b/src/common/IBreakTimer.vala
@@ -20,7 +20,7 @@ namespace BreakTimer.Common {
 [DBus (name = "org.gnome.BreakTimer")]
 public interface IBreakTimer : GLib.Object {
     /** Returns the ID of the break that is currently focused and activated, if any. */
-    public abstract string? get_current_active_break () throws GLib.DBusError, GLib.IOError;
+    public abstract string[] get_current_active_break () throws GLib.DBusError, GLib.IOError;
 
     /** Returns a list of breaks that are currently known to the break daemon. */
     public abstract string[] get_break_ids () throws GLib.DBusError, GLib.IOError;
diff --git a/src/daemon/Application.vala b/src/daemon/Application.vala
index 040ab98..e17f21c 100644
--- a/src/daemon/Application.vala
+++ b/src/daemon/Application.vala
@@ -45,8 +45,12 @@ public class Application : Gtk.Application {
 
         GLib.Environment.set_application_name (app_name);
 
-        string user_cache_path = GLib.Environment.get_user_cache_dir ();
-        this.cache_path = Path.build_filename (user_cache_path, "gnome-break-timer");
+        this.cache_path = GLib.Path.build_filename (
+            GLib.Environment.get_user_cache_dir (),
+            "gnome-break-timer"
+        );
+
+        this.query_end.connect (this.on_query_end_cb);
     }
 
     public override void activate () {
@@ -114,14 +118,28 @@ public class Application : Gtk.Application {
         this.save_state ();
     }
 
+    public void on_query_end_cb () {
+        uint inhibit_cookie = this.inhibit (null, Gtk.ApplicationInhibitFlags.LOGOUT, _("Saving state"));
+        GLib.Idle.add_full (
+            GLib.Priority.HIGH_IDLE,
+            () => {
+                this.save_state ();
+                this.uninhibit (inhibit_cookie);
+                return GLib.Source.REMOVE;
+            }
+        );
+    }
+
     private GLib.File get_state_file () {
         GLib.File cache_dir = GLib.File.new_for_path (this.cache_path);
         try {
-            if (! cache_dir.query_exists ()) cache_dir.make_directory_with_parents ();
+            if (! cache_dir.query_exists ()) {
+                cache_dir.make_directory_with_parents ();
+            }
         } catch (GLib.Error e) {
             GLib.warning ("Error creating cache directory: %s", e.message);
         }
-        string state_file_name = "last-state-%d".printf (DATA_VERSION);
+        string state_file_name = "last-state-%d.json".printf (DATA_VERSION);
         return cache_dir.get_child (state_file_name);
     }
 
diff --git a/src/daemon/BreakManagerDBusObject.vala b/src/daemon/BreakManagerDBusObject.vala
index fbc3f66..fa1f71b 100644
--- a/src/daemon/BreakManagerDBusObject.vala
+++ b/src/daemon/BreakManagerDBusObject.vala
@@ -28,14 +28,13 @@ public class BreakManagerDBusObject : GLib.Object, IBreakTimer {
         this.break_manager = break_manager;
     }
 
-    public string? get_current_active_break () throws GLib.DBusError, GLib.IOError {
-        /* Ask  for focused break */
+    public string[] get_current_active_break () throws GLib.DBusError, GLib.IOError {
         foreach (BreakType break_type in this.break_manager.all_breaks ()) {
             bool is_active = break_type.break_view.has_ui_focus () &&
                 break_type.break_controller.is_active ();
-            if (is_active) return break_type.id;
+            if (is_active) return {break_type.id};
         }
-        return null;
+        return {};
     }
 
     public bool is_active () throws GLib.DBusError, GLib.IOError {
diff --git a/src/daemon/break/BreakView.vala b/src/daemon/break/BreakView.vala
index aa80cf9..d040466 100644
--- a/src/daemon/break/BreakView.vala
+++ b/src/daemon/break/BreakView.vala
@@ -89,7 +89,7 @@ public abstract class BreakView : UIFragment {
     }
 
     protected void show_break_info () {
-        GLib.AppInfo settings_app_info = new GLib.DesktopAppInfo (Config.SETTINGS_APPLICATION_ID);
+        GLib.AppInfo settings_app_info = new GLib.DesktopAppInfo (Config.SETTINGS_DESKTOP_FILE_ID);
         GLib.AppLaunchContext app_launch_context = new GLib.AppLaunchContext ();
         try {
             settings_app_info.launch (null, app_launch_context);
@@ -103,7 +103,13 @@ public abstract class BreakView : UIFragment {
     }
 
     private void notification_action_info_cb () {
-        this.show_break_info ();
+        GLib.Idle.add_full (
+            GLib.Priority.HIGH_IDLE,
+            () => {
+                this.show_break_info ();
+                return false;
+            }
+        );
     }
 
     private void notification_closed_cb () {
diff --git a/src/daemon/util/PausableTimeout.vala b/src/daemon/util/PausableTimeout.vala
index 7185ff5..2f75096 100644
--- a/src/daemon/util/PausableTimeout.vala
+++ b/src/daemon/util/PausableTimeout.vala
@@ -57,7 +57,7 @@ public class PausableTimeout : GLib.Object {
         int delta_millisecs = (int) (time_delta / 1000);
         this.timeout_cb (this, delta_millisecs);
 
-        return true;
+        return GLib.Source.CONTINUE;
     }
 
     public void run_once () {
diff --git a/src/settings/Application.vala b/src/settings/Application.vala
index 8999503..56c5512 100644
--- a/src/settings/Application.vala
+++ b/src/settings/Application.vala
@@ -135,7 +135,7 @@ public class Application : Gtk.Application {
             this.initial_focus = false;
             GLib.Timeout.add (500, () => {
                 this.break_manager.refresh_permissions ();
-                return false;
+                return GLib.Source.REMOVE;
             });
         } else if (focused && this.break_manager.permissions_error != NONE) {
             // Refresh permissions on focus if there was an error, and, for
@@ -149,7 +149,10 @@ public class Application : Gtk.Application {
     private void delayed_start () {
         // Wait 500ms for break_manager to appear
         this.break_manager.break_status_available.connect (this.delayed_start_cb);
-        GLib.Timeout.add (500, () => { delayed_start_cb (); return false; });
+        GLib.Timeout.add (500, () => {
+            delayed_start_cb ();
+            return GLib.Source.REMOVE;
+        });
     }
 
     private void delayed_start_cb () {
diff --git a/src/settings/MainWindow.vala b/src/settings/MainWindow.vala
index 777c5a9..2bd7dd8 100644
--- a/src/settings/MainWindow.vala
+++ b/src/settings/MainWindow.vala
@@ -79,7 +79,13 @@ public class MainWindow : Gtk.ApplicationWindow, GLib.Initable {
 
         private void on_response (int response_id) {
             if (response_id == RESPONSE_OPEN_SETTINGS) {
-                this.main_window.launch_application_settings ();
+                GLib.Idle.add_full (
+                    GLib.Priority.HIGH_IDLE,
+                    () => {
+                        this.main_window.launch_application_settings ();
+                        return GLib.Source.REMOVE;
+                    }
+                );
             } else if (response_id == Gtk.ResponseType.CLOSE) {
                 this.close_message_bar ();
             }
@@ -286,28 +292,32 @@ public class MainWindow : Gtk.ApplicationWindow, GLib.Initable {
         // This feels kind of dirty and it would be nice if there was a better
         // way.
         // TODO: Can we pre-select org.gnome.BreakTimer?
-        // TODO: This should be asynchronous
-
-        GLib.Variant[] parameters = {
-            new GLib.Variant ("(sav)", "applications")
-        };
-        GLib.HashTable<string, Variant> platform_data = new GLib.HashTable<string, Variant> (str_hash, 
str_equal);
-
-        try {
-            IFreedesktopApplication control_center_application = this.dbus_connection.get_proxy_sync (
-                "org.gnome.ControlCenter",
-                "/org/gnome/ControlCenter",
-                GLib.DBusProxyFlags.DO_NOT_AUTO_START,
-                null
-            );
-            control_center_application.activate_action ("launch-panel", parameters, platform_data);
-        } catch (GLib.IOError error) {
-            GLib.warning ("Error connecting to org.gnome.ControlCenter: %s", error.message);
-            return false;
-        } catch (GLib.DBusError error) {
-            GLib.warning ("Error launching org.gnome.ControlCenter: %s", error.message);
-            return false;
-        }
+        // TODO: Vala doesn't provide an easy way to do async dbus method calls,
+        //       so we'll spawn a simple thread for this.
+
+        new GLib.Thread<bool> (null, () => {
+            GLib.Variant[] parameters = {
+                new GLib.Variant ("(sav)", "applications")
+            };
+            GLib.HashTable<string, Variant> platform_data = new GLib.HashTable<string, Variant> (str_hash, 
str_equal);
+
+            try {
+                IFreedesktopApplication control_center_application = this.dbus_connection.get_proxy_sync (
+                    "org.gnome.ControlCenter",
+                    "/org/gnome/ControlCenter",
+                    GLib.DBusProxyFlags.DO_NOT_AUTO_START,
+                    null
+                );
+                control_center_application.activate_action ("launch-panel", parameters, platform_data);
+            } catch (GLib.IOError error) {
+                GLib.warning ("Error connecting to org.gnome.ControlCenter: %s", error.message);
+                return false;
+            } catch (GLib.DBusError error) {
+                GLib.warning ("Error launching org.gnome.ControlCenter: %s", error.message);
+                return false;
+            }
+            return true;
+        });
 
         return true;
     }
diff --git a/src/settings/timerbreak/TimerBreakType.vala b/src/settings/timerbreak/TimerBreakType.vala
index 43c4bfe..5462951 100644
--- a/src/settings/timerbreak/TimerBreakType.vala
+++ b/src/settings/timerbreak/TimerBreakType.vala
@@ -69,7 +69,7 @@ public abstract class TimerBreakType : BreakType {
     private bool update_status_cb () {
         TimerBreakStatus? status = this.get_status ();
         this.update_status (status);
-        return true;
+        return GLib.Source.CONTINUE;
     }
 
     private TimerBreakStatus? get_status () {
diff --git a/src/settings/widgets/CircleCounter.vala b/src/settings/widgets/CircleCounter.vala
index 73fa8ec..9ce5dfb 100644
--- a/src/settings/widgets/CircleCounter.vala
+++ b/src/settings/widgets/CircleCounter.vala
@@ -85,7 +85,8 @@ public class CircleCounter : Gtk.Widget {
 
         double start_angle = 1.5 * Math.PI;
         double progress_angle = this.progress * Math.PI * 2.0;
-        progress_angle = (int)(progress_angle / SNAP_INCREMENT) * SNAP_INCREMENT;
+        int snap_count = (int) (progress_angle / SNAP_INCREMENT);
+        progress_angle = snap_count * SNAP_INCREMENT;
 
         if (this.direction == Direction.COUNT_DOWN) {
             if (progress_angle > 0) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]