2019-01-31 14:07:06 +00:00
|
|
|
/* exported InhibitShortcutsDialog */
|
2023-03-06 01:01:31 +00:00
|
|
|
const {Clutter, GObject, Meta, Pango, Shell, St} = imports.gi;
|
2017-07-14 01:15:17 +00:00
|
|
|
|
|
|
|
const Dialog = imports.ui.dialog;
|
|
|
|
const ModalDialog = imports.ui.modalDialog;
|
2019-02-01 16:44:05 +00:00
|
|
|
const PermissionStore = imports.misc.permissionStore;
|
2017-07-14 01:15:17 +00:00
|
|
|
|
2022-01-21 13:24:02 +00:00
|
|
|
const APP_ALLOWLIST = ['org.gnome.Settings.desktop'];
|
2019-02-01 16:44:05 +00:00
|
|
|
const APP_PERMISSIONS_TABLE = 'gnome';
|
|
|
|
const APP_PERMISSIONS_ID = 'shortcuts-inhibitor';
|
|
|
|
const GRANTED = 'GRANTED';
|
|
|
|
const DENIED = 'DENIED';
|
2017-08-14 00:32:12 +00:00
|
|
|
|
2017-07-14 01:15:17 +00:00
|
|
|
var DialogResponse = Meta.InhibitShortcutsDialogResponse;
|
|
|
|
|
2017-10-31 01:23:39 +00:00
|
|
|
var InhibitShortcutsDialog = GObject.registerClass({
|
2017-07-14 01:15:17 +00:00
|
|
|
Implements: [Meta.InhibitShortcutsDialog],
|
|
|
|
Properties: {
|
2019-08-20 21:43:54 +00:00
|
|
|
'window': GObject.ParamSpec.override('window', Meta.InhibitShortcutsDialog),
|
|
|
|
},
|
2017-10-31 01:23:39 +00:00
|
|
|
}, class InhibitShortcutsDialog extends GObject.Object {
|
2017-10-31 00:03:21 +00:00
|
|
|
_init(window) {
|
2017-10-31 01:23:39 +00:00
|
|
|
super._init();
|
2017-07-14 01:15:17 +00:00
|
|
|
this._window = window;
|
|
|
|
|
|
|
|
this._dialog = new ModalDialog.ModalDialog();
|
|
|
|
this._buildLayout();
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-14 01:15:17 +00:00
|
|
|
|
|
|
|
get window() {
|
|
|
|
return this._window;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-14 01:15:17 +00:00
|
|
|
|
|
|
|
set window(window) {
|
|
|
|
this._window = window;
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-14 01:15:17 +00:00
|
|
|
|
2017-08-14 00:32:12 +00:00
|
|
|
get _app() {
|
|
|
|
let windowTracker = Shell.WindowTracker.get_default();
|
|
|
|
return windowTracker.get_window_app(this._window);
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-08-14 00:32:12 +00:00
|
|
|
|
2019-02-01 16:44:05 +00:00
|
|
|
_shouldUsePermStore() {
|
|
|
|
return this._app && !this._app.is_window_backed();
|
|
|
|
}
|
|
|
|
|
2022-06-23 12:53:29 +00:00
|
|
|
async _saveToPermissionStore(grant) {
|
2019-02-01 16:44:05 +00:00
|
|
|
if (!this._shouldUsePermStore() || this._permStore == null)
|
|
|
|
return;
|
|
|
|
|
2022-06-23 12:53:29 +00:00
|
|
|
try {
|
2022-10-06 12:30:20 +00:00
|
|
|
await this._permStore.SetPermissionAsync(APP_PERMISSIONS_TABLE,
|
2022-06-23 12:53:29 +00:00
|
|
|
true,
|
|
|
|
APP_PERMISSIONS_ID,
|
2022-10-06 12:30:20 +00:00
|
|
|
this._app.get_id(),
|
|
|
|
[grant]);
|
2022-06-23 12:53:29 +00:00
|
|
|
} catch (error) {
|
|
|
|
log(error.message);
|
|
|
|
}
|
2019-02-01 16:44:05 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_buildLayout() {
|
2020-08-12 18:59:01 +00:00
|
|
|
const name = this._app?.get_name() ?? this._window.title;
|
2017-07-14 01:15:17 +00:00
|
|
|
|
2020-01-27 21:49:36 +00:00
|
|
|
let content = new Dialog.MessageDialogContent({
|
|
|
|
title: _('Allow inhibiting shortcuts'),
|
|
|
|
description: name
|
|
|
|
/* Translators: %s is an application name like "Settings" */
|
2022-12-16 21:33:10 +00:00
|
|
|
? _('The app %s wants to inhibit shortcuts').format(name)
|
|
|
|
: _('An app wants to inhibit shortcuts'),
|
2020-01-27 21:49:36 +00:00
|
|
|
});
|
2017-07-14 01:15:17 +00:00
|
|
|
|
2023-03-06 01:01:31 +00:00
|
|
|
const restoreAccel = Meta.prefs_get_keybinding_label('restore-shortcuts');
|
2019-08-20 00:51:42 +00:00
|
|
|
if (restoreAccel) {
|
2020-01-27 21:49:36 +00:00
|
|
|
let restoreLabel = new St.Label({
|
2017-08-11 17:45:02 +00:00
|
|
|
/* Translators: %s is a keyboard shortcut like "Super+x" */
|
2020-01-27 21:49:36 +00:00
|
|
|
text: _('You can restore shortcuts by pressing %s.').format(restoreAccel),
|
|
|
|
style_class: 'message-dialog-description',
|
|
|
|
});
|
2020-06-28 20:28:57 +00:00
|
|
|
restoreLabel.clutter_text.ellipsize = Pango.EllipsizeMode.NONE;
|
|
|
|
restoreLabel.clutter_text.line_wrap = true;
|
2020-01-27 21:49:36 +00:00
|
|
|
content.add_child(restoreLabel);
|
2019-08-20 00:51:42 +00:00
|
|
|
}
|
2017-07-14 01:15:17 +00:00
|
|
|
|
2020-01-27 21:49:36 +00:00
|
|
|
this._dialog.contentLayout.add_child(content);
|
2017-07-14 01:15:17 +00:00
|
|
|
|
2020-03-29 21:51:13 +00:00
|
|
|
this._dialog.addButton({
|
|
|
|
label: _('Deny'),
|
|
|
|
action: () => {
|
|
|
|
this._saveToPermissionStore(DENIED);
|
|
|
|
this._emitResponse(DialogResponse.DENY);
|
|
|
|
},
|
|
|
|
key: Clutter.KEY_Escape,
|
|
|
|
});
|
|
|
|
|
|
|
|
this._dialog.addButton({
|
|
|
|
label: _('Allow'),
|
|
|
|
action: () => {
|
|
|
|
this._saveToPermissionStore(GRANTED);
|
|
|
|
this._emitResponse(DialogResponse.ALLOW);
|
|
|
|
},
|
|
|
|
default: true,
|
|
|
|
});
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-14 01:15:17 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_emitResponse(response) {
|
2017-07-14 01:15:17 +00:00
|
|
|
this.emit('response', response);
|
|
|
|
this._dialog.close();
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-14 01:15:17 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
vfunc_show() {
|
2020-08-04 08:39:29 +00:00
|
|
|
if (this._app && APP_ALLOWLIST.includes(this._app.get_id())) {
|
2017-08-14 00:32:12 +00:00
|
|
|
this._emitResponse(DialogResponse.ALLOW);
|
2019-02-01 16:44:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._shouldUsePermStore()) {
|
2017-08-14 00:32:12 +00:00
|
|
|
this._dialog.open();
|
2019-02-01 16:44:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check with the permission store */
|
|
|
|
let appId = this._app.get_id();
|
2022-06-23 12:53:29 +00:00
|
|
|
this._permStore = new PermissionStore.PermissionStore(async (proxy, error) => {
|
2019-02-01 16:44:05 +00:00
|
|
|
if (error) {
|
|
|
|
log(error.message);
|
|
|
|
this._dialog.open();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-23 12:53:29 +00:00
|
|
|
try {
|
|
|
|
const [permissions] = await this._permStore.LookupAsync(
|
|
|
|
APP_PERMISSIONS_TABLE, APP_PERMISSIONS_ID);
|
|
|
|
|
|
|
|
if (permissions[appId] === undefined) // Not found
|
|
|
|
this._dialog.open();
|
2022-11-18 21:40:31 +00:00
|
|
|
else if (permissions[appId][0] === GRANTED)
|
2022-06-23 12:53:29 +00:00
|
|
|
this._emitResponse(DialogResponse.ALLOW);
|
|
|
|
else
|
|
|
|
this._emitResponse(DialogResponse.DENY);
|
|
|
|
} catch (err) {
|
|
|
|
this._dialog.open();
|
|
|
|
log(err.message);
|
|
|
|
}
|
2019-02-01 16:44:05 +00:00
|
|
|
});
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-14 01:15:17 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
vfunc_hide() {
|
2017-07-14 01:15:17 +00:00
|
|
|
this._dialog.close();
|
|
|
|
}
|
|
|
|
});
|