extensions-app: Bind actions' enabled properties

Most properties that determine the availability of an action are
not expected to change, but now that extensions are represented
as a GObject, we can just generically set up bindings for all
actions instead of explicitly tracking the properties that we
expect to change.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3067>
This commit is contained in:
Florian Müllner 2023-12-19 02:35:33 +01:00 committed by Marge Bot
parent 198c7bbd9c
commit 1f7ca2bf38

View File

@ -34,54 +34,49 @@ export const ExtensionRow = GObject.registerClass({
this._actionGroup = new Gio.SimpleActionGroup(); this._actionGroup = new Gio.SimpleActionGroup();
this.insert_action_group('row', this._actionGroup); this.insert_action_group('row', this._actionGroup);
let action; const actionEntries = [
action = new Gio.SimpleAction({ {
name: 'show-prefs', name: 'show-prefs',
enabled: extension.hasPrefs, activate: () => {
}); this._detailsPopover.popdown();
action.connect('activate', () => { this.get_root().openPrefs(extension);
this._detailsPopover.popdown(); },
this.get_root().openPrefs(extension); enabledProp: 'has-prefs',
}); }, {
this._actionGroup.add_action(action); name: 'show-url',
activate: () => {
action = new Gio.SimpleAction({ this._detailsPopover.popdown();
name: 'show-url', Gio.AppInfo.launch_default_for_uri(
enabled: extension.url !== '', extension.url, this.get_display().get_app_launch_context());
}); },
action.connect('activate', () => { enabledProp: 'url',
this._detailsPopover.popdown(); enabledTransform: s => s !== '',
Gio.AppInfo.launch_default_for_uri( }, {
extension.url, this.get_display().get_app_launch_context()); name: 'uninstall',
}); activate: () => {
this._actionGroup.add_action(action); this._detailsPopover.popdown();
this.get_root().uninstall(extension);
action = new Gio.SimpleAction({ },
name: 'uninstall', enabledProp: 'is-user',
enabled: extension.isUser, }, {
}); name: 'enabled',
action.connect('activate', () => { state: 'false',
this._detailsPopover.popdown(); activate: action => {
this.get_root().uninstall(extension); const state = action.get_state();
}); action.change_state(new GLib.Variant('b', !state.get_boolean()));
this._actionGroup.add_action(action); },
change_state: (a, state) => {
action = new Gio.SimpleAction({ const {uuid} = this._extension;
name: 'enabled', if (state.get_boolean())
state: new GLib.Variant('b', false), this._app.extensionManager.enableExtension(uuid);
}); else
action.connect('activate', () => { this._app.extensionManager.disableExtension(uuid);
const state = action.get_state(); },
action.change_state(new GLib.Variant('b', !state.get_boolean())); enabledProp: 'can-change',
}); },
action.connect('change-state', (a, state) => { ];
const {uuid} = this._extension; this._actionGroup.add_action_entries(actionEntries);
if (state.get_boolean()) this._bindActionEnabled(actionEntries);
this._app.extensionManager.enableExtension(uuid);
else
this._app.extensionManager.disableExtension(uuid);
});
this._actionGroup.add_action(action);
this.title = extension.name; this.title = extension.name;
@ -102,12 +97,32 @@ export const ExtensionRow = GObject.registerClass({
this._extension = ext; this._extension = ext;
} }
_bindActionEnabled(entries) {
for (const entry of entries) {
const {name, enabledProp, enabledTransform} = entry;
if (!enabledProp)
continue;
const action = this._actionGroup.lookup_action(name);
if (enabledTransform) {
this._extension.bind_property_full(enabledProp,
action, 'enabled',
GObject.BindingFlags.SYNC_CREATE,
(bind, source) => [true, enabledTransform(source)],
null);
} else {
this._extension.bind_property(enabledProp,
action, 'enabled',
GObject.BindingFlags.SYNC_CREATE);
}
}
}
_updateState() { _updateState() {
const state = this._extension.state === ExtensionState.ENABLED; const state = this._extension.state === ExtensionState.ENABLED;
const action = this._actionGroup.lookup_action('enabled'); const action = this._actionGroup.lookup_action('enabled');
action.set_state(new GLib.Variant('b', state)); action.set_state(new GLib.Variant('b', state));
action.enabled = this._extension.canChange;
if (!action.enabled) if (!action.enabled)
this._switch.active = state; this._switch.active = state;