-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
110 lines (87 loc) · 3.73 KB
/
extension.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
// This extenstion is based on S410's original ISO8601-ish Clock
// https://gitlab.com/S410/iso8601ish
import { Extension } from "resource:///org/gnome/shell/extensions/extension.js";
import * as Main from "resource:///org/gnome/shell/ui/main.js";
import GLib from "gi://GLib";
import Gio from "gi://Gio";
export default class IsoClock extends Extension {
enable() {
const dateMenu = Main.panel.statusArea.dateMenu;
const clockDisplayBox = dateMenu
.get_children()
.find((x) => x.style_class === "clock-display-box");
this.label = clockDisplayBox?.get_children().find(
(x) =>
x.style_class === "clock" &&
// Make sure it's (hopefully) the clock
// by checking for "∶" (\u2236) (not ascii ":")
x.text?.includes("\u2236")
);
if (!this.label) {
console.error("No clock label? Aborting.");
return;
}
const gnomeSettings = Gio.Settings.new("org.gnome.desktop.interface");
this.gnomeCalendar = Gio.Settings.new("org.gnome.desktop.calendar");
const override = () => {
// Don't do anything if the clock label hasn't actually changed
if (this.newClock == this.label.get_text()) {
return;
}
// Setup the custom clock format based on the clock settings in Gnome Settings
let day, date, week, time;
if (gnomeSettings.get_boolean("clock-show-weekday")) {
day = "%A"
}
if (gnomeSettings.get_boolean("clock-show-date")) {
date = "%Y-%m-%d";
}
if (this.gnomeCalendar.get_boolean("show-weekdate")) {
week = "W%V-%u"
}
if (gnomeSettings.get_string("clock-format") === '24h') {
time = "%H:%M";
} else {
time = "%I:%M %p";
}
if (gnomeSettings.get_boolean("clock-show-seconds")) {
time = time.replace("%M","%M:%S");
}
const format = [day, date, week, time].filter(v => v).join(" ");
// Keep a copy of the default clock text so that we can revert it when the
// extension is disabled
this.defaultClock = this.label.get_text();
// Set the clock label to our new custom format
const now = GLib.DateTime.new_now_local();
this.newClock = now.format(format);
this.label.set_text(this.newClock);
};
// Whenever the clock label updates override with our custom clock format
this.labelHandleId = this.label.connect("notify::text", override);
// We also need to know when the "Week Numbers" setting changes, as week numbers
// don't appear in the default clock. Trigger a refresh by setting clock back to
// its default value. This prevents an edge case where disabling the extension
// after a week number setting change causes unexpected behaviour
this.calendarHandleId = this.gnomeCalendar.connect("changed::show-weekdate", () => {
this.label.set_text(this.defaultClock);
})
override();
}
disable() {
if (this.calendarHandleId) {
this.gnomeCalendar.disconnect(this.calendarHandleId);
this.calendarHandleId = null;
}
if (this.labelHandleId) {
this.label.disconnect(this.labelHandleId);
this.labelHandleId = null;
}
if (this.defaultClock) {
this.label.set_text(this.defaultClock);
}
this.gnomeCalendar = null
this.label = null;
this.newClock = null;
this.defaultClock = null;
}
}