forked from MajortomVR/simple-timer-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.js
More file actions
297 lines (248 loc) · 10.8 KB
/
extension.js
File metadata and controls
297 lines (248 loc) · 10.8 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
import St from 'gi://St';
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
import Clutter from 'gi://Clutter';
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js'; // https://gitlab.gnome.org/GNOME/gnome-shell/-/tree/main/js/ui
import * as MessageTray from 'resource:///org/gnome/shell/ui/messageTray.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import * as Misc from './src/misc.js';
import * as Timer from './src/Timer.js';
import * as Settings from './src/Settings.js';
import * as Hotkey from './src/Hotkey.js';
import { AudioPlayer } from './src/AudioPlayer.js';
export default class TimerExtension extends Extension {
enable() {
this.timer = new Timer.Timer();
this.settings = new Settings.Settings(this.getSettings());
this.audioPlayer = new AudioPlayer();
this.panelButton = new PanelMenu.Button(0, "MainButton", false);
this.panelButton.add_style_class_name('simple-timer-panel-button');
// MAIN PANEL
this.icon = new St.Icon({ icon_name: 'alarm-symbolic', style_class: 'system-status-icon' });
this.timerLabel = new St.Label({ text: '0:00', y_expand: true, y_align: Clutter.ActorAlign.CENTER });
this.timerLabel.hide();
this.panelButtonLayout = new St.BoxLayout();
this.panelButtonLayout.add_child(this.icon);
this.panelButtonLayout.add_child(this.timerLabel);
// Timer Input Field
this.menuTimerInputEntry = new St.Entry({
name: 'time',
primary_icon : new St.Icon({ icon_name : 'media-playback-start-symbolic', icon_size : 24 }),
can_focus : true,
hint_text: _("Enter countdown time..."),
x_expand : true,
y_expand : true,
text: this.settings.getLastTimerInput(),
});
// Focus Input field when panel is opened
this.panelButton.menu.connect('open-state-changed', (menu, isOpen) => {
if (isOpen) {
this.menuOpeningDelayID = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 100, () => {
this.menuTimerInputEntry.grab_key_focus();
this.menuOpeningDelayID = null;
return GLib.SOURCE_REMOVE;
});
}
});
this.menuTimerInputEntry.set_input_purpose(Clutter.TIME);
this.menuTimerInputEntry.clutter_text.set_max_length(12);
// Input Field Event Management
this.menuTimerInputEntry.clutter_text.connect('activate', ()=> {
this.timerStart();
});
this.menuTimerInputEntry.connect('primary-icon-clicked', () => {
this.timerStart();
});
// Timer-Input Text Change Event Handling
this.menuTimerInputEntry.clutter_text.connect('text-changed', ()=> {
let text = this.menuTimerInputEntry.get_text();
let newText = "";
// This filters the time input based on its format, either as a colon-separated format like "3:00:00" or a letter format like "2h 47m 12s".
if (Misc.getTimeInputFormat(text) === Misc.TimeInputFormat.LETTERS) {
newText = Misc.timeInputLetterHandler(text);
} else {
newText = Misc.timeInputColonHandler(text);
}
// If the input filter has changed the input, we update the text input field with the corrected text.
if (text != newText) {
this.menuTimerInputEntry.set_text(newText);
}
});
this.itemInput = new PopupMenu.PopupBaseMenuItem({
reactive : false,
can_focus : false
});
this.itemInput.add_child(this.menuTimerInputEntry);
this.panelButton.menu.addMenuItem(this.itemInput);
// PANEL-MENU
let boxMenuItem = new PopupMenu.PopupBaseMenuItem({ reactive: false, can_focus: false });
let boxLayout = new St.BoxLayout({ x_align: Clutter.ActorAlign.CENTER, x_expand: true });
boxMenuItem.add_child(boxLayout);
// Resume Button
this.menuButtonResume = new PopupMenu.PopupImageMenuItem("", "media-playback-start-symbolic", {style_class: 'control-button'});
this.menuButtonResume.connect('activate', () => {
if (this.timer.isFinished() || this.timer.isStopped()) {
this.timerStart();
} else {
this.timer.resume();
}
this.updateMenuButtonVisibilty();
});
boxLayout.add_child(this.menuButtonResume);
this.panelButton.menu.addMenuItem(boxMenuItem);
// Pause Button
this.menuButtonPause = new PopupMenu.PopupImageMenuItem("", "media-playback-pause-symbolic", {style_class: 'control-button'});
this.menuButtonPause.connect('activate', () => {
this.timer.pause();
this.updateMenuButtonVisibilty();
});
boxLayout.add_child(this.menuButtonPause);
// STOP Button
this.menuButtonStop = new PopupMenu.PopupImageMenuItem("", "media-playback-stop-symbolic", {style_class: 'control-button'});
this.menuButtonStop.connect('activate', () => {
this.audioPlayer.stop();
this.timer.reset();
this.updateTimerLabelStyle();
this.updateTimerLabel();
this.timerLabel.hide();
this.icon.show();
this.updateMenuButtonVisibilty();
});
boxLayout.add_child(this.menuButtonStop);
this.panelButton.add_child(this.panelButtonLayout);
// Create a separator
this.panelButton.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
// Settings entry
const settingsMenuItem = new PopupMenu.PopupImageMenuItem('', 'preferences-system-symbolic', {style_class: 'control-button'});
settingsMenuItem.x_align = Clutter.ActorAlign.CENTER;
settingsMenuItem.connect('activate', () => {
this.openPreferences();
});
this.panelButton.menu.addMenuItem(settingsMenuItem);
// Adds the panel button to the status area, positioned on the right side of the panel.
Main.panel.addToStatusArea(this.uuid, this.panelButton, 0, "right");
// Start
this.updateMenuButtonVisibilty();
this.initMainLoop();
// Check if timer is still running, and if it is -> reload the timer running view
if (this.timer.isRunning()) {
this.timer.update();
this.timerShow();
}
this.hotkey = new Hotkey.Hotkey(this.settings, this.settings.getAlertStartHotkeyID(), this.timerStart.bind(this));
}
disable() {
if (this.hotkey) {
this.hotkey.free();
this.hotkey = null;
}
if (this.audioPlayer) {
this.audioPlayer.stop();
this.audioPlayer = null;
}
if (this.menuOpeningDelayID) {
GLib.Source.remove(this.menuOpeningDelayID);
this.menuOpeningDelayID = null;
}
// The Session-Mode "unlock-dialog" is needed because the timer should also be working on the lock screen.
this.freeMainLoop();
this.panelButton.destroy();
this.panelButton = null;
this.timer = null;
this.settings = null;
}
// Shows Start/Input Timer or Stop Button in the Menu, depending on the current timer state [running/stopped].
updateMenuButtonVisibilty() {
//showStartEntry ? this.menuTimerInputEntry.show() : this.menuTimerInputEntry.hide();
this.handleButtonStyle(this.menuButtonStop, !this.timer.isStopped());
this.handleButtonStyle(this.menuButtonPause, this.timer.isRunning());
this.handleButtonStyle(this.menuButtonResume, !this.timer.isRunning());
}
initMainLoop() {
// Update Timer
this.timeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000, () => {
this.timer.update();
this.updateTimerLabel();
if (this.timer.isFinished() && !this.timer.isNotificationSent()) {
this.timer.setNotificationSent();
this.createTimerFinishedAlert();
this.updateMenuButtonVisibilty();
}
this.updateTimerLabelStyle();
return GLib.SOURCE_CONTINUE;
});
}
freeMainLoop() {
GLib.Source.remove(this.timeout);
this.timeout = null;
}
// Starts the timer and sets the countdown time.
timerStart() {
let timeSeconds = Misc.parseTimeInput( this.menuTimerInputEntry.get_text() );
if (timeSeconds > 0) {
this.settings.setLastTimerInput(this.menuTimerInputEntry.get_text());
this.timer.start(timeSeconds);
this.timerShow();
}
}
// Shows the timer if it is running
timerShow() {
if (this.timer.isRunning()) {
this.updateTimerLabel();
this.updateTimerLabelStyle(false);
this.timerLabel.show();
this.icon.hide();
this.menuButtonStop.show();
this.updateMenuButtonVisibilty();
}
}
// Updates the timer-label with the current time left.
updateTimerLabel() {
this.timerLabel.set_text( Misc.formatTime(this.timer.timeLeftSeconds) );
}
// Shows the Timer in a different style depending on wether an alert was triggered, or not.
updateTimerLabelStyle() {
if (this.timerLabel) {
let style = '';
if (this.timer.isFinished()) {
style = 'countdown-alert';
} else if (this.timer.isPaused()) {
style = 'countdown-paused';
} else {
style = 'countdown';
}
if (this.timerLabel.style_class != style) {
this.timerLabel.style_class = style;
}
}
}
// Swichtes style classes depending on button active status.
handleButtonStyle(button, active) {
button.sensitive = active;
if (active) {
button.remove_style_class_name('img-button-inactive');
} else {
button.add_style_class_name('img-button-inactive');
}
}
// Alert by sending a notification and a sound effect.
createTimerFinishedAlert() {
const defaultAudioFile = GLib.build_filenamev([this.path, '/sfx/Polite.wav']);
const customAudioFile = this.settings.getCustomAlertSfxFile();
const audioFile = Misc.fileExists(customAudioFile) ? customAudioFile : defaultAudioFile;
this.audioPlayer.play(audioFile);
// Send Notification
const systemSource = MessageTray.getSystemSource();
const notification = new MessageTray.Notification({
source: systemSource,
title: 'Timer',
body: 'The timer has finished!',
gicon: new Gio.ThemedIcon({name: 'alarm-symbolic'}),
// Same as `gicon`, but takes a themed icon name
iconName: 'alarm-symbolic'
});
systemSource.addNotification(notification);
}
}