This repository has been archived by the owner on Oct 29, 2022. It is now read-only.
forked from janxyz/custom-hot-corners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
234 lines (203 loc) · 7.99 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
/* Copyright 2017 Jan Runge <[email protected]>
*
* 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 3 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/>.
*/
const Clutter = imports.gi.Clutter;
const St = imports.gi.St;
const Meta = imports.gi.Meta;
const Shell = imports.gi.Shell;
const GObject = imports.gi.GObject;
const Main = imports.ui.main;
const Layout = imports.ui.layout;
const Ripples = imports.ui.ripples;
const Util = imports.misc.util;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Settings = Me.imports.settings;
let _origUpdateHotCorners = Main.layoutManager._updateHotCorners;
let _corners = [];
function init() {
}
function enable() {
Main.layoutManager._updateHotCorners = _updateHotCorners;
Main.layoutManager._updateHotCorners();
}
function disable() {
// This restores the original hot corners
_removeHotCorners();
Main.layoutManager._updateHotCorners = _origUpdateHotCorners;
Main.layoutManager._updateHotCorners();
}
function _removeHotCorners() {
_corners.forEach(c => c.destroy());
_corners = [];
// hot corners might be null
Main.layoutManager.hotCorners.filter(Boolean).forEach(c => c.destroy());
Main.layoutManager.hotCorners = [];
}
function _updateHotCorners() {
_removeHotCorners();
for (let i = 0; i < Main.layoutManager.monitors.length; ++i) {
const corners = Settings.Corner.forMonitor(i, global.display.get_monitor_geometry(i));
for (let corner of corners) {
_corners.push(corner);
// Update all hot corners if something changes
corner.connect('changed', () => _updateHotCorners());
if (corner.action !== 'disabled') {
Main.layoutManager.hotCorners.push(new CustomHotCorner(corner));
}
}
}
}
const CustomHotCorner = GObject.registerClass(
class CustomHotCorner extends Layout.HotCorner {
_init(corner) {
let monitor = Main.layoutManager.monitors[corner.monitorIndex];
super._init(Main.layoutManager, monitor, corner.x, corner.y);
this._corner = corner;
this._monitor = monitor;
let m = new Map([
['toggleOverview', this._toggleOverview],
['showDesktop', this._showDesktop],
['showApplications', this._showApplications],
['runCommand', this._runCommand]
]);
this._actionFunction = m.get(this._corner.action) || function () {};
// Avoid pointer barriers that are at the same position
// but block opposite directions. Neither with X nor with Wayland
// such barriers work.
for (let c of Main.layoutManager.hotCorners) {
if (this._corner.x === c._x && this._corner.y === c._y) {
if (this._corner.top === c._top) {
this._corner.x += this._corner.left ? 1 : -1;
} else if (this._corner.left === c._left) {
this._corner.y += this._corner.top ? 1 : -1;
}
}
}
this._enterd = false;
this._pressureBarrier = new Layout.PressureBarrier(
corner.pressureThreshold,
Layout.HOT_CORNER_PRESSURE_TIMEOUT,
Shell.ActionMode.NORMAL | Shell.ActionMode.OVERVIEW
);
this._pressureBarrier.connect('trigger', this._runAction.bind(this));
this._setupFallbackCornerIfNeeded(Main.layoutManager);
// Rotate the ripple actors according to the corner.
let ltr = (Clutter.get_default_text_direction() ==
Clutter.TextDirection.LTR);
let angle = (this._corner.left && ltr) ? (this._corner.top ? 0 : 270) : (this._corner.top ? 90 : 180);
this._ripples._ripple1.rotation_angle_z = angle;
this._ripples._ripple2.rotation_angle_z = angle;
this._ripples._ripple3.rotation_angle_z = angle;
this.setBarrierSize(corner.barrierSize);
}
// Overridden to allow all 4 monitor corners
setBarrierSize(size) {
// Use code of parent class to remove old barriers but new barriers
// must be created here since the properties are construct only.
super.setBarrierSize(0);
if (size > 0) {
const BD = Meta.BarrierDirection;
this._verticalBarrier = new Meta.Barrier({
display: global.display,
x1: this._corner.x,
x2: this._corner.x,
y1: this._corner.y,
y2: this._corner.top ? this._corner.y + size : this._corner.y - size,
directions: this._corner.left ? BD.POSITIVE_X : BD.NEGATIVE_X
});
this._horizontalBarrier = new Meta.Barrier({
display: global.display,
x1: this._corner.x,
x2: this._corner.left ? this._corner.x + size : this._corner.x - size,
y1: this._corner.y,
y2: this._corner.y,
directions: this._corner.top ? BD.POSITIVE_Y : BD.NEGATIVE_Y
});
this._pressureBarrier.addBarrier(this._verticalBarrier);
this._pressureBarrier.addBarrier(this._horizontalBarrier);
}
}
// Overridden to allow all 4 monitor corners
_setupFallbackCornerIfNeeded(layoutManager) {
if (global.display.supports_extended_barriers())
return;
this.actor = new Clutter.Actor({
name: 'hot-corner-environs',
x: this._corner.x, y: this._corner.y,
width: 3, height: 3,
reactive: true,
scale_x: this._corner.left ? 1 : -1,
scale_y: this._corner.top ? 1 : -1
});
this._cornerActor = new Clutter.Actor({
name: 'hot-corner',
x: 0, y: 0,
width: 1, height: 1,
reactive: true
});
this._cornerActor._delegate = this;
this.actor.add_child(this._cornerActor);
layoutManager.addChrome(this.actor);
this.actor.connect('leave-event', this._onEnvironsLeft.bind(this));
this._cornerActor.connect('enter-event', this._onCornerEntered.bind(this));
this._cornerActor.connect('leave-event', this._onCornerLeft.bind(this));
}
_rippleAnimation() {
this._ripples.playAnimation(this._corner.x, this._corner.y);
}
// Overridden to allow running custom actions
_onCornerEntered() {
if (!this._entered) {
this._entered = true;
this._runAction();
}
return Clutter.EVENT_PROPAGATE;
}
_runAction() {
if (this._monitor.inFullscreen && this._corner.fullscreen) {
this._actionFunction();
} else if (!this._monitor.inFullscreen) {
this._actionFunction();
}
}
_toggleOverview() {
if (Main.overview.shouldToggleByCornerOrButton()) {
this._rippleAnimation();
Main.overview.toggle();
}
}
_showDesktop() {
this._rippleAnimation();
Util.spawn([
'sh',
'-c',
('if wmctrl -m | grep -q -e "mode: OFF" -e "mode: N/A"; ' +
'then wmctrl -k on; else wmctrl -k off; fi')
]);
}
_showApplications() {
this._rippleAnimation();
if (Main.overview.viewSelector._showAppsButton.checked) {
Main.overview.hide();
} else {
Main.overview.viewSelector._toggleAppsPage();
}
}
_runCommand() {
this._rippleAnimation();
Util.spawnCommandLine(this._corner.command);
}
});