elementary / calculator

Calculator app designed for elementary OS
https://elementary.io
GNU General Public License v3.0
80 stars 29 forks source link

Application: use startup () and clean up #237

Closed danirabbit closed 1 year ago

danirabbit commented 1 year ago

Rewrite the application class to be more modern, including using startup () and naming it Application

ryonakano commented 1 year ago

FYI:

$ git diff -w origin/master:src/PantheonCalculator.vala origin/application-rewrite:src/Application.vala > diff
$ cat diff 
diff --git a/src/PantheonCalculator.vala b/src/Application.vala
index 54c2c72..b38fc81 100644
--- a/src/PantheonCalculator.vala
+++ b/src/Application.vala
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2018 elementary LLC. (https://elementary.io)
+ * Copyright 2018-2023 elementary, Inc. (https://elementary.io)
  *           2014 Marvin Beckers <beckersmarvin@gmail.com>
  *
  * This program is free software: you can redistribute it and/or modify
@@ -18,18 +18,21 @@
  * Authored by: Marvin Beckers <beckersmarvin@gmail.com>
  */

-namespace PantheonCalculator {
-    PantheonCalculator.MainWindow window = null;
+public class PantheonCalculator.Application : Gtk.Application {
+    private MainWindow window;

-    public class PantheonCalculatorApp : Gtk.Application {
     construct {
         application_id = "io.elementary.calculator";
         flags = ApplicationFlags.FLAGS_NONE;

         Intl.setlocale (LocaleCategory.ALL, "");
-            GLib.Intl.bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
-            GLib.Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
-            GLib.Intl.textdomain (GETTEXT_PACKAGE);
+        Intl.bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+        Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+        Intl.textdomain (GETTEXT_PACKAGE);
+    }
+
+    public override void startup () {
+        base.startup ();

         var quit_action = new SimpleAction ("quit", null);
         var undo_action = new SimpleAction ("undo", null);
@@ -40,24 +43,18 @@ namespace PantheonCalculator {
         set_accels_for_action ("app.quit", {"<Control>q"});
         set_accels_for_action ("app.undo", {"<Control>z"});

-            quit_action.activate.connect (() => {
-                if (window != null) {
-                    window.destroy ();
-                }
-            });
-
-            undo_action.activate.connect (() => window.undo ());
+        quit_action.activate.connect (quit);
+        undo_action.activate.connect (window.undo);
     }

     public override void activate () {
-            window = new PantheonCalculator.MainWindow ();
+        window = new MainWindow ();
         window.present ();
-            this.add_window (window);
-        }
+
+        add_window (window);
     }

     public static int main (string[] args) {
-        var application = new PantheonCalculatorApp ();
-        return application.run (args);
+        return new PantheonCalculator.Application ().run (args);
     }
 }