diff --git a/electron/main.cjs b/electron/main.cjs index fdd4478..41a3b7b 100644 --- a/electron/main.cjs +++ b/electron/main.cjs @@ -1,6 +1,6 @@ // Folio desktop shell: one window, PDFs (and Word documents, converted) opened from Explorer / "Open with", // files served over app://. -const { app, BrowserWindow, protocol, net, ipcMain, dialog, shell, screen, session } = require("electron"); +const { app, BrowserWindow, Menu, protocol, net, ipcMain, dialog, shell, screen, session } = require("electron"); const crypto = require("node:crypto"); const path = require("node:path"); const fs = require("node:fs"); @@ -57,6 +57,7 @@ protocol.registerSchemesAsPrivileged([ ]); let win = null; +let printMenuItem = null; const OPENABLE = /\.(pdf|docx?)$/i; const pendingFiles = filesFromArgs(process.argv); let rendererReady = false; @@ -372,7 +373,35 @@ function createWindow() { if (choice === 0) e.preventDefault(); }); - win.on("closed", () => { clearTimeout(windowTimer); win = null; rendererReady = false; }); + win.on("closed", () => { clearTimeout(windowTimer); win = null; rendererReady = false; if (printMenuItem) printMenuItem.enabled = false; }); +} + +function setupMacMenu() { + if (!IS_MAC) return; + const menu = Menu.buildFromTemplate([ + { role: "appMenu" }, + { + label: "File", + submenu: [ + { + id: "file-print", + label: "Print…", + accelerator: "CommandOrControl+P", + enabled: false, + click: () => { + if (win && !win.isDestroyed()) win.webContents.send("folio:menu-command", "print"); + }, + }, + { type: "separator" }, + { role: "close" }, + ], + }, + { role: "editMenu" }, + { role: "viewMenu" }, + { role: "windowMenu" }, + ]); + printMenuItem = menu.getMenuItemById("file-print"); + Menu.setApplicationMenu(menu); } const MAX_URL_PDF_BYTES = MAX_DOCUMENT_BYTES; @@ -625,6 +654,9 @@ if (!app.requestSingleInstanceLock()) { on("folio:install-update", () => { if (updateReady) autoUpdater.quitAndInstall(true, true); }); + on("folio:can-print", (_e, enabled) => { + if (printMenuItem) printMenuItem.enabled = !!enabled && !!win && !win.isDestroyed(); + }); on("folio:theme", (_e, theme) => { if (win && Object.hasOwn(TITLEBAR, theme)) { if (!IS_MAC) win.setTitleBarOverlay(TITLEBAR[theme]); @@ -632,6 +664,7 @@ if (!app.requestSingleInstanceLock()) { } }); + setupMacMenu(); createWindow(); setupAutoUpdate(); }); diff --git a/electron/preload.cjs b/electron/preload.cjs index be54853..81d5350 100644 --- a/electron/preload.cjs +++ b/electron/preload.cjs @@ -26,6 +26,12 @@ contextBridge.exposeInMainWorld("folioDesktop", { ipcRenderer.on("folio:tab-command", listener); return () => ipcRenderer.removeListener("folio:tab-command", listener); }, + onMenuCommand(callback) { + const listener = (_e, command) => callback(command); + ipcRenderer.on("folio:menu-command", listener); + return () => ipcRenderer.removeListener("folio:menu-command", listener); + }, + setCanPrint: (enabled) => ipcRenderer.send("folio:can-print", !!enabled), /** Returns the converted PDF to the tab that opened the Word document. */ openDocument: (file) => ipcRenderer.invoke("folio:open-document", webUtils.getPathForFile(file)), ready: () => ipcRenderer.invoke("folio:ready"), diff --git a/scripts/smoke-print.mjs b/scripts/smoke-print.mjs index d5ff33c..228e8a8 100644 --- a/scripts/smoke-print.mjs +++ b/scripts/smoke-print.mjs @@ -26,9 +26,29 @@ try { }); const page = readerPage(await app.firstWindow()); const errors = []; page.on("pageerror", error => errors.push(String(error))); + const printMenu = () => app.evaluate(({ Menu }) => { + const item = Menu.getApplicationMenu()?.getMenuItemById("file-print"); + return item ? { label: item.label, accelerator: item.accelerator, enabled: item.enabled } : null; + }); + if (process.platform === "darwin") { + await page.getByRole("button", { name: /Try a sample/ }).waitFor({ timeout: 30000 }); + assert.deepEqual(await printMenu(), { label: "Print…", accelerator: "CommandOrControl+P", enabled: false }); + } await page.getByRole("button", { name: /Try a sample/ }).click(); await page.locator(".textLayer span:not(.markedContent)").first().waitFor({ timeout: 30000 }); - await page.keyboard.press("ControlOrMeta+p"); + if (process.platform === "darwin") { + assert.equal((await printMenu())?.enabled, true); + await page.keyboard.press("ControlOrMeta+,"); + await page.getByRole("dialog", { name: "Settings" }).waitFor(); + await app.evaluate(({ Menu }) => Menu.getApplicationMenu().getMenuItemById("file-print").click()); + await page.waitForTimeout(200); + assert.equal(await page.getByRole("dialog", { name: "Print", exact: true }).count(), 0); + await page.keyboard.press("Escape"); + await page.getByRole("dialog", { name: "Settings" }).waitFor({ state: "hidden" }); + await app.evaluate(({ Menu }) => Menu.getApplicationMenu().getMenuItemById("file-print").click()); + } else { + await page.keyboard.press("ControlOrMeta+p"); + } await page.getByRole("dialog", { name: "Print", exact: true }).waitFor(); await page.getByRole("tab", { name: "Advanced" }).click(); await page.getByRole("img", { name: "Preview of sheet 1" }).waitFor(); diff --git a/src/App.tsx b/src/App.tsx index b66f856..201243d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1377,6 +1377,14 @@ export default function App({ tabs, tabKey, upgradeNotes }: { tabs: DocumentTabs return () => window.removeEventListener("keydown", onKey); }); + const menuCommandRef = useRef<(command: "print") => void>(() => {}); + menuCommandRef.current = (command) => { + if (tabs.active !== tabKey || command !== "print" || !docRef.current) return; + if (signing || dialog || pending || imageView || printCopy || document.querySelector('[aria-modal="true"]')) return; + void print(); + }; + useEffect(() => window.folioDesktop?.onMenuCommand?.((command) => menuCommandRef.current(command)), []); + // ── Drag & drop anywhere ──────────────────────────────────────────── useEffect(() => { let depth = 0; diff --git a/src/desktop.d.ts b/src/desktop.d.ts index a2e963f..fdf7dca 100644 --- a/src/desktop.d.ts +++ b/src/desktop.d.ts @@ -16,6 +16,8 @@ interface FolioDesktop { onConverting(callback: (name: string, token: string) => void): () => void; onOpenFailed(callback: (message: string, token: string) => void): () => void; onTabCommand(callback: (command: "new" | "close" | "next" | "previous") => void): () => void; + onMenuCommand(callback: (command: "print") => void): () => void; + setCanPrint(enabled: boolean): void; openDocument(file: File): Promise<{ name: string; data: Uint8Array; note?: string }>; ready(): Promise; /** Null when the user declines to open an address on their computer or local network, or the request is stopped. */ diff --git a/src/documentTabs.ts b/src/documentTabs.ts index f3119b7..2f9ac38 100644 --- a/src/documentTabs.ts +++ b/src/documentTabs.ts @@ -58,6 +58,7 @@ export class DocumentTabs { this.revision++; const selected = this.entry(this.active); document.title = selected?.name ? selected.name + " — Folio" : "Folio"; + window.folioDesktop?.setCanPrint?.(!!selected?.contentId); for (const fn of this.listeners) fn(); } private entry(key: number) { return this.entries.find(tab => tab.key === key); }