gnome-shell/subprojects/extensions-app/js/extensionRow.js
Florian Müllner e6624f8ba1 extensionSytem: Rename ENABLED/DISABLED states
The ENABLED state means that an extension's `enable()` method
was called successfully.

This usually matches whether an extension *should* be enabled
according to the enabled-extensions/disabled-extensions settings,
but not necessarily: If an extension had an error or does not
support the currently active mode, its actual state is different.

We currently only expose the actual state to external tooling,
but whether an extension should be enabled is relevant as well,
for example to disable a lock-screen only extension from the
regular session.

For that purpose we will expose a separate `enabled` property.
To avoid confusion with the existing states, change the exposed
names to (IN)ACTIVE.

This does not affect the D-Bus API, as the numeric values of
the states remain unchanged.

https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/7004

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3073>
2024-01-03 20:05:05 +00:00

126 lines
4.2 KiB
JavaScript

import Adw from 'gi://Adw?version=1';
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
import {ExtensionState} from './misc/extensionUtils.js';
import {Extension} from './extensionManager.js';
export const ExtensionRow = GObject.registerClass({
GTypeName: 'ExtensionRow',
Template: 'resource:///org/gnome/Extensions/ui/extension-row.ui',
Properties: {
'extension': GObject.ParamSpec.object(
'extension', null, null,
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
Extension),
},
InternalChildren: [
'detailsPopover',
'versionLabel',
'switch',
'actionsBox',
],
}, class ExtensionRow extends Adw.ActionRow {
constructor(extension) {
super({extension});
this._app = Gio.Application.get_default();
this._actionGroup = new Gio.SimpleActionGroup();
this.insert_action_group('row', this._actionGroup);
const actionEntries = [
{
name: 'show-prefs',
activate: () => {
this._detailsPopover.popdown();
this.get_root().openPrefs(extension);
},
enabledProp: 'has-prefs',
}, {
name: 'show-url',
activate: () => {
this._detailsPopover.popdown();
Gio.AppInfo.launch_default_for_uri(
extension.url, this.get_display().get_app_launch_context());
},
enabledProp: 'url',
enabledTransform: s => s !== '',
}, {
name: 'uninstall',
activate: () => {
this._detailsPopover.popdown();
this.get_root().uninstall(extension);
},
enabledProp: 'is-user',
}, {
name: 'enabled',
state: 'false',
activate: action => {
const state = action.get_state();
action.change_state(new GLib.Variant('b', !state.get_boolean()));
},
change_state: (a, state) => {
const {uuid} = this._extension;
if (state.get_boolean())
this._app.extensionManager.enableExtension(uuid);
else
this._app.extensionManager.disableExtension(uuid);
},
enabledProp: 'can-change',
},
];
this._actionGroup.add_action_entries(actionEntries);
this._bindActionEnabled(actionEntries);
this._extension.bind_property_full('version',
this._versionLabel, 'label',
GObject.BindingFlags.SYNC_CREATE,
(bind, source) => [true, _('Version %s').format(source)],
null);
this._extension.connect('notify::state',
() => this._updateState());
this._updateState();
}
get extension() {
return this._extension ?? null;
}
set 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() {
const state = this._extension.state === ExtensionState.ACTIVE;
const action = this._actionGroup.lookup_action('enabled');
action.set_state(new GLib.Variant('b', state));
if (!action.enabled)
this._switch.active = state;
}
});