[gnome-maps/wip/mlundblad/es6: 5/5] WIP: Use ES6 classes with GObject extensions



commit 771509263c488396911bd14bdd7e1a6ba8a1286e
Author: Marcus Lundblad <ml update uu se>
Date:   Mon Nov 20 21:27:49 2017 +0100

    WIP: Use ES6 classes with GObject extensions

 src/accountListBox.js |   25 ++++++--------
 src/application.js    |   87 ++++++++++++++++++++++++-------------------------
 src/busyMarker.js     |    7 ++--
 src/checkIn.js        |   54 ++++++++++++++----------------
 4 files changed, 82 insertions(+), 91 deletions(-)
---
diff --git a/src/accountListBox.js b/src/accountListBox.js
index e45a21e..39c9357 100644
--- a/src/accountListBox.js
+++ b/src/accountListBox.js
@@ -21,25 +21,24 @@
 
 const Gio = imports.gi.Gio;
 const Goa = imports.gi.Goa;
+const GObject = imports.gi.GObject;
 const Gtk = imports.gi.Gtk;
 const Lang = imports.lang;
 
 const Application = imports.application;
 
-var AccountRow = new Lang.Class({
-    Name: 'AccountRow',
-    Extends: Gtk.ListBoxRow,
+var AccountRow = GObject.registerClass({
     Template: 'resource:///org/gnome/Maps/ui/account-row.ui',
     InternalChildren: [ 'providerLabel',
                         'identityLabel',
                         'providerImage',
                         'attentionNeededImage' ],
-
-    _init: function(params) {
+}, class AccountRow extends Gtk.ListBoxRow {
+    _init(params) {
         this.account = params.account;
         delete params.account;
 
-        this.parent(params);
+        super._init(params);
 
         let account = this.account.get_account();
 
@@ -50,16 +49,14 @@ var AccountRow = new Lang.Class({
     }
 });
 
-var AccountListBox = new Lang.Class({
-    Name: 'AccountListBox',
-    Extends: Gtk.ListBox,
+var AccountListBox = GObject.registerClass({
     Signals: {
         'account-selected': { param_types: [Goa.Object] }
     },
-
-    _init: function(params) {
+}, class AccountListBox extends Gtk.ListBox {
+    _init(params) {
         params.activate_on_single_click = true;
-        this.parent(params);
+        super._init(params);
 
         Application.checkInManager.connect('accounts-refreshed', () => { this.refresh(); });
 
@@ -67,9 +64,9 @@ var AccountListBox = new Lang.Class({
                      (list, row) => { this.emit('account-selected', row.account); });
 
         this.refresh();
-    },
+    }
 
-    refresh: function() {
+    refresh() {
         let accounts = Application.checkInManager.accounts;
 
         this.forall(function(row) {
diff --git a/src/application.js b/src/application.js
index c155ba3..6e0eeac 100644
--- a/src/application.js
+++ b/src/application.js
@@ -62,9 +62,7 @@ var routeQuery = null;
 const _ensuredTypes = [WebKit2.WebView,
                        OSMTypeSearchEntry.OSMTypeSearchEntry];
 
-var Application = new Lang.Class({
-    Name: 'Application',
-    Extends: Gtk.Application,
+var Application = GObject.registerClass({
     Properties: {
         'connected': GObject.ParamSpec.boolean('connected',
                                                '',
@@ -72,24 +70,25 @@ var Application = new Lang.Class({
                                                GObject.ParamFlags.READABLE |
                                                GObject.ParamFlags.WRITABLE)
     },
+}, class Application extends Gtk.Application {
 
     set connected(p) {
         this._connected = p;
         this.notify('connected');
-    },
+    }
 
     get connected() {
         return this._connected;
-    },
+    }
 
-    _init: function() {
+    _init() {
         /* Translators: This is the program name. */
         GLib.set_application_name(_("Maps"));
 
         /* Needed to be able to use in UI files */
         _ensuredTypes.forEach((type) => { GObject.type_ensure(type); });
 
-        this.parent({ application_id: 'org.gnome.Maps',
+        super._init({ application_id: 'org.gnome.Maps',
                       flags: Gio.ApplicationFlags.HANDLES_OPEN });
         this._connected = false;
 
@@ -118,13 +117,13 @@ var Application = new Lang.Class({
 
             return -1;
         });
-    },
+    }
 
-    _checkNetwork: function() {
+    _checkNetwork() {
         this.connected = networkMonitor.connectivity === Gio.NetworkConnectivity.FULL;
-    },
+    }
 
-    _showContact: function(id) {
+    _showContact(id) {
         contactStore.lookup(id, (contact) => {
             this._mainWindow.markBusy();
             if (!contact) {
@@ -136,9 +135,9 @@ var Application = new Lang.Class({
                 this._mainWindow.mapView.showContact(contact);
             });
         });
-    },
+    }
 
-    _onShowContactActivate: function(action, parameter) {
+    _onShowContactActivate(action, parameter) {
         this._createWindow();
         this._checkNetwork();
         this._mainWindow.present();
@@ -153,20 +152,20 @@ var Application = new Lang.Class({
                     this._showContact(id);
             });
         }
-    },
+    }
 
-    _onQuitActivate: function() {
+    _onQuitActivate() {
         this._mainWindow.destroy();
-    },
+    }
 
-    _onOsmAccountSetupActivate: function() {
+    _onOsmAccountSetupActivate() {
         let dialog = osmEdit.createAccountDialog(this._mainWindow, false);
 
         dialog.show();
         dialog.connect('response', () => { dialog.destroy(); });
-    },
+    }
 
-    _addContacts: function() {
+    _addContacts() {
         contactStore.get_contacts().forEach((contact) => {
             contact.geocode(function() {
                 contact.get_places().forEach((p) => {
@@ -180,9 +179,9 @@ var Application = new Lang.Class({
                 });
             });
         });
-    },
+    }
 
-    _initPlaceStore: function() {
+    _initPlaceStore() {
         placeStore = new PlaceStore.PlaceStore({
             recentPlacesLimit: settings.get('recent-places-limit'),
             recentRoutesLimit: settings.get('recent-routes-limit')
@@ -202,18 +201,18 @@ var Application = new Lang.Class({
                     this._addContacts();
             });
         }
-    },
+    }
 
-    _initAppMenu: function() {
+    _initAppMenu() {
         let builder = new Gtk.Builder();
         builder.add_from_resource('/org/gnome/Maps/ui/app-menu.ui');
 
         let menu = builder.get_object('app-menu');
         this.set_app_menu(menu);
-    },
+    }
 
-    vfunc_startup: function() {
-        this.parent();
+    vfunc_startup() {
+        super.vfunc_startup();
 
         GtkClutter.init(null);
 
@@ -237,9 +236,9 @@ var Application = new Lang.Class({
                                                                              'icons']));
         this._initPlaceStore();
         this._initAppMenu();
-    },
+    }
 
-    _initServices: function() {
+    _initServices() {
         settings         = Settings.getSettings('org.gnome.Maps');
         routeQuery       = new RouteQuery.RouteQuery();
         routingDelegator = new RoutingDelegator.RoutingDelegator({ query: routeQuery });
@@ -252,9 +251,9 @@ var Application = new Lang.Class({
         contactStore = new Maps.ContactStore();
         contactStore.load();
         osmEdit = new OSMEdit.OSMEdit();
-    },
+    }
 
-    _createWindow: function() {
+    _createWindow() {
         if (this._mainWindow)
             return;
 
@@ -268,24 +267,24 @@ var Application = new Lang.Class({
                 log('* focus widget: %s'.format(widget));
             });
         }
-    },
+    }
 
-    vfunc_dbus_register: function(connection, path) {
-        this.parent(connection, path);
+    vfunc_dbus_register(connection, path) {
+        super.vfunc_dbus_register(connection, path);
         return true;
-    },
+    }
 
-    vfunc_dbus_unregister: function(connection, path) {
-        this.parent(connection, path);
-    },
+    vfunc_dbus_unregister(connection, path) {
+        super.vfunc_dbus_register(connection, path);
+    }
 
-    vfunc_activate: function() {
+    vfunc_activate() {
         this._createWindow();
         this._checkNetwork();
         this._mainWindow.present();
-    },
+    }
 
-    _openInternal: function(files) {
+    _openInternal(files) {
         if (!this._mainWindow || !this._mainWindow.mapView.view.realized)
             return;
 
@@ -298,9 +297,9 @@ var Application = new Lang.Class({
         } else {
             this._mainWindow.mapView.openShapeLayers(files);
         }
-    },
+    }
 
-    vfunc_open: function(files) {
+    vfunc_open(files) {
         normalStartup = false;
         this.activate();
 
@@ -310,9 +309,9 @@ var Application = new Lang.Class({
         else
             mapView.view.connect('notify::realized',
                                  this._openInternal.bind(this, files));
-    },
+    }
 
-    _onWindowDestroy: function(window) {
+    _onWindowDestroy(window) {
         this._mainWindow = null;
     }
 });
diff --git a/src/busyMarker.js b/src/busyMarker.js
index b53962d..4c16304 100644
--- a/src/busyMarker.js
+++ b/src/busyMarker.js
@@ -18,10 +18,9 @@
  */
 
 const Gtk = imports.gi.Gtk;
+const GObject = imports.gi.GObject;
 const Lang = imports.lang;
 
-var BusyMarker = new Lang.Class({
-    Name: 'BusyMarker',
-    Extends: Gtk.Frame,
+var BusyMarker = GObject.registerClass({
     Template: 'resource:///org/gnome/Maps/ui/busy-marker.ui'
-});
+}, class BusyMarker extends Gtk.Frame {});
diff --git a/src/checkIn.js b/src/checkIn.js
index d75d492..194356b 100644
--- a/src/checkIn.js
+++ b/src/checkIn.js
@@ -28,9 +28,7 @@ const CheckInDialog = imports.checkInDialog;
 const FacebookBackend = imports.facebookBackend;
 const FoursquareBackend = imports.foursquareBackend;
 
-var CheckInManager = new Lang.Class({
-    Name: 'CheckInManager',
-    Extends: GObject.Object,
+var CheckInManager = GObject.registerClass({
     Signals: {
         'accounts-refreshed': { }
     },
@@ -39,10 +37,10 @@ var CheckInManager = new Lang.Class({
                                                 '',
                                                 '',
                                                 GObject.ParamFlags.READABLE)
-    },
-
-    _init: function() {
-        this.parent();
+    }
+}, class CheckInManager extends GObject.Object {
+    _init() {
+        super._init();
 
         try {
             this._goaClient = Goa.Client.new_sync(null);
@@ -66,17 +64,17 @@ var CheckInManager = new Lang.Class({
         }
 
         this._refreshGoaAccounts();
-    },
+    }
 
-    _initBackends: function() {
+    _initBackends() {
         let facebookBackend = new FacebookBackend.FacebookBackend();
         this._backends[facebookBackend.name] = facebookBackend;
 
         let foursquareBackend = new FoursquareBackend.FoursquareBackend();
         this._backends[foursquareBackend.name] = foursquareBackend;
-    },
+    }
 
-    _refreshGoaAccounts: function() {
+    _refreshGoaAccounts() {
         if (!this._goaClient)
             return;
         let accounts = this._goaClient.get_accounts();
@@ -99,39 +97,39 @@ var CheckInManager = new Lang.Class({
 
         this.emit('accounts-refreshed');
         this.notify('hasCheckIn');
-    },
+    }
 
     get client() {
         return this._goaClient;
-    },
+    }
 
     get accounts() {
         return this._accounts;
-    },
+    }
 
     get hasCheckIn() {
         return this._accounts.length > 0;
-    },
+    }
 
-    _getAuthorizer: function(account) {
+    _getAuthorizer(account) {
         return this._authorizers[account.get_account().id];
-    },
+    }
 
-    _getBackend: function(account) {
+    _getBackend(account) {
         return this._backends[account.get_account().provider_type];
-    },
+    }
 
-    performCheckIn: function(account, checkIn, callback, cancellable) {
+    performCheckIn(account, checkIn, callback, cancellable) {
         this._getBackend(account)
             .performCheckIn(this._getAuthorizer(account), checkIn, callback, cancellable);
-    },
+    }
 
-    findPlaces: function(account, latitude, longitude, distance, callback, cancellable) {
+    findPlaces(account, latitude, longitude, distance, callback, cancellable) {
         this._getBackend(account)
             .findPlaces(this._getAuthorizer(account), latitude, longitude, distance, callback, cancellable);
-    },
+    }
 
-    showCheckInDialog: function(parentWindow, place, matchPlace) {
+    showCheckInDialog(parentWindow, place, matchPlace) {
         let dialog = new CheckInDialog.CheckInDialog({ transient_for: parentWindow,
                                                        matchPlace: matchPlace,
                                                        place: place });
@@ -170,14 +168,12 @@ var CheckInManager = new Lang.Class({
     }
 });
 
-var CheckIn = new Lang.Class({
-    Name: 'CheckIn',
-
-    _init: function() {
+class CheckIn {
+    _init() {
         this.message = null;
         this.place = null;
         this.privacy = null;
         this.broadcastFacebook = false;
         this.broadcastTwitter = false;
     }
-});
+};


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