From cda099be242be797b98f00977ab342816856f317 Mon Sep 17 00:00:00 2001 From: Julian Schacher Date: Sun, 22 Jan 2023 15:10:43 +0100 Subject: [PATCH] Refactor: Move PrefsPage class into own file (`PrefsPage.js`) --- src/prefs.js | 77 +-------------------------------- src/prefsModules/PrefsPage.js | 80 +++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 75 deletions(-) create mode 100644 src/prefsModules/PrefsPage.js diff --git a/src/prefs.js b/src/prefs.js index 5874afe..13b326a 100644 --- a/src/prefs.js +++ b/src/prefs.js @@ -1,86 +1,13 @@ /* exported buildPrefsWidget, init */ "use strict"; -const Gtk = imports.gi.Gtk; -const GObject = imports.gi.GObject; -const Adw = imports.gi.Adw; - const ExtensionUtils = imports.misc.extensionUtils; const Me = ExtensionUtils.getCurrentExtension(); -const PrefsBoxOrderListBox = Me.imports.prefsModules.PrefsBoxOrderListBox; -const PrefsBoxOrderListEmptyPlaceholder = Me.imports.prefsModules.PrefsBoxOrderListEmptyPlaceholder; -const PrefsBoxOrderItemRow = Me.imports.prefsModules.PrefsBoxOrderItemRow; -const ScrollManager = Me.imports.prefsModules.ScrollManager; - -var PrefsPage = GObject.registerClass({ - GTypeName: "PrefsPage", - Template: Me.dir.get_child("ui").get_child("prefs-page.ui").get_uri(), - InternalChildren: [ - "left-box", - "center-box", - "right-box" - ] -}, class PrefsPage extends Adw.PreferencesPage { - _init(params = {}) { - super._init(params); - - this._settings = ExtensionUtils.getSettings(); - - // Scroll up or down, when a Drag-and-Drop operation is in progress and - // the user has their cursor either in the upper or lower 10% of this - // widget respectively. - // Pass `this.get_first_child()` to the ScrollManager, since this - // `PrefsPage` extends an `Adw.PreferencesPage` and the first child of - // an `Adw.PreferencesPage` is the built-in `Gtk.ScrolledWindow`. - this._scrollManager = new ScrollManager.ScrollManager(this.get_first_child()); - let controller = new Gtk.DropControllerMotion(); - controller.connect("motion", (_, x, y) => { - // If the pointer is currently in the upper ten percent of this - // widget, then scroll up. - if (y <= this.get_allocated_height() * 0.1) this._scrollManager.startScrollUp(); - // If the pointer is currently in the lower ten percent of this - // widget, then scroll down. - else if (y >= this.get_allocated_height() * 0.9) this._scrollManager.startScrollDown(); - // Otherwise stop scrolling. - else this._scrollManager.stopScrollAll(); - }); - controller.connect("leave", () => { - // Stop scrolling on leave. - this._scrollManager.stopScrollAll(); - }); - this.add_controller(controller); - - // Add custom GTKListBoxes (PrefsBoxOrderListBoxes). - this._left_box_order = new PrefsBoxOrderListBox.PrefsBoxOrderListBox({}, "left-box-order"); - this._left_box.append(this._left_box_order); - this._center_box_order = new PrefsBoxOrderListBox.PrefsBoxOrderListBox({}, "center-box-order"); - this._center_box.append(this._center_box_order); - this._right_box_order = new PrefsBoxOrderListBox.PrefsBoxOrderListBox({}, "right-box-order"); - this._right_box.append(this._right_box_order); - - // Initialize the given `gtkListBox`. - const initializeGtkListBox = (boxOrder, gtkListBox) => { - // Add the items of the given configured box order as - // GtkListBoxRows. - for (const item of boxOrder) { - const listBoxRow = new PrefsBoxOrderItemRow.PrefsBoxOrderItemRow({}, this._scrollManager, item); - gtkListBox.append(listBoxRow); - } - - // Add a placeholder widget for the case, where `gtkListBox` doesn't - // have any GtkListBoxRows. - gtkListBox.set_placeholder(new PrefsBoxOrderListEmptyPlaceholder.PrefsBoxOrderListEmptyPlaceholder()); - }; - - initializeGtkListBox(this._settings.get_strv("left-box-order"), this._left_box_order); - initializeGtkListBox(this._settings.get_strv("center-box-order"), this._center_box_order); - initializeGtkListBox(this._settings.get_strv("right-box-order"), this._right_box_order); - } -}); +const PrefsPage = Me.imports.prefsModules.PrefsPage; function buildPrefsWidget() { - return new PrefsPage(); + return new PrefsPage.PrefsPage(); } function init() { diff --git a/src/prefsModules/PrefsPage.js b/src/prefsModules/PrefsPage.js new file mode 100644 index 0000000..979471a --- /dev/null +++ b/src/prefsModules/PrefsPage.js @@ -0,0 +1,80 @@ +"use strict"; +/* exported PrefsPage */ + +const Gtk = imports.gi.Gtk; +const GObject = imports.gi.GObject; +const Adw = imports.gi.Adw; + +const ExtensionUtils = imports.misc.extensionUtils; +const Me = ExtensionUtils.getCurrentExtension(); + +const PrefsBoxOrderListBox = Me.imports.prefsModules.PrefsBoxOrderListBox; +const PrefsBoxOrderListEmptyPlaceholder = Me.imports.prefsModules.PrefsBoxOrderListEmptyPlaceholder; +const PrefsBoxOrderItemRow = Me.imports.prefsModules.PrefsBoxOrderItemRow; +const ScrollManager = Me.imports.prefsModules.ScrollManager; + +var PrefsPage = GObject.registerClass({ + GTypeName: "PrefsPage", + Template: Me.dir.get_child("ui").get_child("prefs-page.ui").get_uri(), + InternalChildren: [ + "left-box", + "center-box", + "right-box" + ] +}, class PrefsPage extends Adw.PreferencesPage { + _init(params = {}) { + super._init(params); + + this._settings = ExtensionUtils.getSettings(); + + // Scroll up or down, when a Drag-and-Drop operation is in progress and + // the user has their cursor either in the upper or lower 10% of this + // widget respectively. + // Pass `this.get_first_child()` to the ScrollManager, since this + // `PrefsPage` extends an `Adw.PreferencesPage` and the first child of + // an `Adw.PreferencesPage` is the built-in `Gtk.ScrolledWindow`. + this._scrollManager = new ScrollManager.ScrollManager(this.get_first_child()); + let controller = new Gtk.DropControllerMotion(); + controller.connect("motion", (_, x, y) => { + // If the pointer is currently in the upper ten percent of this + // widget, then scroll up. + if (y <= this.get_allocated_height() * 0.1) this._scrollManager.startScrollUp(); + // If the pointer is currently in the lower ten percent of this + // widget, then scroll down. + else if (y >= this.get_allocated_height() * 0.9) this._scrollManager.startScrollDown(); + // Otherwise stop scrolling. + else this._scrollManager.stopScrollAll(); + }); + controller.connect("leave", () => { + // Stop scrolling on leave. + this._scrollManager.stopScrollAll(); + }); + this.add_controller(controller); + + // Add custom GTKListBoxes (PrefsBoxOrderListBoxes). + this._left_box_order = new PrefsBoxOrderListBox.PrefsBoxOrderListBox({}, "left-box-order"); + this._left_box.append(this._left_box_order); + this._center_box_order = new PrefsBoxOrderListBox.PrefsBoxOrderListBox({}, "center-box-order"); + this._center_box.append(this._center_box_order); + this._right_box_order = new PrefsBoxOrderListBox.PrefsBoxOrderListBox({}, "right-box-order"); + this._right_box.append(this._right_box_order); + + // Initialize the given `gtkListBox`. + const initializeGtkListBox = (boxOrder, gtkListBox) => { + // Add the items of the given configured box order as + // GtkListBoxRows. + for (const item of boxOrder) { + const listBoxRow = new PrefsBoxOrderItemRow.PrefsBoxOrderItemRow({}, this._scrollManager, item); + gtkListBox.append(listBoxRow); + } + + // Add a placeholder widget for the case, where `gtkListBox` doesn't + // have any GtkListBoxRows. + gtkListBox.set_placeholder(new PrefsBoxOrderListEmptyPlaceholder.PrefsBoxOrderListEmptyPlaceholder()); + }; + + initializeGtkListBox(this._settings.get_strv("left-box-order"), this._left_box_order); + initializeGtkListBox(this._settings.get_strv("center-box-order"), this._center_box_order); + initializeGtkListBox(this._settings.get_strv("right-box-order"), this._right_box_order); + } +});