2026-04-07 03:42:46 -07:00
|
|
|
import { app, globalShortcut, Menu, nativeImage, Tray } from 'electron';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import { getMainWindow, createMainWindow } from './window';
|
|
|
|
|
import { getShortcuts } from './shortcuts';
|
2026-04-18 14:35:14 -07:00
|
|
|
import { trackEvent } from './analytics';
|
2026-04-07 03:42:46 -07:00
|
|
|
|
|
|
|
|
let tray: Tray | null = null;
|
|
|
|
|
let currentShortcut: string | null = null;
|
|
|
|
|
|
|
|
|
|
function getTrayIcon(): nativeImage {
|
|
|
|
|
const iconName = process.platform === 'win32' ? 'icon.ico' : 'icon.png';
|
|
|
|
|
const iconPath = app.isPackaged
|
|
|
|
|
? path.join(process.resourcesPath, 'assets', iconName)
|
|
|
|
|
: path.join(__dirname, '..', 'assets', iconName);
|
|
|
|
|
const img = nativeImage.createFromPath(iconPath);
|
|
|
|
|
return img.resize({ width: 16, height: 16 });
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-18 14:35:14 -07:00
|
|
|
function showMainWindow(source: 'tray_click' | 'tray_menu' | 'shortcut' = 'tray_click'): void {
|
|
|
|
|
const existing = getMainWindow();
|
|
|
|
|
const reopened = !existing || existing.isDestroyed();
|
|
|
|
|
if (reopened) {
|
|
|
|
|
createMainWindow('/dashboard');
|
2026-04-07 03:42:46 -07:00
|
|
|
} else {
|
2026-04-18 14:35:14 -07:00
|
|
|
existing.show();
|
|
|
|
|
existing.focus();
|
2026-04-07 03:42:46 -07:00
|
|
|
}
|
2026-04-18 14:35:14 -07:00
|
|
|
trackEvent('desktop_main_window_shown', { source, reopened });
|
2026-04-07 03:42:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function registerShortcut(accelerator: string): void {
|
|
|
|
|
if (currentShortcut) {
|
|
|
|
|
globalShortcut.unregister(currentShortcut);
|
|
|
|
|
currentShortcut = null;
|
|
|
|
|
}
|
|
|
|
|
if (!accelerator) return;
|
|
|
|
|
try {
|
2026-04-18 14:35:14 -07:00
|
|
|
const ok = globalShortcut.register(accelerator, () => showMainWindow('shortcut'));
|
2026-04-07 03:42:46 -07:00
|
|
|
if (ok) {
|
|
|
|
|
currentShortcut = accelerator;
|
|
|
|
|
} else {
|
|
|
|
|
console.warn(`[tray] Failed to register General Assist shortcut: ${accelerator}`);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`[tray] Error registering General Assist shortcut:`, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createTray(): Promise<void> {
|
|
|
|
|
if (tray) return;
|
|
|
|
|
|
|
|
|
|
tray = new Tray(getTrayIcon());
|
|
|
|
|
tray.setToolTip('SurfSense');
|
|
|
|
|
|
|
|
|
|
const contextMenu = Menu.buildFromTemplate([
|
2026-04-18 14:35:14 -07:00
|
|
|
{ label: 'Open SurfSense', click: () => showMainWindow('tray_menu') },
|
2026-04-07 03:42:46 -07:00
|
|
|
{ type: 'separator' },
|
2026-04-18 14:35:14 -07:00
|
|
|
{
|
|
|
|
|
label: 'Quit',
|
|
|
|
|
click: () => {
|
|
|
|
|
trackEvent('desktop_tray_quit_clicked');
|
|
|
|
|
app.exit(0);
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-04-07 03:42:46 -07:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
tray.setContextMenu(contextMenu);
|
2026-04-18 14:35:14 -07:00
|
|
|
tray.on('double-click', () => showMainWindow('tray_click'));
|
2026-04-07 03:42:46 -07:00
|
|
|
|
|
|
|
|
const shortcuts = await getShortcuts();
|
|
|
|
|
registerShortcut(shortcuts.generalAssist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function reregisterGeneralAssist(): Promise<void> {
|
|
|
|
|
const shortcuts = await getShortcuts();
|
|
|
|
|
registerShortcut(shortcuts.generalAssist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function destroyTray(): void {
|
|
|
|
|
if (currentShortcut) {
|
|
|
|
|
globalShortcut.unregister(currentShortcut);
|
|
|
|
|
currentShortcut = null;
|
|
|
|
|
}
|
|
|
|
|
tray?.destroy();
|
|
|
|
|
tray = null;
|
|
|
|
|
}
|