-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
371 lines (316 loc) · 11.7 KB
/
Copy pathmain.js
File metadata and controls
371 lines (316 loc) · 11.7 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
'use strict';
/**
* Context Bot — Electron main process.
*
* Responsibilities:
* - Application lifecycle, window creation and the system tray.
* - IPC endpoints consumed by the renderer (invoke/handle pattern).
* - Wiring between the snapshot store, the OS window scanner, the app
* launcher and the WebSocket bridge that talks to the Chrome extension.
*/
const {
app,
BrowserWindow,
Menu,
Tray,
nativeImage,
ipcMain,
shell,
} = require('electron');
const path = require('path');
const { ExtensionBridge } = require('./src/extension-bridge');
const { SnapshotStore } = require('./src/snapshot-store');
const { getOsWindowTitles } = require('./src/window-scanner');
const { relaunchApps } = require('./src/app-launcher');
// Only http(s) URLs are ever reopened. This keeps restore from launching
// arbitrary protocol handlers (file:, chrome:, custom app schemes, ...).
const SAFE_URL_PATTERN = /^https?:\/\//i;
// How many snapshots the tray's restore submenu lists.
const TRAY_SNAPSHOT_LIMIT = 8;
const ICON_PATH = path.join(__dirname, 'assets', 'icon.png');
let mainWindow = null;
let tray = null;
let store = null;
// Distinguishes "user closed the window" (hide to tray) from a real quit.
let isQuitting = false;
const bridge = new ExtensionBridge({
onStatusChange: (connected) => {
// Push connection changes to the renderer so the UI badge stays live.
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('extension:status', connected);
}
},
});
// A second instance would fail to bind the WebSocket port and would fight
// over the snapshot file — enforce a single instance instead.
const hasSingleInstanceLock = app.requestSingleInstanceLock();
if (!hasSingleInstanceLock) {
app.quit();
} else {
app.on('second-instance', showMainWindow);
app.whenReady().then(async () => {
store = new SnapshotStore(path.join(app.getPath('userData'), 'snapshots.json'));
setupApplicationMenu();
bridge.start();
registerIpcHandlers();
createWindow();
await setupTray();
initAutoUpdater();
app.on('activate', () => {
// macOS: re-create or reveal the window when the dock icon is clicked.
if (BrowserWindow.getAllWindows().length === 0) createWindow();
else showMainWindow();
});
});
}
function createWindow() {
mainWindow = new BrowserWindow({
width: 1000,
height: 700,
// Windows/macOS take the app icon from the installer bundle; Linux
// needs it set explicitly for the window/taskbar icon.
icon: ICON_PATH,
// Match the UI theme and defer showing until the first paint so the
// window never flashes white on launch.
backgroundColor: '#121212',
show: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: false,
contextIsolation: true,
sandbox: true,
},
});
mainWindow.once('ready-to-show', () => mainWindow.show());
mainWindow.loadFile(path.join(__dirname, 'public', 'index.html'));
// Closing the window keeps the app alive in the tray; quitting happens
// explicitly through the tray menu (or Cmd+Q on macOS).
mainWindow.on('close', (event) => {
if (!isQuitting && tray) {
event.preventDefault();
mainWindow.hide();
}
});
mainWindow.on('closed', () => {
mainWindow = null;
});
}
function showMainWindow() {
if (!mainWindow || mainWindow.isDestroyed()) {
createWindow();
return;
}
if (!mainWindow.isVisible()) mainWindow.show();
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
function setupApplicationMenu() {
if (process.platform === 'darwin') {
// Keep a minimal native menu on macOS so standard shortcuts
// (copy/paste, hide, quit) keep working.
Menu.setApplicationMenu(
Menu.buildFromTemplate([
{ role: 'appMenu' },
{ role: 'editMenu' },
{ role: 'windowMenu' },
]),
);
} else {
// Hide the default File/Edit/View developer menu on Windows/Linux.
Menu.setApplicationMenu(null);
}
}
/* ------------------------------------------------------------------ *
* System tray
* ------------------------------------------------------------------ */
async function setupTray() {
const icon = nativeImage
.createFromPath(ICON_PATH)
.resize({ width: 16, height: 16 });
if (icon.isEmpty()) {
console.warn('[tray] Icon could not be loaded; tray disabled.');
return;
}
tray = new Tray(icon);
tray.setToolTip('Context Bot');
tray.on('click', showMainWindow);
await refreshTrayMenu();
}
/** Rebuilds the tray menu so its restore list reflects the current snapshots. */
async function refreshTrayMenu() {
if (!tray) return;
const snapshots = await store.load();
const recent = [...snapshots].reverse().slice(0, TRAY_SNAPSHOT_LIMIT);
const restoreItems = recent.length
? recent.map((snapshot) => ({
label: snapshot.name,
click: () => restoreSnapshot(snapshot.id),
}))
: [{ label: 'No snapshots yet', enabled: false }];
tray.setContextMenu(
Menu.buildFromTemplate([
{ label: 'Open Context Bot', click: showMainWindow },
{ type: 'separator' },
{ label: 'Quick Snapshot', click: takeQuickSnapshot },
{ label: 'Restore', submenu: restoreItems },
{ type: 'separator' },
{
label: 'Quit',
click: () => {
isQuitting = true;
app.quit();
},
},
]),
);
}
/** Saves a timestamped snapshot straight from the tray, without the UI. */
async function takeQuickSnapshot() {
const osWindows = await getOsWindowTitles();
const name = `Quick snapshot — ${new Date().toLocaleString()}`;
const snapshots = await store.add({
name,
osWindows,
chromeTabs: bridge.getTabs(),
});
notifyRenderer(snapshots);
await refreshTrayMenu();
}
/** Pushes a fresh snapshot list to the renderer after a tray-driven change. */
function notifyRenderer(snapshots) {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('snapshots:changed', snapshots);
}
}
/* ------------------------------------------------------------------ *
* Restore
* ------------------------------------------------------------------ */
/**
* Groups a snapshot's tabs by the browser window they came from, so the
* extension can recreate the original window layout.
* @returns {string[][]} Groups of safe URLs, one per original window.
*/
function groupTabsByWindow(chromeTabs = []) {
const groups = new Map();
for (const tab of chromeTabs) {
if (!SAFE_URL_PATTERN.test(tab.url || '')) continue;
// Tabs saved before window tracking existed share a single group.
const key = Number.isInteger(tab.windowId) ? tab.windowId : 'legacy';
if (!groups.has(key)) groups.set(key, []);
groups.get(key).push(tab.url);
}
return [...groups.values()];
}
/**
* Restores a snapshot: browser tabs first (via the extension when connected,
* otherwise the default browser), then any known desktop applications.
* @returns {Promise<{restored: number, method: string, apps: string[]}>}
*/
async function restoreSnapshot(id) {
const snapshot = await store.get(id);
if (!snapshot) {
return { restored: 0, method: 'none', apps: [] };
}
const windows = groupTabsByWindow(snapshot.chromeTabs);
const urls = windows.flat();
let method = 'none';
if (urls.length > 0) {
if (bridge.requestOpenTabs(windows)) {
method = 'extension';
} else {
await Promise.all(urls.map((url) => shell.openExternal(url)));
method = 'shell';
}
}
const apps = await relaunchApps(snapshot.osWindows);
console.log(
`[main] Restored "${snapshot.name}": ${urls.length} tabs (${method}), ` +
`${apps.length} apps`,
);
return { restored: urls.length, method, apps };
}
/* ------------------------------------------------------------------ *
* IPC
* ------------------------------------------------------------------ */
function registerIpcHandlers() {
/** Returns all persisted snapshots. */
ipcMain.handle('snapshots:load', () => store.load());
/**
* Scans OS windows and combines them with the live Chrome tab list.
* Returns { osWindows: string[], chromeTabs: [{title, url, windowId}] }.
*/
ipcMain.handle('windows:scan', async () => {
const osWindows = await getOsWindowTitles();
return { osWindows, chromeTabs: bridge.getTabs() };
});
/**
* Saves a new snapshot. Chrome tabs (with URLs) are captured from the
* bridge at save time; OS window titles come from the renderer's last
* scan so the list the user saw is exactly what gets saved.
*/
ipcMain.handle('snapshots:save', async (_event, { name, osWindows = [] } = {}) => {
const snapshots = await store.add({
name,
osWindows,
chromeTabs: bridge.getTabs(),
});
await refreshTrayMenu();
return snapshots;
});
/** Re-captures an existing snapshot with the current workspace state. */
ipcMain.handle('snapshots:update', async (_event, { id, osWindows = [] } = {}) => {
const snapshots = await store.update(id, {
osWindows,
chromeTabs: bridge.getTabs(),
});
await refreshTrayMenu();
return snapshots;
});
/** Renames a snapshot. */
ipcMain.handle('snapshots:rename', async (_event, { id, name } = {}) => {
const snapshots = await store.rename(id, name);
await refreshTrayMenu();
return snapshots;
});
/** Deletes a snapshot and returns the updated list. */
ipcMain.handle('snapshots:delete', async (_event, id) => {
const snapshots = await store.remove(id);
await refreshTrayMenu();
return snapshots;
});
/** Restores a snapshot's tabs and known applications. */
ipcMain.handle('snapshots:restore', (_event, id) => restoreSnapshot(id));
/** Lets the renderer render the correct badge on first paint. */
ipcMain.handle('extension:is-connected', () => bridge.isConnected());
}
/* ------------------------------------------------------------------ *
* Updates and lifecycle
* ------------------------------------------------------------------ */
function initAutoUpdater() {
// Auto-update only makes sense for packaged builds, and unsigned macOS
// builds cannot apply updates (Squirrel.Mac requires a valid code
// signature), so it is limited to Windows and Linux for now.
if (!app.isPackaged || process.platform === 'darwin') return;
let autoUpdater;
try {
({ autoUpdater } = require('electron-updater'));
} catch (err) {
console.warn('[updater] electron-updater not available:', err.message);
return;
}
autoUpdater.on('error', (err) => {
// Update failures must never break the app — log and move on.
console.error('[updater] Update check failed:', err.message);
});
autoUpdater.checkForUpdatesAndNotify();
}
app.on('before-quit', () => {
isQuitting = true;
});
app.on('window-all-closed', () => {
// With a tray icon the app intentionally keeps running in the background.
if (process.platform !== 'darwin' && !tray) app.quit();
});
app.on('will-quit', () => {
bridge.stop();
});