-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathextension.js
More file actions
74 lines (67 loc) · 2.22 KB
/
extension.js
File metadata and controls
74 lines (67 loc) · 2.22 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 St from "gi://St";
import Clutter from "gi://Clutter";
import Pango from "gi://Pango";
import { Extension } from "resource:///org/gnome/shell/extensions/extension.js";
import * as main from "resource:///org/gnome/shell/ui/main.js";
export default class PanelDateFormatExtension extends Extension {
_clockSignal;
_clockMap;
/**
* Enable, called when extension is enabled or when screen is unlocked.
*/
enable() {
// WORKAROUND:
// Set settings for the first time to make it visible in dconf editor
// Please note schema default value must be empty string
if (!this.getSettings().get_string("format")) {
this.getSettings().set_string("format", "%Y.%m.%d %H:%M");
}
this._clockMap = new Map();
this._clockSignal = main.panel.statusArea.dateMenu._clock.connect(
"notify::clock",
this._tick
);
this._tick();
}
/**
* Disable, called when extension is disabled or when screen is locked.
*/
disable() {
main.panel.statusArea.dateMenu._clock.disconnect(this._clockSignal);
this._clockSignal = undefined;
this._clockMap.forEach((label, clockDisplay) => {
clockDisplay.show();
clockDisplay.get_parent().remove_child(label);
label.destroy();
});
this._clockMap = undefined;
}
/**
* It runs every time we need to update clock.
* @return {boolean} Always returns true to loop.
*/
_tick = () => {
const format = this.getSettings().get_string("format");
const text = new GLib.DateTime().format(format);
this._clocks().forEach((clockDisplay) => {
let label = this._clockMap.get(clockDisplay);
if (!label) {
label = new St.Label({ style_class: "clock" });
label.clutter_text.y_align = Clutter.ActorAlign.CENTER;
label.clutter_text.ellipsize = Pango.EllipsizeMode.NONE;
clockDisplay.hide();
clockDisplay.get_parent().insert_child_below(label, clockDisplay);
this._clockMap.set(clockDisplay, label);
}
label.set_text(text);
});
return true;
};
_clocks() {
return [
main.panel,
...(global.dashToPanel?.panels.map((pw) => pw.panel) ?? []),
].map((panel) => panel.statusArea.dateMenu._clockDisplay);
}
}