-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlay.js
369 lines (314 loc) · 11.1 KB
/
overlay.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
"use strict";
const { Clutter, St, Gdk, GObject, Gio, GLib, Shell, Meta } = imports.gi;
const Mainloop = imports.mainloop;
const ByteArray = imports.byteArray;
const Main = imports.ui.main;
const ExtensionManager = Main.extensionManager;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Util = Me.imports.util;
const MonitorManager = Me.imports.monitors.monitorManager;
const Monitor = Me.imports.monitors.monitor;
const Gettext = imports.gettext;
const Domain = Gettext.domain(Me.metadata.uuid);
const _ = Domain.gettext;
const ngettext = Domain.ngettext;
var Overlay = class Overlay extends GObject.Object
{
static { GObject.registerClass(this); }
/**
* Construct a new HUD Overlay. `create()` must be called once other GObjects
* are constructed.
*
* @param {Object} extension
*/
constructor()
{
super();
this._settings = ExtensionUtils.getSettings();
this._connections = [];
this._update = this.update.bind(this);
this.overlay = null;
Main.wm.addKeybinding(
"kb-toggle-overlay",
this._settings,
Meta.KeyBindingFlags.NONE,
Shell.ActionMode.ALL,
() => this.toggleOverlay()
);
Main.layoutManager.connect("monitors-changed", () => this.geometryChanged());
const settingsConnections = {
"changed::show-overlay": this.toggle,
"changed::anchor-corner": this.geometryChanged,
"changed::default-monitor": this.geometryChanged,
"changed::vertical": this.geometryChanged,
"changed::margin-h": this.geometryChanged,
"changed::margin-v": this.geometryChanged,
"changed::padding-h": this.geometryChanged,
"changed::padding-v": this.geometryChanged,
"changed::background-opacity": this.updateStyles,
"changed::foreground-opacity": this.updateStyles,
"changed::background-color": this.updateStyles,
"changed::foreground-color": this.updateStyles,
"changed::border-radius": this.updateStyles,
"changed::font": this.geometryChanged,
"changed::monitors": this.updateMonitors,
};
for (let event in settingsConnections)
{
this._connections.push(
this._settings.connect(event, settingsConnections[event].bind(this))
);
}
if (this._settings.get_boolean("show-overlay"))
{
this.toggle();
}
}
/**
* Creates or updates system monitors.
*/
updateMonitors()
{
// pause updates for the overlay
MonitorManager.removeCallback(this._update);
for (let monitor of MonitorManager.monitors)
{
if (monitor.config.place.includes(Monitor.places.OVERLAY))
{
monitor.box = new St.BoxLayout();
monitor.box.set_vertical(false);
// monitor label
const label = new St.Label({ text: monitor.config.label });
label.set_width(100);
monitor.box.add_child(label);
// add format labels
for (let format of monitor.config.format)
{
const formatLabel = new St.Label({ text: format.toLowerCase() });
formatLabel.set_width(200);
monitor.box.add_child(formatLabel);
}
if (this.overlay) this.overlay.add_child(monitor.box);
}
}
this.geometryChanged();
// start updates for the overlay
MonitorManager.addCallback(this._update);
}
/**
* Called when the kb-toggle-overlay keybind is pressed. Toggles the show-overlay setting.
*/
toggleOverlay()
{
const toggled = !this._settings.get_boolean("show-overlay");
this._settings.set_boolean("show-overlay", toggled);
}
/**
* Toggle the Overlay on and off. Toggling off will destroy any created objects.
*/
toggle()
{
log(_(`${Me.metadata.uuid}: Overlay toggled`));
if (this._settings.get_boolean("show-osd"))
{
const icon = new Gio.ThemedIcon({ name: "utilities-system-monitor-symbolic" });
Main.osdWindowManager.show(
0,
icon,
_(`Overlay toggled\n\nUse ${this._settings.get_strv("kb-toggle-overlay")[0]} to toggle`),
null
);
}
// Show the overlay
if (this._settings.get_boolean("show-overlay"))
{
// Overlay container
this.overlay = new St.BoxLayout();
this.overlay.set_track_hover(true);
this.overlay.set_reactive(true);
this.overlay.connect("notify::hover", () => this.updateStyles());
Main.uiGroup.add_actor(this.overlay);
this.updateMonitors();
this.geometryChanged();
}
// Hide the overlay
else
{
MonitorManager.removeCallback(this._update);
if (this.overlay) this.overlay.destroy();
this.overlay = null;
}
}
/**
* Query the hardware for updates and update overlay labels.
*/
update(results)
{
results.forEach((stats, i) =>
{
const monitor = MonitorManager.monitors[i];
monitor.box.get_children().forEach((child, j) =>
{
if (j > 0) // ignore monitor label
{
const val = stats[monitor.config.format[j - 1].toLowerCase()];
if (typeof val === "number")
{
child.set_text(`${val.toFixed(monitor.config.precision)}`);
}
else
{
child.set_text(`${val}`);
}
}
});
});
}
/**
* Called when a geometry setting like location or size is changed. Updates
* the overlay position.
*/
geometryChanged()
{
this.updateStyles();
this.updateGeometry();
}
/**
* Update geometry-changing styles, locations, and sizes of overlay and monitors.
*/
updateGeometry()
{
const anchor = this._settings.get_int("anchor-corner");
const mI = this._settings.get_int("default-monitor") - 1;
this.monitor = Main.layoutManager.primaryMonitor;
if (mI >= 0) this.monitor = Main.layoutManager.monitors[mI] ?? Main.layoutManager.primaryMonitor;
if (!this.overlay)
{
return;
}
// Monitor margins and padding
for (let i = 0; i < MonitorManager.monitors.length; i++)
{
const monitor = MonitorManager.monitors[i];
monitor.box.set_height(Number(this._settings.get_string("font").match(/\d+/g)[0]) * 2 + 10);
const paddingV = this._settings.get_int("padding-v");
const paddingH = this._settings.get_int("padding-h");
monitor.box.set_style("");
if (this._settings.get_boolean("vertical"))
{
Util.appendStyle(monitor.box,
`margin-left: ${paddingH}px;` +
`margin-right: ${paddingH}px;`
);
if (i === 0)
{
Util.appendStyle(monitor.box, `margin-top: ${paddingV}px;`);
}
else if (i === MonitorManager.monitors.length - 1)
{
Util.appendStyle(monitor.box, `margin-bottom: ${paddingV}px;`);
}
}
else
{
Util.appendStyle(monitor.box,
`margin-top: ${paddingV}px;` +
`margin-bottom: ${paddingV}px;`
);
if (i === 0)
{
Util.appendStyle(monitor.box, `margin-left: ${paddingH}px;`);
}
else if (i === MonitorManager.monitors.length - 1)
{
Util.appendStyle(monitor.box, `margin-right: ${paddingH}px;`);
}
}
monitor.box.ensure_style();
}
// Overlay dimensions and location
this.overlay.set_vertical(this._settings.get_boolean("vertical"));
let x = this.monitor.x;
let y = this.monitor.y;
const height = this.overlay.get_height();
const width = this.overlay.get_width();
// Left corners
if (anchor % 2 == 0)
{
x += this._settings.get_int("margin-h");
}
// Right corners
else
{
x += this.monitor.width - width - this._settings.get_int("margin-h");
}
// Top corners
if (anchor <= 1)
{
y += this._settings.get_int("margin-v");
}
// Bottom corners
else
{
y += this.monitor.height - height - this._settings.get_int("margin-v");
}
this.overlay.set_position(x, y);
//this.overlay.set_size(width, height);
}
/**
* Update non-geometry-changing styles like colors and fonts.
*/
updateStyles()
{
if (!this.overlay)
{
return;
}
const hoverMultiplier = this._settings.get_double("hover-multiplier");
const hover = this.overlay.get_hover();
// Update parent box
let backgroundOpacity = this._settings.get_double("background-opacity");
if (hoverMultiplier && hover)
{
backgroundOpacity *= hoverMultiplier + 1;
}
const backgroundRGBA = Util.stringToColor(this._settings.get_string("background-color"));
const backgroundColor = Util.getCSSColor(backgroundRGBA, backgroundOpacity);
this.overlay.set_style(
`background-color: ${backgroundColor}` +
`border-radius: ${this._settings.get_string("border-radius")}`
);
// Update monitor labels
for (let monitor of MonitorManager.monitors)
{
let foregroundOpacity = this._settings.get_double("foreground-opacity");
if (hoverMultiplier && hover)
{
foregroundOpacity *= hoverMultiplier + 1;
}
const rgba = Util.stringToColor(monitor.config.color);
const color = Util.getCSSColor(rgba, foregroundOpacity);
const font = Util.fontToCSS(this._settings.get_string("font"));
for (let label of monitor.box.get_children())
{
label.set_style(
`color: ${color}` +
`font: ${font}`
);
}
}
}
/**
* Destroy this object and objects it created.
*/
destroy()
{
Main.wm.removeKeybinding("kb-toggle-overlay");
MonitorManager.removeCallback(this._update);
this._connections.forEach((c) => this._settings.disconnect(c));
this._connections = [];
if (this.overlay) this.overlay.destroy();
this.overlay = null;
}
}