-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
311 lines (269 loc) · 8.92 KB
/
main.js
File metadata and controls
311 lines (269 loc) · 8.92 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
const { app, BrowserWindow, ipcMain, Tray, Menu, Notification, nativeTheme, nativeImage, shell } = require('electron');
const path = require('node:path');
const fs = require('node:fs');
let widgetWindow = null;
let dashboardWindow = null;
let categoryWindow = null;
let taskWindow = null;
let tray = null;
let editingTaskId = null;
const dataFile = path.join(app.getPath('userData'), 'task_note_data.json');
// Initialize data
if (!fs.existsSync(dataFile)) {
fs.writeFileSync(dataFile, JSON.stringify({ tasks: [], notes: [], settings: { theme: 'system', pinToTop: false, autoLaunch: false } }));
}
function createDashboardWindow() {
if (dashboardWindow) {
dashboardWindow.show();
dashboardWindow.focus();
return;
}
const isHidden = process.argv.includes('--hidden');
dashboardWindow = new BrowserWindow({
width: 900,
height: 600,
minWidth: 700,
minHeight: 500,
title: "TaskNote Dashboard",
icon: path.join(__dirname, 'notepad.png'),
show: !isHidden,
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true
}
});
dashboardWindow.loadFile('dashboard.html');
dashboardWindow.on('close', (event) => {
if (!app.isQuiting) {
event.preventDefault();
dashboardWindow.hide();
}
return false;
});
}
function createWidgetWindow() {
if (widgetWindow) {
widgetWindow.show();
widgetWindow.focus();
return;
}
widgetWindow = new BrowserWindow({
width: 350,
height: 450,
minWidth: 250,
minHeight: 300,
icon: path.join(__dirname, 'notepad.png'),
frame: false,
show: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true
}
});
widgetWindow.loadFile('index.html');
// Load initial settings for pin to top
try {
const data = JSON.parse(fs.readFileSync(dataFile));
if (data.settings?.pinToTop) widgetWindow.setAlwaysOnTop(true);
} catch(e) {}
widgetWindow.on('close', (event) => {
if (!app.isQuiting) {
event.preventDefault();
widgetWindow.hide();
}
return false;
});
}
function createCategoryWindow() {
if (categoryWindow) {
categoryWindow.show();
categoryWindow.focus();
return;
}
categoryWindow = new BrowserWindow({
width: 350,
height: 220,
icon: path.join(__dirname, 'notepad.png'),
frame: false,
resizable: false,
alwaysOnTop: true,
show: true,
center: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true
}
});
categoryWindow.setMenuBarVisibility(false);
categoryWindow.loadFile('category.html');
categoryWindow.on('closed', () => { categoryWindow = null; });
}
function createTaskWindow(taskId = null) {
editingTaskId = taskId;
if (taskWindow) {
taskWindow.show();
taskWindow.focus();
// If it was already open, we might need to tell it to refresh for a different taskId
taskWindow.webContents.send('set-task-id', taskId);
return;
}
taskWindow = new BrowserWindow({
width: 600,
height: 400,
icon: path.join(__dirname, 'notepad.png'),
frame: false,
resizable: false,
alwaysOnTop: true,
show: true,
center: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true
}
});
taskWindow.setMenuBarVisibility(false);
taskWindow.loadFile('task_form.html');
taskWindow.on('closed', () => {
taskWindow = null;
editingTaskId = null;
});
}
function createTray() {
const iconPath = path.join(__dirname, 'notepad.png');
const icon = nativeImage.createFromPath(iconPath);
tray = new Tray(icon);
tray.setToolTip('TaskNote App');
const contextMenu = Menu.buildFromTemplate([
{ label: 'Open Dashboard', click: () => createDashboardWindow() },
{ label: 'Open Sticky Widget', click: () => createWidgetWindow() },
{ type: 'separator' },
{ label: 'Quit', click: () => { app.isQuiting = true; app.quit(); } }
]);
tray.setContextMenu(contextMenu);
tray.on('click', () => {
createDashboardWindow();
});
}
app.whenReady().then(() => {
createDashboardWindow(); // Open dashboard by default
createWidgetWindow(); // Open widget alongside it
createTray();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createDashboardWindow();
});
setInterval(checkReminders, 10000);
});
// IPC Handlers
ipcMain.handle('get-data', () => {
try { return JSON.parse(fs.readFileSync(dataFile)); }
catch(e) { return { tasks: [], notes: [], settings: {} }; }
});
ipcMain.on('save-data', (event, data) => {
fs.writeFileSync(dataFile, JSON.stringify(data, null, 2));
// Broadcast to all windows
if (dashboardWindow && !dashboardWindow.isDestroyed()) dashboardWindow.webContents.send('data-updated');
if (widgetWindow && !widgetWindow.isDestroyed()) widgetWindow.webContents.send('data-updated');
});
ipcMain.on('open-dashboard', () => createDashboardWindow());
ipcMain.on('open-widget', () => createWidgetWindow());
ipcMain.on('open-category-window', () => createCategoryWindow());
ipcMain.on('close-category-window', () => {
if (categoryWindow) categoryWindow.close();
});
ipcMain.on('open-task-window', (event, taskId) => createTaskWindow(taskId));
ipcMain.on('close-task-window', () => {
if (taskWindow) taskWindow.close();
});
ipcMain.handle('get-editing-task-id', () => editingTaskId);
// Make sure the widget is the one being pinned
ipcMain.on('toggle-pin', (event, pin) => {
if (widgetWindow) widgetWindow.setAlwaysOnTop(pin);
});
// Window controls for the frameless widget
ipcMain.on('open-external', (event, url) => {
shell.openExternal(url);
});
ipcMain.on('window-control', (event, command) => {
if (!widgetWindow) return;
if (command === 'minimize') widgetWindow.minimize();
if (command === 'maximize') {
if (widgetWindow.isMaximized()) widgetWindow.unmaximize();
else widgetWindow.maximize();
}
if (command === 'close') widgetWindow.hide();
});
ipcMain.on('set-theme', (event, theme) => {
if (theme === 'dark' || theme === 'light') {
nativeTheme.themeSource = theme;
} else {
nativeTheme.themeSource = 'system';
}
});
ipcMain.handle('toggle-auto-launch', (event, enable) => {
app.setLoginItemSettings({
openAtLogin: enable,
path: app.getPath('exe'),
args: ['--hidden']
});
return app.getLoginItemSettings().openAtLogin;
});
// Reminders Check
let notifiedTasks = new Set();
function checkReminders() {
try {
const data = JSON.parse(fs.readFileSync(dataFile));
const now = new Date();
data.tasks.forEach(task => {
if (task.done) return;
if (task.customReminder) {
const customDate = new Date(task.customReminder);
const timeDiff = customDate.getTime() - now.getTime();
// Exact expiry
if (timeDiff <= 0 && timeDiff > -60000) {
showNotification(task.id + '_custom', 'Task Due!', `Task: "${task.title}" is due now.`);
}
// Before expiry
if (task.reminderBefore) {
const beforeDiff = task.reminderBefore * 60000;
if (timeDiff <= beforeDiff && timeDiff > beforeDiff - 60000) {
const bDays = Math.floor(task.reminderBefore / 1440);
const bHours = Math.floor((task.reminderBefore % 1440) / 60);
const bMins = task.reminderBefore % 60;
let labelParts = [];
if (bDays > 0) labelParts.push(`${bDays} days`);
if (bHours > 0) labelParts.push(`${bHours} hours`);
if (bMins > 0) labelParts.push(`${bMins} mins`);
const label = labelParts.join(' ');
showNotification(task.id + '_before', 'Upcoming Task!', `Task: "${task.title}" is due in ${label}.`);
}
}
}
if (task.date && task.time) {
const taskDateTime = new Date(`${task.date}T${task.time}`);
const timeDiff = taskDateTime.getTime() - now.getTime();
if (task.reminders.includes('1day') && timeDiff > 0 && timeDiff <= 86400000 && timeDiff > 86340000) {
showNotification(task.id + '_1day', 'Reminder (1 Day)', `Task: ${task.title} is due tomorrow.`);
}
if (task.reminders.includes('1hour') && timeDiff > 0 && timeDiff <= 3600000 && timeDiff > 3540000) {
showNotification(task.id + '_1hour', 'Reminder (1 Hour)', `Task: ${task.title} is due in 1 hour.`);
}
if (timeDiff <= 0 && timeDiff > -60000) {
showNotification(task.id + '_now', 'Task Due Now!', `Task: ${task.title} is due now!`);
}
}
});
} catch (e) { }
}
function showNotification(id, title, body) {
if (notifiedTasks.has(id)) return;
if (Notification.isSupported()) {
const notif = new Notification({ title, body });
notif.show();
notifiedTasks.add(id);
}
}