-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathextension.js
More file actions
74 lines (59 loc) · 2.02 KB
/
Copy pathextension.js
File metadata and controls
74 lines (59 loc) · 2.02 KB
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
import GLib from 'gi://GLib';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
const MARGIN = 8;
const REAPPLY_DELAYS_MS = [0, 60, 180, 400];
export default class GitifyWindowPlacementExtension extends Extension {
enable() {
this._timeouts = new Set();
this._mapId = global.window_manager.connect('map', (_wm, actor) =>
this._onWindowMapped(actor.meta_window),
);
}
disable() {
global.window_manager.disconnect(this._mapId);
this._mapId = null;
for (const id of this._timeouts) {
GLib.Source.remove(id);
}
this._timeouts = null;
}
_onWindowMapped(window) {
if (!isGitifyWindow(window)) {
return;
}
for (const delay of REAPPLY_DELAYS_MS) {
const id = GLib.timeout_add(GLib.PRIORITY_DEFAULT, delay, () => {
this._timeouts.delete(id);
this._place(window);
return GLib.SOURCE_REMOVE;
});
this._timeouts.add(id);
}
}
_place(window) {
const workArea = Main.layoutManager.getWorkAreaForMonitor(Main.layoutManager.primaryIndex);
const frame = window.get_frame_rect();
const maxX = workArea.x + workArea.width - frame.width - MARGIN;
const icon = trayIconRect();
const x =
icon === null
? maxX
: clamp(Math.round(icon.get_center().x - frame.width / 2), workArea.x + MARGIN, maxX);
window.move_frame(false, x, workArea.y + MARGIN);
}
}
function isGitifyWindow(window) {
const wmClass = window?.get_wm_class()?.toLowerCase() ?? '';
return wmClass.includes('gitify') || (wmClass === 'electron' && window.get_title() === 'Gitify');
}
function trayIconRect() {
const indicator = Object.values(Main.panel.statusArea).find(
(item) => item?._indicator?.id?.toLowerCase() === 'gitify',
);
const actor = indicator?.container ?? indicator;
return actor?.visible ? actor.get_transformed_extents() : null;
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}