From a7ec7583aaa17b74a5a5f216328337ad9b1ccccd Mon Sep 17 00:00:00 2001 From: Didier Roche Date: Thu, 1 Nov 2018 13:50:30 +0100 Subject: [PATCH] extensionPrefs: Attach extension object to each row Each row represents an extension, so it makes sense to associate the rows with the actual extensions instead of linking rows and extensions by looking up the UUID in the external extensions map in ExtensionUtils. This will also make it much easier to stop using the shared extension loading / map in favor of the extension D-Bus API. https://bugzilla.gnome.org/show_bug.cgi?id=789852 --- js/extensionPrefs/main.js | 124 +++++++++++++++++++------------------- 1 file changed, 61 insertions(+), 63 deletions(-) diff --git a/js/extensionPrefs/main.js b/js/extensionPrefs/main.js index be543a4ba..0f7e6d76b 100644 --- a/js/extensionPrefs/main.js +++ b/js/extensionPrefs/main.js @@ -27,59 +27,36 @@ var Application = GObject.registerClass({ flags: Gio.ApplicationFlags.HANDLES_COMMAND_LINE }); - this._extensionPrefsModules = {}; - this._startupUuid = null; this._loaded = false; this._skipMainWindow = false; } - _extensionAvailable(uuid) { - let extension = ExtensionUtils.extensions[uuid]; + _showPrefs(uuid) { + let row = this._extensionSelector.get_children().find(c => { + return c.uuid === uuid && c.hasPrefs; + }); - if (!extension) + if (!row) return false; - if (!extension.dir.get_child('prefs.js').query_exists(null)) - return false; - - return true; - } - - _getExtensionPrefsModule(extension) { - let uuid = extension.metadata.uuid; - - if (this._extensionPrefsModules.hasOwnProperty(uuid)) - return this._extensionPrefsModules[uuid]; - - ExtensionUtils.installImporter(extension); - - let prefsModule = extension.imports.prefs; - prefsModule.init(extension.metadata); - - this._extensionPrefsModules[uuid] = prefsModule; - return prefsModule; - } - - _selectExtension(uuid) { - if (!this._extensionAvailable(uuid)) - return; - - let extension = ExtensionUtils.extensions[uuid]; let widget; try { - let prefsModule = this._getExtensionPrefsModule(extension); - widget = prefsModule.buildPrefsWidget(); + widget = row.prefsModule.buildPrefsWidget(); } catch (e) { - widget = this._buildErrorUI(extension, e); + widget = this._buildErrorUI(row, e); } - let dialog = new Gtk.Window({ modal: !this._skipMainWindow, - type_hint: Gdk.WindowTypeHint.DIALOG }); - dialog.set_titlebar(new Gtk.HeaderBar({ show_close_button: true, - title: extension.metadata.name, - visible: true })); + let dialog = new Gtk.Window({ + modal: !this._skipMainWindow, + type_hint: Gdk.WindowTypeHint.DIALOG + }); + dialog.set_titlebar(new Gtk.HeaderBar({ + show_close_button: true, + title: row.name, + visible: true + })); if (this._skipMainWindow) { this.add_window(dialog); @@ -96,7 +73,7 @@ var Application = GObject.registerClass({ dialog.show(); } - _buildErrorUI(extension, exc) { + _buildErrorUI(row, exc) { let scroll = new Gtk.ScrolledWindow({ hscrollbar_policy: Gtk.PolicyType.NEVER, propagate_natural_height: true @@ -168,7 +145,7 @@ var Application = GObject.registerClass({ let clipboard = Gtk.Clipboard.get_default(w.get_display()); // markdown for pasting in gitlab issues let lines = [ - `The settings of extension ${extension.uuid} had an error:`, + `The settings of extension ${row.uuid} had an error:`, '```', `${exc}`, '```', @@ -190,13 +167,13 @@ var Application = GObject.registerClass({ label: _("Homepage"), tooltip_text: _("Visit extension homepage"), no_show_all: true, - visible: extension.metadata.url != null + visible: row.url != null }); toolbar.add(urlButton); urlButton.connect('clicked', w => { let context = w.get_display().get_app_launch_context(); - Gio.AppInfo.launch_default_for_uri(extension.metadata.url, context); + Gio.AppInfo.launch_default_for_uri(row.url, context); }); let expandedBox = new Gtk.Box({ @@ -255,9 +232,7 @@ var Application = GObject.registerClass({ } _sortList(row1, row2) { - let name1 = ExtensionUtils.extensions[row1.uuid].metadata.name; - let name2 = ExtensionUtils.extensions[row2.uuid].metadata.name; - return name1.localeCompare(name2); + return row1.name.localeCompare(row2.name); } _updateHeader(row, before) { @@ -276,11 +251,10 @@ var Application = GObject.registerClass({ } _extensionFound(finder, extension) { - let row = new ExtensionRow(extension.uuid); + let row = new ExtensionRow(extension); - row.prefsButton.visible = this._extensionAvailable(row.uuid); row.prefsButton.connect('clicked', () => { - this._selectExtension(row.uuid); + this._showPrefs(row.uuid); }); row.show_all(); @@ -293,8 +267,8 @@ var Application = GObject.registerClass({ else this._mainStack.visible_child_name = 'placeholder'; - if (this._startupUuid && this._extensionAvailable(this._startupUuid)) - this._selectExtension(this._startupUuid); + if (this._startupUuid) + this._showPrefs(this._startupUuid); this._startupUuid = null; this._skipMainWindow = false; this._loaded = true; @@ -323,11 +297,9 @@ var Application = GObject.registerClass({ // Strip off "extension:///" prefix which fakes a URI, if it exists uuid = stripPrefix(uuid, "extension:///"); - if (this._extensionAvailable(uuid)) - this._selectExtension(uuid); - else if (!this._loaded) + if (!this._loaded) this._startupUuid = uuid; - else + else if (!this._showPrefs(uuid)) this._skipMainWindow = false; } return 0; @@ -511,10 +483,11 @@ class DescriptionLabel extends Gtk.Label { var ExtensionRow = GObject.registerClass( class ExtensionRow extends Gtk.ListBoxRow { - _init(uuid) { + _init(extension) { super._init(); - this.uuid = uuid; + this._extension = extension; + this._prefsModule = null; this._settings = new Gio.Settings({ schema_id: 'org.gnome.shell' }); this._settings.connect('changed::enabled-extensions', () => { @@ -532,9 +505,23 @@ class ExtensionRow extends Gtk.ListBoxRow { this._buildUI(); } - _buildUI() { - let extension = ExtensionUtils.extensions[this.uuid]; + get uuid() { + return this._extension.uuid; + } + get name() { + return this._extension.metadata.name; + } + + get hasPrefs() { + return this._extension.hasPrefs; + } + + get url() { + return this._extension.metadata.url; + } + + _buildUI() { let hbox = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL, hexpand: true, margin_end: 24, spacing: 24, margin: 12 }); @@ -544,19 +531,20 @@ class ExtensionRow extends Gtk.ListBoxRow { spacing: 6, hexpand: true }); hbox.add(vbox); - let name = GLib.markup_escape_text(extension.metadata.name, -1); + let name = GLib.markup_escape_text(this.name, -1); let label = new Gtk.Label({ label: '' + name + '', use_markup: true, halign: Gtk.Align.START }); vbox.add(label); - let desc = extension.metadata.description.split('\n')[0]; + let desc = this._extension.metadata.description.split('\n')[0]; label = new DescriptionLabel({ label: desc, wrap: true, lines: 2, ellipsize: Pango.EllipsizeMode.END, xalign: 0, yalign: 0 }); vbox.add(label); let button = new Gtk.Button({ valign: Gtk.Align.CENTER, + visible: this.hasPrefs, no_show_all: true }); button.set_image(new Gtk.Image({ icon_name: 'emblem-system-symbolic', icon_size: Gtk.IconSize.BUTTON, @@ -580,11 +568,10 @@ class ExtensionRow extends Gtk.ListBoxRow { } _canEnable() { - let extension = ExtensionUtils.extensions[this.uuid]; let checkVersion = !this._settings.get_boolean('disable-extension-version-validation'); return !this._settings.get_boolean('disable-user-extensions') && - !(checkVersion && ExtensionUtils.isOutOfDate(extension)); + !(checkVersion && ExtensionUtils.isOutOfDate(this._extension)); } _isEnabled() { @@ -612,6 +599,17 @@ class ExtensionRow extends Gtk.ListBoxRow { } while (pos != -1); this._settings.set_strv('enabled-extensions', extensions); } + + get prefsModule() { + if (!this._prefsModule) { + ExtensionUtils.installImporter(this._extension); + + this._prefsModule = this._extension.imports.prefs; + this._prefsModule.init(this._extension.metadata); + } + + return this._prefsModule; + } }); function initEnvironment() {