-
Notifications
You must be signed in to change notification settings - Fork 47
/
extension.js
476 lines (398 loc) · 15.5 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
const { Clutter, Gio, GLib, GObject, Meta, Shell, St } = imports.gi;
const AppDisplay = imports.ui.appDisplay;
const { AppMenu } = imports.ui.appMenu;
const AltTab = imports.ui.altTab;
const ExtensionUtils = imports.misc.extensionUtils;
const extension = ExtensionUtils.getCurrentExtension();
const Main = imports.ui.main;
const Overview = imports.ui.overview;
const OverviewControls = imports.ui.overviewControls;
const SwitcherPopup = imports.ui.switcherPopup;
const Util = imports.misc.util;
const WorkspacesView = imports.ui.workspacesView;
const WorkspaceThumbnail = imports.ui.workspaceThumbnail;
var applications = extension.imports.applications;
var { Service } = extension.imports.dbus_service;
var { OVERVIEW_WORKSPACES, OVERVIEW_APPLICATIONS, OVERVIEW_LAUNCHER } = extension.imports.overview;
var { overview_visible, overview_show, overview_hide, overview_toggle } = extension.imports.overview;
var { CosmicTopBarButton } = extension.imports.topBarButton;
var { settings_new_schema } = extension.imports.settings;
let activities_signal_show = null;
let appMenu_signal_show = null;
let workspaces_button = null;
let applications_button = null;
let signal_overlay_key = null;
let signal_monitors_changed = null;
let signal_notify_checked = null;
let search_signal_showing = null;
let original_signal_overlay_key = null;
let settings = null;
let touchpad_settings = null;
let service = null;
let injections = [];
function inject(object, parameter, replacement) {
injections.push({
"object": object,
"parameter": parameter,
"value": object[parameter]
});
object[parameter] = replacement;
}
const CLOCK_CENTER = 0;
const CLOCK_LEFT = 1;
const CLOCK_RIGHT = 2;
function getWorkspacesDisplay() {
return Main.overview._overview._controls._workspacesDisplay;
}
let indicatorPad = null;
function clock_alignment(alignment) {
// Clock Alignement breaks Date Menu, when other extensions like Dash2Panel are used
let dash2Panel = Main.extensionManager.lookup("dash-to-panel@jderose9.github.com");
if(dash2Panel && dash2Panel.state == ExtensionUtils.ExtensionState.ENABLED){
return;
}
if (Main.layoutManager.monitors.length == 0) {
return;
}
const dateMenu = Main.panel.statusArea['dateMenu'];
const container = dateMenu.container;
const parent = container.get_parent();
if (parent != null) {
parent.remove_child (container);
}
const banner_width = Main.panel.statusArea.dateMenu._messageList.width;
const banner_offset = Main.layoutManager.monitors[0].width - banner_width;
let clock_padding = false;
Main.messageTray._bannerBin.width = banner_width;
if (alignment == CLOCK_LEFT) {
Main.panel._leftBox.insert_child_at_index(container, 0);
Main.messageTray._bannerBin.x = -banner_offset;
} else if (alignment == CLOCK_RIGHT) {
Main.panel._rightBox.add_actor(container);
Main.messageTray._bannerBin.x = banner_offset;
} else {
Main.panel._centerBox.add_actor(container);
Main.messageTray._bannerBin.x = 0;
clock_padding = true;
}
const dateMenuBox = dateMenu.get_child_at_index(0);
if (indicatorPad == null) {
indicatorPad = dateMenuBox.get_child_at_index(0);
}
if (clock_padding) {
if (indicatorPad.get_parent() == null) {
dateMenuBox.insert_child_at_index(indicatorPad, 0);
}
} else {
if (indicatorPad.get_parent() != null) {
dateMenuBox.remove_child(indicatorPad);
}
}
}
var overlay_key_action = OVERVIEW_LAUNCHER;
function overlay_key() {
overview_toggle(overlay_key_action);
}
function overlay_key_changed(settings) {
if (overview_visible(overlay_key_action)) {
overview_hide(overlay_key_action);
}
overlay_key_action = settings.get_enum("overlay-key-action");
}
function show_application_menu(show) {
if (show) {
if (appMenu_signal_show != null) {
Main.panel.statusArea.appMenu.disconnect(appMenu_signal_show);
appMenu_signal_show = null;
}
Main.panel.statusArea.appMenu.show();
} else {
appMenu_signal_show = Main.panel.statusArea.appMenu.connect("show", function() {
Main.panel.statusArea.appMenu.hide();
});
Main.panel.statusArea.appMenu.hide();
}
}
function switch_workspace(direction) {
// Adapted from _showWorkspaceSwitcher
let workspaceManager = global.display.get_workspace_manager();
// Do not switch if workspaces are disabled
if (!Main.sessionMode.hasWorkspaces) {
return;
}
// Do not switch if there is only one workspace
if (workspaceManager.n_workspaces == 1) {
return;
}
// Do not switch if workspaces are vertical but direction is not
if (workspaceManager.layout_rows == -1 &&
direction != Meta.MotionDirection.UP &&
direction != Meta.MotionDirection.DOWN) {
return;
}
// Do not switch if workspaces are horizontal but direction is not
if (workspaceManager.layout_columns == -1 &&
direction != Meta.MotionDirection.LEFT &&
direction != Meta.MotionDirection.RIGHT) {
return;
}
// Find active workspace and new workspace in switch direction
let activeWorkspace = workspaceManager.get_active_workspace();
let newWorkspace = activeWorkspace.get_neighbor(direction);
// If the new workspace is different from the active one, switch to it
if (newWorkspace != activeWorkspace) {
newWorkspace.activate(global.get_current_time());
}
}
function swap_for_natural(direction) {
switch(direction) {
case Meta.MotionDirection.UP:
return Meta.MotionDirection.DOWN
case Meta.MotionDirection.DOWN:
return Meta.MotionDirection.UP
case Meta.MotionDirection.LEFT:
return Meta.MotionDirection.RIGHT
case Meta.MotionDirection.RIGHT:
return Meta.MotionDirection.LEFT
default:
return direction
}
}
function apply_scroll_settings(direction) {
if (touchpad_settings && touchpad_settings.get_boolean('natural-scroll')) {
return swap_for_natural(direction)
}
return direction
}
function gesture_up() {
switch_workspace(apply_scroll_settings(Meta.MotionDirection.UP));
}
function gesture_down() {
switch_workspace(apply_scroll_settings(Meta.MotionDirection.DOWN));
}
function gesture_left() {
if (overview_visible(OVERVIEW_WORKSPACES)) {
overview_hide(OVERVIEW_WORKSPACES);
} else if (overview_visible(OVERVIEW_APPLICATIONS)) {
overview_hide(OVERVIEW_APPLICATIONS);
} else {
overview_show(OVERVIEW_WORKSPACES);
}
}
function gesture_right() {
if (overview_visible(OVERVIEW_WORKSPACES)) {
overview_hide(OVERVIEW_WORKSPACES);
} else if (overview_visible(OVERVIEW_APPLICATIONS)) {
overview_hide(OVERVIEW_APPLICATIONS);
} else {
overview_show(OVERVIEW_APPLICATIONS);
}
}
function monitors_changed() {
clock_alignment(settings.get_enum("clock-alignment"));
}
function gnome_40_enable() {
// No overview at start-up
if (Main.layoutManager._startingUp && Main.sessionMode.hasOverview) {
inject(Main.sessionMode, "hasOverview", false);
Main.layoutManager.connect('startup-complete', () => {
Main.sessionMode.hasOverview = true;
});
}
// Disable type to search
inject(Main.overview._overview._controls._searchController, '_onStageKeyPress', function (actor, event) {
if (Main.modalCount > 1)
return Clutter.EVENT_PROPAGATE;
let symbol = event.get_key_symbol();
if (symbol === Clutter.KEY_Escape) {
Main.overview.hide();
return Clutter.EVENT_STOP;
}
return Clutter.EVENT_PROPAGATE;
});
applications.enable();
const overview_show = Main.overview.show;
inject(Main.overview, 'show', function() {
overview_show.call(this);
applications.hide();
});
const overview_hide = Main.overview.hide;
inject(Main.overview, 'hide', function() {
overview_hide.call(this);
applications.hide();
});
}
function gnome_40_disable() {
applications.disable();
}
function init(metadata) {}
function enable() {
gnome_40_enable();
// Raise first window on alt-tab
inject(AltTab.AppSwitcherPopup.prototype, "_finish", function (timestamp) {
let appIcon = this._items[this._selectedIndex];
if (this._currentWindow < 0)
Main.activateWindow(appIcon.cachedWindows[0], timestamp);
else
Main.activateWindow(appIcon.cachedWindows[this._currentWindow], timestamp);
SwitcherPopup.SwitcherPopup.prototype._finish.apply(this, [timestamp]);
});
// Pop Shop details
inject(AppMenu.prototype, "_updateDetailsVisibility", function () {
this._detailsItem.visible = false;
const sw = this._appSystem.lookup_app('io.elementary.appcenter.desktop');
if (sw !== null) {
this._detailsItem = this.addAction(_('Show Details'), async () => {
const id = this._app.get_id();
Util.trySpawn(["io.elementary.appcenter", "appstream://" + id]);
Main.overview.hide();
});
}
});
// Hide activities button
activities_signal_show = Main.panel.statusArea.activities.connect("show", function() {
Main.panel.statusArea.activities.hide();
});
Main.panel.statusArea.activities.hide();
settings = settings_new_schema(extension.metadata["settings-schema"]);
// Hide app menu
show_application_menu(settings.get_boolean("show-application-menu"));
settings.connect("changed::show-application-menu", () => {
show_application_menu(settings.get_boolean("show-application-menu"));
});
// Load overlay key action and keep it up to date with settings
overlay_key_changed(settings);
settings.connect("changed::overlay-key-action", () => {
overlay_key_changed(settings);
});
// Add workspaces button
//TODO: this removes the curved selection corner, do we care?
workspaces_button = new CosmicTopBarButton(settings, OVERVIEW_WORKSPACES);
Main.panel.addToStatusArea("cosmic_workspaces", workspaces_button, 0, "left");
// Add applications button
applications_button = new CosmicTopBarButton(settings, OVERVIEW_APPLICATIONS);
Main.panel.addToStatusArea("cosmic_applications", applications_button, 1, "left");
// Hide search
// This signal cannot be connected until Main.overview is initialized
GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
if (!Main.overview._initCalled)
return GLib.SOURCE_CONTINUE;
Main.overview.searchEntry.hide();
return GLib.SOURCE_REMOVE;
});
inject(Main.layoutManager, "_updateVisibility", function () {
let windowsVisible = (Main.sessionMode.hasWindows && !this._inOverview) || Main.overview.dash.showAppsButton.checked;
global.window_group.visible = windowsVisible;
global.top_window_group.visible = windowsVisible;
this._trackedActors.forEach(this._updateActorVisibility.bind(this));
});
// Block original overlay key handler
original_signal_overlay_key = GObject.signal_handler_find(global.display, { signalId: "overlay-key" });
if (original_signal_overlay_key !== null) {
global.display.block_signal_handler(original_signal_overlay_key);
}
// Connect modified overlay key handler
const A11Y_SCHEMA = 'org.gnome.desktop.a11y.keyboard';
const STICKY_KEYS_ENABLE = 'stickykeys-enable';
let _a11ySettings = new Gio.Settings({ schema_id: A11Y_SCHEMA });
signal_overlay_key = global.display.connect("overlay-key", () => {
if (!_a11ySettings.get_boolean(STICKY_KEYS_ENABLE))
overlay_key();
});
// Make applications shortcut hide/show overview
const SHELL_KEYBINDINGS_SCHEMA = 'org.gnome.shell.keybindings';
Main.wm.removeKeybinding('toggle-application-view');
Main.wm.addKeybinding(
'toggle-application-view',
new Gio.Settings({ schema_id: SHELL_KEYBINDINGS_SCHEMA }),
Meta.KeyBindingFlags.IGNORE_AUTOREPEAT,
Shell.ActionMode.NORMAL |
Shell.ActionMode.OVERVIEW,
() => overview_toggle(OVERVIEW_APPLICATIONS)
);
settings.connect("changed::clock-alignment", () => {
clock_alignment(settings.get_enum("clock-alignment"));
});
// Connect monitors-changed handler
signal_monitors_changed = Main.layoutManager.connect('monitors-changed', monitors_changed);
monitors_changed();
const DESKTOP_PERIPHERALS_TOUCHPAD_SCHEMA = 'org.gnome.desktop.peripherals.touchpad'
touchpad_settings = new Gio.Settings({ schema_id: DESKTOP_PERIPHERALS_TOUCHPAD_SCHEMA })
// Enable dbus service
service = new Service();
service.GestureLeft = () => { gesture_left(); };
service.GestureRight = () => { gesture_right(); };
service.GestureUp = () => { gesture_up(); };
service.GestureDown = () => { gesture_down(); };
service.ToggleApplications = () => { overview_toggle(OVERVIEW_APPLICATIONS); };
service.ToggleLauncher = () => { overview_toggle(OVERVIEW_LAUNCHER); };
service.ToggleWorkspaces = () => { overview_toggle(OVERVIEW_WORKSPACES); };
}
function disable() {
// Disable dbus service
if (service !== null) {
service.dbus.unexport()
service.destroy();
service = null;
}
gnome_40_disable();
// Disconnect monitors-changed handler
if (signal_monitors_changed !== null) {
Main.layoutManager.disconnect(signal_monitors_changed);
signal_monitors_changed = null;
}
// Restore applications shortcut
const SHELL_KEYBINDINGS_SCHEMA = 'org.gnome.shell.keybindings';
Main.wm.removeKeybinding('toggle-application-view');
let obj = Main.overview._overview._controls;
Main.wm.addKeybinding(
'toggle-application-view',
new Gio.Settings({ schema_id: SHELL_KEYBINDINGS_SCHEMA }),
Meta.KeyBindingFlags.IGNORE_AUTOREPEAT,
Shell.ActionMode.NORMAL |
Shell.ActionMode.OVERVIEW,
obj._toggleAppsPage.bind(obj)
);
// Disconnect modified overlay key handler
if (signal_overlay_key !== null) {
global.display.disconnect(signal_overlay_key);
signal_overlay_key = null;
}
// Unblock original overlay key handler
if (original_signal_overlay_key !== null) {
global.display.unblock_signal_handler(original_signal_overlay_key);
original_signal_overlay_key = null;
}
// Show search
if (signal_notify_checked !== null) {
Main.overview.dash.showAppsButton.disconnect(signal_notify_checked);
signal_notify_checked = null;
}
if (search_signal_showing !== null) {
Main.overview.disconnect(search_signal_showing);
search_signal_showing = null;
}
Main.overview.searchEntry.show();
// Reset background changes
Main.overview._overview.remove_style_class_name("cosmic-solid-bg");
// Remove applications button
applications_button.destroy();
applications_button = null;
// Remove workspaces button
workspaces_button.destroy();
workspaces_button = null;
// Show app menu
show_application_menu(true);
// Show activities button
Main.panel.statusArea.activities.disconnect(activities_signal_show);
activities_signal_show = null;
Main.panel.statusArea.activities.show();
// Remove injections
let i;
for(i in injections) {
let injection = injections[i];
injection["object"][injection["parameter"]] = injection["value"];
}
clock_alignment(CLOCK_CENTER);
touchpad_settings = null;
}