From 7a8b6360285610129c9c42f2ca877e15fa1dcb39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCllner?= Date: Fri, 30 Jul 2021 02:33:54 +0200 Subject: [PATCH] dbusServides/extensions: Use Adw.PreferencesWindow Adapt the new GNOME platform API for the upcoming 42. This makes sure that we get the latest version of the stylesheet, as well as support for the new dark mode. Using the dedicated preference API also gives extensions with more complex preferences an easier and standardized way for implementing multi-page preferences. Part-of: --- .../extensions/css/application.css | 2 + .../extensions/extensionPrefsDialog.js | 57 +++++- js/dbusServices/extensions/main.js | 4 +- .../extensions/ui/extension-error-page.ui | 167 +++++++++--------- 4 files changed, 132 insertions(+), 98 deletions(-) diff --git a/js/dbusServices/extensions/css/application.css b/js/dbusServices/extensions/css/application.css index 133e858f5..4ef406607 100644 --- a/js/dbusServices/extensions/css/application.css +++ b/js/dbusServices/extensions/css/application.css @@ -1,3 +1,5 @@ +.error-page preferencespage { margin: 30px; } + .expander { padding: 12px; } .expander.expanded { border: 0 solid @borders; border-bottom-width: 1px; } .expander-toolbar { diff --git a/js/dbusServices/extensions/extensionPrefsDialog.js b/js/dbusServices/extensions/extensionPrefsDialog.js index cdcb85215..fe2b02afb 100644 --- a/js/dbusServices/extensions/extensionPrefsDialog.js +++ b/js/dbusServices/extensions/extensionPrefsDialog.js @@ -1,20 +1,20 @@ // -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*- /* exported ExtensionPrefsDialog */ -const { Gdk, Gio, GObject, Gtk } = imports.gi; +const { Adw, Gdk, Gio, GLib, GObject, Gtk } = imports.gi; const ExtensionUtils = imports.misc.extensionUtils; var ExtensionPrefsDialog = GObject.registerClass({ GTypeName: 'ExtensionPrefsDialog', -}, class ExtensionPrefsDialog extends Gtk.Window { +}, class ExtensionPrefsDialog extends Adw.PreferencesWindow { _init(extension) { super._init({ title: extension.metadata.name, default_width: 600, default_height: 400, + search_enabled: false, }); - this.set_titlebar(new Gtk.HeaderBar()); try { ExtensionUtils.installImporter(extension); @@ -26,12 +26,52 @@ var ExtensionPrefsDialog = GObject.registerClass({ prefsModule.init(extension.metadata); const widget = prefsModule.buildPrefsWidget(); - this.set_child(widget); + const page = this._wrapWidget(widget); + this.add(page); } catch (e) { - this.set_child(new ExtensionPrefsErrorPage(extension, e)); + this._showErrorPage(e); logError(e, 'Failed to open preferences'); } } + + set titlebar(w) { + this.set_titlebar(w); + } + + // eslint-disable-next-line camelcase + set_titlebar() { + // intercept fatal libadwaita error, show error page instead + GLib.idle_add(GLib.PRIORITY_DEFAULT, () => { + this._showErrorPage( + new Error('set_titlebar() is not supported for Adw.Window')); + return GLib.SOURCE_REMOVE; + }); + } + + _showErrorPage(e) { + while (this.visible_page) + this.remove(this.visible_page); + + const extension = ExtensionUtils.getCurrentExtension(); + this.add(new ExtensionPrefsErrorPage(extension, e)); + } + + _wrapWidget(widget) { + if (widget instanceof Adw.PreferencesPage) + return widget; + + const page = new Adw.PreferencesPage(); + if (widget instanceof Adw.PreferencesGroup) { + page.add(widget); + return page; + } + + const group = new Adw.PreferencesGroup(); + group.add(widget); + page.add(group); + + return page; + } }); const ExtensionPrefsErrorPage = GObject.registerClass({ @@ -43,7 +83,7 @@ const ExtensionPrefsErrorPage = GObject.registerClass({ 'revealer', 'errorView', ], -}, class ExtensionPrefsErrorPage extends Gtk.Widget { +}, class ExtensionPrefsErrorPage extends Adw.PreferencesPage { static _classInit(klass) { super._classInit(klass); @@ -61,9 +101,8 @@ const ExtensionPrefsErrorPage = GObject.registerClass({ } _init(extension, error) { - super._init({ - layout_manager: new Gtk.BinLayout(), - }); + super._init(); + this._addCustomStylesheet(); this._uuid = extension.uuid; diff --git a/js/dbusServices/extensions/main.js b/js/dbusServices/extensions/main.js index 97a28aa98..b13e52e3c 100644 --- a/js/dbusServices/extensions/main.js +++ b/js/dbusServices/extensions/main.js @@ -3,14 +3,14 @@ imports.gi.versions.Gdk = '4.0'; imports.gi.versions.Gtk = '4.0'; -const { GObject, Gtk } = imports.gi; +const { Adw, GObject } = imports.gi; const pkg = imports.package; const { DBusService } = imports.dbusService; const { ExtensionsService } = imports.extensionsService; function main() { - Gtk.init(); + Adw.init(); pkg.initFormat(); GObject.gtypeNameBasedOnJSPath = true; diff --git a/js/dbusServices/extensions/ui/extension-error-page.ui b/js/dbusServices/extensions/ui/extension-error-page.ui index eb0a044cc..ea089fb49 100644 --- a/js/dbusServices/extensions/ui/extension-error-page.ui +++ b/js/dbusServices/extensions/ui/extension-error-page.ui @@ -1,107 +1,100 @@ -