diff --git a/electron/main.cjs b/electron/main.cjs index d8f36fd..f2d92b6 100644 --- a/electron/main.cjs +++ b/electron/main.cjs @@ -18,6 +18,7 @@ const { MAX_DOCUMENT_BYTES, checkDiskPath, resolvePath, readRegular } = require( if (!app.isPackaged && process.env.FOLIO_TEST_USER_DATA) app.setPath("userData", process.env.FOLIO_TEST_USER_DATA); // Automated tests drive a window that never appears, so a local run doesn't take over the screen or focus. const TEST_HIDDEN = !app.isPackaged && process.env.FOLIO_TEST_HIDDEN === "1"; +const TEST_REGION = !app.isPackaged && process.env.FOLIO_TEST_USER_DATA ? (process.env.FOLIO_TEST_REGION || "US") : null; // macOS gives every ordinary app a Dock icon, whatever its windows do. An accessory app has none and can't come // forward by itself. The test scripts start a copy of Electron that is one from its first moment // (scripts/electron-path.mjs); this covers a hidden launch of the ordinary Electron, set right away because the Dock @@ -277,6 +278,9 @@ function createWindow() { ...(IS_MAC ? { trafficLightPosition: { x: 18, y: 18 } } : { titleBarOverlay: TITLEBAR[theme] }), webPreferences: { preload: path.join(__dirname, "preload.cjs"), + // The OS region (not the UI language) decides the default paper size. Test runs use US (Letter) unless + // FOLIO_TEST_REGION names another region, so smoke tests give the same result on every machine. + additionalArguments: [`--folio-region=${TEST_REGION ?? app.getLocaleCountryCode() ?? ""}`], contextIsolation: true, sandbox: true, plugins: true, // Chromium's PDF viewer, used for printing diff --git a/electron/preload.cjs b/electron/preload.cjs index 81d5350..e8fb92e 100644 --- a/electron/preload.cjs +++ b/electron/preload.cjs @@ -36,6 +36,7 @@ contextBridge.exposeInMainWorld("folioDesktop", { openDocument: (file) => ipcRenderer.invoke("folio:open-document", webUtils.getPathForFile(file)), ready: () => ipcRenderer.invoke("folio:ready"), platform: process.platform, + region: (process.argv.find(a => a.startsWith("--folio-region=")) || "").slice("--folio-region=".length), openDownload: () => ipcRenderer.invoke("folio:open-download"), downloadOcrModel: (code) => ipcRenderer.invoke("folio:download-ocr-model", code), /** diff --git a/src/PrintDialog.tsx b/src/PrintDialog.tsx index ef14462..953ab36 100644 --- a/src/PrintDialog.tsx +++ b/src/PrintDialog.tsx @@ -1,7 +1,7 @@ import { useEffect, useMemo, useRef, useState, type CSSProperties } from "react"; import type { PDFDocumentProxy } from "./pdf"; import { Modal } from "./Dialogs"; -import { DEFAULT_PRINT, parsePages, planSheets, checkPrintBudget, overlapFromPoints, overlapToPoints, type OverlapUnit, type PageSize, type PrintSettings } from "./printLayout"; +import { DEFAULT_PRINT, loadPaper, savePaper, parsePages, planSheets, checkPrintBudget, overlapFromPoints, overlapToPoints, type OverlapUnit, type PageSize, type PrintSettings } from "./printLayout"; import { renderPrintSheet, encodePrintSheet } from "./printRender"; import "./print.css"; @@ -55,7 +55,7 @@ export function PrintDialog({ pdf, currentPage, name, onPrint, onClose }: { pdf: PDFDocumentProxy; currentPage: number; name: string; onPrint: () => void; onClose: () => void; }) { const [advanced, setAdvanced] = useState(false); - const [settings, setSettings] = useState(DEFAULT_PRINT); + const [settings, setSettings] = useState(() => ({ ...DEFAULT_PRINT, paper: loadPaper() })); const [overlapUnit, setOverlapUnit] = useState("in"); const [selection, setSelection] = useState("all"); const [range, setRange] = useState(""); @@ -202,7 +202,7 @@ export function PrintDialog({ pdf, currentPage, name, onPrint, onClose }: { {selection === "range" && setRange(e.target.value)} />}
Paper and quality - +

Orientation

{(["auto", "portrait", "landscape"] as const).map(value => )}
diff --git a/src/desktop.d.ts b/src/desktop.d.ts index fdf7dca..026d778 100644 --- a/src/desktop.d.ts +++ b/src/desktop.d.ts @@ -29,6 +29,8 @@ interface FolioDesktop { notifyOnly: boolean }>; /** "win32", "darwin", or "linux". */ platform: string; + /** Two-letter country of the OS region, empty when unknown. */ + region?: string; /** Opens the releases page (macOS, where updates are downloaded manually). */ openDownload(): Promise; downloadOcrModel(code: string): Promise>; diff --git a/src/printLayout.ts b/src/printLayout.ts index 4f7f0bd..2206550 100644 --- a/src/printLayout.ts +++ b/src/printLayout.ts @@ -12,6 +12,26 @@ export const PAPER = { Letter: { width: 612, height: 792 }, A4: { width: 595.28, export const DEFAULT_PRINT: PrintSettings = { mode: "size", sizing: "fit", scale: 100, paper: "Letter", orientation: "auto", perSheet: 2, grayscale: false, toner: false, dpi: 150, overlapPoints: 18 }; +// Letter is standard only in these regions; everywhere else prints on A4. An unknown region keeps Letter. +const LETTER_REGIONS = new Set(["US", "CA", "MX", "PH", "PR", "CL", "CO", "VE", "GT", "SV", "CR", "PA", "DO", "BO", "NI"]); +export function defaultPaper(region?: string): PrintSettings["paper"] { + return !region || LETTER_REGIONS.has(region.toUpperCase()) ? "Letter" : "A4"; +} + +const PAPER_KEY = "folio:print-paper"; +/** The paper last chosen in the print dialog, else the default for the OS region. */ +export function initialPaper(region?: string, stored?: string | null): PrintSettings["paper"] { + return stored === "Letter" || stored === "A4" || stored === "Legal" ? stored : defaultPaper(region); +} +export function loadPaper(): PrintSettings["paper"] { + let stored: string | null = null; + try { stored = localStorage.getItem(PAPER_KEY); } catch { /* storage unavailable, use the default */ } + return initialPaper(typeof window === "undefined" ? undefined : window.folioDesktop?.region, stored); +} +export function savePaper(paper: PrintSettings["paper"]) { + try { localStorage.setItem(PAPER_KEY, paper); } catch { /* not remembered, harmless */ } +} + export type OverlapUnit = "in" | "mm"; export const overlapToPoints = (value: number, unit: OverlapUnit) => value * 72 / (unit === "mm" ? 25.4 : 1); export const overlapFromPoints = (points: number, unit: OverlapUnit) => points / 72 * (unit === "mm" ? 25.4 : 1); diff --git a/src/printPaper.test.ts b/src/printPaper.test.ts new file mode 100644 index 0000000..bdc1e4a --- /dev/null +++ b/src/printPaper.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from "vitest"; +import { defaultPaper, initialPaper } from "./printLayout"; + +describe("default paper size", () => { + it("uses A4 outside the Letter regions and Letter where it is standard", () => { + expect(defaultPaper("DE")).toBe("A4"); + expect(defaultPaper("gb")).toBe("A4"); + expect(defaultPaper("US")).toBe("Letter"); + expect(defaultPaper("")).toBe("Letter"); + }); + it("prefers the paper chosen last, and ignores junk in storage", () => { + expect(initialPaper("DE", "Legal")).toBe("Legal"); + expect(initialPaper("US", "A4")).toBe("A4"); + expect(initialPaper("DE", "A3")).toBe("A4"); + }); +});