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: () => {
});
action.connect('activate', () => {
this._detailsPopover.popdown(); this._detailsPopover.popdown();
this.get_root().openPrefs(extension); this.get_root().openPrefs(extension);
}); },
this._actionGroup.add_action(action); enabledProp: 'has-prefs',
}, {
action = new Gio.SimpleAction({
name: 'show-url', name: 'show-url',
enabled: extension.url !== '', activate: () => {
});
action.connect('activate', () => {
this._detailsPopover.popdown(); this._detailsPopover.popdown();
Gio.AppInfo.launch_default_for_uri( Gio.AppInfo.launch_default_for_uri(
extension.url, this.get_display().get_app_launch_context()); extension.url, this.get_display().get_app_launch_context());
}); },
this._actionGroup.add_action(action); enabledProp: 'url',
enabledTransform: s => s !== '',
action = new Gio.SimpleAction({ }, {
name: 'uninstall', name: 'uninstall',
enabled: extension.isUser, activate: () => {
});
action.connect('activate', () => {
this._detailsPopover.popdown(); this._detailsPopover.popdown();
this.get_root().uninstall(extension); this.get_root().uninstall(extension);
}); },
this._actionGroup.add_action(action); enabledProp: 'is-user',
}, {
action = new Gio.SimpleAction({
name: 'enabled', name: 'enabled',
state: new GLib.Variant('b', false), state: 'false',
}); activate: action => {
action.connect('activate', () => {
const state = action.get_state(); const state = action.get_state();
action.change_state(new GLib.Variant('b', !state.get_boolean())); action.change_state(new GLib.Variant('b', !state.get_boolean()));
}); },
action.connect('change-state', (a, state) => { change_state: (a, state) => {
const {uuid} = this._extension; const {uuid} = this._extension;
if (state.get_boolean()) if (state.get_boolean())
this._app.extensionManager.enableExtension(uuid); this._app.extensionManager.enableExtension(uuid);
else else
this._app.extensionManager.disableExtension(uuid); this._app.extensionManager.disableExtension(uuid);
}); },
this._actionGroup.add_action(action); enabledProp: 'can-change',
},
];
this._actionGroup.add_action_entries(actionEntries);
this._bindActionEnabled(actionEntries);
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;