[gnome-break-timer/combine-application-classes] Shrink Application classes to build a single main.vala




commit d488c46dda19f5a2db44812888d275f104b151e1
Author: Dylan McCall <dylan dylanmccall ca>
Date:   Sat Nov 28 23:19:07 2020 -0800

    Shrink Application classes to build a single main.vala
    
    Ref #3

 src/{settings => }/Application.vala                |  94 +++++-------------
 .../{Application.vala => ApplicationContext.vala}  |  52 +++-------
 src/daemon/SessionStatus.vala                      |   5 -
 src/daemon/meson.build                             |  18 +---
 src/main.vala                                      |  34 +++++++
 src/meson.build                                    |  19 ++++
 src/settings/ApplicationContext.vala               | 109 +++++++++++++++++++++
 src/settings/BreakManager.vala                     |   6 +-
 src/settings/MainWindow.vala                       |   2 +-
 src/settings/meson.build                           |  18 +---
 10 files changed, 205 insertions(+), 152 deletions(-)
---
diff --git a/src/settings/Application.vala b/src/Application.vala
similarity index 55%
rename from src/settings/Application.vala
rename to src/Application.vala
index f7b744d..eb266b0 100644
--- a/src/settings/Application.vala
+++ b/src/Application.vala
@@ -18,9 +18,16 @@
  * SPDX-License-Identifier: GPL-3.0-or-later
  */
 
-namespace BreakTimer.Settings {
+using BreakTimer.Common;
+
+namespace BreakTimer {
 
 public class Application : Gtk.Application {
+    public const string APP_NAME = _("GNOME Break Timer");
+
+    // Keep running for one minute after the last break is disabled
+    private const int ACTIVITY_TIMEOUT_MS = 60 * TimeUnit.MILLISECONDS_IN_SECONDS;
+
     private const string STYLE_DATA =
         """
         ._settings-title {
@@ -50,27 +57,26 @@ public class Application : Gtk.Application {
         }
         """;
 
-    private BreakManager break_manager;
-    private MainWindow main_window;
-    private bool initial_focus = true;
+    private Daemon.ApplicationContext daemon_context;
+    private Settings.ApplicationContext settings_context;
 
     public Application () {
         GLib.Object (
-            application_id: Config.SETTINGS_APPLICATION_ID,
-            flags: GLib.ApplicationFlags.FLAGS_NONE
+            application_id: Config.APPLICATION_ID,
+            flags: GLib.ApplicationFlags.FLAGS_NONE,
+            inactivity_timeout: ACTIVITY_TIMEOUT_MS,
+            register_session: true
         );
+        GLib.Environment.set_application_name (APP_NAME);
+        this.daemon_context = new Daemon.ApplicationContext (this);
+        this.settings_context = new Settings.ApplicationContext (this);
     }
 
     public override void activate () {
         base.activate ();
 
-        if (this.break_manager.is_working ()) {
-            this.main_window.present ();
-        } else {
-            // Something may be wrong, but it could just be a delay before the
-            // break daemon starts. We'll wait before showing the main window.
-            this.delayed_start ();
-        }
+        this.daemon_context.activate ();
+        this.settings_context.activate ();
     }
 
     public override void startup () {
@@ -102,65 +108,13 @@ public class Application : Gtk.Application {
 
         this.set_accels_for_action ("app.quit", {"<Primary>q"});
 
-        this.break_manager = new BreakManager (this);
-        try {
-            this.break_manager.init (null);
-        } catch (GLib.Error error) {
-            GLib.error ("Error initializing break_manager: %s", error.message);
-        }
-
-        this.main_window = new MainWindow (this, this.break_manager);
-        try {
-            this.main_window.init (null);
-        } catch (GLib.Error error) {
-            GLib.error ("Error initializing main_window: %s", error.message);
-        }
-
-        if (Config.BUILD_PROFILE == "development") {
-            this.main_window.get_style_context ().add_class ("devel");
-        }
-
-        this.main_window.window_state_event.connect (this.on_main_window_window_state_event);
+        this.daemon_context.startup ();
+        this.settings_context.startup ();
     }
 
-    private bool on_main_window_window_state_event (Gdk.EventWindowState event) {
-        bool focused = (
-            Gdk.WindowState.FOCUSED in event.changed_mask &&
-            Gdk.WindowState.FOCUSED in event.new_window_state
-        );
-
-        if (focused && this.initial_focus && this.break_manager.master_enabled) {
-            // We should always refresh permissions at startup if enabled. Wait
-            // for a moment after the main window is focused before doing this,
-            // because it may trigger a system dialog.
-            this.initial_focus = false;
-            GLib.Timeout.add (500, () => {
-                this.break_manager.refresh_permissions ();
-                return GLib.Source.REMOVE;
-            });
-        } else if (focused && this.break_manager.permissions_error != NONE) {
-            // Refresh permissions on focus if there was an error, and, for
-            // example, we are returning from GNOME Settings
-            this.break_manager.refresh_permissions ();
-        }
-
-        return false;
-    }
-
-    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 GLib.Source.REMOVE;
-        });
-    }
-
-    private void delayed_start_cb () {
-        this.break_manager.break_status_available.disconnect (this.delayed_start_cb);
-        if (! this.main_window.is_visible ()) {
-            this.main_window.present ();
-        }
+    public override void shutdown () {
+        this.daemon_context.shutdown ();
+        this.settings_context.shutdown ();
     }
 
     private void on_about_activate_cb () {
diff --git a/src/daemon/Application.vala b/src/daemon/ApplicationContext.vala
similarity index 80%
rename from src/daemon/Application.vala
rename to src/daemon/ApplicationContext.vala
index a1d74f4..a3e2a92 100644
--- a/src/daemon/Application.vala
+++ b/src/daemon/ApplicationContext.vala
@@ -23,12 +23,12 @@ using BreakTimer.Daemon.Activity;
 
 namespace BreakTimer.Daemon {
 
-public class Application : Gtk.Application {
-    const string app_name = _("GNOME Break Timer");
+public class ApplicationContext : GLib.Object {
+    public const string APP_NAME = _("GNOME Break Timer");
+
     const int DATA_VERSION = 0;
 
-    // Keep running for one minute after the last break is disabled
-    private const int ACTIVITY_TIMEOUT_MS = 60 * TimeUnit.MILLISECONDS_IN_SECONDS;
+    private Gtk.Application application;
 
     // Consider saved state valid if it was created in the last 10 seconds
     private const int SAVE_STATE_INTERVAL = 10 * TimeUnit.MILLISECONDS_IN_SECONDS;
@@ -42,15 +42,8 @@ public class Application : Gtk.Application {
     private string cache_path;
     private int64 state_saved_time_ms;
 
-    public Application () {
-        GLib.Object (
-            application_id: Config.DAEMON_APPLICATION_ID,
-            flags: ApplicationFlags.FLAGS_NONE,
-            inactivity_timeout: ACTIVITY_TIMEOUT_MS,
-            register_session: true
-        );
-
-        GLib.Environment.set_application_name (app_name);
+    public ApplicationContext (Gtk.Application application) {
+        this.application = application;
 
         this.cache_path = GLib.Path.build_filename (
             GLib.Environment.get_user_cache_dir (),
@@ -58,29 +51,16 @@ public class Application : Gtk.Application {
         );
         this.state_saved_time_ms = 0;
 
-        this.query_end.connect (this.on_query_end_cb);
+        application.query_end.connect (this.on_query_end_cb);
     }
 
-    public override void activate () {
-        base.activate ();
+    public void activate () {
     }
 
-    public override void startup () {
-        base.startup ();
-
-        Notify.init (app_name);
+    public void startup () {
+        Notify.init (APP_NAME);
 
-        /* set up custom gtk style for application */
-        Gdk.Screen screen = Gdk.Screen.get_default ();
-        Gtk.CssProvider style_provider = new Gtk.CssProvider ();
-
-        Gtk.StyleContext.add_provider_for_screen (
-            screen,
-            style_provider,
-            Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
-        );
-
-        this.session_status = new SessionStatus (this);
+        this.session_status = new SessionStatus ();
         try {
             this.session_status.init (null);
         } catch (GLib.Error error) {
@@ -101,7 +81,7 @@ public class Application : Gtk.Application {
             GLib.error ("Error initializing activity_monitor: %s", error.message);
         }
 
-        this.ui_manager = new UIManager (this, session_status);
+        this.ui_manager = new UIManager (this.application, session_status);
         try {
             this.ui_manager.init (null);
         } catch (GLib.Error error) {
@@ -120,19 +100,17 @@ public class Application : Gtk.Application {
         this.activity_monitor.start ();
     }
 
-    public override void shutdown () {
-        base.shutdown ();
-
+    public void shutdown () {
         this.save_state ();
     }
 
     public void on_query_end_cb () {
-        uint inhibit_cookie = this.inhibit (null, Gtk.ApplicationInhibitFlags.LOGOUT, _("Saving state"));
+        uint inhibit_cookie = this.application.inhibit (null, Gtk.ApplicationInhibitFlags.LOGOUT, _("Saving 
state"));
         GLib.Idle.add_full (
             GLib.Priority.HIGH_IDLE,
             () => {
                 this.save_state ();
-                this.uninhibit (inhibit_cookie);
+                this.application.uninhibit (inhibit_cookie);
                 return GLib.Source.REMOVE;
             }
         );
diff --git a/src/daemon/SessionStatus.vala b/src/daemon/SessionStatus.vala
index b75b19b..52670f7 100644
--- a/src/daemon/SessionStatus.vala
+++ b/src/daemon/SessionStatus.vala
@@ -27,15 +27,10 @@ namespace BreakTimer.Daemon {
  * case we are unable to connect.
  */
 public class SessionStatus : GLib.Object, ISessionStatus, GLib.Initable {
-    private Gtk.Application application;
     private GLib.DBusConnection dbus_connection;
     private IGnomeScreenSaver? screensaver;
     private bool screensaver_is_active = false;
 
-    public SessionStatus (Gtk.Application application) {
-        this.application = application;
-    }
-
     public bool init (GLib.Cancellable? cancellable) throws GLib.Error {
         this.dbus_connection = GLib.Bus.get_sync (GLib.BusType.SESSION, cancellable);
 
diff --git a/src/daemon/meson.build b/src/daemon/meson.build
index 348496d..1e87088 100644
--- a/src/daemon/meson.build
+++ b/src/daemon/meson.build
@@ -3,7 +3,7 @@ daemon_lib_sources = files(
     'activity/ActivityMonitor.vala',
     'activity/MutterActivityMonitorBackend.vala',
     'activity/UserActivity.vala',
-    'Application.vala',
+    'ApplicationContext.vala',
     'break/BreakController.vala',
     'break/BreakType.vala',
     'break/BreakView.vala',
@@ -55,19 +55,3 @@ daemon_lib_dep = declare_dependency(
     include_directories: include_directories('.')
 )
 
-daemon_exe_sources = files(
-    'main.vala'
-)
-
-executable(
-    'gnome-break-timer-daemon',
-    sources : [
-        daemon_exe_sources
-    ],
-    dependencies : [
-        daemon_lib_dep,
-        break_timer_resources_dep
-    ],
-    install: true,
-    install_dir: bindir
-)
diff --git a/src/main.vala b/src/main.vala
new file mode 100644
index 0000000..e1c631a
--- /dev/null
+++ b/src/main.vala
@@ -0,0 +1,34 @@
+/* main.vala
+ *
+ * Copyright 2020 Dylan McCall <dylan dylanmccall ca>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+namespace BreakTimer {
+
+public int main (string[] args) {
+    Intl.setlocale (LocaleCategory.ALL, "");
+    Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALE_DIR);
+    Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
+    Intl.textdomain (Config.GETTEXT_PACKAGE);
+
+    Application application = new Application ();
+    int status = application.run (args);
+    return status;
+}
+
+}
diff --git a/src/meson.build b/src/meson.build
index eae1670..418549e 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -17,3 +17,22 @@ config_lib_dep = declare_dependency(
 subdir('common')
 subdir('settings')
 subdir('daemon')
+
+main_exe_sources = files(
+    'Application.vala',
+    'main.vala'
+)
+
+executable(
+    'gnome-break-timer',
+    sources : [
+        main_exe_sources
+    ],
+    dependencies : [
+        daemon_lib_dep,
+        settings_lib_dep,
+        break_timer_resources_dep
+    ],
+    install: true,
+    install_dir: bindir
+)
diff --git a/src/settings/ApplicationContext.vala b/src/settings/ApplicationContext.vala
new file mode 100644
index 0000000..be01d09
--- /dev/null
+++ b/src/settings/ApplicationContext.vala
@@ -0,0 +1,109 @@
+/* Application.vala
+ *
+ * Copyright 2020 Dylan McCall <dylan dylanmccall ca>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+namespace BreakTimer.Settings {
+
+public class ApplicationContext : GLib.Object {
+    private Gtk.Application application;
+    private BreakManager break_manager;
+    private MainWindow main_window;
+    private bool initial_focus = true;
+
+    public ApplicationContext (Gtk.Application application) {
+        this.application = application;
+    }
+
+    public void activate () {
+        if (this.break_manager.is_working ()) {
+            this.main_window.present ();
+        } else {
+            // Something may be wrong, but it could just be a delay before the
+            // break daemon starts. We'll wait before showing the main window.
+            this.delayed_start ();
+        }
+    }
+
+    public void startup () {
+        this.break_manager = new BreakManager ();
+        try {
+            this.break_manager.init (null);
+        } catch (GLib.Error error) {
+            GLib.error ("Error initializing break_manager: %s", error.message);
+        }
+
+        this.main_window = new MainWindow (this.application, this.break_manager);
+        try {
+            this.main_window.init (null);
+        } catch (GLib.Error error) {
+            GLib.error ("Error initializing main_window: %s", error.message);
+        }
+
+        if (Config.BUILD_PROFILE == "development") {
+            this.main_window.get_style_context ().add_class ("devel");
+        }
+
+        this.main_window.window_state_event.connect (this.on_main_window_window_state_event);
+    }
+
+    public void shutdown () {
+    }
+
+    private bool on_main_window_window_state_event (Gdk.EventWindowState event) {
+        bool focused = (
+            Gdk.WindowState.FOCUSED in event.changed_mask &&
+            Gdk.WindowState.FOCUSED in event.new_window_state
+        );
+
+        if (focused && this.initial_focus && this.break_manager.master_enabled) {
+            // We should always refresh permissions at startup if enabled. Wait
+            // for a moment after the main window is focused before doing this,
+            // because it may trigger a system dialog.
+            this.initial_focus = false;
+            GLib.Timeout.add (500, () => {
+                this.break_manager.refresh_permissions ();
+                return GLib.Source.REMOVE;
+            });
+        } else if (focused && this.break_manager.permissions_error != NONE) {
+            // Refresh permissions on focus if there was an error, and, for
+            // example, we are returning from GNOME Settings
+            this.break_manager.refresh_permissions ();
+        }
+
+        return false;
+    }
+
+    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 GLib.Source.REMOVE;
+        });
+    }
+
+    private void delayed_start_cb () {
+        this.break_manager.break_status_available.disconnect (this.delayed_start_cb);
+        if (! this.main_window.is_visible ()) {
+            this.main_window.present ();
+        }
+    }
+}
+
+}
diff --git a/src/settings/BreakManager.vala b/src/settings/BreakManager.vala
index 7404838..1b89eb7 100644
--- a/src/settings/BreakManager.vala
+++ b/src/settings/BreakManager.vala
@@ -29,8 +29,6 @@ using BreakTimer.Settings.RestBreak;
 namespace BreakTimer.Settings {
 
 public class BreakManager : GLib.Object {
-    private Application application;
-
     private IBreakTimer break_daemon;
 
     private GLib.List<BreakType> breaks;
@@ -58,9 +56,7 @@ public class BreakManager : GLib.Object {
         BACKGROUND_NOT_ALLOWED
     }
 
-    public BreakManager (Application application) {
-        this.application = application;
-
+    public BreakManager () {
         this.settings = new GLib.Settings (Config.APPLICATION_ID);
 
         this.breaks = new GLib.List<BreakType> ();
diff --git a/src/settings/MainWindow.vala b/src/settings/MainWindow.vala
index 061a8be..04d6df0 100644
--- a/src/settings/MainWindow.vala
+++ b/src/settings/MainWindow.vala
@@ -100,7 +100,7 @@ public class MainWindow : Gtk.ApplicationWindow, GLib.Initable {
         }
     }
 
-    public MainWindow (Application application, BreakManager break_manager) {
+    public MainWindow (Gtk.Application application, BreakManager break_manager) {
         GLib.Object (application: application);
 
         this.break_manager = break_manager;
diff --git a/src/settings/meson.build b/src/settings/meson.build
index f4c5f3f..d400a52 100644
--- a/src/settings/meson.build
+++ b/src/settings/meson.build
@@ -1,5 +1,5 @@
 settings_lib_sources = files(
-    'Application.vala',
+    'ApplicationContext.vala',
     'break/BreakInfoWidget.vala',
     'break/BreakSettingsWidget.vala',
     'break/BreakStatusWidget.vala',
@@ -55,19 +55,3 @@ settings_lib_dep = declare_dependency(
     include_directories: include_directories('.')
 )
 
-settings_exe_sources = files(
-    'main.vala'
-)
-
-executable(
-    'gnome-break-timer-settings',
-    sources : [
-        settings_exe_sources
-    ],
-    dependencies : [
-        settings_lib_dep,
-        break_timer_resources_dep
-    ],
-    install: true,
-    install_dir: bindir
-)


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