2019-01-31 14:07:06 +00:00
|
|
|
/* exported InhibitShortcutsDialog */
|
2020-06-28 20:28:57 +00:00
|
|
|
const { Clutter, Gio, GLib, GObject, Gtk, 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
|
|
|
|
|
|
|
const WAYLAND_KEYBINDINGS_SCHEMA = 'org.gnome.mutter.wayland.keybindings';
|
|
|
|
|
2017-08-14 00:32:12 +00:00
|
|
|
const APP_WHITELIST = ['gnome-control-center.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
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_getRestoreAccel() {
|
2017-07-14 01:15:17 +00:00
|
|
|
let settings = new Gio.Settings({ schema_id: WAYLAND_KEYBINDINGS_SCHEMA });
|
|
|
|
let accel = settings.get_strv('restore-shortcuts')[0] || '';
|
|
|
|
return Gtk.accelerator_get_label.apply(null,
|
|
|
|
Gtk.accelerator_parse(accel));
|
2017-10-31 01:23:39 +00:00
|
|
|
}
|
2017-07-14 01:15:17 +00:00
|
|
|
|
2019-02-01 16:44:05 +00:00
|
|
|
_shouldUsePermStore() {
|
|
|
|
return this._app && !this._app.is_window_backed();
|
|
|
|
}
|
|
|
|
|
|
|
|
_saveToPermissionStore(grant) {
|
|
|
|
if (!this._shouldUsePermStore() || this._permStore == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let permissions = {};
|
|
|
|
permissions[this._app.get_id()] = [grant];
|
|
|
|
let data = GLib.Variant.new('av', {});
|
|
|
|
|
|
|
|
this._permStore.SetRemote(APP_PERMISSIONS_TABLE,
|
|
|
|
true,
|
|
|
|
APP_PERMISSIONS_ID,
|
|
|
|
permissions,
|
|
|
|
data,
|
|
|
|
(result, error) => {
|
|
|
|
if (error != null)
|
|
|
|
log(error.message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_buildLayout() {
|
2017-08-14 00:32:12 +00:00
|
|
|
let name = this._app ? 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" */
|
|
|
|
? _('The application %s wants to inhibit shortcuts').format(name)
|
|
|
|
: _('An application wants to inhibit shortcuts'),
|
|
|
|
});
|
2017-07-14 01:15:17 +00:00
|
|
|
|
|
|
|
let restoreAccel = this._getRestoreAccel();
|
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
|
|
|
|
|
|
|
this._dialog.addButton({ label: _("Deny"),
|
|
|
|
action: () => {
|
2019-02-01 16:44:05 +00:00
|
|
|
this._saveToPermissionStore(DENIED);
|
2017-07-14 01:15:17 +00:00
|
|
|
this._emitResponse(DialogResponse.DENY);
|
|
|
|
},
|
|
|
|
key: Clutter.KEY_Escape });
|
|
|
|
|
|
|
|
this._dialog.addButton({ label: _("Allow"),
|
|
|
|
action: () => {
|
2019-02-01 16:44:05 +00:00
|
|
|
this._saveToPermissionStore(GRANTED);
|
2017-07-14 01:15:17 +00:00
|
|
|
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() {
|
2018-07-14 20:56:22 +00:00
|
|
|
if (this._app && APP_WHITELIST.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();
|
|
|
|
this._permStore = new PermissionStore.PermissionStore((proxy, error) => {
|
|
|
|
if (error) {
|
|
|
|
log(error.message);
|
|
|
|
this._dialog.open();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._permStore.LookupRemote(APP_PERMISSIONS_TABLE,
|
|
|
|
APP_PERMISSIONS_ID,
|
2019-08-20 00:20:08 +00:00
|
|
|
(res, err) => {
|
|
|
|
if (err) {
|
2019-02-01 16:44:05 +00:00
|
|
|
this._dialog.open();
|
2019-08-20 00:20:08 +00:00
|
|
|
log(err.message);
|
2019-02-01 16:44:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-01 13:41:55 +00:00
|
|
|
let [permissions] = res;
|
2019-02-01 16:44:05 +00:00
|
|
|
if (permissions[appId] === undefined) // Not found
|
|
|
|
this._dialog.open();
|
|
|
|
else if (permissions[appId] == GRANTED)
|
|
|
|
this._emitResponse(DialogResponse.ALLOW);
|
|
|
|
else
|
|
|
|
this._emitResponse(DialogResponse.DENY);
|
|
|
|
});
|
|
|
|
});
|
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();
|
|
|
|
}
|
|
|
|
});
|