Debian Codegraph @

extensions/windowsNavigator/extension.js

raw
1 // SPDX-FileCopyrightText: 2011 Maxim Ermilov <[email protected]>
2 // SPDX-FileCopyrightText: 2011 Giovanni Campagna <[email protected]>
3 // SPDX-FileCopyrightText: 2017 Florian Müllner <[email protected]>
4 // SPDX-FileCopyrightText: 2019 Marco Trevisan (Treviño) <[email protected]>
5 // SPDX-FileCopyrightText: 2020 Thun Pin <[email protected]>
6 //
7 // SPDX-License-Identifier: GPL-2.0-or-later
8
9 import Clutter from 'gi://Clutter';
10 import Graphene from 'gi://Graphene';
11 import St from 'gi://St';
12
13 import * as Main from 'resource:///org/gnome/shell/ui/main.js';
14 import * as OverviewControls from 'resource:///org/gnome/shell/ui/overviewControls.js';
15 import {InjectionManager} from 'resource:///org/gnome/shell/extensions/extension.js';
16 import {WindowPreview} from 'resource:///org/gnome/shell/ui/windowPreview.js';
17 import {Workspace} from 'resource:///org/gnome/shell/ui/workspace.js';
18 import {WorkspacesView} from 'resource:///org/gnome/shell/ui/workspacesView.js';
19
20 const WINDOW_SLOT = 4;
21
22 export default class Extension {
23 enable() {
24 const previewProto = WindowPreview.prototype;
25
26 this._injectionManager = new InjectionManager();
27 this._injectionManager.overrideMethod(previewProto, '_init', originalMethod => {
28 /* eslint-disable no-invalid-this */
29 return function (...args) {
30 originalMethod.call(this, ...args);
31
32 this._text = new St.Label({
33 style_class: 'extension-windowsNavigator-window-tooltip',
34 visible: false,
35 });
36
37 this._text.add_constraint(new Clutter.BindConstraint({
38 source: this.windowContainer,
39 coordinate: Clutter.BindCoordinate.POSITION,
40 }));
41 this._text.add_constraint(new Clutter.AlignConstraint({
42 source: this.windowContainer,
43 align_axis: Clutter.AlignAxis.X_AXIS,
44 pivot_point: new Graphene.Point({x: 0.5, y: -1}),
45 factor: this._closeButtonSide === St.Side.LEFT ? 1 : 0,
46 }));
47 this._text.add_constraint(new Clutter.AlignConstraint({
48 source: this.windowContainer,
49 align_axis: Clutter.AlignAxis.Y_AXIS,
50 pivot_point: new Graphene.Point({x: -1, y: 0.5}),
51 factor: 0,
52 }));
53
54 this.add_child(this._text);
55 };
56 /* eslint-enable */
57 });
58 this._injectionManager.overrideMethod(previewProto, 'showTooltip', () => {
59 /* eslint-disable no-invalid-this */
60 return function (text) {
61 this._text.set({text});
62 this._text.show();
63 };
64 /* eslint-enable */
65 });
66 this._injectionManager.overrideMethod(previewProto, 'hideTooltip', () => {
67 /* eslint-disable no-invalid-this */
68 return function () {
69 this._text?.hide();
70 };
71 /* eslint-enable */
72 });
73
74 const workspaceProto = Workspace.prototype;
75 this._injectionManager.overrideMethod(workspaceProto, '_init', originalMethod => {
76 /* eslint-disable no-invalid-this */
77 return function (...args) {
78 originalMethod.call(this, ...args);
79
80 if (this.metaWorkspace && this.metaWorkspace.index() < 9) {
81 this._tip = new St.Label({
82 style_class: 'extension-windowsNavigator-window-tooltip',
83 visible: false,
84 });
85 this.add_child(this._tip);
86
87 this.connect('notify::scale-x', () => {
88 this._tip.set_scale(1 / this.scale_x, 1 / this.scale_x);
89 });
90 } else {
91 this._tip = null;
92 }
93 };
94 /* eslint-enable */
95 });
96 this._injectionManager.overrideMethod(workspaceProto, 'vfunc_allocate', originalMethod => {
97 /* eslint-disable no-invalid-this */
98 return function (box) {
99 originalMethod.call(this, box);
100
101 this._tip?.allocate_preferred_size(0, 0);
102 };
103 /* eslint-enable */
104 });
105 this._injectionManager.overrideMethod(workspaceProto, 'showTooltip', () => {
106 /* eslint-disable no-invalid-this */
107 return function () {
108 if (!this._tip)
109 return;
110 this._tip.text = (this.metaWorkspace.index() + 1).toString();
111 this._tip.show();
112 this.set_child_below_sibling(this._tip, null);
113 };
114 /* eslint-enable */
115 });
116 this._injectionManager.overrideMethod(workspaceProto, 'hideTooltip', () => {
117 /* eslint-disable no-invalid-this */
118 return function () {
119 this._tip?.hide();
120 };
121 /* eslint-enable */
122 });
123 this._injectionManager.overrideMethod(workspaceProto, 'getWindowWithTooltip', () => {
124 /* eslint-disable no-invalid-this */
125 return function (id) {
126 const {layoutManager} = this._container;
127 const slot = layoutManager._windowSlots[id - 1];
128 return slot ? slot[WINDOW_SLOT].metaWindow : null;
129 };
130 /* eslint-enable */
131 });
132 this._injectionManager.overrideMethod(workspaceProto, 'showWindowsTooltips', () => {
133 /* eslint-disable no-invalid-this */
134 return function () {
135 const {layoutManager} = this._container;
136 for (let i = 0; i < layoutManager._windowSlots.length; i++) {
137 if (layoutManager._windowSlots[i])
138 layoutManager._windowSlots[i][WINDOW_SLOT].showTooltip(`${i + 1}`);
139 }
140 };
141 /* eslint-enable */
142 });
143 this._injectionManager.overrideMethod(workspaceProto, 'hideWindowsTooltips', () => {
144 /* eslint-disable no-invalid-this */
145 return function () {
146 const {layoutManager} = this._container;
147 for (const i in layoutManager._windowSlots) {
148 if (layoutManager._windowSlots[i])
149 layoutManager._windowSlots[i][WINDOW_SLOT].hideTooltip();
150 }
151 };
152 /* eslint-enable */
153 });
154
155 const viewProto = WorkspacesView.prototype;
156 this._injectionManager.overrideMethod(viewProto, '_init', originalMethod => {
157 /* eslint-disable no-invalid-this */
158 return function (...args) {
159 originalMethod.call(this, ...args);
160
161 this._pickWorkspace = false;
162 this._pickWindow = false;
163 global.stage.connectObject(
164 'key-press-event', this._onKeyPress.bind(this),
165 'key-release-event', this._onKeyRelease.bind(this),
166 this);
167 };
168 /* eslint-enable */
169 });
170 this._injectionManager.overrideMethod(viewProto, '_hideTooltips', () => {
171 /* eslint-disable no-invalid-this */
172 return function () {
173 if (global.stage.get_key_focus() === null)
174 global.stage.set_key_focus(this._prevFocusActor);
175 this._pickWindow = false;
176 for (let i = 0; i < this._workspaces.length; i++)
177 this._workspaces[i].hideWindowsTooltips();
178 };
179 /* eslint-enable */
180 });
181 this._injectionManager.overrideMethod(viewProto, '_hideWorkspacesTooltips', () => {
182 /* eslint-disable no-invalid-this */
183 return function () {
184 global.stage.set_key_focus(this._prevFocusActor);
185 this._pickWorkspace = false;
186 for (let i = 0; i < this._workspaces.length; i++)
187 this._workspaces[i].hideTooltip();
188 };
189 /* eslint-enable */
190 });
191 this._injectionManager.overrideMethod(viewProto, '_onKeyRelease', () => {
192 /* eslint-disable no-invalid-this */
193 return function (actor, event) {
194 if (this._pickWindow &&
195 (event.get_key_symbol() === Clutter.KEY_Alt_L ||
196 event.get_key_symbol() === Clutter.KEY_Alt_R))
197 this._hideTooltips();
198 if (this._pickWorkspace &&
199 (event.get_key_symbol() === Clutter.KEY_Control_L ||
200 event.get_key_symbol() === Clutter.KEY_Control_R))
201 this._hideWorkspacesTooltips();
202 };
203 /* eslint-enable */
204 });
205 this._injectionManager.overrideMethod(viewProto, '_onKeyPress', () => {
206 /* eslint-disable no-invalid-this */
207 return function (actor, event) {
208 const {ControlsState} = OverviewControls;
209 if (this._overviewAdjustment.value !== ControlsState.WINDOW_PICKER)
210 return Clutter.EVENT_PROPAGATE;
211
212 const workspaceManager = global.workspace_manager;
213
214 if ((event.get_key_symbol() === Clutter.KEY_Alt_L ||
215 event.get_key_symbol() === Clutter.KEY_Alt_R) &&
216 !this._pickWorkspace) {
217 this._prevFocusActor = global.stage.get_key_focus();
218 global.stage.set_key_focus(null);
219 this._active = workspaceManager.get_active_workspace_index();
220 this._pickWindow = true;
221 this._workspaces[workspaceManager.get_active_workspace_index()].showWindowsTooltips();
222 return Clutter.EVENT_STOP;
223 }
224 if ((event.get_key_symbol() === Clutter.KEY_Control_L ||
225 event.get_key_symbol() === Clutter.KEY_Control_R) &&
226 !this._pickWindow) {
227 this._prevFocusActor = global.stage.get_key_focus();
228 global.stage.set_key_focus(null);
229 this._pickWorkspace = true;
230 for (let i = 0; i < this._workspaces.length; i++)
231 this._workspaces[i].showTooltip();
232 return Clutter.EVENT_STOP;
233 }
234
235 if (global.stage.get_key_focus() !== null)
236 return Clutter.EVENT_PROPAGATE;
237
238 // ignore shift presses, they're required to get numerals in azerty keyboards
239 if ((this._pickWindow || this._pickWorkspace) &&
240 (event.get_key_symbol() === Clutter.KEY_Shift_L ||
241 event.get_key_symbol() === Clutter.KEY_Shift_R))
242 return Clutter.EVENT_STOP;
243
244 if (this._pickWindow) {
245 if (this._active !== workspaceManager.get_active_workspace_index()) {
246 this._hideTooltips();
247 return Clutter.EVENT_PROPAGATE;
248 }
249
250 let c = event.get_key_symbol() - Clutter.KEY_KP_0;
251 if (c > 9 || c <= 0) {
252 c = event.get_key_symbol() - Clutter.KEY_0;
253 if (c > 9 || c <= 0) {
254 this._hideTooltips();
255 log(c);
256 return Clutter.EVENT_PROPAGATE;
257 }
258 }
259
260 const win = this._workspaces[this._active].getWindowWithTooltip(c);
261 this._hideTooltips();
262
263 if (win)
264 Main.activateWindow(win, global.get_current_time());
265
266 return Clutter.EVENT_STOP;
267 }
268 if (this._pickWorkspace) {
269 let c = event.get_key_symbol() - Clutter.KEY_KP_0;
270 if (c > 9 || c <= 0) {
271 c = event.get_key_symbol() - Clutter.KEY_0;
272 if (c > 9 || c <= 0) {
273 this._hideWorkspacesTooltips();
274 return Clutter.EVENT_PROPAGATE;
275 }
276 }
277
278 const workspace = this._workspaces[c - 1];
279 if (workspace !== undefined)
280 workspace.metaWorkspace.activate(global.get_current_time());
281
282 this._hideWorkspacesTooltips();
283 return Clutter.EVENT_STOP;
284 }
285 return Clutter.EVENT_PROPAGATE;
286 };
287 /* eslint-enable */
288 });
289 }
290
291 disable() {
292 this._injectionManager.clear();
293 this._injectionManager = null;
294 }
295 }