-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
177 lines (142 loc) · 5.55 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//////////////////////////////////////////////////////////////////////////////////////////
// _ _ ____ _ _ ___ ____ //
// |_/ |__| |\ | | \ | | This file belongs to Kando, the cross-platform //
// | \_ | | | \| |__/ |__| pie menu. Read more on github.com/kando-menu/kando //
// //
//////////////////////////////////////////////////////////////////////////////////////////
// SPDX-FileCopyrightText: Simon Schneegans <code@simonschneegans.de>
// SPDX-License-Identifier: MIT
'use strict';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import Meta from 'gi://Meta';
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
import {Shortcuts} from './src/Shortcuts.js';
import {InputManipulator} from './src/InputManipulator.js';
// This is the DBus interface which will be exported by this extension. It provides
// methods to get information about the currently focused window and the mouse pointer
// position. It also allows to move the mouse pointer, simulate key strokes and bind
// shortcuts.
const DBUS_INTERFACE = `
<node>
<interface name="org.gnome.Shell.Extensions.KandoIntegration">
<method name="GetWMInfo">
<arg name="windowTitle" type="s" direction="out" />
<arg name="windowClass" type="s" direction="out" />
<arg name="pointerX" type="i" direction="out" />
<arg name="pointerY" type="i" direction="out" />
</method>
<method name="MovePointer">
<arg name="dx" type="i" direction="in" />
<arg name="dy" type="i" direction="in" />
</method>
<method name="SimulateKeys">
<arg name="keys" type="a(ibi)" direction="in" />
</method>
<method name="BindShortcut">
<arg name="shortcut" type="s" direction="in" />
<arg name="success" type="b" direction="out" />
</method>
<method name="UnbindShortcut">
<arg name="shortcut" type="s" direction="in" />
<arg name="success" type="b" direction="out" />
</method>
<method name="UnbindAllShortcuts">
</method>
<signal name="ShortcutPressed">
<arg name="shortcut" type="s"/>
</signal>
</interface>
</node>`;
export default class KandoIntegration extends Extension {
// Exports the DBus interface.
enable() {
// Do nothing on X11.
if (!Meta.is_wayland_compositor()) {
return;
}
// This is used to get the desktop's text scaling factor.
this._shellSettings = new Gio.Settings({schema: 'org.gnome.desktop.interface'});
this._dbus = Gio.DBusExportedObject.wrapJSObject(DBUS_INTERFACE, this);
this._dbus.export(Gio.DBus.session, '/org/gnome/shell/extensions/KandoIntegration');
this._shortcuts = new Shortcuts();
this._inputManipulator = new InputManipulator();
this._shortcuts.connect('activated', (s, shortcut) => {
this._dbus.emit_signal('ShortcutPressed', new GLib.Variant('(s)', [shortcut]));
});
// Re-bind all shortcuts that were bound before the extension was disabled.
this._settings = this.getSettings();
this._settings.get_strv('shortcuts').forEach((shortcut) => {
this._shortcuts.bind(shortcut);
});
}
// Unbinds all shortcuts and unexports the DBus interface.
disable() {
// Do nothing on X11.
if (!Meta.is_wayland_compositor()) {
return;
}
this._shellSettings = null;
this._dbus.flush();
this._dbus.unexport();
this._dbus = null;
this._shortcuts.destroy();
this._shortcuts = null;
this._settings = null;
this._inputManipulator = null;
}
// Returns the title and class of the currently focused window as well as the current
// pointer position.
GetWMInfo() {
let windowName = '';
let windowClass = '';
for (let actor of global.get_window_actors()) {
if (actor.meta_window.has_focus()) {
windowName = actor.meta_window.get_title();
windowClass = actor.meta_window.get_wm_class();
break;
}
}
const [x, y] = global.get_pointer();
const scalingFactor = this._shellSettings.get_double('text-scaling-factor');
return [
windowName, windowClass, Math.round(x / scalingFactor),
Math.round(y / scalingFactor)
];
}
// Warps the mouse pointer by the given distance.
MovePointer(dx, dy) {
this._inputManipulator.movePointer(dx, dy);
}
// Simulates the given key strokes. The keys argument is an array of arrays. Each
// sub-array contains three elements: The keysym, a boolean indicating whether the key
// should be pressed or released and an optional delay in milliseconds.
SimulateKeys(keys) {
this._inputManipulator.simulateKeys(keys);
}
// Binds the given shortcut. When it's pressed, the "ShortcutPressed" signal will be
// emitted.
BindShortcut(shortcut) {
const success = this._shortcuts.bind(shortcut);
if (success) {
const shortcuts = this._settings.get_strv('shortcuts');
shortcuts.push(shortcut);
this._settings.set_strv('shortcuts', shortcuts);
}
return success;
}
// Unbinds a previously bound shortcut.
UnbindShortcut(shortcut) {
const success = this._shortcuts.unbind(shortcut);
if (success) {
const shortcuts = this._settings.get_strv('shortcuts');
this._settings.set_strv('shortcuts', shortcuts.filter((s) => s !== shortcut));
}
return success;
}
// Unbinds all previously bound shortcuts.
UnbindAllShortcuts() {
this._shortcuts.unbindAll();
this._settings.set_strv('shortcuts', []);
}
}