-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.js
191 lines (170 loc) · 6.03 KB
/
user.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
// -*- mode: gnome-shell -*-
var Meta = imports.gi.Meta;
var Clutter = imports.gi.Clutter;
var St = imports.gi.St;
var Main = imports.ui.main;
var Shell = imports.gi.Shell;
var workspaceManager = global.workspace_manager;
// Extension local imports
var Extension, Me, Tiling, Utils, App, Keybindings, Examples, Settings;
const wmClassesToMoveToScratch = [
'evolution-alarm-notify', // Wayland
'Evolution-alarm-notify', // X11
];
const directions = {
Left: 'h',
Down: 'j',
Up: 'k',
Right: 'l',
};
// ============================================
// Utility Functions
// ============================================
/** Returns the last workspace that was active on the active monitor. */
function getPreviousSpaceOnMonitor() {
let spaces = Tiling.spaces;
const mru = [...spaces.stack];
const from = mru.indexOf(spaces.selectedSpace);
let to = from;
do {
to = (to + 1) % mru.length;
} while (to !== from && mru[to].monitor !== spaces.selectedSpace.monitor);
return mru[to];
}
/** Returns the neighboring monitor in the given direction relative to the active monitor. */
function getMonitor(direction) {
let display = global.display;
let spaces = Tiling.spaces;
let currentSpace = spaces.selectedSpace;
let monitor = currentSpace.monitor;
let i = display.get_monitor_neighbor_index(monitor.index, direction);
let opposite;
switch (direction) {
case Meta.DisplayDirection.RIGHT:
opposite = Meta.DisplayDirection.LEFT;
break;
case Meta.DisplayDirection.LEFT:
opposite = Meta.DisplayDirection.RIGHT;
break;
case Meta.DisplayDirection.UP:
opposite = Meta.DisplayDirection.DOWN;
break;
case Meta.DisplayDirection.DOWN:
opposite = Meta.DisplayDirection.UP;
break;
}
let n = i;
if (i === -1) {
let i = monitor.index;
while (i !== -1) {
n = i;
i = display.get_monitor_neighbor_index(n, opposite);
}
}
return Main.layoutManager.monitors[n];
}
// ============================================
// Keybindings
// ============================================
function activateWorkspaceOnCurrentMonitor() {
/**
* Focuses the workspace with the given index.
*
* If the workspace is currently visible on a monitor, the respective monitor gets focused.
* Otherwise, the workspace is activated on the monitor that currently has focus.
*/
function activateSpace(targetIndex) {
let spaces = Tiling.spaces;
let currentSpace = spaces.selectedSpace;
let monitor = currentSpace.monitor;
let target = workspaceManager.get_workspace_by_index(targetIndex);
let targetSpace = spaces.spaceOf(target);
let targetSpaceIsVisible = spaces.monitors.get(targetSpace.monitor) === targetSpace;
if (!targetSpaceIsVisible) {
targetSpace.setMonitor(monitor);
spaces.monitors.set(monitor, targetSpace);
}
targetSpace.workspace.activate(global.get_current_time());
}
for (let k = 0; k <= 9; k++) {
let targetIndex = (k + 9) % 10;
Keybindings.bindkey(`<Super><Shift>${k}`, `goto-space-${targetIndex}`, () =>
activateSpace(targetIndex),
);
}
}
function moveSpaceToMonitor(basebinding = '<super><alt>') {
/**
* Move the active workspace to a neighboring monitor in the given direction.
*
* The active monitor switches back the last workspace that was active on that monitor.
*/
function moveTo(direction) {
let spaces = Tiling.spaces;
let currentSpace = spaces.selectedSpace;
let nextMonitor = getMonitor(direction);
getPreviousSpaceOnMonitor().workspace.activate(global.get_current_time());
currentSpace.setMonitor(nextMonitor);
spaces.monitors.set(nextMonitor, currentSpace);
currentSpace.workspace.activate(global.get_current_time());
}
for (let arrow of ['Down', 'Left', 'Up', 'Right']) {
Keybindings.bindkey(`${basebinding}${arrow}`, `move-space-monitor-${arrow}`, () => {
moveTo(Meta.DisplayDirection[arrow.toUpperCase()]);
});
Keybindings.bindkey(
`${basebinding}${directions[arrow]}`,
`move-space-monitor-${arrow}-alt`,
() => {
moveTo(Meta.DisplayDirection[arrow.toUpperCase()]);
},
);
}
}
/**
* Activates the next empty workspace on the currently active monitor.
*/
function goToNextEmptyWorkspace(binding = "<Super><Shift>Return") {
Keybindings.bindkey(binding, "go-to-next-empty-space", () => {
let spaces = Tiling.spaces;
targetSpace = [...spaces.values()].find((space) => space.length === 0);
if (targetSpace) {
targetSpace.workspace.activate(global.get_current_time());
}
});
}
// ============================================
// Hooks
// ============================================
function init() {
// Runs _only_ once on startup
// Initialize extension imports here to make gnome-shell-reload work
Extension = imports.misc.extensionUtils.getCurrentExtension();
Me = Extension.imports.user;
Tiling = Extension.imports.tiling;
Utils = Extension.imports.utils;
Keybindings = Extension.imports.keybindings;
Examples = Extension.imports.examples;
App = Extension.imports.app;
Settings = Extension.imports.settings;
// Keybindings
moveSpaceToMonitor();
activateWorkspaceOnCurrentMonitor();
goToNextEmptyWorkspace();
Examples.keybindings.cycleMonitor();
Examples.keybindings.reorderWorkspace();
Examples.keybindings.cycleEdgeSnap();
// Window properties
for (const wm_class of wmClassesToMoveToScratch) {
Settings.defwinprop({
wm_class,
scratch_layer: true,
});
}
}
function enable() {
// Runs on extension reloads, eg. when unlocking the session
}
function disable() {
// Runs on extension reloads eg. when locking the session (`<super>L).
}