-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
295 lines (250 loc) · 9.69 KB
/
Copy pathmain.js
File metadata and controls
295 lines (250 loc) · 9.69 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
const { app, Tray, Menu, Notification, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const fs = require('fs');
const { spawn } = require('child_process');
// Menubar app — no Dock icon
app.dock.hide();
// ── Config ────────────────────────────────────────────────────────────────────
let bgImagePath = null;
function configPath() { return path.join(app.getPath('userData'), 'config.json'); }
function loadConfig() {
try {
const c = JSON.parse(fs.readFileSync(configPath(), 'utf8'));
bgImagePath = c.bgImagePath || null;
intervalMin = c.intervalMin || 20;
durationSec = c.durationSec || 20;
} catch {}
}
function saveConfig() {
fs.writeFileSync(configPath(), JSON.stringify({ bgImagePath, intervalMin, durationSec }), 'utf8');
}
function sendBg() {
if (breakWin && !breakWin.isDestroyed()) breakWin.webContents.send('bg', bgImagePath);
}
// ── Timer constants ──────────────────────────────────────────────────────────
let intervalMin = 20;
let durationSec = 20;
function EYE_INTERVAL_SEC() { return intervalMin * 60; }
function EYE_REST_SEC() { return durationSec; }
// ── Timer state ──────────────────────────────────────────────────────────────
let state = 'idle';
let remaining = EYE_INTERVAL_SEC();
let tickInterval = null;
let tray = null;
let breakWin = null; // eye break popup
let bgPickerWin = null; // bg picker modal
let settingsWin = null; // settings modal
// ── Helpers ──────────────────────────────────────────────────────────────────
function fmt(secs) {
const m = String(Math.floor(secs / 60)).padStart(2, '0');
const s = String(secs % 60).padStart(2, '0');
return `${m}:${s}`;
}
function notify(title, body) {
if (Notification.isSupported()) {
new Notification({ title, body, silent: false }).show();
}
}
function getTotal() {
return state === 'rest' ? EYE_REST_SEC() : EYE_INTERVAL_SEC();
}
function sendState() {
const payload = { eyeState: state, eyeRemaining: remaining, eyeTotal: getTotal() };
if (breakWin && !breakWin.isDestroyed()) breakWin.webContents.send('state', payload);
}
function rebuildMenu() {
const label =
state === 'rest' ? `Look away! ${fmt(remaining)}` :
`Next break in ${fmt(remaining)}`;
tray.setContextMenu(Menu.buildFromTemplate([
{ label, enabled: false },
{ type: 'separator' },
{ label: 'Reset timer', click: reset },
{ type: 'separator' },
{ label: 'Change background…', click: openBgPicker },
{ label: 'Settings…', click: openSettings },
{ type: 'separator' },
{ label: 'Quit', accelerator: 'Command+Q', click: () => app.quit() },
]));
}
function updateTray() {
const title = fmt(remaining);
tray.setTitle(title);
rebuildMenu();
sendState();
}
// ── Timer actions ─────────────────────────────────────────────────────────────
function startIdle() {
state = 'idle';
remaining = EYE_INTERVAL_SEC();
updateTray();
}
function startRest() {
state = 'rest';
remaining = EYE_REST_SEC();
notify('Time for an eye break!', 'Look 20 feet away for 20 seconds.');
createBreakWindow(() => {
breakWin.show();
breakWin.focus();
sendState();
});
updateTray();
}
function reset() {
startIdle();
}
// ── Main tick ─────────────────────────────────────────────────────────────────
function tick() {
remaining -= 1;
if (remaining <= 0) {
if (state === 'idle') {
startRest();
} else if (state === 'rest') {
if (breakWin && !breakWin.isDestroyed()) breakWin.close();
spawn('afplay', [path.join(__dirname, 'assets', 'complete.mp3')]);
notify('Eye break done!', 'Great job. Timer reset to 20 minutes.');
startIdle();
}
} else {
updateTray();
}
}
// ── Break popup window ────────────────────────────────────────────────────────
function createBreakWindow(onReady) {
if (breakWin && !breakWin.isDestroyed()) breakWin.close();
const trayBounds = tray.getBounds();
const winWidth = 360;
const winHeight = 380;
const x = Math.round(trayBounds.x + trayBounds.width / 2 - winWidth / 2);
const y = Math.round(trayBounds.y + trayBounds.height + 8);
breakWin = new BrowserWindow({
width: winWidth,
height: winHeight,
x,
y,
show: false,
frame: false,
resizable: false,
transparent: true,
alwaysOnTop: true,
skipTaskbar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
},
});
breakWin.loadFile(path.join(__dirname, 'renderer', 'break.html'));
breakWin.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
breakWin.setAlwaysOnTop(true, 'screen-saver');
breakWin.webContents.once('did-finish-load', () => {
sendBg();
if (onReady) onReady();
});
breakWin.on('closed', () => { breakWin = null; });
}
// ── Background picker window ──────────────────────────────────────────────────
function openBgPicker() {
if (bgPickerWin && !bgPickerWin.isDestroyed()) {
bgPickerWin.focus();
return;
}
bgPickerWin = new BrowserWindow({
width: 360,
height: 540,
resizable: false,
frame: false,
transparent: true,
webPreferences: {
preload: path.join(__dirname, 'preload-picker.js'),
contextIsolation: true,
nodeIntegration: false,
},
});
bgPickerWin.loadFile(path.join(__dirname, 'renderer', 'bg-picker.html'));
bgPickerWin.webContents.on('did-finish-load', () => {
bgPickerWin.webContents.send('picker-init', bgImagePath);
});
bgPickerWin.on('closed', () => { bgPickerWin = null; });
}
// ── Settings window ───────────────────────────────────────────────────────────
function openSettings() {
if (settingsWin && !settingsWin.isDestroyed()) {
settingsWin.focus();
return;
}
settingsWin = new BrowserWindow({
width: 320,
height: 220,
resizable: false,
frame: false,
transparent: true,
webPreferences: {
preload: path.join(__dirname, 'preload-settings.js'),
contextIsolation: true,
nodeIntegration: false,
},
});
settingsWin.loadFile(path.join(__dirname, 'renderer', 'settings.html'));
settingsWin.webContents.on('did-finish-load', () => {
settingsWin.webContents.send('settings-init', { intervalMin, durationSec });
});
settingsWin.on('closed', () => { settingsWin = null; });
}
// ── App ready ─────────────────────────────────────────────────────────────────
app.whenReady().then(() => {
const { nativeImage } = require('electron');
const rawIcon = nativeImage.createFromPath(path.join(__dirname, 'assets', 'tray-icon-custom.png'));
const icon = rawIcon.resize({ width: 18, height: 18 });
tray = new Tray(icon);
tray.setToolTip('Pause');
loadConfig();
tray.on('click', () => tray.popUpContextMenu());
ipcMain.on('picker-select', (_, p) => {
bgImagePath = p;
saveConfig();
sendBg();
if (bgPickerWin && !bgPickerWin.isDestroyed()) {
bgPickerWin.webContents.send('picker-preview', p);
}
});
ipcMain.on('picker-choose', async () => {
const { filePaths } = await dialog.showOpenDialog({
properties: ['openFile'],
filters: [{ name: 'Images', extensions: ['png', 'jpg', 'jpeg', 'webp'] }],
});
if (filePaths[0]) {
bgImagePath = filePaths[0];
saveConfig();
sendBg();
if (bgPickerWin && !bgPickerWin.isDestroyed()) {
bgPickerWin.webContents.send('picker-preview', filePaths[0]);
}
}
if (bgPickerWin && !bgPickerWin.isDestroyed()) bgPickerWin.focus();
});
ipcMain.on('picker-close', () => {
if (bgPickerWin && !bgPickerWin.isDestroyed()) bgPickerWin.close();
});
ipcMain.on('settings-close', () => {
if (settingsWin && !settingsWin.isDestroyed()) settingsWin.close();
});
ipcMain.on('settings-save', (_, { intervalMin: m, durationSec: s }) => {
intervalMin = m;
durationSec = s;
saveConfig();
if (settingsWin && !settingsWin.isDestroyed()) settingsWin.close();
// Reset the idle timer so new interval takes effect immediately
if (state === 'idle' || state === 'snoozed') startIdle();
});
ipcMain.on('get-app-path', (e) => { e.returnValue = __dirname; });
ipcMain.on('reset', reset);
ipcMain.on('skip', () => { if (breakWin && !breakWin.isDestroyed()) breakWin.close(); startIdle(); });
ipcMain.on('quit', () => app.quit());
startIdle();
tickInterval = setInterval(tick, 1000);
});
app.on('window-all-closed', () => {});
app.on('before-quit', () => {
if (tickInterval) clearInterval(tickInterval);
});