feat(desktop): tray + lancement au login + auto-update (C5)
tray.ts : icône de barre système (Ouvrir, bascule « Launch at login », Quitter). autostart.ts : login items (Windows/macOS) + fichier ~/.config/autostart/*.desktop (Linux). updater.ts : electron-updater (provider generic -> release Gitea), no-op en dev, actif Windows/Linux. main.ts : fermeture de fenêtre = réduction dans le tray, quit explicite via tray/menu, arrêt propre du daemon avant sortie. icon.png ajouté aux extraResources (tray en packagé). Vérifié : typecheck contre l'API Electron + electron-updater, bundle esbuild.
This commit is contained in:
44
packages/desktop/src/main/autostart.ts
Normal file
44
packages/desktop/src/main/autostart.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { app } from 'electron';
|
||||
import { existsSync, mkdirSync, unlinkSync, writeFileSync } from 'node:fs';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { homedir } from 'node:os';
|
||||
|
||||
// Lancement au login. Windows/macOS : API Electron (login items). Linux : fichier .desktop dans
|
||||
// ~/.config/autostart (pas d'API Electron fiable pour l'autostart Linux).
|
||||
|
||||
function desktopFile(): string {
|
||||
return join(homedir(), '.config', 'autostart', 'arboretum.desktop');
|
||||
}
|
||||
|
||||
function desktopEntry(): string {
|
||||
const exec = process.env.APPIMAGE ?? app.getPath('exe');
|
||||
return `[Desktop Entry]
|
||||
Type=Application
|
||||
Name=Arboretum
|
||||
Exec=${exec}
|
||||
Terminal=false
|
||||
X-GNOME-Autostart-enabled=true
|
||||
`;
|
||||
}
|
||||
|
||||
export function isAutoStartEnabled(): boolean {
|
||||
if (process.platform === 'linux') return existsSync(desktopFile());
|
||||
return app.getLoginItemSettings().openAtLogin;
|
||||
}
|
||||
|
||||
export function setAutoStart(enabled: boolean): void {
|
||||
if (process.platform === 'linux') {
|
||||
if (enabled) {
|
||||
mkdirSync(dirname(desktopFile()), { recursive: true });
|
||||
writeFileSync(desktopFile(), desktopEntry());
|
||||
} else {
|
||||
try {
|
||||
unlinkSync(desktopFile());
|
||||
} catch {
|
||||
/* déjà absent */
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
app.setLoginItemSettings({ openAtLogin: enabled });
|
||||
}
|
||||
@@ -1,25 +1,25 @@
|
||||
import { app, BrowserWindow, session, shell, type BrowserWindowConstructorOptions } from 'electron';
|
||||
import { app, BrowserWindow, session, shell, type BrowserWindowConstructorOptions, type Tray } from 'electron';
|
||||
import { join } from 'node:path';
|
||||
import { startDaemon, type DaemonHandle } from './daemon';
|
||||
import { seedSessionCookie } from './auth';
|
||||
import { loadWindowState, saveWindowState } from './window-state';
|
||||
import { createTray } from './tray';
|
||||
import { initUpdater } from './updater';
|
||||
|
||||
const PARTITION = 'persist:arboretum';
|
||||
const PORT = 7317;
|
||||
|
||||
let daemon: DaemonHandle | null = null;
|
||||
let win: BrowserWindow | null = null;
|
||||
let tray: Tray | null = null;
|
||||
let isQuitting = false;
|
||||
let shuttingDown = false;
|
||||
|
||||
// Instance unique : deux instances = deux daemons/ports en conflit.
|
||||
if (!app.requestSingleInstanceLock()) {
|
||||
app.quit();
|
||||
} else {
|
||||
app.on('second-instance', () => {
|
||||
if (!win) return;
|
||||
if (win.isMinimized()) win.restore();
|
||||
win.focus();
|
||||
});
|
||||
app.on('second-instance', showWindow);
|
||||
app.whenReady().then(bootstrap).catch((err: unknown) => {
|
||||
console.error('[arboretum-desktop] bootstrap failed:', err);
|
||||
app.quit();
|
||||
@@ -31,6 +31,20 @@ async function bootstrap(): Promise<void> {
|
||||
daemon = await startDaemon({ dataDir, port: PORT, onLog: (l) => process.stdout.write(l) });
|
||||
await seedSessionCookie(PARTITION, daemon.url, daemon.token);
|
||||
createWindow(daemon.url);
|
||||
tray = createTray({ show: showWindow, quit: quitApp });
|
||||
initUpdater();
|
||||
}
|
||||
|
||||
function showWindow(): void {
|
||||
if (!win) return;
|
||||
if (win.isMinimized()) win.restore();
|
||||
win.show();
|
||||
win.focus();
|
||||
}
|
||||
|
||||
function quitApp(): void {
|
||||
isQuitting = true;
|
||||
app.quit();
|
||||
}
|
||||
|
||||
function createWindow(url: string): void {
|
||||
@@ -73,7 +87,16 @@ function createWindow(url: string): void {
|
||||
const b = win.getBounds();
|
||||
saveWindowState({ width: b.width, height: b.height, x: b.x, y: b.y, maximized: win.isMaximized() });
|
||||
};
|
||||
win.on('close', persist);
|
||||
|
||||
// Fermer la fenêtre = réduire dans le tray (l'app continue en arrière-plan) ; quitter réellement
|
||||
// se fait via le menu du tray ou le menu applicatif.
|
||||
win.on('close', (e) => {
|
||||
persist();
|
||||
if (!isQuitting) {
|
||||
e.preventDefault();
|
||||
win?.hide();
|
||||
}
|
||||
});
|
||||
win.on('closed', () => {
|
||||
win = null;
|
||||
});
|
||||
@@ -81,9 +104,9 @@ function createWindow(url: string): void {
|
||||
void win.loadURL(`${url}/`);
|
||||
}
|
||||
|
||||
app.on('window-all-closed', () => void shutdown());
|
||||
app.on('before-quit', (e) => {
|
||||
// Ne pas quitter avant l'arrêt propre du daemon enfant.
|
||||
isQuitting = true;
|
||||
if (daemon && !shuttingDown) {
|
||||
e.preventDefault();
|
||||
void shutdown();
|
||||
@@ -97,6 +120,8 @@ async function shutdown(): Promise<void> {
|
||||
await daemon?.stop();
|
||||
} finally {
|
||||
daemon = null;
|
||||
tray?.destroy();
|
||||
tray = null;
|
||||
app.quit();
|
||||
}
|
||||
}
|
||||
|
||||
40
packages/desktop/src/main/tray.ts
Normal file
40
packages/desktop/src/main/tray.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { app, Menu, Tray, nativeImage } from 'electron';
|
||||
import { join } from 'node:path';
|
||||
import { isAutoStartEnabled, setAutoStart } from './autostart';
|
||||
|
||||
function iconPath(): string {
|
||||
return app.isPackaged
|
||||
? join(process.resourcesPath, 'icon.png')
|
||||
: join(__dirname, '..', 'resources', 'icon.png');
|
||||
}
|
||||
|
||||
/** Icône de barre système : ouvrir la fenêtre, basculer le lancement au login, quitter. */
|
||||
export function createTray(opts: { show: () => void; quit: () => void }): Tray {
|
||||
const image = nativeImage.createFromPath(iconPath());
|
||||
const tray = new Tray(image.isEmpty() ? nativeImage.createEmpty() : image.resize({ width: 18, height: 18 }));
|
||||
tray.setToolTip('Arboretum');
|
||||
|
||||
const buildMenu = (): void => {
|
||||
tray.setContextMenu(
|
||||
Menu.buildFromTemplate([
|
||||
{ label: 'Open Arboretum', click: opts.show },
|
||||
{ type: 'separator' },
|
||||
{
|
||||
label: 'Launch at login',
|
||||
type: 'checkbox',
|
||||
checked: isAutoStartEnabled(),
|
||||
click: (item) => {
|
||||
setAutoStart(item.checked);
|
||||
buildMenu();
|
||||
},
|
||||
},
|
||||
{ type: 'separator' },
|
||||
{ label: 'Quit', click: opts.quit },
|
||||
]),
|
||||
);
|
||||
};
|
||||
|
||||
buildMenu();
|
||||
tray.on('click', opts.show);
|
||||
return tray;
|
||||
}
|
||||
13
packages/desktop/src/main/updater.ts
Normal file
13
packages/desktop/src/main/updater.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { app } from 'electron';
|
||||
import { autoUpdater } from 'electron-updater';
|
||||
|
||||
// Vérifie les mises à jour (provider generic -> assets de release Gitea, cf. electron-builder.yml).
|
||||
// No-op en dev (pas de app-update.yml). Auto-update effectif : Windows (nsis) + Linux (AppImage) ;
|
||||
// macOS reste manuel tant que l'app n'est pas signée.
|
||||
export function initUpdater(): void {
|
||||
if (!app.isPackaged) return;
|
||||
autoUpdater.autoDownload = true;
|
||||
void autoUpdater.checkForUpdatesAndNotify().catch(() => {
|
||||
/* hors ligne / pas de release publiée : silencieux */
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user