Skip to content

Commit

Permalink
Port GObject classes to JS6 classes
Browse files Browse the repository at this point in the history
This patch is required to make hamster-shell-extension work on
GNOME 3.32. At the same time, it breaks compatibility with older
gnome-shell versions that don't support ES6 class syntax.

See https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/361
Fixes projecthamster#307

Heavily based on work by Ernestas Kulik <ekulik@redhat.com>
  • Loading branch information
mwilck committed May 10, 2019
1 parent a86f648 commit e2a5edf
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 82 deletions.
24 changes: 12 additions & 12 deletions extension/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ const Lang = imports.lang;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();

const HamsterSettingsWidget = new GObject.Class({
Name: 'ProjectHamster.Prefs.HamsterSettingsWidget',
GTypeName: 'HamsterSettingsWidget',
Extends: Gtk.VBox,
const HamsterSettingsWidget = GObject.registerClass(
class HamsterSettingsWidget extends Gtk.VBox {
_init(params) {
super._init(params);

this.name = 'ProjectHamster.Prefs.HamsterSettingsWidget';

_init : function(params) {
this.parent(params);
this.margin = 10;

this._settings = ExtensionUtils.getSettings();
Expand Down Expand Up @@ -114,9 +114,9 @@ const HamsterSettingsWidget = new GObject.Class({
let version_text = ExtensionUtils.getCurrentExtension().metadata.version;
let version_label_text = "You are running hamster-shell-extension version " + version_text;
vbox.add(new Gtk.Label({label: version_label_text, margin_top: 10}));
},
}

_onPlacementChange: function(widget) {
_onPlacementChange(widget) {
let [success, iter] = widget.get_active_iter();
if (!success)
return;
Expand All @@ -127,9 +127,9 @@ const HamsterSettingsWidget = new GObject.Class({
return;

this._settings.set_int("panel-placement", newPlacement);
},
}

_onAppearanceChange: function(widget) {
_onAppearanceChange(widget) {
let [success, iter] = widget.get_active_iter();
if (!success)
return;
Expand All @@ -140,9 +140,9 @@ const HamsterSettingsWidget = new GObject.Class({
return;

this._settings.set_int("panel-appearance", newAppearance);
},
}

_onHotkeyChange: function(widget, bananas) {
_onHotkeyChange(widget, bananas) {
//global.log(widget, bananas);
let hotkey = widget.get_text();
let [key, mods] = Gtk.accelerator_parse(hotkey);
Expand Down
18 changes: 8 additions & 10 deletions extension/widgets/categoryTotalsWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const Lang = imports.lang;
const St = imports.gi.St;
const Clutter = imports.gi.Clutter;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;

const Me = imports.misc.extensionUtils.getCurrentExtension();
const Stuff = Me.imports.stuff;
Expand All @@ -33,19 +34,16 @@ const Stuff = Me.imports.stuff;
/**
* Custom Label widget that displays category totals.
*/
var CategoryTotalsWidget = new Lang.Class({
Name: 'CategoryTotals',
Extends: St.Label,

_init: function() {
this.parent({style_class: 'summary-label'});

},
var CategoryTotalsWidget = GObject.registerClass(
class CategoryTotals extends St.Label {
_init() {
super._init({style_class: 'summary-label'});
}

/**
* Recompute values and replace old string with new one based on passed facts.
*/
refresh: function(facts) {
refresh(facts) {
/**
* Construct a string representing category totals.
*/
Expand All @@ -67,5 +65,5 @@ var CategoryTotalsWidget = new Lang.Class({
}

this.set_text(getString(facts));
},
}
});
26 changes: 13 additions & 13 deletions extension/widgets/factsBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const PopupMenu = imports.ui.popupMenu;
const Clutter = imports.gi.Clutter;
const Mainloop = imports.mainloop;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;

const Gettext = imports.gettext.domain('hamster-shell-extension');
const _ = Gettext.gettext;
Expand All @@ -43,11 +44,10 @@ const TodaysFactsWidget = Me.imports.widgets.todaysFactsWidget.TodaysFactsWidget
* well as todays facts.
* @class
*/
var FactsBox = new Lang.Class({
Name: 'FactsBox',
Extends: PopupMenu.PopupBaseMenuItem,
_init: function(controller, panelWidget) {
this.parent({reactive: false});
var FactsBox =
class FactsBox extends PopupMenu.PopupBaseMenuItem {
constructor(controller, panelWidget) {
super({reactive: false});

this._controller = controller;

Expand Down Expand Up @@ -79,32 +79,32 @@ var FactsBox = new Lang.Class({
// Setup category summery
this.summaryLabel = new CategoryTotalsWidget();
main_box.add(this.summaryLabel);
},
}

// [FIXME]
// The best solution would be to listen for a 'FactsChanged' Signal that carries the new
// facts as payload and just refresh with this. But for now we stick with this
// simpler version.
refresh: function(facts, ongoingFact) {
refresh(facts, ongoingFact) {
this.todaysFactsWidget.refresh(facts, ongoingFact);
this.summaryLabel.refresh(facts);

},
}

/**
* Focus the fact entry and make sure todaysFactsWidget are scrolled to the bottom.
*/
focus: function() {
focus() {
Mainloop.timeout_add(20, Lang.bind(this, function() {
this._scrollAdjustment.value = this._scrollAdjustment.upper;
global.stage.set_key_focus(this.ongoingFactEntry);
}));
},
}

/**
* Remove any existing focus.
*/
unfocus: function() {
unfocus() {
global.stage.set_key_focus(null);
},
});
}
};
21 changes: 10 additions & 11 deletions extension/widgets/ongoingFactEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Copyright (c) 2016 - 2018 Eric Goller / projecthamster <elbenfreund@projecthamst


const Lang = imports.lang;
const GObject = imports.gi.GObject;
const St = imports.gi.St;
const Clutter = imports.gi.Clutter;

Expand All @@ -39,12 +40,10 @@ const Me = imports.misc.extensionUtils.getCurrentExtension();
*
*
*/
var OngoingFactEntry = new Lang.Class({
Name: 'OngoingFactEntry',
Extends: St.Entry,

_init: function(controller) {
this.parent({
var OngoingFactEntry = GObject.registerClass(
class OngoingFactEntry extends St.Entry {
_init(controller) {
super._init({
name: 'searchEntry',
can_focus: true,
track_hover: true,
Expand All @@ -59,7 +58,7 @@ var OngoingFactEntry = new Lang.Class({
this._runningActivitiesQuery = null;
this.clutter_text.connect('activate', Lang.bind(this, this._onEntryActivated));
this.clutter_text.connect('key-release-event', Lang.bind(this, this._onKeyReleaseEvent));
},
}

/**
* Callback for when ``ongoingFactEntry`` gets activated.
Expand All @@ -70,13 +69,13 @@ var OngoingFactEntry = new Lang.Class({
*
* @callback FactsBox~_onEntryActivated
*/
_onEntryActivated: function() {
_onEntryActivated() {
let text = this.get_text();
this._controller.apiProxy.AddFactRemote(text, 0, 0, false, Lang.bind(this, function(response, error) {
// not interested in the new id - this shuts up the warning
}));
this.set_text('');
},
}

/**
* Callback triggered after key release.
Expand All @@ -85,7 +84,7 @@ var OngoingFactEntry = new Lang.Class({
*
* @callback FactsBox~_onKeyReleaseEvent
*/
_onKeyReleaseEvent: function(textItem, evt) {
_onKeyReleaseEvent(textItem, evt) {
/**
* Check if the passed key is on our list of keys to be ignored.
*/
Expand Down Expand Up @@ -185,5 +184,5 @@ var OngoingFactEntry = new Lang.Class({
this._prevText = completion.toLowerCase();
}
}
},
}
});
49 changes: 24 additions & 25 deletions extension/widgets/panelWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Copyright (c) 2016 - 2018 Eric Goller / projecthamster <elbenfreund@projecthamst

const Lang = imports.lang;
const Gio = imports.gi.Gio;
const GObject = imports.gi.GObject;
const Clutter = imports.gi.Clutter;
const PanelMenu = imports.ui.panelMenu;
const St = imports.gi.St;
Expand Down Expand Up @@ -55,14 +56,12 @@ const Stuff = Me.imports.stuff;
*
* @class
*/
var PanelWidget = new Lang.Class({
Name: 'PanelWidget',
Extends: PanelMenu.Button,

_init: function(controller) {
var PanelWidget = GObject.registerClass(
class PanelWidget extends PanelMenu.Button {
_init(controller) {
// [FIXME]
// What is the parameter?
this.parent(0.0);
super._init(0.0);

this._controller = controller;
// [FIXME]
Expand Down Expand Up @@ -140,7 +139,7 @@ var PanelWidget = new Lang.Class({
this.timeout = GLib.timeout_add_seconds(0, 60, Lang.bind(this, this.refresh));
this.connect('destroy', Lang.bind(this, this._disableRefreshTimer));
this.refresh();
},
}

/**
* This is our main 'update/refresh' method.
Expand All @@ -152,7 +151,7 @@ var PanelWidget = new Lang.Class({
* required facts etc and pass them to the relevant sub-widget's
* refresh methods.
*/
refresh: function() {
refresh() {
/**
* We need to wrap our actual refresh code in this callback for now as
* I am having major difficulties using a syncronous dbus method call to
Expand Down Expand Up @@ -197,21 +196,21 @@ var PanelWidget = new Lang.Class({
// here.
this._controller.apiProxy.GetTodaysFactsRemote(Lang.bind(this, _refresh));
return GLib.SOURCE_CONTINUE;
},
}

/**
* Open 'popup menu' containing the bulk of the extension widgets.
*/
show: function() {
show() {
this.menu.open();
},
}

/**
* Close/Open the 'popup menu' depending on previous state.
*/
toggle: function() {
toggle() {
this.menu.toggle();
},
}


/**
Expand All @@ -220,7 +219,7 @@ var PanelWidget = new Lang.Class({
* Depending on the 'display mode' set in the extensions settings this has
* slightly different consequences.
*/
updatePanelDisplay: function(fact) {
updatePanelDisplay(fact) {
/**
* Return a text string representing the passed fact suitable for the panelLabel.
*
Expand Down Expand Up @@ -262,7 +261,7 @@ var PanelWidget = new Lang.Class({
this.panelLabel.show();
break;
}
},
}

/**
* Disable the refresh timer.
Expand All @@ -272,9 +271,9 @@ var PanelWidget = new Lang.Class({
* This method is actually a callback triggered on the destroy
* signal.
*/
_disableRefreshTimer: function() {
_disableRefreshTimer() {
GLib.source_remove(this.timeout);
},
}

/**
* Callback to be triggered when an *ongoing fact* is stopped.
Expand All @@ -283,7 +282,7 @@ var PanelWidget = new Lang.Class({
* This will get the current time and issue the ``StopTracking``
* method call to the dbus interface.
*/
_onStopTracking: function() {
_onStopTracking() {
let now = new Date();
let epochSeconds = Date.UTC(now.getFullYear(),
now.getMonth(),
Expand All @@ -293,25 +292,25 @@ var PanelWidget = new Lang.Class({
now.getSeconds());
epochSeconds = Math.floor(epochSeconds / 1000);
this._controller.apiProxy.StopTrackingRemote(GLib.Variant.new('i', [epochSeconds]));
},
}

/**
* Callback that triggers opening of the *Overview*-Window.
*
* @callback panelWidget~_onOpenOverview
*/
_onOpenOverview: function() {
_onOpenOverview() {
this._controller.windowsProxy.overviewSync();
},
}

/**
* Callback that triggers opening of the *Add Fact*-Window.
*
* @callback panelWidget~_onOpenAddFact
*/
_onOpenAddFact: function() {
_onOpenAddFact() {
this._controller.windowsProxy.editSync(GLib.Variant.new('i', [0]));
},
}

/**
* Callback that triggers opening of the *Add Fact*-Window.
Expand All @@ -320,7 +319,7 @@ var PanelWidget = new Lang.Class({
*
* Note: This will open the GUI settings, not the extension settings!
*/
_onOpenSettings: function() {
_onOpenSettings() {
this._controller.windowsProxy.preferencesSync();
},
}
});
Loading

0 comments on commit e2a5edf

Please sign in to comment.