/
desklet.js
77 lines (62 loc) · 2.45 KB
/
desklet.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
const St = imports.gi.St;
const CinnamonDesktop = imports.gi.CinnamonDesktop;
const Desklet = imports.ui.desklet;
const Settings = imports.ui.settings;
class CinnamonClockDesklet extends Desklet.Desklet {
constructor(metadata, desklet_id) {
super(metadata, desklet_id);
this._date = new St.Label({style_class: "clock-desklet-label"});
this.setContent(this._date);
this.setHeader(_("Clock"));
this.clock = new CinnamonDesktop.WallClock();
this.clock_notify_id = 0;
this.settings = new Settings.DeskletSettings(this, this.metadata["uuid"], desklet_id);
this.settings.bind("date-format", "format");
this.settings.bind("font-size", "size", this._onSettingsChanged);
this.settings.bind("text-color", "color", this._onSettingsChanged);
this.settings.bind("use-custom-format", "use_custom_format", this._onSettingsChanged);
this._menu.addSettingsAction(_("Date and Time Settings"), "calendar")
}
_clockNotify(obj, pspec, data) {
this._updateClock();
}
_onSettingsChanged() {
this._date.style="font-size: " + this.size + "pt;\ncolor: " + this.color;
this._updateFormatString();
this._updateClock();
}
on_desklet_added_to_desktop() {
this._onSettingsChanged();
if (this.clock_notify_id == 0) {
this.clock_notify_id = this.clock.connect("notify::clock", () => this._clockNotify());
}
}
on_desklet_removed() {
if (this.clock_notify_id > 0) {
this.clock.disconnect(this.clock_notify_id);
this.clock_notify_id = 0;
}
}
_updateFormatString() {
if (this.use_custom_format) {
if (!this.clock.set_format_string(this.format)) {
global.logError("Clock desklet: bad format - check your string.");
this.clock.set_format_string("~FORMAT ERROR~ %l:%M %p");
}
} else {
this.clock.set_format_string(null);
}
}
_updateClock() {
if (this.use_custom_format) {
this._date.set_text(this.clock.get_clock());
} else {
let default_format = this.clock.get_default_time_format();
default_format = default_format.replace('%l', '%-l')
this._date.set_text(this.clock.get_clock_for_format(default_format));
}
}
}
function main(metadata, desklet_id) {
return new CinnamonClockDesklet(metadata, desklet_id);
}