Skip to content

Commit

Permalink
timer@Severga: Preserves settings despite applet restart (#5133)
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiux authored Nov 22, 2023
1 parent eb6d093 commit ec3a068
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
40 changes: 36 additions & 4 deletions timer@Severga/files/timer@Severga/applet.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ TimerApplet.prototype = {
this.menu.addMenuItem(this.timeMenuItem);
this.turnOffSwitchMenuItem = new PopupMenu.PopupSwitchMenuItem(_("Turn off computer on timer"));
this.turnOffSwitchMenuItem.actor.set_style("background: #6B8355");
this.turnOffSwitchMenuItem.connect("toggled", Lang.bind(this, this._turn_off_toggled));
this.menu.addMenuItem(this.turnOffSwitchMenuItem);
this.menu.addMenuItem(new PopupMenu.PopupMenuItem("", {reactive: false}));
this.quickMenuItem = new PopupMenu.PopupSubMenuMenuItem(_("Quick setup"));
Expand Down Expand Up @@ -216,12 +217,14 @@ TimerApplet.prototype = {
this.endTime = new Date(); this.endTime.setHours(this.hSpin.value); this.endTime.setMinutes(this.mSpin.value); this.endTime.setSeconds(this.sSpin.value);
this.period = Math.floor((this.endTime.getTime() - Date.now()) / 1000);
if (this.period > 0) {
this.saved_endTime = this.endTime.getTime();
this.periodH = Math.floor(this.period / 3600);
this.periodM = Math.floor(this.period % 3600 / 60);
this.periodS = this.period % 60;
this._tick();
}
else {
this.saved_endTime = 0;
this.period = 0;
this.periodH = 0;
this.periodM = 0;
Expand All @@ -241,12 +244,14 @@ TimerApplet.prototype = {
this.endTime = new Date(); this.endTime.setHours(this.hSpin.value + 24); this.endTime.setMinutes(this.mSpin.value); this.endTime.setSeconds(this.sSpin.value);
this.period = Math.floor((this.endTime.getTime() - Date.now()) / 1000);
if (this.period > 0) {
this.saved_endTime = this.endTime.getTime();
this.periodH = Math.floor(this.period / 3600);
this.periodM = Math.floor(this.period % 3600 / 60);
this.periodS = this.period % 60;
this._tick();
}
else {
this.saved_endTime = 0;
this.period = 0;
this.periodH = 0;
this.periodM = 0;
Expand All @@ -268,8 +273,13 @@ TimerApplet.prototype = {
this.periodS = this.sSpin.value;
this.period = this.periodH * 3600 + this.periodM * 60 + this.periodS;
this.endTime = new Date(Date.now() + this.period * 1000);
if (this.period) this._tick();
else this.timeMenuItem.setLabel(_("At: --:--:--"));
if (this.period) {
this.saved_endTime = this.endTime.getTime();
this._tick();
} else {
this.saved_endTime = 0;
this.timeMenuItem.setLabel(_("At: --:--:--"));
}
}.bind(this));
this.preciseMenuItem.menu.addAction(_("Add preset"), function () {
let p = this.applet.hSpin.value * 3600 + this.applet.mSpin.value * 60 + this.applet.sSpin.value;
Expand All @@ -291,6 +301,12 @@ TimerApplet.prototype = {
this.settings.bindProperty(Settings.BindingDirection.IN, "showNotifications", "showNotifications", null, null);
this.settings.bindProperty(Settings.BindingDirection.IN, "openSubmenu", "openSubmenu", null, null);
this.settings.bindProperty(Settings.BindingDirection.BIDIRECTIONAL, "preset", "preset", null, null);
this.settings.bindProperty(Settings.BindingDirection.BIDIRECTIONAL, "saved_endTime", "saved_endTime", null, null);
this.settings.bindProperty(Settings.BindingDirection.BIDIRECTIONAL, "turn_off", "turn_off", null, null);
},

_turn_off_toggled: function() {
this.turn_off = this.turnOffSwitchMenuItem.state
},

_createPresetsMenuItem: function () {
Expand Down Expand Up @@ -318,8 +334,13 @@ TimerApplet.prototype = {
this.applet.periodM = Math.floor(this.preset % 3600 / 60);
this.applet.periodS = this.preset % 60;
this.applet.endTime = new Date(Date.now() + this.preset * 1000);
if (this.preset) this.applet._tick();
else this.applet.timeMenuItem.setLabel(_("At: --:--:--"));
if (this.preset) {
this.applet.saved_endTime = this.applet.endTime.getTime();
this.applet._tick();
} else {
this.applet.saved_endTime = 0;
this.applet.timeMenuItem.setLabel(_("At: --:--:--"));
}
}.bind({applet: this, preset: this.preset[i]}));
// Preset item delete button
let del=new St.Button({label: _("[ × ]")});
Expand Down Expand Up @@ -378,6 +399,15 @@ TimerApplet.prototype = {
this.menu.toggle();
},

on_applet_added_to_panel: function() {
if (this.turn_off)
this.turnOffSwitchMenuItem.setToggleState(this.turn_off);
if (this.saved_endTime > Date.now()) {
this.endTime.setTime(this.saved_endTime);
this._tick()
}
},

on_applet_removed_from_panel: function(event) {
this._remove_timeout();
this._remove_flashTimeout();
Expand Down Expand Up @@ -449,8 +479,10 @@ TimerApplet.prototype = {
this.periodM = Math.floor(this.period % 3600 / 60);
this.periodS = this.period % 60;
this.endTime = new Date(Date.now() + this.period * 1000);
this.saved_endTime = this.endTime.getTime();
}
else {
this.saved_endTime = 0;
this.period = 0;
this.periodH = 0;
this.periodM = 0;
Expand Down
10 changes: 9 additions & 1 deletion timer@Severga/files/timer@Severga/settings-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,13 @@
],
"description": "Presets",
"tooltip": "Presets"
}
},
"saved_endTime": {
"type": "generic",
"default": 0
},
"turn_off": {
"type": "generic",
"default": false
}
}

0 comments on commit ec3a068

Please sign in to comment.