[gnome-weather/wip/ewlsh/gtk4] File cleanup
- From: Evan Welsh <ewlsh src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-weather/wip/ewlsh/gtk4] File cleanup
- Date: Sun, 30 Jan 2022 22:05:34 +0000 (UTC)
commit ef56c7cc3251ce7b6988181dfc313598cb798990
Author: Evan Welsh <contact evanwelsh com>
Date: Sun Jan 30 14:05:29 2022 -0800
File cleanup
src/app/application.js | 227 +++++++++++++++++++++++++
src/app/main.js | 260 +----------------------------
src/app/shell.js | 57 +++++++
src/org.gnome.Weather.in | 2 +-
src/org.gnome.Weather.src.gresource.xml.in | 2 +
5 files changed, 292 insertions(+), 256 deletions(-)
---
diff --git a/src/app/application.js b/src/app/application.js
new file mode 100644
index 0000000..cacbd14
--- /dev/null
+++ b/src/app/application.js
@@ -0,0 +1,227 @@
+//
+// Copyright (c) 2012 Giovanni Campagna <scampa giovanni gmail com>
+//
+// Gnome Weather 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 2 of the License, or (at your
+// option) any later version.
+//
+// Gnome Weather 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 Gnome Weather; if not, write to the Free Software Foundation,
+// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+import Adw from 'gi://Adw';
+import Gio from 'gi://Gio';
+import GLib from 'gi://GLib';
+import GObject from 'gi://GObject';
+import Gtk from 'gi://Gtk';
+import GWeather from 'gi://GWeather';
+
+// ensure the type before we call to GtkBuilder
+import './entry.js';
+
+import * as Util from '../misc/util.js';
+import * as Window from './window.js';
+import * as World from '../shared/world.js';
+import * as CurrentLocationController from './currentLocationController.js';
+
+import { ShellIntegration } from './shell.js';
+
+export const Application = GObject.registerClass(
+ class WeatherApplication extends Adw.Application {
+
+ _init() {
+ super._init({
+ application_id: pkg.name,
+ flags: Gio.ApplicationFlags.CAN_OVERRIDE_APP_ID,
+ });
+ let name_prefix = '';
+ if (pkg.name.endsWith('Devel')) {
+ name_prefix = '(Development) ';
+ }
+ GLib.set_application_name(name_prefix + _("Weather"));
+ Gtk.Window.set_default_icon_name(pkg.name);
+
+ this._mainWindow = undefined;
+ }
+
+ get mainWindow() {
+ return this._mainWindow;
+ }
+
+ set mainWindow(value) {
+ this._mainWindow = value;
+ }
+
+ _onQuit() {
+ this.quit();
+ }
+
+ _onShowLocation(action, parameter) {
+ let location = this.world.deserialize(parameter.deep_unpack());
+ let win = this._createWindow();
+
+ let info = this.model.addNewLocation(location, false);
+ win.showInfo(info, false);
+ this._showWindowWhenReady(win);
+ }
+
+ _onShowSearch(action, parameter) {
+ let text = parameter.deep_unpack();
+ let win = this._createWindow();
+
+ win.showSearch(text);
+ this._showWindowWhenReady(win);
+ }
+
+ vfunc_startup() {
+ super.vfunc_startup();
+
+ Util.loadStyleSheet('resource:///org/gnome/Weather/application.css');
+
+ this.world = GWeather.Location.get_world();
+ this.model = new World.WorldModel(this.world, true);
+ this.currentLocationController = new
CurrentLocationController.CurrentLocationController(this.model);
+
+ this.model.load();
+
+
+ this.model.connect('notify::loading', () => {
+ if (this.model.loading)
+ this.mark_busy();
+ else
+ this.unmark_busy();
+ });
+ if (this.model.loading)
+ this.mark_busy();
+
+ let quitAction = new Gio.SimpleAction({
+ enabled: true,
+ name: 'quit'
+ });
+ quitAction.connect('activate', () => this._onQuit());
+ this.add_action(quitAction);
+
+ let showLocationAction = new Gio.SimpleAction({
+ enabled: true,
+ name: 'show-location',
+ parameter_type: new GLib.VariantType('v'),
+ });
+ showLocationAction.connect('activate', (action, parameter) => {
+ this._onShowLocation(action, parameter);
+ });
+ this.add_action(showLocationAction);
+
+ let showSearchAction = new Gio.SimpleAction({
+ enabled: true,
+ name: 'show-search',
+ parameter_type: new GLib.VariantType('v'),
+ })
+ showSearchAction.connect('activate', (action, parameter) => {
+ this._onShowSearch(action, parameter);
+ });
+ this.add_action(showSearchAction);
+
+ let gwSettings = new Gio.Settings({ schema_id: 'org.gnome.GWeather' });
+ // we would like to use g_settings_create_action() here
+ // but that does not handle correctly the case of 'default'
+ // we would also like to use g_settings_bind_with_mapping(), but that
+ // function is not introspectable (two callbacks, one destroy notify)
+ // so we hand code the behavior we want
+ function resolveDefaultTemperatureUnit(unit) {
+ unit = GWeather.TemperatureUnit.to_real(unit);
+ if (unit == GWeather.TemperatureUnit.CENTIGRADE)
+ return new GLib.Variant('s', 'centigrade');
+ else if (unit == GWeather.TemperatureUnit.FAHRENHEIT)
+ return new GLib.Variant('s', 'fahrenheit');
+ else
+ return new GLib.Variant('s', 'default');
+ }
+ let temperatureAction = new Gio.SimpleAction({
+ enabled: true,
+ name: 'temperature-unit',
+ state: resolveDefaultTemperatureUnit(gwSettings.get_enum('temperature-unit')),
+ parameter_type: new GLib.VariantType('s')
+ });
+ temperatureAction.connect('activate', function (action, parameter) {
+ action.change_state(parameter);
+ })
+ temperatureAction.connect('change-state', function (action, state) {
+ gwSettings.set_value('temperature-unit', state);
+ });
+ gwSettings.connect('changed::temperature-unit', function () {
+ temperatureAction.state =
resolveDefaultTemperatureUnit(gwSettings.get_enum('temperature-unit'));
+ });
+ this.add_action(temperatureAction);
+
+ this.set_accels_for_action("win.selection-mode", ["Escape"]);
+ this.set_accels_for_action("win.select-all", ["<Primary>a"]);
+ this.set_accels_for_action("app.quit", ["<Primary>q"]);
+ }
+
+ vfunc_dbus_register(conn, path) {
+ this._shellIntegration = new ShellIntegration();
+ this._shellIntegration.export(conn, path);
+ return true;
+ }
+
+ vfunc_dbus_unregister(conn, path) {
+ this._shellIntegration.unexport(conn);
+ }
+
+ _createWindow() {
+ const window = new Window.MainWindow({ application: this });
+
+ // Store a weak reference to the window for cleanup...
+ this.mainWindow = window;
+
+ return window;
+ }
+
+ _showWindowWhenReady(win) {
+ let notifyId;
+ win.present();
+ if (this.model.loading) {
+ let timeoutId;
+ let model = this.model;
+
+ timeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000, function () {
+ log('Timeout during model load, perhaps the network is not available?');
+ model.disconnect(notifyId);
+
+ return false;
+ });
+ notifyId = this.model.connect('notify::loading', function (model) {
+ if (model.loading)
+ return;
+
+ model.disconnect(notifyId);
+ GLib.source_remove(timeoutId);
+ });
+ }
+
+ return win;
+ }
+
+ vfunc_activate() {
+ let win = this._createWindow();
+ win.showDefault();
+ this._showWindowWhenReady(win);
+ }
+
+ vfunc_shutdown() {
+ GWeather.Info.store_cache();
+ this.model.saveSettingsNow();
+
+ // Ensure our main window is cleaned up before we exit.
+ this.mainWindow?.run_dispose();
+ this.mainWindow = undefined;
+
+ super.vfunc_shutdown();
+ }
+ });
diff --git a/src/app/main.js b/src/app/main.js
index d66851d..22ba956 100644
--- a/src/app/main.js
+++ b/src/app/main.js
@@ -16,10 +16,6 @@
// with Gnome Weather; if not, write to the Free Software Foundation,
// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-pkg.initFormat();
-pkg.initGettext();
-globalThis.ngettext = imports.gettext.ngettext;
-
import 'gi://Gdk?version=4.0';
import 'gi://Gio?version=2.0';
import 'gi://GLib?version=2.0';
@@ -30,262 +26,16 @@ import 'gi://GWeather?version=4.0';
import * as system from 'system';
-import Adw from 'gi://Adw';
import Gio from 'gi://Gio';
-import GLib from 'gi://GLib';
-import GObject from 'gi://GObject';
-import Gtk from 'gi://Gtk';
-import GWeather from 'gi://GWeather';
-
-// ensure the type before we call to GtkBuilder
-import './entry.js';
-
-import * as Util from '../misc/util.js';
-import * as Window from './window.js';
-import * as World from '../shared/world.js';
-import * as CurrentLocationController from './currentLocationController.js';
-
-
-const ShellIntegrationInterface = new TextDecoder().decode(
- Gio.resources_lookup_data('/org/gnome/shell/ShellWeatherIntegration.xml', 0).get_data()
-);
-
-const MAIN_WINDOW = {};
-
-const Application = GObject.registerClass(
- class WeatherApplication extends Adw.Application {
-
- _init() {
- super._init({
- application_id: pkg.name,
- flags: Gio.ApplicationFlags.CAN_OVERRIDE_APP_ID,
- });
- let name_prefix = '';
- if (pkg.name.endsWith('Devel')) {
- name_prefix = '(Development) ';
- }
- 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() {
- this.quit();
- }
-
- _onShowLocation(action, parameter) {
- let location = this.world.deserialize(parameter.deep_unpack());
- let win = this._createWindow();
-
- let info = this.model.addNewLocation(location, false);
- win.showInfo(info, false);
- this._showWindowWhenReady(win);
- }
-
- _onShowSearch(action, parameter) {
- let text = parameter.deep_unpack();
- let win = this._createWindow();
-
- win.showSearch(text);
- this._showWindowWhenReady(win);
- }
-
- vfunc_startup() {
- super.vfunc_startup();
-
- Util.loadStyleSheet('resource:///org/gnome/Weather/application.css');
-
- this.world = GWeather.Location.get_world();
- this.model = new World.WorldModel(this.world, true);
- this.currentLocationController = new
CurrentLocationController.CurrentLocationController(this.model);
-
- this.model.load();
-
-
- this.model.connect('notify::loading', () => {
- if (this.model.loading)
- this.mark_busy();
- else
- this.unmark_busy();
- });
- if (this.model.loading)
- this.mark_busy();
-
- let quitAction = new Gio.SimpleAction({
- enabled: true,
- name: 'quit'
- });
- quitAction.connect('activate', () => this._onQuit());
- this.add_action(quitAction);
-
- let showLocationAction = new Gio.SimpleAction({
- enabled: true,
- name: 'show-location',
- parameter_type: new GLib.VariantType('v'),
- });
- showLocationAction.connect('activate', (action, parameter) => {
- this._onShowLocation(action, parameter);
- });
- this.add_action(showLocationAction);
- let showSearchAction = new Gio.SimpleAction({
- enabled: true,
- name: 'show-search',
- parameter_type: new GLib.VariantType('v'),
- })
- showSearchAction.connect('activate', (action, parameter) => {
- this._onShowSearch(action, parameter);
- });
- this.add_action(showSearchAction);
+import {Application} from './application.js';
- let gwSettings = new Gio.Settings({ schema_id: 'org.gnome.GWeather' });
- // we would like to use g_settings_create_action() here
- // but that does not handle correctly the case of 'default'
- // we would also like to use g_settings_bind_with_mapping(), but that
- // function is not introspectable (two callbacks, one destroy notify)
- // so we hand code the behavior we want
- function resolveDefaultTemperatureUnit(unit) {
- unit = GWeather.TemperatureUnit.to_real(unit);
- if (unit == GWeather.TemperatureUnit.CENTIGRADE)
- return new GLib.Variant('s', 'centigrade');
- else if (unit == GWeather.TemperatureUnit.FAHRENHEIT)
- return new GLib.Variant('s', 'fahrenheit');
- else
- return new GLib.Variant('s', 'default');
- }
- let temperatureAction = new Gio.SimpleAction({
- enabled: true,
- name: 'temperature-unit',
- state: resolveDefaultTemperatureUnit(gwSettings.get_enum('temperature-unit')),
- parameter_type: new GLib.VariantType('s')
- });
- temperatureAction.connect('activate', function (action, parameter) {
- action.change_state(parameter);
- })
- temperatureAction.connect('change-state', function (action, state) {
- gwSettings.set_value('temperature-unit', state);
- });
- gwSettings.connect('changed::temperature-unit', function () {
- temperatureAction.state =
resolveDefaultTemperatureUnit(gwSettings.get_enum('temperature-unit'));
- });
- this.add_action(temperatureAction);
-
- this.set_accels_for_action("win.selection-mode", ["Escape"]);
- this.set_accels_for_action("win.select-all", ["<Primary>a"]);
- this.set_accels_for_action("app.quit", ["<Primary>q"]);
- }
-
- vfunc_dbus_register(conn, path) {
- this._shellIntegration = new ShellIntegration();
- this._shellIntegration.export(conn, path);
- return true;
- }
-
- vfunc_dbus_unregister(conn, path) {
- this._shellIntegration.unexport(conn);
- }
-
- _createWindow() {
- const window = new Window.MainWindow({ application: this });
-
- // Store a weak reference to the window for cleanup...
- this.mainWindow = window;
-console.log('created...')
- return window;
- }
-
- _showWindowWhenReady(win) {
- let notifyId;
- win.present();
- if (this.model.loading) {
- let timeoutId;
- let model = this.model;
-
- timeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000, function () {
- log('Timeout during model load, perhaps the network is not available?');
- model.disconnect(notifyId);
-
- return false;
- });
- notifyId = this.model.connect('notify::loading', function (model) {
- if (model.loading)
- return;
-
- model.disconnect(notifyId);
- GLib.source_remove(timeoutId);
- });
- }
-
- return win;
- }
-
- vfunc_activate() {
- let win = this._createWindow();
- win.showDefault();
- this._showWindowWhenReady(win);
- }
-
- vfunc_shutdown() {
- GWeather.Info.store_cache();
- this.model.saveSettingsNow();
-
- // Ensure our main window is cleaned up before we exit.
- this.mainWindow?.run_dispose();
-
- super.vfunc_shutdown();
- }
- });
-
-let ShellIntegration = class ShellIntegration {
- constructor() {
- this._impl = Gio.DBusExportedObject.wrapJSObject(
- ShellIntegrationInterface, this);
-
- this._settings = new Gio.Settings({ schema_id: 'org.gnome.Weather' });
-
- this._settings.connect('changed::automatic-location', () => {
- this._impl.emit_property_changed('AutomaticLocation',
- new GLib.Variant('b', this.AutomaticLocation));
- });
- this._settings.connect('changed::locations', () => {
- this._impl.emit_property_changed('Locations',
- new GLib.Variant('av', this.Locations));
- });
- }
-
- export(connection, path) {
- return this._impl.export(connection, path);
- }
-
- unexport(connection) {
- return this._impl.unexport_from_connection(connection);
- }
-
- get AutomaticLocation() {
- return this._settings.get_boolean('automatic-location');
- }
-
- get Locations() {
- return this._settings.get_value('locations').deep_unpack();
- }
-};
+pkg.initFormat();
+pkg.initGettext();
+globalThis.ngettext = imports.gettext.ngettext;
globalThis.getApp = function () {
return Gio.Application.get_default();
};
-let application = new Application();
-
-application.run([system.programInvocationName, ...system.programArgs]);
-application = null;
-
+new Application().run([system.programInvocationName, ...system.programArgs]);
diff --git a/src/app/shell.js b/src/app/shell.js
new file mode 100644
index 0000000..eeab176
--- /dev/null
+++ b/src/app/shell.js
@@ -0,0 +1,57 @@
+//
+// Copyright (c) 2012 Giovanni Campagna <scampa giovanni gmail com>
+//
+// Gnome Weather 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 2 of the License, or (at your
+// option) any later version.
+//
+// Gnome Weather 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 Gnome Weather; if not, write to the Free Software Foundation,
+// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+import Gio from 'gi://Gio';
+import GLib from 'gi://GLib';
+
+const ShellIntegrationInterface = new TextDecoder().decode(
+ Gio.resources_lookup_data('/org/gnome/shell/ShellWeatherIntegration.xml', 0).get_data()
+);
+
+export class ShellIntegration {
+ constructor() {
+ this._impl = Gio.DBusExportedObject.wrapJSObject(
+ ShellIntegrationInterface, this);
+
+ this._settings = new Gio.Settings({ schema_id: 'org.gnome.Weather' });
+
+ this._settings.connect('changed::automatic-location', () => {
+ this._impl.emit_property_changed('AutomaticLocation',
+ new GLib.Variant('b', this.AutomaticLocation));
+ });
+ this._settings.connect('changed::locations', () => {
+ this._impl.emit_property_changed('Locations',
+ new GLib.Variant('av', this.Locations));
+ });
+ }
+
+ export(connection, path) {
+ return this._impl.export(connection, path);
+ }
+
+ unexport(connection) {
+ return this._impl.unexport_from_connection(connection);
+ }
+
+ get AutomaticLocation() {
+ return this._settings.get_boolean('automatic-location');
+ }
+
+ get Locations() {
+ return this._settings.get_value('locations').deep_unpack();
+ }
+};
diff --git a/src/org.gnome.Weather.in b/src/org.gnome.Weather.in
index 901f7d1..3d3b4bc 100755
--- a/src/org.gnome.Weather.in
+++ b/src/org.gnome.Weather.in
@@ -5,6 +5,6 @@ imports.package.init({ name: "@APP_ID@",
libdir: "@libdir@" });
import(`resource:///org/gnome/Weather@profile@/js/app/main.js`).catch(error => {
- console.error(error);
+ logError(error);
imports.system.exit(1);
});
diff --git a/src/org.gnome.Weather.src.gresource.xml.in b/src/org.gnome.Weather.src.gresource.xml.in
index 6bea654..1383c50 100644
--- a/src/org.gnome.Weather.src.gresource.xml.in
+++ b/src/org.gnome.Weather.src.gresource.xml.in
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/gnome/Weather@profile@/js">
+ <file>app/application.js</file>
<file>app/city.js</file>
<file>app/currentLocationController.js</file>
<file>app/hourlyForecast.js</file>
@@ -9,6 +10,7 @@
<file>app/dailyForecast.js</file>
<file>app/entry.js</file>
<file>app/main.js</file>
+ <file>app/shell.js</file>
<file>app/window.js</file>
<file>app/world.js</file>
<file>misc/util.js</file>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]