forked from CZ-NIC/run-or-raise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
175 lines (148 loc) · 6.01 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
const Main = imports.ui.main;
const Mainloop = imports.mainloop;
const Meta = imports.gi.Meta;
const Lang = imports.lang;
const Shell = imports.gi.Shell;
KeyManager = new Lang.Class({ // based on https://superuser.com/questions/471606/gnome-shell-extension-key-binding/1182899#1182899
Name: 'MyKeyManager',
_init: function() {
this.grabbers = new Map()
global.display.connect(
'accelerator-activated',
Lang.bind(this, function(display, action, deviceId, timestamp){
log('Accelerator Activated: [display={}, action={}, deviceId={}, timestamp={}]',
display, action, deviceId, timestamp)
this._onAccelerator(action)
}))
},
listenFor: function(accelerator, callback){
log('Trying to listen for hot key [accelerator={}]', accelerator)
let action = global.display.grab_accelerator(accelerator)
log("action:")
log(action)
if(action == Meta.KeyBindingAction.NONE) {
log('Unable to grab accelerator [binding={}]', accelerator)
} else {
log('Grabbed accelerator [action={}]', action)
let name = Meta.external_binding_name_for_action(action)
log('Received binding name for action [name={}, action={}]',
name, action)
log('Requesting WM to allow binding [name={}]', name)
Main.wm.allowKeybinding(name, Shell.ActionMode.ALL)
this.grabbers.set(action, {
name: name,
accelerator: accelerator,
callback: callback,
action: action
})
}
},
_onAccelerator: function(action) {
let grabber = this.grabbers.get(action)
if(grabber) {
this.grabbers.get(action).callback()
} else {
log('No listeners [action={}]', action)
}
}
});
Controller = new Lang.Class({ // based on https://superuser.com/questions/471606/gnome-shell-extension-key-binding/1182899#1182899
Name: 'MyController',
//This is a javascript-closure which will return the event handler
//for each hotkey with it's id. (id=1 For <Super>+1 etc)
jumpapp: function(shortcut) {
function _prepare(s) {
if(s.substr(0,1) === "/" && s.slice(-1) === "/") {
return [new RegExp(s.substr(1, s.length-2)), "search"];
}
else {
return [s, "indexOf"];
}
}
return function() {
var launch = shortcut[1].trim();
var wm_class, wmFn, title, titleFn;
[wm_class, wmFn] = _prepare(shortcut[2].trim());
[title, titleFn] = _prepare(shortcut[3].trim());
let seen = 0;
for (let w of global.get_window_actors()) {
var wm = w.get_meta_window();
if(wm_class) { // seek by class
if(wm.get_wm_class()[wmFn](wm_class) > -1 && (!title || wm.get_title()[titleFn](title) > -1)) {
seen = wm; // wm_class AND if set, title must match
break;
}
} else if( (title && (wm.get_title()[titleFn](title) > -1) ) || // seek by title
(!title && ((wm.get_wm_class().toLowerCase().indexOf(launch.toLowerCase()) > -1) || // seek by launch-command in wm_class
(wm.get_title().toLowerCase().indexOf(launch.toLowerCase()) > -1))) // seek by launch-command in title
) {
seen = wm;
break;
}
}
if(seen) {
wm.activate(0);
} else {
imports.misc.util.spawnCommandLine(launch);
}
return;
}
},
enable: function() {
try {
var s = Shell.get_file_contents_utf8_sync(confpath);
}
catch(e) {
log("Run or raise: can't load confpath" + confpath + ", creating new file from default");
imports.misc.util.spawnCommandLine("cp " + defaultconfpath + " " + confpath);
try {
var s = Shell.get_file_contents_utf8_sync(defaultconfpath); // it seems confpath file is not ready yet, reading defaultconfpath
}
catch(e) {
log("Run or raise: Failed to create default file")
return;
}
}
this.shortcuts = s.split("\n");
this.keyManager = new KeyManager();
for(let line of this.shortcuts) {
try {
if(line[0] == "#" || line.trim() == "") {
continue;
}
let s = line.split(",")
if(s.length > 2) { // shortcut, launch, wm_class, title
this.keyManager.listenFor(s[0].trim(), this.jumpapp(s))
} else { // shortcut, command
this.keyManager.listenFor(s[0].trim(), function() {imports.misc.util.spawnCommandLine(s[1].trim())})
}
} catch(e) {
log("Run or raise: can't parse line: " + line)
}
}
},
disable: function() {
for (let it of this.keyManager.grabbers) {
try {
global.display.ungrab_accelerator(it[1].action)
Main.wm.allowKeybinding(it[1].name, Shell.ActionMode.NONE)
}
catch(e) {
log("Run or raise: error removing keybinding " + it[1].name)
log(e)
}
}
}
});
var app, confpath, defaultconfpath;
function init(settings) {
confpath = settings.path + "/shortcuts.conf";
defaultconfpath = settings.path + "/shortcuts.default";
app = new Controller();
}
function enable(settings) {
app.enable();
}
function disable() {
app.disable();
}