[gnome-weather/wip/ewlsh/gtk4] Improve window cleanup handling



commit 4ea43136944c6181a68427e5c9b97477e80365fa
Author: Evan Welsh <contact evanwelsh com>
Date:   Sun Jan 2 01:14:39 2022 -0600

    Improve window cleanup handling

 src/app/main.js        | 29 ++++++++++++++++++++++-------
 src/app/thermometer.js | 20 ++++----------------
 src/app/world.js       |  6 +++---
 3 files changed, 29 insertions(+), 26 deletions(-)
---
diff --git a/src/app/main.js b/src/app/main.js
index 49f0c72..40bbe6f 100644
--- a/src/app/main.js
+++ b/src/app/main.js
@@ -57,6 +57,8 @@ function initEnvironment() {
     };
 }
 
+const MAIN_WINDOW = {};
+
 const Application = GObject.registerClass(
     class WeatherApplication extends Adw.Application {
 
@@ -71,6 +73,17 @@ const Application = GObject.registerClass(
             }
             GLib.set_application_name(name_prefix + _("Weather"));
             Gtk.Window.set_default_icon_name(pkg.name);
+
+            // TODO: Use WeakRef once GJS migrates to mozjs91
+            this._mainWindow = new WeakMap();
+        }
+
+        get mainWindow() {
+            return this._mainWindow.get(MAIN_WINDOW);
+        }
+
+        set mainWindow(value) {
+            this._mainWindow.set(MAIN_WINDOW, value);
         }
 
         _onQuit() {
@@ -190,7 +203,12 @@ const Application = GObject.registerClass(
         }
 
         _createWindow() {
-            return new Window.MainWindow({ application: this });
+            const window = new Window.MainWindow({ application: this });
+
+            // Store a weak reference to the window for cleanup...
+            this.mainWindow = window;
+
+            return window;
         }
 
         _showWindowWhenReady(win) {
@@ -231,6 +249,9 @@ const Application = GObject.registerClass(
             GWeather.Info.store_cache();
             this.model.saveSettingsNow();
 
+            // Ensure our main window is cleaned up before we exit.
+            this.mainWindow?.run_dispose();
+
             super.vfunc_shutdown();
         }
     });
@@ -274,12 +295,6 @@ function main(argv) {
 
     let application = new Application();
 
-    application.connect("window-removed", (_, window) => {
-        if (window instanceof Window.MainWindow) {
-            window.run_dispose();
-        }
-    });
-
     application.run(argv);
 
     application = null;
diff --git a/src/app/thermometer.js b/src/app/thermometer.js
index ba5f4b6..67959a3 100644
--- a/src/app/thermometer.js
+++ b/src/app/thermometer.js
@@ -76,11 +76,15 @@ const ThermometerScale = GObject.registerClass({
   }
 
   vfunc_root() {
+    super.vfunc_root();
+
     this.inner.set_parent(this);
   }
 
   vfunc_unroot() {
     this.inner.unparent();
+
+    super.vfunc_unroot();
   }
 
   vfunc_map() {
@@ -174,22 +178,6 @@ var Thermometer = GObject.registerClass({
     this.bind_property_full('range', this._highLabel, 'label', GObject.BindingFlags.DEFAULT, range => {
       return [!!range, Util.formatTemperature(range?.dailyHigh) ?? ''];
     }, null);
-
-    // Expression version
-    // const highExpression = new Gtk.ClosureExpression(String, thermometer => {
-    //   return Util.formatTemperature(thermometer.range?.dailyHigh) ?? '';
-    // }, [new Gtk.PropertyExpression(this, null, 'range')]);
-
-    // const lowExpression = new Gtk.ClosureExpression(String, thermometer => {
-    //   return Util.formatTemperature(thermometer.range?.dailyLow) ?? '';
-    // }, [new Gtk.PropertyExpression(this, null, 'range')]);
-
-    // highExpression.bind(this._highLabel, 'label', this);
-    // lowExpression.bind(this._lowLabel, 'label', this);
-  }
-
-  vfunc_unroot() {
-    super.vfunc_unroot();
   }
 });
 
diff --git a/src/app/world.js b/src/app/world.js
index 3279a55..36797fd 100644
--- a/src/app/world.js
+++ b/src/app/world.js
@@ -131,10 +131,10 @@ class _WorldContentView extends Gtk.Popover {
             this._onLocationAdded(this.model, list[i], list[i]._isCurrentLocation);
     }
 
-    _cleanup() {
-        this._locationEntry._cleanup();
-
+    vfunc_unroot() {
         this._listbox.set_header_func(null);
+
+        super.vfunc_unroot();
     }
 
     refilter() {


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