forked from blackjackshellac/kitchenTimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimers.js
More file actions
1215 lines (1022 loc) · 33.1 KB
/
Copy pathtimers.js
File metadata and controls
1215 lines (1022 loc) · 33.1 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* taskTimer: Gnome Shell taskTimer Extension
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const GETTEXT_DOMAIN = 'tasktimer';
const I18n = imports.i18n;
const _ = I18n.init(GETTEXT_DOMAIN).gettext;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const {GLib, St, Clutter, Gio} = imports.gi;
const Main = imports.ui.main;
const PopupMenu = imports.ui.popupMenu;
const Utils = Me.imports.utils;
const Settings = Me.imports.settings.Settings;
const Notifier = Me.imports.notifier;
const Logger = Me.imports.logger.Logger;
const HMS = Me.imports.hms.HMS;
const AlarmTimer = Me.imports.alarm_timer.AlarmTimer;
const SessionManagerInhibitor = Me.imports.inhibitor.SessionManagerInhibitor;
const KeyboardShortcuts = Me.imports.keyboard_shortcuts.KeyboardShortcuts;
const ProgressIcon = Me.imports.progress_icon.ProgressIcon;
const Storage = Me.imports.storage.Storage;
const TIMERS_SAVE_PATH = GLib.build_filenamev([GLib.get_user_data_dir(), 'tasktimer', 'timers.json']);
/** Same throttle as timers_core PERIODIC_TIMERS_FILE_SAVE_MS (extension timers.json). */
const PERIODIC_TIMERS_SAVE_MS = 30000;
// Utility to save all timers
function saveAllTimers(timersInstance) {
// Save all timers (active, expired, etc.)
const timersData = timersInstance.map(timer => (typeof timer.toJSON === 'function' ? timer.toJSON() : timer));
Storage.saveJSON(TIMERS_SAVE_PATH, timersData);
}
// Utility to load all timers
function loadAllTimers() {
const timersData = Storage.loadJSON(TIMERS_SAVE_PATH);
if (!timersData) return [];
// Convert the loaded data back to Timer objects
return timersData.map(timerData => {
if (typeof timerData === 'object' && timerData !== null) {
return Timer.fromJSON(timerData);
}
return timerData;
});
}
const date_options = { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric' };
const mixerControl = imports.ui.status.volume.getMixerControl();
var Timers = class Timers extends Array {
constructor(...args) {
super(...args);
// id => timer
this._lookup = {};
/** Throttles periodic save from timer ticks (see maybePeriodicSaveTimersJson). */
this._lastPeriodicTimersSaveMs = 0;
this._settings = new Settings();
this._attached = false;
this.accel = new KeyboardShortcuts(this.settings);
this.logger = new Logger('kt timers', this.settings);
this._progressIcon = new ProgressIcon(this.logger);
try {
// Preference order for icons: tasktimer-* first, then kitchen-timer-blackjackshellac-*
const iconFiles = [
'tasktimer-full.png',
'tasktimer-full.svg',
'kitchen-timer-blackjackshellac-full.png',
'kitchen-timer-blackjackshellac-full.svg'
];
let iconFound = false;
for (const iconFile of iconFiles) {
const iconPath = `${Me.path}/icons/${iconFile}`;
if (GLib.file_test(iconPath, GLib.FileTest.EXISTS)) {
this._fullIcon = Gio.icon_new_for_string(iconPath);
iconFound = true;
break;
}
}
if (!iconFound) {
this.logger.warn('Failed to load icons');
this._fullIcon = Gio.icon_new_for_string('image-missing-symbolic');
}
} catch (e2) {
this.logger.warn('Failed to load icons: ' + e2.message);
this._fullIcon = Gio.icon_new_for_string('image-missing-symbolic');
}
// requires this._settings
this._notifier = new Notifier.Annoyer(this);
this._inhibitor = new SessionManagerInhibitor(this.settings);
}
/**
* Persist timers.json at most once per PERIODIC_TIMERS_SAVE_MS while timers tick.
* Avoids N redundant writes when N timers share the same save window.
*/
maybePeriodicSaveTimersJson(nowMs) {
if (nowMs - this._lastPeriodicTimersSaveMs < PERIODIC_TIMERS_SAVE_MS) {
return;
}
this._lastPeriodicTimersSaveMs = nowMs;
saveAllTimers(this);
}
static attach(indicator) {
if (!timersInstance) {
timersInstance = new Timers();
}
// reload settings
timersInstance._settings = new Settings();
timersInstance._inhibitor.settings = timersInstance._settings;
timersInstance.logger.settings = timersInstance._settings;
timersInstance.logger.info("Attaching indicator");
timersInstance.indicator = indicator;
timersInstance.refresh();
timersInstance.settings.settings.connect('changed::accel-enable', () => {
timersInstance.logger.debug('accel-enable has changed');
timersInstance.toggle_keyboard_shortcuts();
});
if (timersInstance.settings.accel_enable) {
timersInstance.enable_keyboard_shortcuts();
}
timersInstance.attached = true;
timersInstance.warn_volume = true;
return timersInstance;
}
static detach() {
if (!timersInstance) {
return;
}
timersInstance.logger.info("Detaching indicator from timers");
timersInstance.attached = false;
timersInstance.indicator = undefined;
}
static getInstance() {
return timersInstance;
}
toggle_keyboard_shortcuts() {
if (this.settings.accel_enable) {
this.enable_keyboard_shortcuts();
} else {
this.disable_keyboard_shortcuts();
}
}
enable_keyboard_shortcuts() {
this.accel.listenFor(this.settings.accel_show_endtime, () => {
let set=!this.settings.show_endtime;
this.logger.debug("Toggling show endtime to %s", set);
this.settings.show_endtime = set;
});
this.accel.listenFor(this.settings.accel_stop_next, () => {
this.stop_next();
});
}
disable_keyboard_shortcuts() {
this.accel.remove(this.settings.accel_show_endtime);
this.accel.remove(this.settings.accel_stop_next);
}
get indicator() {
return this._indicator;
}
set indicator(indicator) {
this._indicator = indicator;
}
get attached() {
return this._attached;
}
set attached(bool) {
this._attached = bool;
}
progress_gicon(degrees) {
return this._progressIcon.get(degrees);
}
get fullIcon() {
return this._fullIcon;
}
get notifier() {
return this._notifier;
}
get inhibitor() {
return this._inhibitor;
}
get settings() {
return this._settings;
}
inc_prefer_presets(inc) {
this.settings.prefer_presets += inc;
this.logger.debug("prefer_presets=%d", this.settings.prefer_presets);
}
get box() {
return this.indicator === undefined ? undefined : this.indicator._box;
}
get panel_name() {
return this.indicator === undefined ? undefined : this.indicator._panel_name;
}
get panel_label() {
return this.indicator === undefined ? undefined : this.indicator._panel_label;
}
set_panel_name(text, has_name=true) {
var label = this.panel_name;
if (label) {
label.set_text(this.settings.show_label && has_name && text.length > 0 ? text : _('taskTimer'));
}
}
set_panel_label(text) {
var label = this.panel_label;
if (label) {
label.set_text(this.settings.show_time ? text : "");
}
}
refresh() {
this.logger.debug("Timers refresh");
// First, load any saved timers from storage
const savedTimers = loadAllTimers();
if (savedTimers && savedTimers.length > 0) {
this.logger.debug(`Loaded ${savedTimers.length} timers from storage`);
savedTimers.forEach(timer => {
if (!this.lookup(timer.id)) {
this.add(timer);
this.logger.debug(`Added saved timer: ${timer.name}`);
}
});
}
// Then load timers from settings
var settings_timers = this.settings.unpack_timers();
settings_timers.forEach( (settings_timer) => {
var id=settings_timer.id;
var timer = this.lookup(id);
if (timer) {
timer.refresh_with(settings_timer);
this.logger.debug("Found %s timer [%s]: %s",
(timer.quick ? "quick" : "preset"),
timer.name,
(timer.running ? "running" : "not running"));
} else {
//this.logger.debug("Timer [%s] not found, id=%s", settings_timer.name, settings_timer.id);
timer = Timer.fromSettingsTimer(settings_timer);
settings_timer.id = timer.id;
this.add(timer);
}
});
// Restore any running timers
this.restoreRunningTimers();
// Clean up any invalid timers
for (let i = 0; i < this.length; i++) {
var timer=this[i];
if (timer.still_valid(settings_timers)) {
continue;
}
if (this.remove(timer, i)) {
i--;
}
}
}
saveRunningTimers() {
var running=[];
this.sort_by_running().forEach( (timer) => {
if (timer.running) {
this.logger.debug("Saving running timer state id=%s start=%d end=%d", timer.id, timer._start, timer._end);
var run_state = {
id: timer.id,
start: timer._start,
end: timer._end, // Save the end time
persist: timer.persist_alarm
}
if (timer.alarm_timer) {
run_state.alarm_timer = timer.alarm_timer.save();
}
running.push(run_state);
}
});
this.settings.running = JSON.stringify(running);
}
restoreRunningTimers() {
let run_states = this.settings.run_states;
if (!run_states || run_states.length === 0) {
this.logger.debug("No running timers to restore");
return;
}
for (let run_state of run_states) {
let timer = this.lookup(run_state.id);
if (!timer) {
this.logger.warn(`Timer with id ${run_state.id} not found during restoreRunningTimers.`);
continue;
}
timer.persist_alarm = run_state.persist;
if (timer.alarm_timer) {
timer.alarm_timer = AlarmTimer.restore(run_state.alarm_timer);
}
// Use saved _end time to determine if timer is still active
const now = Date.now();
// Restore the original start and end times
timer._start = run_state.start;
// If end time was saved, use it; otherwise calculate it from start + duration
if (run_state.end) {
timer._end = run_state.end;
} else {
timer._end = timer._start + timer.duration_ms();
}
if (timer._end > now) {
// Set up volume monitoring if enabled (similar to go() method)
if (this.settings.play_sound && this.settings.volume_level_warn) {
let stream = mixerControl.get_default_sink();
if (stream) {
if (timer.notify_volume === undefined) {
timer.notify_volume = stream.connect('notify::volume', timer.check_volume.bind(timer));
}
if (timer.notify_muted === undefined) {
timer.notify_muted = stream.connect('notify::is-muted', timer.check_volume.bind(timer));
}
}
}
// Set up volume warning state
this.warn_volume = true;
timer.check_volume();
// Directly restart the interval with the existing _end time
timer._interval_id = Utils.setInterval(timer.timer_callback, timer._interval_ms, timer);
timer._state = TimerState.RUNNING;
// Set up session inhibitor for restored running timer
timersInstance.inhibitor.inhibit_timer(timer);
this.logger.debug(`Restored timer: ${timer.toString()}, remaining: ${Math.round((timer._end - now)/1000)} seconds`);
} else {
// Timer has expired during downtime - trigger the completion notification
const tdiff = now - timer._end;
timer.expired = true;
timer._state = TimerState.EXPIRED;
// Show notification for expired timer
const reason = _("Timer completed %s late at").format(new HMS(tdiff/1000).toString(true));
const time = new Date(now).toLocaleTimeString();
const text = "%s due at %s".format(timer.name, timer.end_time());
timersInstance.notifier.notify(timer, text, "%s %s", reason, time);
this.logger.debug(`Timer expired during downtime: ${timer.toString()}, was late by ${Math.round(tdiff/1000)} seconds`);
}
}
}
lookup(id) {
if (id === undefined || id.length === 0) {
// new timer, not in lookup yet
return undefined;
}
if (this._lookup[id] !== undefined) {
return this._lookup[id];
}
if (this.attached) {
this.logger.debug("timer %s not found in lookup table - shouldn't happen", id);
}
// this shouldn't happen
for (let i=0; i < this.length; i++) {
var t=this[i];
if (t.id == id) {
this.logger.warn("adding timer to lookup hash %s:%s", t.name, t.id);
this._lookup[id] = t;
return t;
}
}
}
// Called during restore after loading timers from storage, to handle expired timers
restoreExpiredTimer(timer, now) {
// Timer has expired during downtime - trigger the completion notification
const tdiff = now - timer._end;
timer.expired = true;
timer._state = TimerState.EXPIRED;
// Check if we already have an active notification for this timer
if (timersInstance.notifier && timersInstance.notifier._activeNotifications &&
timersInstance.notifier._activeNotifications.has(timer.id)) {
this.logger.debug("Skipping duplicate notification for expired timer during restore: %s", timer.name);
return;
}
// Show notification for expired timer
const reason = _("Timer completed %s late at").format(new HMS(tdiff/1000).toString(true));
const time = new Date(now).toLocaleTimeString();
const text = "%s due at %s".format(timer.name, timer.end_time());
timersInstance.notifier.notify(timer, text, "%s %s", reason, time);
this.logger.debug(`Timer expired during downtime: ${timer.toString()}, was late by ${Math.round(tdiff/1000)} seconds`);
if (this.attached) {
this.logger.debug("Timer id=[%s] not found", id);
}
return undefined;
}
isEmpty() {
return this.length === 0;
}
get sort_by_duration() {
return this.settings.sort_by_duration;
}
get sort_descending() {
return this.settings.sort_descending;
}
get sort_by_name() {
return this.settings.sort_by_name;
}
// list of enabled timers sorted according to the sort properties
sorted(params={running:true}) {
var timers_array = [...this];
if (!params.running) {
timers_array=timers_array.filter(timer => !timer.running);
}
var direction = this.sort_descending ? -1 : 1;
if (this.sort_by_name) {
this.logger.debug('sort by name');
timers_array.sort( (a,b) => {
var na = (a.name || '').toLowerCase();
var nb = (b.name || '').toLowerCase();
return (na < nb ? -1 : na > nb ? 1 : 0) * direction;
});
} else if (this.sort_by_duration) {
this.logger.debug('sort by duration');
timers_array.sort( (a,b) => (a.duration-b.duration)*direction);
}
return timers_array.filter(timer => (timer.enabled || timer.quick));
}
sort_by_running() {
var running_timers = [...this].filter(timer => timer.running);
var now=Date.now();
return running_timers.sort( (a,b) => {
return a._end-b._end;
});
}
stop_next() {
let timer=this.sort_by_running()[0];
if (timer) {
this.logger.debug("Stopping timer %s", timer.name);
timer.stop();
} else {
this.logger.debug("No running timers");
}
}
timer_by_id(id) {
var tbid = this.filter(timer => timer.id == id);
return tbid.length == 0 ? null : tbid[0];
}
is_dupe(timer) {
return this.get_dupe(timer) !== undefined;
}
get_dupe(timer) {
if (!this.settings.detect_dupes) {
return undefined;
}
for (let i=0; i < this.length; i++) {
var t=this[i];
if (timer.duration == t.duration && timer.quick == t.quick && timer.name == t.name) {
return t;
}
}
return undefined;
}
/**
* Add the new timer, after checking that it has no dupes
*
* timer is the new timer to add to timers
*
* @returns the new timer if it is not the original, otherwise the dupe
* returns undefined if the timer could not be added
*/
add_check_dupes(timer) {
var tdupe = this.get_dupe(timer);
if (tdupe !== undefined) {
if (tdupe.running) {
tdupe.notify(_("Duplicate timer is already running"));
return undefined;
}
return tdupe;
}
return this.add(timer) ? timer : undefined;
}
add(timer) {
if (!timer.quick && timer.name.length == 0) {
this.logger.warn('Refusing to create unnamed preset timer');
return false;
}
if (timer.duration <= 0) {
this.logger.warn(`Refusing to create zero length timer ${timer.name}`);
return false;
}
//this.logger.info(`Adding timer ${timer.name} of duration ${timer.duration} seconds quick=${timer.quick}`);
this.logger.info("Adding timer [%s] of duration %d seconds [%s], quick=%s", timer.name, timer.duration, timer.id, timer.quick);
this.push(timer);
this._lookup[timer.id] = timer;
if (this.attached) {
// don't pack timers if the indicator is attaching and refreshing timers from settings
this.settings.pack_timers(this);
}
return true;
}
remove(timer, i=undefined) {
if (i === undefined) {
// we don't know index of timer
i = this.indexOf(timer);
if (i == -1) {
return false;
}
}
timer.disable();
// remove from timers
this.splice(i, 1);
this.logger.debug("timer %s has been purged", timer.name);
this.settings.pack_timers(this);
delete this._lookup[timer.id];
return true;
}
}
// timers is a singleton class
var timersInstance = new Timers();
//Object.freeze(timersInstance);
var TimerState = {
INIT: 0,
RESET: 1,
RUNNING: 2,
EXPIRED: 3
}
var Timer = class Timer {
constructor(name, duration_secs, id=undefined) {
// Ensure timersInstance is defined
if (typeof timersInstance === 'undefined' || timersInstance === null) {
timersInstance = new Timers();
}
var debug = timersInstance.settings.debug;
this._enabled = true;
this._quick = false;
this._interval_ms = debug ? 500 : 250;
this._duration_secs = duration_secs;
this._state = TimerState.INIT;
this._id = Utils.uuid(id);
this._label = null;
this._gicon = null;
this._start = 0;
this._end = 0;
this._persist_alarm = false;
this._interval_id = undefined;
// will be undefined if it's not an alarm timer
this._alarm_timer = AlarmTimer.matchRegex(name);
if (this._alarm_timer) {
this._alarm_timer.debug = timersInstance.settings;
}
// this calls the setter
this.name = name;
this.logger = new Logger(`kt timer: ${this.name}`, timersInstance.settings);
this.logger.info(`Create timer [${this.name}] duration=[${duration_secs}]`);
}
check_volume() {
if (!this.timers.settings.play_sound || !this.timers.settings.volume_level_warn) {
return;
}
let stream = mixerControl.get_default_sink();
if (stream) {
let result = {
max: mixerControl.get_vol_max_norm(),
vol: 0,
level: -1,
muted: false
}
result.vol = stream.volume;
result.muted = stream.is_muted;
result.level = Math.floor(result.vol * 100 / result.max);
var volume_threshold = this.timers.settings.volume_threshold;
if (result.muted || result.level < volume_threshold) {
if (this.timers.warn_volume) {
this.timers.warn_volume = false;
//Utils.logObjectPretty(result);
let msg=this.logger.warn(_('volume level is low for running timer: %d %%'), result.level);
this.timers.notifier.warning(this, this.name, msg);
}
} else {
if (!this.timers.warn_volume) {
this.logger.debug('volume level %d above threshold %d', result.level, volume_threshold);
this.timers.warn_volume = true;
}
}
}
}
toString() {
return `[${this._name}:${this._id}] state=${this._state} start=${this._start} end=${this._end} dur=${this._duration_secs} iid=${this._interval_id}`;
}
static fromResult(result) {
var timer = new Timer(result.name, result.hms.toSeconds());
timer.quick = result.quick;
timer.alarm_timer = result.alarm_timer;
return timer;
}
get timers() {
if (typeof timersInstance === 'undefined' || timersInstance === null) {
timersInstance = new Timers();
}
return timersInstance;
}
get id() {
return this._id;
}
get enabled() {
return this._enabled;
}
set enabled(bool) {
this._enabled = bool;
}
disable() {
this._enabled = false;
}
get quick() {
return this._quick;
}
set quick(bool) {
this._quick = bool;
}
// Timer.new('foo', 50).name is 'foo'
get name() {
return this._name;
}
automatic_name(hms) {
return hms.toName();
}
end_time() {
return new Date(this._end).toLocaleTimeString();
}
set name(name) {
var hms = new HMS(this.duration);
this._name = name.length > 0 ? name : this.automatic_name(hms);
this._has_name = this._name != this.automatic_name(hms);
}
get has_name() {
return this._has_name;
}
get duration() {
if (this.alarm_timer) {
var hms = this.alarm_timer.hms();
return hms.toSeconds();
}
return this._duration_secs;
}
set duration(duration) {
this._duration_secs = duration;
}
duration_ms() {
return this.duration * 1000;
}
// can be null if not initialized or closed
get label() {
return this._label;
}
// menu label or null if closed
set label(label) {
//this.logger.debug(label ? `Timer label set to ${label}`: 'Timer label set to null');
this._label = label;
}
get alarm_timer() {
return this._alarm_timer;
}
set alarm_timer(val) {
this._alarm_timer = val;
}
get persist_alarm() {
return this._persist_alarm;
}
set persist_alarm(b) {
this._persist_alarm = b === undefined ? false : b;
timersInstance.saveRunningTimers();
}
toggle_persist_alarm() {
this.persist_alarm = !this.persist_alarm;
}
get remaining_secs() {
if (!this.running) {
return 0;
}
return Math.floor((this._end - Date.now()) / 1000);
}
label_progress(hms=undefined) {
if (timersInstance.indicator === undefined || timersInstance.attached === false) {
return;
}
if (!this.label) {
return;
}
if (hms === undefined) {
hms=this.remaining_hms();
}
this.label.set_text(hms.toString());
}
/*
0
1 - 15
2 - 30
3 - 45
...
22 - 330
23 - 345
24 - 360
*/
degree_progress(chunk=15) {
if (this.running) {
// 360/15 = 24
var chunks = Math.floor(360 / chunk);
var delta = Date.now() - this._start;
var progress = Math.floor(delta / (this._end-this._start) * chunks);
if (progress >= chunks) {
progress = chunks-1;
}
return (progress)*chunk;
}
return 0;
}
icon_progress() {
if (timersInstance.indicator === undefined) {
return;
}
if (!timersInstance.settings.show_progress) {
return;
}
var key = this.degree_progress();
// Use the progress icon instead of the PNG logo
let progressGicon = this.timers.progress_gicon(key);
// Create the icon with the progress icon
var icon = new St.Icon({
gicon: progressGicon,
style_class: 'system-status-icon'
});
icon.set_icon_size(20);
var panel_box = timersInstance.box;
if (panel_box) {
var current = panel_box.get_child_at_index(0);
panel_box.replace_child(current, icon);
current.destroy();
}
}
get running() {
return (this._state === TimerState.RUNNING);
}
get expired() {
return (this._state === TimerState.EXPIRED);
}
get reset() {
return (this._state == TimerState.RESET);
}
set running(bool) {
if (bool) { this._state = TimerState.RUNNING };
}
set expired(bool) {
if (bool) { this._state = TimerState.EXPIRED; }
}
set reset(bool) {
if (bool) { this._state = TimerState.RESET; }
}
timer_callback(timer) {
var now = Date.now();
var end = timer._end;
if (now > end) {
timer.expired = true;
}
if (timer.expired || timer.reset) {
return timer.stop_callback(now);
}
timersInstance.inhibitor.inhibit_timer(timer);
let hms = timer.remaining_hms(now);
timer.label_progress(hms);
var running_timers = timersInstance.sort_by_running();
if (running_timers.length > 0 && running_timers[0] == timer) {
timer.icon_progress();
timersInstance.set_panel_name(timer.name, timer.has_name);
var panel_label;
if (timersInstance.settings.show_endtime) {
panel_label = new Date(timer._end + 1000).toLocaleTimeString();
} else {
panel_label = hms.toString(true);
}
timersInstance.set_panel_label(panel_label);
}
timersInstance.maybePeriodicSaveTimersJson(now);
return true;
}
remaining_hms(now=undefined) {
let delta;
if (this.running) {
if (now === undefined) {
now = Date.now();
}
if (this.alarm_timer) {
this._end = this.alarm_timer.end();
}
delta = Math.ceil((this._end-now) / 1000);
} else {
delta = this.duration;
}
return new HMS(delta < 0 ? 0 : delta);
}
stop_callback(now) {
var tdiff = now - this._end;
var early = tdiff < 0;
var late = tdiff > 2000;
// shouldn't be necessary, but we'll make sure
if (early) {
this.reset = true;
tdiff = -tdiff;
} else {
this.expired = true;
}
this.logger.info('Timer has ended %s: state=%d', early ? "early" : (late ? "late" : "on time"), this._state);
Utils.clearInterval(this._interval_id);
this._interval_id = undefined;
// Prevent duplicate notifications
if (timersInstance.notifier && timersInstance.notifier._activeNotifications &&
timersInstance.notifier._activeNotifications.has(this.id)) {
this.logger.debug("Skipping duplicate notification for timer: %s", this.name);
// Still perform cleanup
var hms = new HMS(this.duration);
this.label_progress(hms);
this.icon_progress();
timersInstance.set_panel_name("");
timersInstance.set_panel_label("");
timersInstance.saveRunningTimers();
saveAllTimers(timersInstance);
return false;
}
var stdiff = early || late ? new HMS(tdiff/1000).toString(true) : "";
var reason;
var text = "%s due at %s".format(this.name, this.end_time())
if (early) {
reason = _("Timer stopped %s early at").format(stdiff);
} else if (late) {
reason = _("Timer completed %s late at").format(stdiff);
} else {
text = this.name;
saveAllTimers(timersInstance); // Save timer state when completed normally
reason = _("Timer completed on time at");
}
var time=new Date(now).toLocaleTimeString();
timersInstance.notifier.notify(this, text, "%s %s", reason, time);
var hms2 = new HMS(this.duration);
this.label_progress(hms2);
this.icon_progress();
timersInstance.set_panel_name("");
timersInstance.set_panel_label("");
timersInstance.saveRunningTimers();
saveAllTimers(timersInstance); // Save all timers to persistent storage