-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
501 lines (450 loc) · 16.4 KB
/
main.js
File metadata and controls
501 lines (450 loc) · 16.4 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
const electron = require('electron');
const path = require('path');
const fs = require('fs');
const { ElectronChromeExtensions } = require('electron-chrome-extensions');
const Store = require('electron-store').default;
console.log('Electron version:', process.versions.electron); // Diagnostic log
const store = new Store();
let mainWindow;
let tray;
let alwaysOnTop = store.get('alwaysOnTop', false); // Persisted always-on-top state
let showTitleBar = store.get('showTitleBar', true); // Persisted show-title-bar state, enabled by default
let stayHidden = false; // Flag to prevent auto re-show
let reShowTimeoutId = null; // Track the current re-show timer
let extensions;
const DEFAULT_WINDOW_WIDTH = 300;
const DEFAULT_WINDOW_HEIGHT = 500; // Swapped to DEFAULT_ prefix for clarity
function createWindow() {
// Load persisted window bounds
const savedBounds = store.get('windowBounds', {});
mainWindow = new electron.BrowserWindow({
width: savedBounds.width || DEFAULT_WINDOW_WIDTH,
height: savedBounds.height || DEFAULT_WINDOW_HEIGHT,
x: typeof savedBounds.x === 'number' ? savedBounds.x : undefined,
y: typeof savedBounds.y === 'number' ? savedBounds.y : undefined,
minWidth: 200,
minHeight: 300,
resizable: true,
show: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
},
icon: getIconPath(),
autoHideMenuBar: true, // hide menu bar by default
alwaysOnTop: alwaysOnTop, // initialize with persisted state
skipTaskbar: true, // do not show in taskbar
frame: showTitleBar, // control title bar visibility
});
// Save window bounds on move/resize
mainWindow.on('move', () => {
const bounds = mainWindow.getBounds();
store.set('windowBounds', bounds);
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.send('window-moved-or-resized');
}
});
mainWindow.on('resize', () => {
const bounds = mainWindow.getBounds();
store.set('windowBounds', bounds);
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.send('window-moved-or-resized');
}
});
// Get stored URL or use default
const defaultUrl = 'https://keep.google.com/u/0/';
const keepUrl = store.get('keepUrl', defaultUrl);
console.log('Loading URL on window creation:', keepUrl);
mainWindow.loadURL(keepUrl);
mainWindow.webContents.on('did-finish-load', () => {});
mainWindow.on('close', (e) => {
// Hide window instead of closing (app stays in tray)
if (!electron.app.isQuiting) {
e.preventDefault();
mainWindow.hide();
startReShowTimer();
}
return false;
});
// Hide menu bar if not already hidden
mainWindow.setMenuBarVisibility(false);
}
function getIconPath() {
// Use a default icon if none exists
const iconIco = path.join(__dirname, 'icon.ico');
if (fs.existsSync(iconIco)) return iconIco;
const iconPng = path.join(__dirname, 'icon.png');
if (fs.existsSync(iconPng)) return iconPng;
return undefined; // fallback to Electron default
}
function startReShowTimer() {
if (reShowTimeoutId) clearTimeout(reShowTimeoutId);
const timerMinutes = store.get('reShowTimer', 15);
console.log(`Scheduling re-show in ${timerMinutes} minutes`);
reShowTimeoutId = setTimeout(() => {
if (stayHidden) {
console.log('Stay Hidden is enabled, looping timer...');
startReShowTimer();
} else {
console.log('Re-showing hidden memo...');
if (mainWindow) {
mainWindow.showInactive();
}
reShowTimeoutId = null;
}
}, timerMinutes * 60 * 1000);
}
function toggleWindow() {
if (mainWindow.isVisible()) {
mainWindow.hide();
startReShowTimer();
} else {
mainWindow.showInactive();
if (reShowTimeoutId) {
clearTimeout(reShowTimeoutId);
reShowTimeoutId = null;
}
}
updateTrayMenu();
}
function createTray() {
const icon = getIconPath();
tray = new electron.Tray(icon || undefined);
tray.setToolTip('Google Keep Memo Pad');
tray.on('click', () => {
toggleWindow();
});
updateTrayMenu();
}
function updateTrayMenu() {
if (!tray) return;
const isVisible = mainWindow && mainWindow.isVisible();
const contextMenu = electron.Menu.buildFromTemplate([
{
label: isVisible ? 'Hide Memo' : 'Show Memo',
click: () => {
toggleWindow();
},
},
...(!isVisible ? [{
label: 'Stay Hidden',
type: 'checkbox',
checked: stayHidden,
click: () => {
stayHidden = !stayHidden;
updateTrayMenu();
},
}] : []),
{
label: 'Always on Top',
type: 'checkbox',
checked: alwaysOnTop,
click: () => {
alwaysOnTop = !alwaysOnTop;
store.set('alwaysOnTop', alwaysOnTop);
if (mainWindow) mainWindow.setAlwaysOnTop(alwaysOnTop);
updateTrayMenu();
},
},
{
label: 'Show Title Bar',
type: 'checkbox',
checked: showTitleBar,
click: () => {
showTitleBar = !showTitleBar;
store.set('showTitleBar', showTitleBar);
if (mainWindow) {
// Recreate window to apply frame change
const currentBounds = mainWindow.getBounds();
mainWindow.close();
mainWindow = null; // Clear reference to old window
createWindow();
mainWindow.setBounds(currentBounds);
mainWindow.showInactive();
}
updateTrayMenu();
},
},
{
label: 'Settings',
click: async () => {
try {
// Get the current URL from the store
const defaultUrl = 'https://keep.google.com/u/0/';
const currentUrl = store.get('keepUrl', defaultUrl);
console.log('Current URL from store:', currentUrl);
// Create a custom input dialog using BrowserWindow with proper preload script
const inputWindow = new electron.BrowserWindow({
parent: mainWindow,
modal: true,
width: 500,
height: 260,
minimizable: false,
maximizable: false,
resizable: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'url-dialog-preload.js')
},
autoHideMenuBar: true,
title: 'Settings',
});
// Create HTML content for the input dialog
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<title>Settings</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
margin: 0;
padding: 20px;
color: #333;
display: flex;
flex-direction: column;
justify-content: center;
height: 100vh;
box-sizing: border-box;
}
.container {
display: flex;
flex-direction: column;
gap: 16px;
}
.setting-group {
display: flex;
flex-direction: column;
}
label {
margin-bottom: 8px;
font-weight: 500;
}
input {
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 14px;
width: 100%;
box-sizing: border-box;
}
.button-row {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
}
.button-row button {
flex: 0 0 auto;
}
.current-page {
margin-right: auto;
background-color: #f5f5f5;
border: 1px solid #ddd;
}
.current-page:hover {
background-color: #e8e8e8;
}
.cancel {
background-color: #e0e0e0;
}
.save {
background-color: #2196F3;
color: white;
}
button {
padding: 8px 16px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
white-space: nowrap;
}
</style>
</head>
<body>
<div class="container">
<div class="setting-group">
<label for="urlInput">Default URL (leave blank to reset):</label>
<input type="url" id="urlInput" value="${currentUrl}" placeholder="https://keep.google.com/u/0/#LIST/..." />
</div>
<div class="setting-group">
<label for="timerInput">Automatically un-hide memo after (minutes):</label>
<input type="number" id="timerInput" value="${store.get('reShowTimer', 15)}" min="1" />
</div>
<div class="button-row">
<button class="current-page" onclick="useCurrentPage()">Use Current Page</button>
<button class="cancel" onclick="window.electronAPI.cancel()">Cancel</button>
<button class="save" onclick="window.electronAPI.saveSettings({
url: document.getElementById('urlInput').value,
timer: document.getElementById('timerInput').value
})">Save</button>
</div>
</div>
<script>
async function useCurrentPage() {
try {
const currentUrl = await window.electronAPI.getCurrentPageUrl();
if (currentUrl) {
document.getElementById('urlInput').value = currentUrl;
}
} catch (error) {
console.error('Error getting current page URL:', error);
}
}
</script>
</body>
</html>
`;
// Create a variable to store the result
let result = null;
let userCancelled = false;
// Set up IPC handlers
const { ipcMain } = electron;
// Handler for settings selection
ipcMain.once('settings-saved', (event, settings) => {
console.log('Settings received via IPC:', settings);
result = settings;
inputWindow.close();
});
// Handler for cancellation
ipcMain.once('url-dialog-cancelled', () => {
console.log('Dialog cancelled via IPC');
userCancelled = true;
inputWindow.close();
});
// Handler for getting current page URL
ipcMain.once('get-current-page-url', (event) => {
try {
const currentPageUrl = mainWindow.webContents.getURL();
console.log('Current page URL requested:', currentPageUrl);
event.sender.send('current-page-url-response', currentPageUrl);
} catch (error) {
console.error('Error getting current page URL:', error);
event.sender.send('current-page-url-response', '');
}
});
// Load the HTML content
inputWindow.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(htmlContent)}`);
// Wait for the window to close
await new Promise(resolve => {
inputWindow.on('closed', resolve);
});
// Clean up IPC handlers
ipcMain.removeAllListeners('url-selected');
ipcMain.removeAllListeners('url-dialog-cancelled');
ipcMain.removeAllListeners('get-current-page-url');
// If user cancelled or closed the window without selecting settings
if (userCancelled || result === null) {
console.log('User cancelled or closed the dialog without selecting settings');
return; // Exit without making changes
}
const { url, timer } = result;
// 1. Process Timer
if (timer && !isNaN(parseInt(timer))) {
const newTimer = parseInt(timer);
console.log('Saving re-show timer to store:', newTimer);
store.set('reShowTimer', newTimer);
// If window is hidden, restart timer to apply new setting
if (mainWindow && !mainWindow.isVisible()) {
startReShowTimer();
}
}
// 2. Process URL
if (url.trim() === '') {
console.log('Resetting URL to default');
store.delete('keepUrl');
// Verify the URL was deleted correctly
const checkUrl = store.get('keepUrl', 'DEFAULT_NOT_SET');
console.log('URL after reset (should be DEFAULT_NOT_SET):', checkUrl);
// Reload the window with the default URL immediately
console.log('Reloading window with default URL:', defaultUrl);
mainWindow.loadURL(defaultUrl);
} else {
// Basic URL validation
try {
new URL(url); // Check if it's a valid URL format
// Save the URL to the store
console.log('Saving URL to store:', url);
store.set('keepUrl', url);
// Verify the URL was saved correctly
const savedUrl = store.get('keepUrl');
console.log('URL retrieved from store after saving:', savedUrl);
// Reload the window with the new URL immediately
console.log('Reloading window with URL:', url);
mainWindow.loadURL(url);
} catch (e) {
console.error('Invalid URL:', e);
electron.dialog.showErrorBox('Invalid URL', 'The URL you entered is not valid. Please try again.');
}
}
} catch (error) {
console.error('Error in Set Default Note URL click handler:', error);
electron.dialog.showErrorBox('Error', 'Could not set default URL. Please check the console for details.');
}
},
},
{ type: 'separator' },
{
label: 'Quit',
click: () => {
electron.app.isQuiting = true;
electron.app.quit();
},
},
]);
tray.setContextMenu(contextMenu);
}
electron.app.on('ready', async () => {
// Initialize ElectronChromeExtensions with required license
extensions = new ElectronChromeExtensions({ license: "GPL-3.0" });
// Load the unpacked Chrome extension
const extPath = path.join(__dirname, 'chrome-google-keep-full-screen');
try {
const loadedExt = await (mainWindow ? mainWindow.webContents.session : electron.session.defaultSession).loadExtension(extPath, { allowFileAccess: true });
console.log('Loaded extension:', loadedExt);
} catch (err) {
console.error('Failed to load extension:', err);
}
createWindow();
createTray();
// Open DevTools for debugging
if (mainWindow) {
// mainWindow.webContents.openDevTools();
}
if (mainWindow) {
mainWindow.once('ready-to-show', () => {
mainWindow.showInactive(); // Show window at startup
});
}
});
electron.app.on('window-all-closed', (e) => {
// Don't quit app when all windows are closed (keep in tray)
e.preventDefault();
});
electron.app.on('activate', () => {
if (mainWindow) mainWindow.showInactive();
});
const gotTheLock = electron.app.requestSingleInstanceLock();
if (!gotTheLock) {
electron.app.quit();
}
electron.app.on('web-contents-created', (event, contents) => {
contents.setWindowOpenHandler(({ url }) => {
electron.shell.openExternal(url);
return { action: 'deny' };
});
});
// --- IPC handlers for drag region tracking ---
const { ipcMain, screen } = electron;
ipcMain.handle('get-cursor-position', () => {
// Returns { x, y } in screen coordinates
return screen.getCursorScreenPoint();
});
ipcMain.handle('get-window-bounds', () => {
if (!mainWindow) return null;
// Returns { x, y, width, height }
return mainWindow.getBounds();
});