Commit 6977789e authored by Christopher Davis's avatar Christopher Davis

Use ES6 classes

The way we set up our classes was old and too verbose. Using
ES6 classes allows our code to be cleaner.
parent 2770208b
......@@ -19,8 +19,8 @@
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Gnome = imports.gi.GnomeDesktop;
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
const Forecast = imports.app.forecast;
const WForecast = imports.app.weeklyForecast;
......@@ -31,9 +31,7 @@ const SPINNER_SIZE = 128;
const SCROLLING_ANIMATION_TIME = 400000; //us
var WeatherWidget = new Lang.Class({
Name: 'WeatherWidget',
Extends: Gtk.Frame,
var WeatherWidget = GObject.registerClass({
Template: 'resource:///org/gnome/Weather/weather-widget.ui',
InternalChildren: ['contentFrame', 'outerGrid', 'conditionsImage',
'temperatureLabel', 'conditionsLabel',
......@@ -41,11 +39,12 @@ var WeatherWidget = new Lang.Class({
'leftButton', 'rightButton',
'forecast-today-grid', 'forecast-tomorrow-grid',
'forecast-today', 'forecast-tomorrow'],
}, class WeatherWidget extends Gtk.Frame {
_init: function(params) {
_init(params) {
params = Params.fill(params, { shadow_type: Gtk.ShadowType.NONE,
name: 'weather-page' });
this.parent(params);
super._init(params);
this._currentStyle = null;
this._info = null;
......@@ -100,9 +99,9 @@ var WeatherWidget = new Lang.Class({
this._beginScrollAnimation(target);
});
},
}
_syncLeftRightButtons: function() {
_syncLeftRightButtons() {
let hadjustment = this._forecastStack.visible_child.get_hadjustment();
if ((hadjustment.get_upper() - hadjustment.get_lower()) == hadjustment.page_size) {
this._leftButton.set_sensitive(false);
......@@ -117,9 +116,9 @@ var WeatherWidget = new Lang.Class({
this._leftButton.set_sensitive(true);
this._rightButton.set_sensitive(true);
}
},
}
_beginScrollAnimation: function(target) {
_beginScrollAnimation(target) {
let start = this.get_frame_clock().get_frame_time();
let end = start + SCROLLING_ANIMATION_TIME;
......@@ -127,9 +126,9 @@ var WeatherWidget = new Lang.Class({
this.remove_tick_callback(this._tickId);
this._tickId = this.add_tick_callback(() => this._animate(target, start, end));
},
}
_animate: function(target, start, end) {
_animate(target, start, end) {
let hadjustment = this._forecastStack.visible_child.get_hadjustment();
let value = hadjustment.value;
let t = 1.0;
......@@ -145,9 +144,9 @@ var WeatherWidget = new Lang.Class({
this._tickId = 0;
return false;
}
},
}
clear: function() {
clear() {
for (let t of ['today', 'tomorrow'])
this._forecasts[t].clear();
......@@ -155,23 +154,23 @@ var WeatherWidget = new Lang.Class({
this.remove_tick_callback(this._tickId);
this._tickId = 0;
}
},
}
_getStyleClass: function(info) {
_getStyleClass(info) {
let icon = info.get_icon_name();
let name = icon.replace(/(-\d{3})/, "");
return name;
},
}
setTimeVisible: function(visible) {
setTimeVisible(visible) {
this._timeGrid.visible = visible;
},
}
setTime: function(time) {
setTime(time) {
this._timeLabel.label = time;
},
}
update: function(info) {
update(info) {
this._info = info;
this._conditionsLabel.label = Util.getWeatherConditions(info);
......@@ -201,14 +200,13 @@ var WeatherWidget = new Lang.Class({
}
});
var WeatherView = new Lang.Class({
Name: 'WeatherView',
Extends: Gtk.Stack,
var WeatherView = GObject.registerClass({
Template: 'resource:///org/gnome/Weather/city.ui',
InternalChildren: ['spinner'],
InternalChildren: ['spinner']
}, class WeatherView extends Gtk.Stack {
_init: function(params) {
this.parent(params);
_init(params) {
super._init(params);
this._infoPage = new WeatherWidget();
this.add_named(this._infoPage, 'info');
......@@ -222,11 +220,11 @@ var WeatherView = new Lang.Class({
this._clockHandlerId = 0;
this._desktopSettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.interface' });
},
}
get info() {
return this._info;
},
}
set info(info) {
if (this._updateId) {
......@@ -243,32 +241,32 @@ var WeatherView = new Lang.Class({
if (info.is_valid())
this._onUpdate(info);
}
},
}
_onDestroy: function() {
_onDestroy() {
if (this._updateId) {
this._info.disconnect(this._updateId);
this._updateId = 0;
}
},
}
update: function() {
update() {
this.visible_child_name = 'loading';
this._spinner.start();
this._infoPage.clear();
getApp().model.updateInfo(this._info);
},
}
_onUpdate: function(info) {
_onUpdate(info) {
this._infoPage.clear();
this._infoPage.update(info);
this._updateTime();
this._spinner.stop();
this.visible_child_name = 'info';
},
}
setTimeVisible: function(visible) {
setTimeVisible(visible) {
if (this._clockHandlerId && !visible) {
this._wallClock.disconnect(this._clockHandlerId);
this._clockHandlerId = 0;
......@@ -279,13 +277,13 @@ var WeatherView = new Lang.Class({
}
this._infoPage.setTimeVisible(visible);
},
}
_updateTime: function() {
_updateTime() {
this._infoPage.setTime(this._getTime());
},
}
_getTime: function() {
_getTime() {
if (this._info != null) {
let location = this._info.location;
let tz = GLib.TimeZone.new(location.get_timezone().get_tzid());
......
......@@ -18,8 +18,8 @@
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
const Params = imports.misc.params;
const Util = imports.misc.util;
......@@ -27,13 +27,11 @@ const Util = imports.misc.util;
// In microseconds
const ONE_HOUR = 3600*1000*1000;
var ForecastBox = new Lang.Class({
Name: 'ForecastBox',
Extends: Gtk.Frame,
var ForecastBox = GObject.registerClass(class ForecastBox extends Gtk.Frame {
_init: function(params) {
_init(params) {
params = Params.fill(params, { shadow_type: Gtk.ShadowType.NONE });
this.parent(params);
super._init(params);
this.get_accessible().accessible_name = _("Forecast");
this._settings = new Gio.Settings({ schema_id: 'org.gnome.desktop.interface' });
......@@ -47,11 +45,11 @@ var ForecastBox = new Lang.Class({
this.add(this._grid);
this._hasForecastInfo = false;
},
}
// Ensure that infos are sufficiently spaced, and
// remove infos for the wrong day
_preprocess: function(now, infos) {
_preprocess(now, infos) {
let ret = [];
let i;
let current;
......@@ -91,9 +89,9 @@ var ForecastBox = new Lang.Class({
}
return ret;
},
}
update: function(infos, day) {
update(infos, day) {
let now = GLib.DateTime.new_now_local();
if (day == 'tomorrow')
now = now.add_days(1);
......@@ -115,9 +113,9 @@ var ForecastBox = new Lang.Class({
visible: true });
this._grid.attach(label, 0, 0, 1, 1);
}
},
}
_addOneInfo: function(info, col) {
_addOneInfo(info, col) {
let [ok, date] = info.get_value_update();
let datetime = GLib.DateTime.new_from_unix_local(date);
......@@ -149,13 +147,13 @@ var ForecastBox = new Lang.Class({
this._grid.attach(temperature, col, 2, 1, 1);
this._hasForecastInfo = true;
},
}
clear: function() {
clear() {
this._grid.foreach(function(w) { w.destroy(); });
},
}
hasForecastInfo: function() {
hasForecastInfo() {
return this._hasForecastInfo;
}
});
......@@ -28,9 +28,9 @@ pkg.require({ 'Gdk': '3.0',
const Gdk = imports.gi.Gdk;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const GWeather = imports.gi.GWeather;
const Lang = imports.lang;
const Util = imports.misc.util;
const Window = imports.app.window;
......@@ -43,40 +43,39 @@ function initEnvironment() {
};
}
const Application = new Lang.Class({
Name: 'WeatherApplication',
Extends: Gtk.Application,
const Application = GObject.registerClass(
class WeatherApplication extends Gtk.Application {
_init: function() {
this.parent({ application_id: pkg.name,
_init() {
super._init({ application_id: pkg.name,
flags: (Gio.ApplicationFlags.CAN_OVERRIDE_APP_ID | Gio.ApplicationFlags.FLAGS_NONE) });
GLib.set_application_name(_("Weather"));
Gtk.Window.set_default_icon_name("org.gnome.Weather");
},
}
_onQuit: function() {
_onQuit() {
this.quit();
},
}
_onShowLocation: function(action, parameter) {
_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: function(action, parameter) {
_onShowSearch(action, parameter) {
let text = parameter.deep_unpack();
let win = this._createWindow();
win.showSearch(text);
this._showWindowWhenReady(win);
},
}
vfunc_startup: function() {
this.parent();
vfunc_startup() {
super.vfunc_startup();
// ensure the type before we call to GtkBuilder
GWeather.LocationEntry;
......@@ -144,13 +143,13 @@ const Application = new Lang.Class({
this.add_accelerator("Escape", "win.selection-mode", new GLib.Variant('b', false));
this.add_accelerator("<Primary>a", "win.select-all", null);
this.add_accelerator("<Primary>q", "app.quit", null);
},
}
_createWindow: function() {
_createWindow() {
return new Window.MainWindow({ application: this });
},
}
_showWindowWhenReady: function(win) {
_showWindowWhenReady(win) {
let notifyId;
if (this.model.loading) {
......@@ -176,19 +175,19 @@ const Application = new Lang.Class({
}
return win;
},
}
vfunc_activate: function() {
vfunc_activate() {
let win = this._createWindow();
win.showDefault();
this._showWindowWhenReady(win);
},
}
vfunc_shutdown: function() {
vfunc_shutdown() {
GWeather.Info.store_cache();
this.model.saveSettingsNow();
this.parent();
super.vfunc_shutdown();
}
});
......
......@@ -18,21 +18,20 @@
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
const Params = imports.misc.params;
const Util = imports.misc.util;
var WeeklyForecastFrame = new Lang.Class({
Name: 'WeeklyForecastFrame',
Extends: Gtk.Frame,
var WeeklyForecastFrame = GObject.registerClass(
class WeeklyForecastFrame extends Gtk.Frame {
_init: function(params) {
_init(params) {
params = Params.fill(params, { shadow_type: Gtk.ShadowType.NONE,
name: 'weekly-forecast-frame',
width_request: 150 });
this.parent(params);
super._init(params);
this.get_accessible().accessible_name = _("Weekly Forecast");
this._settings = new Gio.Settings({ schema_id: 'org.gnome.desktop.interface' });
......@@ -45,10 +44,10 @@ var WeeklyForecastFrame = new Lang.Class({
row_homogeneous: true });
this.add(this._grid);
},
}
// get infos for the correct day
_preprocess: function(infos, day) {
_preprocess(infos, day) {
let ret = [];
let i;
......@@ -103,9 +102,9 @@ var WeeklyForecastFrame = new Lang.Class({
infoCount++;
}
return ret;
},
}
update: function(infos) {
update(infos) {
let day = GLib.DateTime.new_now_local();
day = day.add_days(1);
......@@ -145,9 +144,9 @@ var WeeklyForecastFrame = new Lang.Class({
grid.show();
this._grid.attach(grid, 0, i, 1, 1);
}
},
}
clear: function() {
clear() {
this._grid.foreach(function(w) { w.destroy(); });
}
});
......@@ -16,9 +16,9 @@
// with Gnome Weather; if not, write to the Free Software Foundation,
// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const GWeather = imports.gi.GWeather;
const Lang = imports.lang;
const City = imports.app.city;
const CurrentLocationController = imports.app.currentLocationController;
......@@ -32,12 +32,11 @@ const Page = {
CITY: 1
};
var MainWindow = new Lang.Class({
Name: 'MainWindow',
Extends: Gtk.ApplicationWindow,
var MainWindow = GObject.registerClass(
class MainWindow extends Gtk.ApplicationWindow {
_init: function(params) {
this.parent(params);
_init(params) {
super._init(params);
this._world = this.application.world;
this.currentInfo = null;
......@@ -102,20 +101,20 @@ var MainWindow = new Lang.Class({
this._pageWidgets[Page.CITY][i].hide();
this._showingDefault = false;
},
}
update: function() {
update() {
this._cityView.update();
},
}
_searchLocationChanged: function(entry) {
_searchLocationChanged(entry) {
if (entry.location) {
let info = this._model.addNewLocation(entry.location, false);
this.showInfo(info, false);
}
},
}
_getTitle: function() {
_getTitle() {
if (this._currentPage == Page.SEARCH)
return [_("Select Location"), null];
......@@ -133,9 +132,9 @@ var MainWindow = new Lang.Class({
return [city.get_name(), country.get_name()];
else
return [city.get_name(), null];
},
}
_goToPage: function(page) {
_goToPage(page) {
for (let i = 0; i < this._pageWidgets[this._currentPage].length; i++)
this._pageWidgets[this._currentPage][i].hide();
......@@ -150,9 +149,9 @@ var MainWindow = new Lang.Class({
let [title, subtitle] = this._getTitle();
this._header.title = title;
this._header.subtitle = subtitle;
},
}
showDefault: function() {
showDefault() {
this._showingDefault = true;
let clc = this.application.currentLocationController;
let autoLocation = clc.autoLocation;
......@@ -161,9 +160,9 @@ var MainWindow = new Lang.Class({
this.showInfo(this._model.getCurrentLocation(), false);
else if (autoLocation != CurrentLocationController.AutoLocation.ENABLED)
this.showInfo(this._model.getRecent(), false);
},
}
showSearch: function(text) {
showSearch(text) {
this._showingDefault = false;
this._cityView.setTimeVisible(false);
this._stack.set_visible_child(this._searchView);
......@@ -171,9 +170,9 @@ var MainWindow = new Lang.Class({
this._searchEntry.text = text;
if (text.length > 0)
this._searchEntry.get_completion().complete();
},
}
showInfo: function(info, isCurrentLocation) {
showInfo(info, isCurrentLocation) {
if (!info) {
if (isCurrentLocation && this._showingDefault)
this.showDefault();
......@@ -209,9 +208,9 @@ var MainWindow = new Lang.Class({
this._worldView.refilter();
this._stack.set_visible_child(this._cityView);
this._goToPage(Page.CITY);
},
}
_showAbout: function() {
_showAbout() {
let artists = [ 'Jakub Steiner <jimmac@gmail.com>',
'Pink Sherbet Photography (D. Sharon Pruitt)',
'Elliott Brown',
......@@ -252,9 +251,9 @@ var MainWindow = new Lang.Class({
aboutDialog.connect('response', function() {
aboutDialog.destroy();
});
},
}
_close: function() {
_close() {
this.destroy();
}
});
......@@ -21,20 +21,18 @@ const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Gtk = imports.gi.Gtk;
const GWeather = imports.gi.GWeather;
const Lang = imports.lang;
const CurrentLocationController = imports.app.currentLocationController;
const Params = imports.misc.params;
const Util = imports.misc.util;
var WorldContentView = new Lang.Class({
Name: 'WorldContentView',
Extends: Gtk.Popover,
var WorldContentView = GObject.registerClass(
class WorldContentView extends Gtk.Popover {
_init: function(application, window, params) {
_init(application, window, params) {
params = Params.fill(params, { hexpand: false, vexpand: false });
this.parent(params);
super._init(params);
this.get_accessible().accessible_name = _("World view");
......@@ -113,34 +111,34 @@ var WorldContentView = new Lang.Class({
let list = this.model.getAll();
for (let i = list.length - 1; i >= 0; i--)
this._onLocationAdded(this.model, list[i], list[i]._isCurrentLocation);
},
}
refilter: function() {
refilter() {
this._listbox.invalidate_filter();
},
}
_syncStackPopover: function() {
_syncStackPopover() {
if (this.model.length == 1)
this._stackPopover.set_visible_child_name("search-grid");
else
this._stackPopover.set_visible_child_name("locations-grid");
},
}
_filterListbox: function(row) {
_filterListbox(row) {
return this._window.currentInfo == null ||
row._info != this._window.currentInfo;
},
}
_locationChanged: function(entry) {
_locationChanged(entry) {
if (entry.location) {
let info = this.model.addNewLocation(entry.location, false);
this._window.showInfo(info, false);
this.hide();
entry.location = null;
}
},
}
_onLocationAdded: function(model, info, isCurrentLocation) {
_onLocationAdded(model, info, isCurrentLocation) {
let location = info.location;
let grid = new Gtk.Grid({ orientation: Gtk.Orientation.HORIZONTAL,
......@@ -212,9 +210,9 @@ var WorldContentView = new Lang.Class({
});
this._syncStackPopover();
},
}
_onLocationRemoved: function(model, info) {
_onLocationRemoved(model, info) {
let rows = this._listbox.get_children();
for (let row of rows) {
......@@ -232,5 +230,5 @@ var WorldContentView = new Lang.Class({
this._currentLocationAdded = false;
this._syncStackPopover();
},
}
});
......@@ -25,8 +25,8 @@ pkg.require({ 'Gio': '2.0',
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const GWeather = imports.gi.GWeather;
const Lang = imports.lang;
const Util = imports.misc.util;
const SearchProvider = imports.service.searchProvider;
......@@ -38,12 +38,11 @@ function initEnvironment() {
};
}
const BackgroundService = new Lang.Class({
Name: 'WeatherBackgroundService',
Extends: Gio.Application,
const BackgroundService = GObject.registerClass(
class WeatherBackgroundService extends Gio.Application {
_init: function() {
this.parent({ application_id: pkg.name,
_init() {
super._init({ application_id: pkg.name,
flags: Gio.ApplicationFlags.IS_SERVICE,
inactivity_timeout: 60000 });
GLib.set_application_name(_("Weather"));
......@@ -52,31 +51,31 @@ const BackgroundService = new Lang.Class({
if (!pkg.moduledir.startsWith('resource://'))
this.debug = true;
},
}
_onQuit: function() {
_onQuit() {
this.quit();
},
}
vfunc_dbus_register: function(connection, path) {
vfunc_dbus_register(connection, path) {
this.parent(connection, path);
this._searchProvider.export(connection, path);
return true;
},
}
/*
Can't do until GApplication is fixed.
vfunc_dbus_unregister: function(connection, path) {
vfunc_dbus_unregister(connection, path) {
this._searchProvider.unexport(connection);
this.parent(connection, path);
super.vfunc_dbus_unregister(connection, path);
},
*/
vfunc_startup: function() {
this.parent();
vfunc_startup() {
super.vfunc_startup();
this.world = GWeather.Location.get_world();
this.model = new World.WorldModel(this.world, false);
......@@ -91,16 +90,16 @@ const BackgroundService = new Lang.Class({
Util.initActions(this,
[{ name: 'quit',
activate: this._onQuit }]);
},
}
vfunc_activate: function() {
vfunc_activate() {
// do nothing, this is a background service
},
}
vfunc_shutdown: function() {
vfunc_shutdown()