-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathui.ts
259 lines (234 loc) · 8.16 KB
/
ui.ts
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
import { St, Meta, Mtk, Clutter, Shell } from '@gi.ext';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { Monitor } from 'resource:///org/gnome/shell/ui/layout.js';
export const getMonitors = (): Monitor[] => Main.layoutManager.monitors;
export const isPointInsideRect = (
point: { x: number; y: number },
rect: Mtk.Rectangle,
): boolean => {
return (
point.x >= rect.x &&
point.x <= rect.x + rect.width &&
point.y >= rect.y &&
point.y <= rect.y + rect.height
);
};
export const clampPointInsideRect = (
point: { x: number; y: number },
rect: Mtk.Rectangle,
): { x: number; y: number } => {
const clamp = (n: number, min: number, max: number) =>
Math.min(Math.max(n, min), max);
return {
x: clamp(point.x, rect.x, rect.x + rect.width),
y: clamp(point.y, rect.y, rect.y + rect.height),
};
};
export const isTileOnContainerBorder = (
tilePos: Mtk.Rectangle,
container: Mtk.Rectangle,
): {
isTop: boolean;
isRight: boolean;
isLeft: boolean;
isBottom: boolean;
} => {
// compare two values and return true if their are equal with a max error of 2
const almostEqual = (first: number, second: number) =>
Math.abs(first - second) <= 1;
const isLeft = almostEqual(tilePos.x, container.x);
const isTop = almostEqual(tilePos.y, container.y);
const isRight = almostEqual(
tilePos.x + tilePos.width,
container.x + container.width,
);
const isBottom = almostEqual(
tilePos.y + tilePos.height,
container.y + container.height,
);
return {
isTop,
isRight,
isBottom,
isLeft,
};
};
export type TileGapsInfo = {
gaps: Clutter.Margin;
isTop: boolean;
isRight: boolean;
isBottom: boolean;
isLeft: boolean;
};
export const buildTileGaps = (
tilePos: Mtk.Rectangle,
innerGaps: Clutter.Margin,
outerGaps: Clutter.Margin,
container: Mtk.Rectangle,
scalingFactor: number = 1,
): TileGapsInfo => {
const { isTop, isRight, isBottom, isLeft } = isTileOnContainerBorder(
tilePos,
container,
);
const margin = new Clutter.Margin();
margin.top = (isTop ? outerGaps.top : innerGaps.top / 2) * scalingFactor;
margin.bottom =
(isBottom ? outerGaps.bottom : innerGaps.bottom / 2) * scalingFactor;
margin.left =
(isLeft ? outerGaps.left : innerGaps.left / 2) * scalingFactor;
margin.right =
(isRight ? outerGaps.right : innerGaps.right / 2) * scalingFactor;
return {
gaps: margin,
isTop,
isRight,
isBottom,
isLeft,
};
};
export const getMonitorScalingFactor = (monitorIndex: number) => {
const scalingFactor = St.ThemeContext.get_for_stage(
global.get_stage(),
).get_scale_factor();
if (scalingFactor === 1)
return global.display.get_monitor_scale(monitorIndex);
return scalingFactor;
};
export const getScalingFactorOf = (widget: St.Widget): [boolean, number] => {
const [hasReference, scalingReference] = widget
.get_theme_node()
.lookup_length('scaling-reference', true);
// if the reference is missing, then the parent opted out of scaling the child
if (!hasReference) return [true, 1];
// if the scalingReference is not 1, then the scaling factor is already applied on styles (but not on width and height)
const [hasValue, monitorScalingFactor] = widget
.get_theme_node()
.lookup_length('monitor-scaling-factor', true);
if (!hasValue) return [true, 1];
return [scalingReference !== 1, monitorScalingFactor / scalingReference];
};
export const enableScalingFactorSupport = (
widget: St.Widget,
monitorScalingFactor?: number,
) => {
if (!monitorScalingFactor) return;
widget.set_style(`${getScalingFactorSupportString(monitorScalingFactor)};`);
};
export const getScalingFactorSupportString = (monitorScalingFactor: number) => {
return `scaling-reference: 1px; monitor-scaling-factor: ${monitorScalingFactor}px`;
};
export function getWindowsOfMonitor(monitor: Monitor): Meta.Window[] {
return global.workspaceManager
.get_active_workspace()
.list_windows()
.filter(
(win) =>
win.get_window_type() === Meta.WindowType.NORMAL &&
Main.layoutManager.monitors[win.get_monitor()] === monitor,
);
}
export function buildMarginOf(value: number): Clutter.Margin {
const margin = new Clutter.Margin();
margin.top = value;
margin.bottom = value;
margin.left = value;
margin.right = value;
return margin;
}
export function buildMargin(params: {
top?: number;
bottom?: number;
left?: number;
right?: number;
}): Clutter.Margin {
const margin = new Clutter.Margin();
if (params.top) margin.top = params.top;
if (params.bottom) margin.bottom = params.bottom;
if (params.left) margin.left = params.left;
if (params.right) margin.right = params.right;
return margin;
}
export function buildRectangle(
params: { x?: number; y?: number; width?: number; height?: number } = {},
): Mtk.Rectangle {
return new Mtk.Rectangle({
x: params.x || 0,
y: params.y || 0,
width: params.width || 0,
height: params.height || 0,
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getEventCoords(event: any): number[] {
return event.get_coords ? event.get_coords() : [event.x, event.y]; // GNOME 40-44
}
export function buildBlurEffect(sigma: number): Shell.BlurEffect {
// changes in GNOME 46+
// The sigma in Shell.BlurEffect should be replaced by radius. Since the sigma value
// is radius / 2.0, the radius value will be sigma * 2.0.
const effect = new Shell.BlurEffect();
effect.set_mode(Shell.BlurMode.BACKGROUND); // blur what is behind the widget
effect.set_brightness(1);
if (effect.set_radius) {
effect.set_radius(sigma * 2);
} else {
// @ts-expect-error "set_sigma is available in old shell versions (<= 45)"
effect.set_sigma(sigma);
}
return effect;
}
function getTransientOrParent(window: Meta.Window): Meta.Window {
const transient = window.get_transient_for();
return window.is_attached_dialog() && transient !== null
? transient
: window;
}
export function filterUnfocusableWindows(
windows: Meta.Window[],
): Meta.Window[] {
// we want to filter out
// - top-level windows which are precluded by dialogs
// - anything tagged skip-taskbar
// - duplicates
return windows
.map(getTransientOrParent)
.filter((win: Meta.Window, idx: number, arr: Meta.Window[]) => {
// typings indicate win will not be null, but this check is found
// in the source, so...
return win !== null && !win.skipTaskbar && arr.indexOf(win) === idx;
});
}
/** From Gnome Shell: https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/main/js/ui/altTab.js#L53 */
export function getWindows(workspace?: Meta.Workspace): Meta.Window[] {
if (!workspace) workspace = global.workspaceManager.get_active_workspace();
// We ignore skip-taskbar windows in switchers, but if they are attached
// to their parent, their position in the MRU list may be more appropriate
// than the parent; so start with the complete list ...
// ... map windows to their parent where appropriate ...
return filterUnfocusableWindows(
global.display.get_tab_list(Meta.TabList.NORMAL_ALL, workspace),
);
}
export function squaredEuclideanDistance(
pointA: { x: number; y: number },
pointB: { x: number; y: number },
) {
return (
(pointA.x - pointB.x) * (pointA.x - pointB.x) +
(pointA.y - pointB.y) * (pointA.y - pointB.y)
);
}
// Compatibility for GNOME 48+ where 'vertical' was deprecated in favor of 'orientation'
export function setWidgetOrientation(
widget: { vertical?: boolean; orientation?: Clutter.Orientation },
vertical: boolean,
) {
if (widget.orientation) {
widget.orientation = vertical
? Clutter.Orientation.VERTICAL
: Clutter.Orientation.HORIZONTAL;
} else {
widget.vertical = vertical;
}
}