forked from mijorus/smile-gnome-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
101 lines (85 loc) · 3.4 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
/* extension.js
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Author: Lorenzo Paderi
*/
const { GLib, Gio, St, Clutter, Gdk } = imports.gi;
class Extension {
constructor() {
// console.log('extension initialized');
this.virtualKeyboard = undefined;
this.clipboard = undefined;
this.dbusSignalId = undefined;
this.timeouts = [];
}
getVirtualKeyboard() {
if (this.virtualKeyboard) {
return this.virtualKeyboard;
}
let deviceType = Clutter.InputDeviceType.KEYBOARD_DEVICE;
this.virtualKeyboard = Clutter.get_default_backend().get_default_seat().create_virtual_device(deviceType);
return this.virtualKeyboard;
}
disableTimeouts() {
for (let t of this.timeouts) {
GLib.Source.remove(t);
}
this.timeouts = [];
}
pasteEmoji(copiedText) {
this.clipboard.get_text(St.ClipboardType.PRIMARY, (__, text) => {
if (text !== copiedText) {
this.clipboard.set_text(St.ClipboardType.PRIMARY, copiedText);
}
const t1 = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 250, () => {
this.getVirtualKeyboard().notify_keyval(Clutter.get_current_event_time(), Clutter.KEY_Control_L, Clutter.KeyState.RELEASED);
this.getVirtualKeyboard().notify_keyval(Clutter.get_current_event_time(), Clutter.KEY_Control_L, Clutter.KeyState.PRESSED);
this.getVirtualKeyboard().notify_keyval(Clutter.get_current_event_time(), Clutter.KEY_v, Clutter.KeyState.PRESSED);
});
const t2 = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 300, () => {
this.getVirtualKeyboard().notify_keyval(Clutter.get_current_event_time(), Clutter.KEY_Control_L, Clutter.KeyState.RELEASED);
this.getVirtualKeyboard().notify_keyval(Clutter.get_current_event_time(), Clutter.KEY_v, Clutter.KeyState.RELEASED);
});
this.timeouts.push(t1, t2);
})
}
enable() {
this.clipboard = St.Clipboard.get_default();
this.disableTimeouts();
this.dbusSignalId = Gio.DBus.session.signal_subscribe(
null,
'it.mijorus.smile',
'CopiedEmoji',
'/it/mijorus/smile/actions',
null,
Gio.DBusSignalFlags.NONE,
(connection, sender_name, object_path, interface_name, signal_name, params) => {
this.pasteEmoji(params.get_child_value(0).get_string()[0])
},
);
}
disable() {
this.disableTimeouts();
// unsub
if (this.dbusSignalId !== undefined) {
Gio.DBus.session.signal_unsubscribe(this.dbusSignalId)
}
}
}
function init() {
return new Extension();
}