Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): stop Ctrl+M from minimizing the window (closes #9753)

matt wilkie262782be7f1a
diff --git a/apps/desktop/src/services/window.spec.ts b/apps/desktop/src/services/window.spec.ts
index 33c12dc337..f68fd19da5 100644
--- a/apps/desktop/src/services/window.spec.ts
+++ b/apps/desktop/src/services/window.spec.ts
@@ -1,3 +1,4 @@
+import type { MenuItemConstructorOptions } from "electron";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
type Handler = (...args: unknown[]) => unknown;
@@ -172,7 +173,8 @@ const fakeApp = {
relaunch: vi.fn(),
exit: vi.fn(),
quit: vi.fn(),
- on: vi.fn((event: string, cb: Handler) => state.appOn.set(event, cb))
+ on: vi.fn((event: string, cb: Handler) => state.appOn.set(event, cb)),
+ whenReady: vi.fn(() => Promise.resolve())
};
const electronSurface = {
@@ -184,6 +186,10 @@ const electronSurface = {
ClipboardItem: FakeClipboardItem,
nativeTheme: { themeSource: "system" },
BrowserWindow: fakeBrowserWindowClass,
+ Menu: {
+ setApplicationMenu: vi.fn(),
+ buildFromTemplate: vi.fn((template: MenuItemConstructorOptions[]) => ({ template }))
+ },
ipcMain: {
on: (channel: string, fn: Handler) => state.ipcOn.set(channel, fn),
handle: (channel: string, fn: Handler) => state.ipcHandle.set(channel, fn),
@@ -1031,6 +1037,54 @@ describe("window service", () => {
});
});
+ describe("application menu", () => {
+ const realPlatform = process.platform;
+
+ async function setupWindowingOn(platform: NodeJS.Platform) {
+ vi.clearAllMocks();
+ Object.defineProperty(process, "platform", { value: platform, configurable: true });
+ setupWindowing();
+ await Promise.resolve(); // let whenReady().then(...) run
+ }
+
+ function flattenItems(items: MenuItemConstructorOptions[]): MenuItemConstructorOptions[] {
+ return items.flatMap((item) => [
+ item,
+ ...(Array.isArray(item.submenu) ? flattenItems(item.submenu) : [])
+ ]);
+ }
+
+ afterEach(() => {
+ Object.defineProperty(process, "platform", { value: realPlatform, configurable: true });
+ });
+
+ it("installs a menu without the minimize accelerator, except on macOS", async () => {
+ const { buildFromTemplate, setApplicationMenu } = electronSurface.Menu;
+
+ for (const platform of ["linux", "win32"] as const) {
+ await setupWindowingOn(platform);
+
+ expect(buildFromTemplate).toHaveBeenCalledTimes(1);
+ const menu = buildFromTemplate.mock.results[0]?.value;
+ expect(setApplicationMenu.mock.calls).toEqual([[menu]]);
+
+ const items = flattenItems(buildFromTemplate.mock.calls[0]?.[0] ?? []);
+ const roles = items.map((item) => item.role);
+ expect(roles).toEqual(
+ expect.arrayContaining(["fileMenu", "editMenu", "viewMenu", "close"])
+ );
+ expect(roles).not.toContain("minimize");
+ // Without its own submenu, a `windowMenu` gets Electron's defaults and `minimize`.
+ const defaultWindowMenus = items.filter((item) =>
+ item.role === "windowMenu" && !Array.isArray(item.submenu));
+ expect(defaultWindowMenus).toEqual([]);
+ }
+
+ await setupWindowingOn("darwin");
+ expect(setApplicationMenu).not.toHaveBeenCalled();
+ });
+ });
+
describe("DB_INITIALIZED subscription", () => {
beforeEach(() => {
state.eventSubs.clear();
diff --git a/apps/desktop/src/services/window.ts b/apps/desktop/src/services/window.ts
index 3580a3d1fe..674e0a196e 100644
--- a/apps/desktop/src/services/window.ts
+++ b/apps/desktop/src/services/window.ts
@@ -502,6 +502,8 @@ export function setupWindowing() {
isQuitting = true;
});
+ electron.app.whenReady().then(setupApplicationMenu);
+
electron.ipcMain.on("reload-all-windows", () => {
for (const win of electron.BrowserWindow.getAllWindows()) {
win.reload();
@@ -673,6 +675,25 @@ export function setupWindowing() {
});
}
+/**
+ * Installs an application menu without the `minimize` role. `setMenuBarVisibility(false)` keeps
+ * the menu accelerators active, and `minimize` binds Ctrl+M, the text editor's math shortcut.
+ */
+function setupApplicationMenu() {
+ // Cmd+M is standard on macOS. The app can be ready before `initializeCore()` makes
+ // `coreUtils.isMac()` usable, so this reads `process.platform`.
+ if (process.platform === "darwin") {
+ return;
+ }
+
+ electron.Menu.setApplicationMenu(electron.Menu.buildFromTemplate([
+ { role: "fileMenu" },
+ { role: "editMenu" },
+ { role: "viewMenu" },
+ { role: "windowMenu", submenu: [{ role: "close" }] }
+ ]));
+}
+
export default {
createMainWindow,
createExtraWindow,