Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(desktop): address PR review feedback
Elian Doran0789f02cd67c
diff --git a/apps/client/src/services/utils.spec.ts b/apps/client/src/services/utils.spec.tsindex 695489201a..97108ba357 100644--- a/apps/client/src/services/utils.spec.ts+++ b/apps/client/src/services/utils.spec.ts@@ -351,7 +351,7 @@ describe("electron-aware helpers", () => {it("reloadTray invokes electron tray when present and is a no-op otherwise", () => {const reloadTray = vi.fn();- (window as any).electronApi = { tray: { reloadTray } };+ (window as any).electronApi = { systemIntegration: { reloadTray } };utils.reloadTray();expect(reloadTray).toHaveBeenCalled();@@ -359,6 +359,16 @@ describe("electron-aware helpers", () => {expect(() => utils.reloadTray()).not.toThrow();});+ it("reapplyLaunchOnStartup invokes electron systemIntegration when present and is a no-op otherwise", () => {+ const reapplyLaunchOnStartup = vi.fn();+ (window as any).electronApi = { systemIntegration: { reapplyLaunchOnStartup } };+ utils.reapplyLaunchOnStartup();+ expect(reapplyLaunchOnStartup).toHaveBeenCalled();++ delete (window as any).electronApi;+ expect(() => utils.reapplyLaunchOnStartup()).not.toThrow();+ });+it("clearBrowserCache calls electron clearCache when available", async () => {const clearCache = vi.fn(async () => {});(window as any).electronApi = { window: { clearCache } };diff --git a/apps/desktop/src/main.spec.ts b/apps/desktop/src/main.spec.tsindex 90b025dfd9..9de1e4cefd 100644--- a/apps/desktop/src/main.spec.ts+++ b/apps/desktop/src/main.spec.ts@@ -67,6 +67,7 @@ const h = vi.hoisted(() => ({applyLaunchOnStartup: vi.fn(),wasLaunchedHidden: vi.fn(() => false),disableTray: false,+ mainWindow: null as FakeWindow | null,unregisterAll: vi.fn(),// Controllable server start so tests can simulate a slow/hanging server.startServer: (() => Promise.resolve({})) as () => Promise<unknown>@@ -171,6 +172,7 @@ vi.mock("./services/request", () => ({ default: class {} }));vi.mock("./services/window", () => ({default: {getLastFocusedWindow: () => h.lastFocusedWindow,+ getMainWindow: () => h.mainWindow,createExtraWindow: (...a: unknown[]) => h.createExtraWindow(...a),createMainWindow: (...a: unknown[]) => h.createMainWindow(...a),createSetupWindow: (...a: unknown[]) => h.createSetupWindow(...a),@@ -223,6 +225,7 @@ function resetState() {h.isDbInitialized = true;h.securitySettings = {};h.lastFocusedWindow = null;+ h.mainWindow = null;h.entityChangeIds = [];h.locale = null;h.formattingLocale = null;@@ -458,6 +461,15 @@ describe("app event handlers", () => {expect(h.createMainWindow).toHaveBeenCalledTimes(2);expect(show).toHaveBeenCalled();expect(focus).toHaveBeenCalled();++ // Hidden-on-autostart window was never focused → fall back to the main window.+ const mainShow = vi.fn();+ const mainFocus = vi.fn();+ h.lastFocusedWindow = null;+ h.mainWindow = { isMinimized: () => false, restore: vi.fn(), show: mainShow, focus: mainFocus };+ await activate?.();+ expect(mainShow).toHaveBeenCalled();+ expect(mainFocus).toHaveBeenCalled();});it("does not register activate on non-darwin even when DB is initialized", async () => {diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.tsindex f5e63833cd..bf61ef5921 100644--- a/apps/desktop/src/main.ts+++ b/apps/desktop/src/main.ts@@ -279,9 +279,10 @@ async function onReady() {if (BrowserWindow.getAllWindows().length === 0) {await windowService.createMainWindow();} else {- // Close-to-tray may have hidden the window rather than destroying- // it, so a dock-icon click must reveal the existing window.- const win = windowService.getLastFocusedWindow();+ // Close-to-tray, or hide-on-autostart, may have left a hidden+ // window that was never focused, so fall back to the main window+ // to reveal it on a dock-icon click.+ const win = windowService.getLastFocusedWindow() ?? windowService.getMainWindow();win?.show();win?.focus();}diff --git a/apps/desktop/src/services/auto_launch.spec.ts b/apps/desktop/src/services/auto_launch.spec.tsindex b06357f002..fe5c8ae672 100644--- a/apps/desktop/src/services/auto_launch.spec.ts+++ b/apps/desktop/src/services/auto_launch.spec.ts@@ -80,12 +80,14 @@ beforeEach(() => {state.wasOpenedAsHidden = false;state.ipcOn.clear();delete process.env.APPIMAGE;+ delete process.env.XDG_CONFIG_HOME;});afterEach(() => {setPlatform(ORIGINAL_PLATFORM);setExecPath(ORIGINAL_EXECPATH);delete process.env.APPIMAGE;+ delete process.env.XDG_CONFIG_HOME;});describe("auto_launch", () => {@@ -164,6 +166,17 @@ describe("auto_launch", () => {expect(content).toContain('Exec="/home/user/Apps/Trilium.AppImage"');});+ it("honours $XDG_CONFIG_HOME for the Linux autostart directory", () => {+ setPlatform("linux");+ state.launchOnStartup = true;+ process.env.XDG_CONFIG_HOME = "/home/user/.xdgconfig";++ applyLaunchOnStartup();++ const [file] = state.writeFileSync.mock.calls[0] as [string, string];+ expect(file).toBe(path.join("/home/user/.xdgconfig", "autostart", "trilium.desktop"));+ });+it("removes the .desktop file on Linux when disabled", () => {setPlatform("linux");state.launchOnStartup = false;diff --git a/apps/desktop/src/services/auto_launch.ts b/apps/desktop/src/services/auto_launch.tsindex 843b79b3a0..3fdb1cd47e 100644--- a/apps/desktop/src/services/auto_launch.ts+++ b/apps/desktop/src/services/auto_launch.ts@@ -7,8 +7,6 @@ import path from "path";// Electron's app.setLoginItemSettings() covers macOS and Windows but is a no-op on// Linux, where autostart is instead a freedesktop ".desktop" file dropped into the// per-user autostart directory. We therefore branch on the platform.-const LINUX_AUTOSTART_DIR = path.join(os.homedir(), ".config", "autostart");-const LINUX_DESKTOP_FILE = path.join(LINUX_AUTOSTART_DIR, "trilium.desktop");// We tag the autostart command so the app can tell, at launch, that it was started// by the OS at login (and should hide to the tray) rather than launched manually@@ -65,14 +63,24 @@ export function setupAutoLaunch() {}function applyLinuxAutostart(enabled: boolean, hidden: boolean) {+ const autostartDir = getLinuxAutostartDir();+ const desktopFile = path.join(autostartDir, "trilium.desktop");if (enabled) {- fs.mkdirSync(LINUX_AUTOSTART_DIR, { recursive: true });- fs.writeFileSync(LINUX_DESKTOP_FILE, buildLinuxDesktopEntry(hidden));+ fs.mkdirSync(autostartDir, { recursive: true });+ fs.writeFileSync(desktopFile, buildLinuxDesktopEntry(hidden));} else {- fs.rmSync(LINUX_DESKTOP_FILE, { force: true });+ fs.rmSync(desktopFile, { force: true });}}+function getLinuxAutostartDir() {+ // XDG Base Directory spec: use $XDG_CONFIG_HOME when set and non-empty,+ // otherwise fall back to ~/.config. Read lazily so the env is honoured at apply+ // time rather than module load.+ const configHome = process.env.XDG_CONFIG_HOME || path.join(os.homedir(), ".config");+ return path.join(configHome, "autostart");+}+function buildLinuxDesktopEntry(hidden: boolean) {// When packaged as an AppImage, APPIMAGE points at the bundle to relaunch;// otherwise fall back to the running executable.diff --git a/packages/trilium-core/src/routes/api/options.ts b/packages/trilium-core/src/routes/api/options.tsindex e67461943e..5d3ba8a51c 100644--- a/packages/trilium-core/src/routes/api/options.ts+++ b/packages/trilium-core/src/routes/api/options.ts@@ -96,7 +96,6 @@ const ALLOWED_OPTIONS = new Set<OptionNames>(["hideOnAutoStart","eraseUnusedAttachmentsAfterSeconds","eraseUnusedAttachmentsAfterTimeScale",- "disableTray","customSearchEngineName","customSearchEngineUrl","editedNotesOpenInRibbon",