Skip to content
Open
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
4 changes: 4 additions & 0 deletions electron/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions electron/preload.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
/**
Expand Down
6 changes: 3 additions & 3 deletions src/PrintDialog.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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<OverlapUnit>("in");
const [selection, setSelection] = useState("all");
const [range, setRange] = useState("");
Expand Down Expand Up @@ -202,7 +202,7 @@ export function PrintDialog({ pdf, currentPage, name, onPrint, onClose }: {
</select></div>
{selection === "range" && <input aria-label="Page range" placeholder="1-3, 5" value={range} onChange={e => setRange(e.target.value)} />}</section>
<details><summary>Paper and quality</summary><label className="print-inline">Print quality<select aria-label="Print quality" value={settings.dpi} onChange={e => update("dpi", +e.target.value as PrintSettings["dpi"])}>{[150, 300, 600].map(dpi => <option key={dpi} value={dpi}>{dpi} dpi</option>)}</select></label>
<label className="print-inline">Paper size<select value={settings.paper} onChange={e => update("paper", e.target.value as PrintSettings["paper"])}>{["Letter", "A4", "Legal"].map(p => <option key={p}>{p}</option>)}</select></label>
<label className="print-inline">Paper size<select value={settings.paper} onChange={e => { const paper = e.target.value as PrintSettings["paper"]; savePaper(paper); update("paper", paper); }}>{["Letter", "A4", "Legal"].map(p => <option key={p}>{p}</option>)}</select></label>
<h3>Orientation</h3><div className="print-radio-row">{(["auto", "portrait", "landscape"] as const).map(value => <label key={value}><input type="radio" name="print-orientation" checked={settings.orientation === value} onChange={() => update("orientation", value)} />{value[0].toUpperCase() + value.slice(1)}</label>)}</div>
<label className="print-check"><input type="checkbox" checked={settings.grayscale} onChange={e => update("grayscale", e.target.checked)} />Print in grayscale</label>
<label className="print-check"><input type="checkbox" checked={settings.toner} onChange={e => update("toner", e.target.checked)} />Save ink/toner</label>
Expand Down
2 changes: 2 additions & 0 deletions src/desktop.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
downloadOcrModel(code: string): Promise<Uint8Array<ArrayBuffer>>;
Expand Down
20 changes: 20 additions & 0 deletions src/printLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/printPaper.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});