2026-03-20 20:06:21 +02:00
|
|
|
import { app, BrowserWindow } from 'electron';
|
2026-04-07 03:42:46 -07:00
|
|
|
|
|
|
|
|
let isQuitting = false;
|
2026-03-20 19:44:48 +02:00
|
|
|
import { registerGlobalErrorHandlers, showErrorDialog } from './modules/errors';
|
2026-03-20 19:55:44 +02:00
|
|
|
import { startNextServer } from './modules/server';
|
2026-04-07 03:42:46 -07:00
|
|
|
import { createMainWindow, getMainWindow } from './modules/window';
|
2026-03-20 19:55:44 +02:00
|
|
|
import { setupDeepLinks, handlePendingDeepLink } from './modules/deep-links';
|
2026-03-20 19:59:20 +02:00
|
|
|
import { setupAutoUpdater } from './modules/auto-updater';
|
2026-03-20 20:01:13 +02:00
|
|
|
import { setupMenu } from './modules/menu';
|
2026-03-24 19:24:41 +02:00
|
|
|
import { registerQuickAsk, unregisterQuickAsk } from './modules/quick-ask';
|
2026-04-02 14:29:12 +02:00
|
|
|
import { registerAutocomplete, unregisterAutocomplete } from './modules/autocomplete';
|
2026-04-02 11:17:49 +05:30
|
|
|
import { registerFolderWatcher, unregisterFolderWatcher } from './modules/folder-watcher';
|
2026-03-20 20:06:21 +02:00
|
|
|
import { registerIpcHandlers } from './ipc/handlers';
|
2026-04-07 03:42:46 -07:00
|
|
|
import { createTray, destroyTray } from './modules/tray';
|
2026-04-07 20:18:42 +02:00
|
|
|
import { initAnalytics, shutdownAnalytics, trackEvent } from './modules/analytics';
|
2026-03-17 16:22:14 +02:00
|
|
|
|
2026-03-20 19:44:48 +02:00
|
|
|
registerGlobalErrorHandlers();
|
2026-03-18 19:49:50 +02:00
|
|
|
|
2026-03-20 19:55:44 +02:00
|
|
|
if (!setupDeepLinks()) {
|
|
|
|
|
app.quit();
|
|
|
|
|
}
|
2026-03-17 16:22:14 +02:00
|
|
|
|
2026-03-20 20:06:21 +02:00
|
|
|
registerIpcHandlers();
|
2026-03-17 16:22:14 +02:00
|
|
|
|
|
|
|
|
app.whenReady().then(async () => {
|
2026-04-07 20:18:42 +02:00
|
|
|
initAnalytics();
|
|
|
|
|
trackEvent('desktop_app_launched');
|
2026-03-18 20:10:30 +02:00
|
|
|
setupMenu();
|
2026-03-18 19:49:59 +02:00
|
|
|
try {
|
|
|
|
|
await startNextServer();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
showErrorDialog('Failed to start SurfSense', error);
|
|
|
|
|
setTimeout(() => app.quit(), 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-04-02 13:44:57 +02:00
|
|
|
|
2026-04-07 03:42:46 -07:00
|
|
|
await createTray();
|
|
|
|
|
|
|
|
|
|
const win = createMainWindow('/dashboard');
|
|
|
|
|
|
|
|
|
|
// Minimize to tray instead of closing the app
|
|
|
|
|
win.on('close', (e) => {
|
|
|
|
|
if (!isQuitting) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
win.hide();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-07 00:43:40 -07:00
|
|
|
await registerQuickAsk();
|
|
|
|
|
await registerAutocomplete();
|
2026-04-02 11:17:49 +05:30
|
|
|
registerFolderWatcher();
|
2026-03-19 20:20:26 +02:00
|
|
|
setupAutoUpdater();
|
2026-03-17 16:22:14 +02:00
|
|
|
|
2026-03-20 19:55:44 +02:00
|
|
|
handlePendingDeepLink();
|
2026-03-17 17:32:28 +02:00
|
|
|
|
2026-03-17 16:22:14 +02:00
|
|
|
app.on('activate', () => {
|
2026-04-07 03:42:46 -07:00
|
|
|
const mw = getMainWindow();
|
|
|
|
|
if (!mw || mw.isDestroyed()) {
|
2026-04-03 19:57:48 +02:00
|
|
|
createMainWindow('/dashboard');
|
2026-04-07 03:42:46 -07:00
|
|
|
} else {
|
|
|
|
|
mw.show();
|
|
|
|
|
mw.focus();
|
2026-03-17 16:22:14 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-07 03:42:46 -07:00
|
|
|
// Keep running in the background — the tray "Quit" calls app.exit()
|
2026-03-17 16:22:14 +02:00
|
|
|
app.on('window-all-closed', () => {
|
2026-04-07 03:42:46 -07:00
|
|
|
// Do nothing: the app stays alive in the tray
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.on('before-quit', () => {
|
|
|
|
|
isQuitting = true;
|
2026-03-17 16:22:14 +02:00
|
|
|
});
|
|
|
|
|
|
2026-04-07 20:18:42 +02:00
|
|
|
let didCleanup = false;
|
|
|
|
|
app.on('will-quit', async (e) => {
|
|
|
|
|
if (didCleanup) return;
|
|
|
|
|
didCleanup = true;
|
|
|
|
|
e.preventDefault();
|
2026-03-24 19:24:41 +02:00
|
|
|
unregisterQuickAsk();
|
2026-04-02 14:29:12 +02:00
|
|
|
unregisterAutocomplete();
|
2026-04-02 11:17:49 +05:30
|
|
|
unregisterFolderWatcher();
|
2026-04-07 03:42:46 -07:00
|
|
|
destroyTray();
|
2026-04-07 20:18:42 +02:00
|
|
|
await shutdownAnalytics();
|
|
|
|
|
app.exit();
|
2026-03-17 16:22:14 +02:00
|
|
|
});
|