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