forked from linuxmint/cinnamon-spices-applets
-
Notifications
You must be signed in to change notification settings - Fork 1
/
applet.js
343 lines (283 loc) · 13 KB
/
applet.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
/*
* World Clock Calendar applet [email protected]
* Fork of the Cinnamon calendar applet with support for displaying multiple timezones.
* version 1.2
*/
/* global imports, global */
/* exported _onVertSepRepaint, main */
"use strict";
const EXTENSION_UUID = "[email protected]";
const APPLET_DIR = imports.ui.appletManager.appletMeta[EXTENSION_UUID].path;
const Gettext = imports.gettext;
const Applet = imports.ui.applet;
const Lang = imports.lang;
const Main = imports.ui.main;
const Mainloop = imports.mainloop;
const Clutter = imports.gi.Clutter;
const St = imports.gi.St;
const Util = imports.misc.util;
const PopupMenu = imports.ui.popupMenu;
const UPowerGlib = imports.gi.UPowerGlib;
const Settings = imports.ui.settings;
let Calendar;
if (typeof require !== 'undefined') {
Calendar = require('./calendar');
} else {
const AppletDir = imports.ui.appletManager.applets[EXTENSION_UUID];
Calendar = AppletDir.calendar;
}
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const CinnamonDesktop = imports.gi.CinnamonDesktop;
let DEFAULT_FORMAT = _("%l:%M %p");
Gettext.bindtextdomain(EXTENSION_UUID, GLib.get_home_dir() + "/.local/share/locale")
function _(str) {
return Gettext.dgettext(EXTENSION_UUID, str);
}
function _onVertSepRepaint (area)
{
let cr = area.get_context();
let themeNode = area.get_theme_node();
let [width, height] = area.get_surface_size();
let stippleColor = themeNode.get_color("-stipple-color");
let stippleWidth = themeNode.get_length("-stipple-width");
let x = Math.floor(width/2) + 0.5;
cr.moveTo(x, 0);
cr.lineTo(x, height);
Clutter.cairo_set_source_color(cr, stippleColor);
cr.setDash([1, 3], 1); // Hard-code for now
cr.setLineWidth(stippleWidth);
cr.stroke();
}
function rpad(str, pad_with, length) {
while (str.length < length) { str = str + pad_with; }
return str;
}
function MyApplet(orientation, panel_height, instance_id) {
this._init(orientation, panel_height, instance_id);
}
MyApplet.prototype = {
__proto__: Applet.TextApplet.prototype,
_init: function(orientation, panel_height, instance_id) {
Applet.TextApplet.prototype._init.call(this, orientation, panel_height, instance_id);
try {
this.settings = new Settings.AppletSettings(this, EXTENSION_UUID, this.instance_id);
this.menuManager = new PopupMenu.PopupMenuManager(this);
this._orientation = orientation;
this._initContextMenu();
this._initRightClickMenu();
this.clock = new CinnamonDesktop.WallClock();
this.clock_notify_id = 0;
this._calendarArea = new St.BoxLayout({name: "calendarArea" });
this.menu.addActor(this._calendarArea);
// Fill up the first column
let vbox = new St.BoxLayout({vertical: true});
this._calendarArea.add(vbox);
// Date
this._date = new St.Label();
this._date.style_class = "datemenu-date-label";
vbox.add(this._date);
this._eventSource = null;
this._eventList = null;
// Calendar
this._calendar = new Calendar.Calendar(this.settings);
vbox.add(this._calendar.actor);
// World Clocks
let separator = new PopupMenu.PopupSeparatorMenuItem();
separator.setColumnWidths(1);
vbox.add(separator.actor, {y_align: St.Align.END, expand: true, y_fill: false});
let addWorldClocks = Lang.bind(this, function() {
if (this._worldclocks_box) { this._worldclocks_box.destroy(); }
this._worldclocks_box = new St.BoxLayout({vertical: true});
// add to the calendarArea vbox instead of a new worldclocksArea so that the calendar resizes to the
// full width of the applet drop-down (in case the world clocks are very wide!)
vbox.add(this._worldclocks_box);
this._worldclocks = [];
this._worldclock_labels = [];
var i;
for (i in this.worldclocks) {
if (typeof(this.worldclocks[i]) == 'string')
this._worldclocks[i] = this.worldclocks[i].split("|");
}
for (i in this._worldclocks) {
this._worldclocks[i][1] = GLib.TimeZone.new(this._worldclocks[i][1]);
let tz = new St.BoxLayout({vertical: false});
let tz_label = new St.Label({ style_class: "calendar", text: _(this._worldclocks[i][0]) });
tz.add(tz_label, {x_align: St.Align.START, expand: true, x_fill: false});
this._worldclock_labels[i] = new St.Label({ style_class: "calendar" });
tz.add(this._worldclock_labels[i], {x_align: St.Align.END, expand: true, x_fill: false});
this._worldclocks_box.add(tz);
}
this.max_length = this._worldclocks.reduce(function (a, b) { return a[0].length > b[0].length ? a : b; })[0].length;
});
// Track changes to clock settings
this._dateFormat = DEFAULT_FORMAT;
this._dateFormatFull = _("%A %B %e, %Y");
// connect to system clock settings
this.desktop_settings = new Gio.Settings({ schema_id: "org.cinnamon.desktop.interface" });
this.desktop_settings.connect("changed::clock-use-24h", Lang.bind(this, function(key) {
this.on_settings_changed();
}));
this.desktop_settings.connect("changed::clock-show-seconds", Lang.bind(this, function(key) {
this.on_settings_changed();
}));
this.settings.bindProperty(Settings.BindingDirection.IN, "use-custom-format", "use_custom_format", this.on_settings_changed, null);
this.settings.bindProperty(Settings.BindingDirection.IN, "custom-format", "custom_format", this.on_settings_changed, null);
this.settings.bindProperty(Settings.BindingDirection.IN, "keybinding", "keybinding", this._onKeySettingsUpdated, null);
Main.keybindingManager.addHotKey(EXTENSION_UUID, this.keybinding, Lang.bind(this, this.on_applet_clicked));
// Track changes to world-clock settings
this.settings.bindProperty(Settings.BindingDirection.IN, "worldclocks", "worldclocks", addWorldClocks, null);
this.settings.bindProperty(Settings.BindingDirection.IN, "worldclocks-timeformat", "worldclocks_timeformat", addWorldClocks, null);
// https://bugzilla.gnome.org/show_bug.cgi?id=655129
this._upClient = new UPowerGlib.Client();
try {
this._upClient.connect("notify-resume", this._updateClockAndDate);
this._upClient.connect("notify-resume", addWorldClocks);
} catch (e) {
this._upClient.connect("notify::resume", this._updateClockAndDate);
this._upClient.connect("notify::resume", addWorldClocks);
}
// Start the clock
this.on_settings_changed();
addWorldClocks();
}
catch (e) {
global.logError(e);
}
},
_onKeySettingsUpdated: function() {
if (this.keybinding != null)
Main.keybindingManager.addHotKey(EXTENSION_UUID, this.keybinding, Lang.bind(this, this.on_applet_clicked));
this.on_applet_clicked;
},
_clockNotify(obj, pspec, data) {
this._updateClockAndDate();
},
on_applet_clicked: function() {
this.menu.toggle();
},
on_settings_changed: function() {
this._updateFormatString();
this._updateClockAndDate();
},
// reads system clock setting to apply "24h" and "seconds" setting to this applet
_updateFormatString() {
if (this.use_custom_format) {
this._dateFormat = this.custom_format;
} else {
let use_24h = this.desktop_settings.get_boolean("clock-use-24h");
let show_seconds = this.desktop_settings.get_boolean("clock-show-seconds");
if (use_24h) {
if (show_seconds) {
this._dateFormat = "%H:%M:%S";
} else {
this._dateFormat = "%H:%M%";
}
} else {
if (show_seconds) {
this._dateFormat = "%l:%M:%S %p";
} else {
this._dateFormat = DEFAULT_FORMAT;
}
}
}
},
on_custom_format_button_pressed: function() {
Util.spawnCommandLine("xdg-open http:https://www.foragoodstrftime.com/");
},
_launch_dateandtime_settings: function() {
this.menu.close();
Util.spawnCommandLine("cinnamon-settings calendar");
},
_updateClockAndDate: function() {
let displayDate = GLib.DateTime.new_now_local();
let dateFormattedFull = displayDate.format(this._dateFormatFull);
let label_string = displayDate.format(this._dateFormat);
if (!label_string) {
global.logError("Calendar applet: bad time format string - check your string.");
label_string = "~CLOCK FORMAT ERROR~ " + displayDate.toLocaleFormat(DEFAULT_FORMAT);
}
this.set_applet_label(label_string);
let tooltip = [];
tooltip.push(dateFormattedFull);
for (var i in this._worldclocks) {
let tz = this._get_world_time(displayDate, this._worldclocks[i][1]);
this._worldclock_labels[i].set_text(tz);
tooltip.push(rpad(_(this._worldclocks[i][0]), "\xA0", this.max_length + 10) + tz);
}
this.set_applet_tooltip(tooltip.join("\n"));
if (dateFormattedFull !== this._lastDateFormattedFull) {
this._date.set_text(dateFormattedFull);
this._lastDateFormattedFull = dateFormattedFull;
}
},
on_applet_added_to_panel: function() {
this.on_settings_changed();
if (this.clock_notify_id == 0) {
this.clock_notify_id = this.clock.connect("notify::clock", () => this._clockNotify());
}
},
on_applet_removed_from_panel: function() {
if (this.clock_notify_id > 0) {
this.clock.disconnect(this.clock_notify_id);
this.clock_notify_id = 0;
}
},
_initContextMenu: function () {
if (this._calendarArea) this._calendarArea.unparent();
if (this.menu) this.menuManager.removeMenu(this.menu);
this.menu = new Applet.AppletPopupMenu(this, this._orientation);
this.menuManager.addMenu(this.menu);
if (this._calendarArea){
this.menu.addActor(this._calendarArea);
this._calendarArea.show_all();
}
// Whenever the menu is opened, select today
this.menu.connect("open-state-changed", Lang.bind(this, function(menu, isOpen) {
if (isOpen) {
let now = new Date();
/* Passing true to setDate() forces events to be reloaded. We
* want this behavior, because
*
* o It will cause activation of the calendar server which is
* useful if it has crashed
*
* o It will cause the calendar server to reload events which
* is useful if dynamic updates are not supported or not
* properly working
*
* Since this only happens when the menu is opened, the cost
* isn"t very big.
*/
this._calendar.setDate(now, true);
// No need to update this._eventList as ::selected-date-changed
// signal will fire
}
}));
},
on_orientation_changed: function (orientation) {
this._orientation = orientation;
this._initContextMenu();
},
_get_world_time: function(time, tz) {
return time.to_timezone(tz).format(this.worldclocks_timeformat).trim();
},
_launch_applet_config: function() {
Util.spawn(["cinnamon-settings", "applets", EXTENSION_UUID]);
},
_launch_worldclocks_config: function() {
Util.spawnCommandLine("/usr/bin/env python3 " + APPLET_DIR + "/world_clock_calendar_settings.py --instance-id " + this.instance_id);
},
_initRightClickMenu: function () {
// this._applet_context_menu.addMenuItem(new Applet.MenuItem(
// _("Configure applet"), "system-run-symbolic", Lang.bind(this, this._launch_applet_config)));
this._applet_context_menu.addMenuItem(new Applet.MenuItem(
_("Edit World Clocks"), "system-run-symbolic", Lang.bind(this, this._launch_worldclocks_config)));
this._applet_context_menu.addMenuItem(new Applet.MenuItem(
_("Date and Time Settings"), "system-run-symbolic", Lang.bind(this, this._launch_dateandtime_settings)));
},
};
function main(metadata, orientation, panel_height, instance_id) {
let myApplet = new MyApplet(orientation, panel_height, instance_id);
return myApplet;
}