2019-01-31 09:07:06 -05:00
|
|
|
/* exported InhibitShortcutsDialog */
|
2020-06-28 16:28:57 -04:00
|
|
|
const { Clutter, Gio, GLib, GObject, Gtk, Meta, Pango, Shell, St } = imports.gi;
|
2017-07-13 21:15:17 -04:00
|
|
|
|
|
|
|
const Dialog = imports.ui.dialog;
|
|
|
|
const ModalDialog = imports.ui.modalDialog;
|
2019-02-01 11:44:05 -05:00
|
|
|
const PermissionStore = imports.misc.permissionStore;
|
2017-07-13 21:15:17 -04:00
|
|
|
|
|
|
|
const WAYLAND_KEYBINDINGS_SCHEMA = 'org.gnome.mutter.wayland.keybindings';
|
|
|
|
|
2017-08-13 20:32:12 -04:00
|
|
|
const APP_WHITELIST = ['gnome-control-center.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
|
|
|
|
2017-07-13 21:15:17 -04:00
|
|
|
var DialogResponse = Meta.InhibitShortcutsDialogResponse;
|
|
|
|
|
2017-10-30 21:23:39 -04:00
|
|
|
var 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
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_getRestoreAccel() {
|
2017-07-13 21:15:17 -04: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-30 21:23:39 -04:00
|
|
|
}
|
2017-07-13 21:15:17 -04:00
|
|
|
|
2019-02-01 11:44:05 -05: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-30 20:03:21 -04:00
|
|
|
_buildLayout() {
|
2017-08-13 20:32:12 -04:00
|
|
|
let name = this._app ? 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" */
|
|
|
|
? _('The application %s wants to inhibit shortcuts').format(name)
|
|
|
|
: _('An application wants to inhibit shortcuts'),
|
|
|
|
});
|
2017-07-13 21:15:17 -04:00
|
|
|
|
|
|
|
let restoreAccel = this._getRestoreAccel();
|
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
|
|
|
|
|
|
|
this._dialog.addButton({ label: _("Deny"),
|
|
|
|
action: () => {
|
2019-02-01 11:44:05 -05:00
|
|
|
this._saveToPermissionStore(DENIED);
|
2017-07-13 21:15:17 -04:00
|
|
|
this._emitResponse(DialogResponse.DENY);
|
|
|
|
},
|
|
|
|
key: Clutter.KEY_Escape });
|
|
|
|
|
|
|
|
this._dialog.addButton({ label: _("Allow"),
|
|
|
|
action: () => {
|
2019-02-01 11:44:05 -05:00
|
|
|
this._saveToPermissionStore(GRANTED);
|
2017-07-13 21:15:17 -04:00
|
|
|
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() {
|
2018-07-14 16:56:22 -04:00
|
|
|
if (this._app && APP_WHITELIST.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();
|
|
|
|
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-19 20:20:08 -04:00
|
|
|
(res, err) => {
|
|
|
|
if (err) {
|
2019-02-01 11:44:05 -05:00
|
|
|
this._dialog.open();
|
2019-08-19 20:20:08 -04:00
|
|
|
log(err.message);
|
2019-02-01 11:44:05 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-01 08:41:55 -05:00
|
|
|
let [permissions] = res;
|
2019-02-01 11:44:05 -05: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-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();
|
|
|
|
}
|
|
|
|
});
|