-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesklet.js
More file actions
234 lines (198 loc) · 8.08 KB
/
desklet.js
File metadata and controls
234 lines (198 loc) · 8.08 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
const St = imports.gi.St;
const Desklet = imports.ui.desklet;
const Settings = imports.ui.settings;
const Mainloop = imports.mainloop;
const Lang = imports.lang;
const Gio = imports.gi.Gio;
const UUID = "cinamodoro@software-by-design";
const DESKLET_DIR = imports.ui.deskletManager.deskletMeta[UUID].path;
const Main = imports.ui.main;
class PomodoroDesklet extends Desklet.Desklet {
constructor(metadata, desklet_id) {
super(metadata, desklet_id);
this._container = new St.BoxLayout({ vertical: true, style_class: "main-container" });
this._timerContainer = new St.BoxLayout({ style_class: "timer_container" });
this._controlsContainer = new St.BoxLayout({ style_class: "controls_container" });
// Timer display
this._timeLabel = new St.Label({ style_class: "time_label" });
this._stateLabel = new St.Label({ style_class: "state_label" });
// Control buttons
this._startButton = new St.Button({
style_class: "button",
child: new St.Label({ text: "Start" })
});
this._resetButton = new St.Button({
style_class: "button",
child: new St.Label({ text: "Reset" })
});
// Initialize timer state
this.isRunning = false;
this.isWorkTime = true;
this.timeRemaining = 0;
this.pomodoroCycles = 0; // Track completed Pomodoro cycles
this.timerLoop = null;
// Bind settings
this.settings = new Settings.DeskletSettings(this, this.metadata["uuid"], desklet_id);
this.settings.bind("work-duration", "workDuration", this._onSettingsChanged);
this.settings.bind("break-duration", "breakDuration", this._onSettingsChanged);
this.settings.bind("long-break-duration", "longBreakDuration", this._onSettingsChanged);
this.settings.bind("cycles-before-long-break", "cyclesBeforeLongBreak", this._onSettingsChanged);
this.settings.bind("font-size", "fontSize", this._onSettingsChanged);
this.settings.bind("text-color", "textColor", this._onSettingsChanged);
this.settings.bind("background-color", "bgColor", this._onSettingsChanged);
this.settings.bind("play-sound", "playSound", null);
this.settings.bind("auto-start-next-timer", "autoStartNextTimer", null);
this.settings.bind("show-notifications", "showNotifications", null); // New setting for notifications
// Build UI
this._buildLayout();
this._connectSignals();
this._resetTimer();
}
_buildLayout() {
this._timerContainer.add(this._timeLabel);
this._timerContainer.add(this._stateLabel);
this._controlsContainer.add(this._startButton);
this._controlsContainer.add(this._resetButton);
this._container.add(this._timerContainer);
this._container.add(this._controlsContainer);
this.setContent(this._container);
this.setHeader(_("Cinamodoro - a pomodoro timer"));
}
_connectSignals() {
this._startButton.connect('clicked', Lang.bind(this, this._toggleTimer));
this._resetButton.connect('clicked', Lang.bind(this, this._resetTimer));
}
_toggleTimer() {
if (!this.isRunning && this.timeRemaining === 0 && !this.autoStartNextTimer) {
// If the timer has ended and autoStartNextTimer is false, set to continue
this._continueTimer();
this._startButton.child.set_text("Pause");
} else if (!this.isRunning) {
this._startTimer();
this._startButton.child.set_text("Pause");
} else if (this.isRunning) {
this._pauseTimer();
this._startButton.child.set_text("Start");
}
}
_startTimer() {
if (this.timerLoop) {
Mainloop.source_remove(this.timerLoop);
}
this.timerLoop = Mainloop.timeout_add_seconds(1, Lang.bind(this, this._updateTimer));
}
_pauseTimer() {
if (this.timerLoop) {
this._showAlert("Timer paused!");
Mainloop.source_remove(this.timerLoop);
this.timerLoop = null;
}
}
_resetTimer() {
this._pauseTimer();
this.isRunning = false;
this.isWorkTime = true; // Reset to work time when resetting
this.pomodoroCycles = 0; // Reset cycle count
this.timeRemaining = this.workDuration * 60; // Reset to work duration
this._startButton.child.set_text("Start");
this._updateDisplay();
}
_updateTimer() {
if (this.timeRemaining > 0) {
this.timeRemaining--;
this._updateDisplay();
return true;
} else {
this._onTimerComplete();
return false;
}
}
_updateDisplay() {
let minutes = Math.floor(this.timeRemaining / 60);
let seconds = this.timeRemaining % 60;
this._timeLabel.set_text(`${minutes}:${seconds.toString().padStart(2, '0')}`);
this._stateLabel.set_text(this.isWorkTime ? "Work Time" : "Break Time");
}
_onTimerComplete() {
this._playNotificationSound();
this.isWorkTime = !this.isWorkTime;
// Increment cycle count only when transitioning from work to break
if (!this.isWorkTime) {
this.pomodoroCycles++; // Increment cycle count on work time
}
// Check if it's time for a long break
if (!this.isWorkTime && this.pomodoroCycles >= this.cyclesBeforeLongBreak) {
this.timeRemaining = this.longBreakDuration * 60;
this.pomodoroCycles = 0; // Reset cycle count after long break
this._showAlert("Well done! Time for a long break!");
} else {
this.timeRemaining = this.isWorkTime ?
this.workDuration * 60 :
this.breakDuration * 60;
if (this.isWorkTime) {
this._showAlert("Break time over - get back to work!");
} else {
this._showAlert("It's break time - kick back and relax!");
}
}
// Check if auto-start is enabled
if (this.autoStartNextTimer) {
this._startTimer(); // Ensure the timer starts again
} else {
this._startButton.child.set_text("Continue");
this._startButton.show();
}
}
_playNotificationSound() {
if (this.playSound) {
let soundFile = Gio.File.new_for_path(DESKLET_DIR + "/sounds/cashier.ogg");
if (this.isWorkTime) {
soundFile = Gio.File.new_for_path(DESKLET_DIR + "/sounds/cashier.ogg");
} else {
soundFile = Gio.File.new_for_path(DESKLET_DIR + "/sounds/schoolBell.ogg");
}
let player = global.display.get_sound_player();
// Error handling for sound playback
if (player) {
player.play_from_file(soundFile, "", null);
} else {
this._showAlert("Sound player is not available.");
}
}
}
_onSettingsChanged() {
console.log("Text Color:", this.textColor);
console.log("Background Color:", this.bgColor);
this._timeLabel.style = `
font-size: ${this.fontSize}px;
color: ${this.textColor};
`;
this._stateLabel.style = `
font-size: ${this.fontSize * 0.5}px;
color: ${this.textColor};
`;
this._container.style = `background-color: ${this.bgColor};`;
// Update button text color
this._startButton.child.set_style(`color: ${this.textColor};`);
this._resetButton.child.set_style(`color: ${this.textColor};`);
if (!this.isRunning) {
this._resetTimer();
}
}
on_desklet_removed() {
this._pauseTimer();
}
_continueTimer() {
this._showAlert("Pomodoro cycle completed!");
this._startTimer();
this._startButton.child.set_text("Pause");
}
_showAlert(message) {
if (this.showNotifications) {
Main.notify("Pomodoro Timer", message);
}
}
}
function main(metadata, desklet_id) {
return new PomodoroDesklet(metadata, desklet_id);
}