Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions electron/main.cjs
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -625,13 +654,17 @@ 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]);
win.setBackgroundColor(TITLEBAR[theme].color);
}
});

setupMacMenu();
createWindow();
setupAutoUpdate();
});
Expand Down
6 changes: 6 additions & 0 deletions electron/preload.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
22 changes: 21 additions & 1 deletion scripts/smoke-print.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/desktop.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>;
/** Null when the user declines to open an address on their computer or local network, or the request is stopped. */
Expand Down
1 change: 1 addition & 0 deletions src/documentTabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down