From b1f4af2974ec3f1ffb6d8ebb8cc71b98e4bfe2cb Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 19:46:08 -0500 Subject: [PATCH] L-227: the app creator installs the framework runtime (native and web) with the startup language's fonts; test install helpers (rfix1). L-228: fonts_ready goes; a language change installs its fonts (lead) --- apps/pdfcraft-web/Cargo.toml | 4 +- apps/pdfcraft-web/src/api.rs | 2 - apps/pdfcraft-web/src/main.rs | 32 +++++++++++-- apps/pdfcraft/Cargo.toml | 5 +- apps/pdfcraft/src/main.rs | 65 ++++++++++++++++++++++---- apps/pdfcraft/src/tests.rs | 8 ++++ crates/ui-egui/src/app.rs | 20 +++++--- crates/ui-egui/src/control.rs | 9 +--- crates/ui-egui/src/control/host.rs | 6 --- crates/ui-egui/src/lib.rs | 1 - crates/ui-egui/tests/it/support/mod.rs | 11 +++++ 11 files changed, 123 insertions(+), 40 deletions(-) diff --git a/apps/pdfcraft-web/Cargo.toml b/apps/pdfcraft-web/Cargo.toml index 778937f3bc..8388b8e903 100644 --- a/apps/pdfcraft-web/Cargo.toml +++ b/apps/pdfcraft-web/Cargo.toml @@ -11,8 +11,8 @@ workspace = true [dependencies] pdfcraft-ui-egui = { workspace = true } -# The framework, through its facade (contract head). -craft = { workspace = true, features = ["automation"] } +# The framework, through its facade (contract head), with the UI runtime the canvas is installed with. +craft = { workspace = true, features = ["automation", "ui", "window"] } # The host messages (storytold/craft-libs): one contract and one bridge for the web builds. # The page command API (storytold/craft-libs#413): window.pdfcraft over the UI control channel. craft-web-api = { git = "https://github.com/storytold/craft-libs", rev = "527c2750e37fb8bf576dc9a9e07ffc60b9d63717", version = "0.1.0" } diff --git a/apps/pdfcraft-web/src/api.rs b/apps/pdfcraft-web/src/api.rs index 38e267c28b..7ab42edc89 100644 --- a/apps/pdfcraft-web/src/api.rs +++ b/apps/pdfcraft-web/src/api.rs @@ -33,7 +33,6 @@ mod web { use std::rc::Rc; use std::sync::mpsc::{Receiver, Sender, TryRecvError}; - use craft::automation::{ControlRequest, ControlResponse, Dispatch, REPLY_DEADLINE}; use craft_web_api::methods::Methods; use craft_web_api::page::{PageApi, Request}; @@ -114,5 +113,4 @@ mod tests { assert_eq!(command_params(json!({"command": "file.word_count", "params": {"a": 1}})), json!({"id": "file.word_count", "params": {"a": 1}})); assert_eq!(command_params(json!({"command": "file.save", "params": {}})), json!({"id": "file.save", "params": {}})); } - } diff --git a/apps/pdfcraft-web/src/main.rs b/apps/pdfcraft-web/src/main.rs index 26a80e1282..1ef3ae644e 100644 --- a/apps/pdfcraft-web/src/main.rs +++ b/apps/pdfcraft-web/src/main.rs @@ -22,6 +22,9 @@ fn main() { canvas, options, Box::new(|cc| { + // PLAN: assumed pdfcraft_ui_egui::settings::open() -> Store over craft::settings::BrowserFiles on wasm32, with the former PrintCraft key migrated (pdf-shell-w9, SH-C29, L-117). + let settings = pdfcraft_ui_egui::settings::open(); + install_runtime(cc, startup_lang(settings.get().behavior.language.as_deref()))?; let ctx = cc.egui_ctx.clone(); let wake = craft::host::Wake::new(move || ctx.request_repaint()); // Files to open: a `?file=` download, the embedding page's `pdfcraft:open`, a drop. @@ -30,8 +33,6 @@ fn main() { let files = craft::web_host::files::BrowserFiles::new(wake, host::MAX_OPEN_BYTES); let app_host = craft::host::Host::builder(pdfcraft_ui_egui::APP_INFO).files(Arc::new(files)).build(); let mut app = PdfCraftApp::with_host(Arc::new(app_host), AppServices::system(&cc.egui_ctx, intake)); - // PLAN: assumed pdfcraft_ui_egui::settings::open() -> Store over craft::settings::BrowserFiles on wasm32, with the former PrintCraft key migrated (pdf-shell-w9, SH-C29, L-117). - let settings = pdfcraft_ui_egui::settings::open(); app.apply_settings(&settings.get()); app.attach_settings(settings); // `?file=` opens a PDF from a URL (same-origin or CORS-enabled). @@ -45,7 +46,10 @@ fn main() { Err(e) => { eframe::web_sys::console::error_1(&format!("PdfCraft: could not fetch {url}: {e}").into()); // Shown in the app too, not only in the console (#173). - let text = pdfcraft_ui_egui::i18n::fmt(pdfcraft_ui_egui::i18n::t("Couldn't open {name}: {e}"), &[("name", &name), ("e", &e)]); + let text = pdfcraft_ui_egui::i18n::fmt( + pdfcraft_ui_egui::i18n::t("Couldn't open {name}: {e}"), + &[("name", &name), ("e", &e)], + ); let _ = craft::ui::notices::NoticeCenter::new().post(&ctx, &craft::ui::notices::PostedNotice::new(text), &[]); } } @@ -91,6 +95,28 @@ fn main() { }); } +/// The language the interface starts in: the saved preference, else the browser's. +#[cfg(target_arch = "wasm32")] +fn startup_lang(saved: Option<&str>) -> pdfcraft_ui_egui::i18n::Lang { + use pdfcraft_ui_egui::i18n::{AUTO, Lang, normalize_pref}; + Lang::from_pref(saved.and_then(normalize_pref).unwrap_or(AUTO)) +} + +/// Installs the framework runtime on eframe's context, with the interface fonts of `lang`. Once +/// per context, before the app is built. The browser draws the page's frame and opens no OS +/// windows, so the spec names no frame and no app id. +#[cfg(target_arch = "wasm32")] +fn install_runtime(cc: &eframe::CreationContext<'_>, lang: pdfcraft_ui_egui::i18n::Lang) -> Result<(), String> { + use craft::host::target::ShellInfo; + use craft::ui::InstallSpec; + let ctx = cc.egui_ctx.clone(); + let system = craft::ui::runtime::env::SystemReader::start(move || ctx.request_repaint()); + let spec = InstallSpec::new(ShellInfo::default(), std::sync::Arc::new(craft::window::system::SystemWindows)).system(system); + craft::ui::shell::embed::install(&cc.egui_ctx, spec, &pdfcraft_ui_egui::fonts::ui_fonts(lang)) + .map(drop) + .map_err(|error| format!("the runtime could not be installed: {error}")) +} + #[cfg(target_arch = "wasm32")] fn query_param(key: &str) -> Option { let search = web_sys::window()?.location().search().ok()?; diff --git a/apps/pdfcraft/Cargo.toml b/apps/pdfcraft/Cargo.toml index 0e6299f6fa..be9cd9a4ea 100644 --- a/apps/pdfcraft/Cargo.toml +++ b/apps/pdfcraft/Cargo.toml @@ -12,8 +12,9 @@ workspace = true # Help ▸ Check for updates (issue #28): one HTTPS request to GitHub, over rustls with the OS's # trusted roots. No bundled root list (webpki-roots and webpki-root-certs are CDLA-licensed). pdfcraft-ui-egui = { workspace = true } -# The framework, through its facade (contract head): host, platform, automation, pages and quit. -craft = { workspace = true, features = ["platform", "automation", "pages", "quit"] } +# The framework, through its facade (contract head): host, platform, automation, pages and quit, +# and the UI runtime and window policy the window is installed with. +craft = { workspace = true, features = ["platform", "automation", "pages", "quit", "ui", "window"] } # `build_info`: the version and compile-time build label for `--version` and the log. pdfcraft-engine = { workspace = true } # `crypto_env`: the cryptography environment the engine opens files with. diff --git a/apps/pdfcraft/src/main.rs b/apps/pdfcraft/src/main.rs index fded6c35a5..d40746fb46 100644 --- a/apps/pdfcraft/src/main.rs +++ b/apps/pdfcraft/src/main.rs @@ -266,13 +266,19 @@ fn main() -> eframe::Result { // Who draws the window frame: the platform and Preferences ▸ Use system title bar decide, // unless `PDFCRAFT_TITLEBAR` overrides them. The store is read here and opened again by the // app (each window attempt opens its own: the OpenGL retry runs the creator again). - let title_bar = pdfcraft_ui_egui::settings::open().get().layout.system_title_bar.map_or(Default::default(), |on| { - if on { craft::window::policy::TitleBarPref::System } else { craft::window::policy::TitleBarPref::Integrated } - }); + // The interface language is read the same way, for the fonts the runtime installs with. + let (title_bar, saved_lang) = { + let settings = pdfcraft_ui_egui::settings::open(); + let saved = settings.get(); + let title_bar = saved.layout.system_title_bar.map_or(Default::default(), |on| { + if on { craft::window::policy::TitleBarPref::System } else { craft::window::policy::TitleBarPref::Integrated } + }); + (title_bar, saved.behavior.language.clone()) + }; let frame = craft::window::policy::decide_for(craft::window::Prefix::new("PDFCRAFT"), craft::window::policy::Backend::from_env(), title_bar); - let integrated = !matches!(frame.frame, craft::window::policy::Frame::Native); + let lang = startup_lang(saved_lang.as_deref(), &options); let choice = renderer_choice(std::env::var("PDFCRAFT_RENDERER").ok().as_deref()); - let launch = Launch { files, options, control, create_images, combine, integrated, two_paged }; + let launch = Launch { files, options, control, create_images, combine, frame, lang, two_paged }; // Finder, Open With and the Dock deliver files as Apple events, not arguments; catch the one // that launched us as well as later ones. Lives until the event loop returns. #[cfg(target_os = "macos")] @@ -282,7 +288,7 @@ fn main() -> eframe::Result { let first = if choice == RendererChoice::Gl { eframe::Renderer::Glow } else { eframe::Renderer::Wgpu }; let result = eframe::run_native( "PdfCraft", - native_options(launch.integrated, first), + native_options(launch.integrated(), first), app_creator( launch.clone(), Rc::clone(&started), @@ -300,7 +306,7 @@ fn main() -> eframe::Result { log::error!("the GPU renderer (wgpu) didn't start: {e}. Starting with OpenGL instead; set PDFCRAFT_RENDERER=gl to skip wgpu."); eframe::run_native( "PdfCraft", - native_options(launch.integrated, eframe::Renderer::Glow), + native_options(launch.integrated(), eframe::Renderer::Glow), app_creator( launch, started, @@ -433,11 +439,49 @@ struct Launch { create_images: bool, /// `--combine`: the files go to the Combine files tab. combine: bool, - /// The app's bar is the title bar. - integrated: bool, + /// Who draws the root window's frame. + frame: craft::window::policy::Decision, + /// The language the interface starts in, whose fonts are installed with the runtime. + lang: pdfcraft_ui_egui::i18n::Lang, two_paged: bool, } +impl Launch { + /// Whether the app's bar is the title bar. + fn integrated(&self) -> bool { + !matches!(self.frame.frame, craft::window::policy::Frame::Native) + } +} + +/// The language the interface starts in: the last valid `--language`, else the saved preference, +/// else the system's, as the app applies them once it is created. +fn startup_lang(saved: Option<&str>, options: &[(String, String)]) -> pdfcraft_ui_egui::i18n::Lang { + use pdfcraft_ui_egui::i18n::{AUTO, Lang, normalize_pref}; + let given = options.iter().rev().filter(|(key, _)| key == "language").find_map(|(_, value)| normalize_pref(value)); + Lang::from_pref(given.or_else(|| saved.and_then(normalize_pref)).unwrap_or(AUTO)) +} + +/// Installs the framework runtime on eframe's context, with the root window's frame and the +/// interface fonts of `lang`. Once per context, before the app is built. +fn install_runtime( + cc: &eframe::CreationContext<'_>, + frame: craft::window::policy::Decision, + lang: pdfcraft_ui_egui::i18n::Lang, +) -> Result<(), String> { + use craft::host::target::{Backend, ShellInfo}; + use craft::ui::InstallSpec; + use craft::window::system::SystemWindows; + let ctx = cc.egui_ctx.clone(); + let system = craft::ui::runtime::env::SystemReader::start(move || ctx.request_repaint()); + let spec = InstallSpec::new(ShellInfo::default().backend(Backend::from_env()), std::sync::Arc::new(SystemWindows)) + .system(system) + .app_id(APP_ID) + .frame(frame); + craft::ui::shell::embed::install(&cc.egui_ctx, spec, &pdfcraft_ui_egui::fonts::ui_fonts(lang)) + .map(drop) + .map_err(|error| format!("the runtime could not be installed: {error}")) +} + /// Decode `png` as a [`winit::window::Icon`] for the Windows taskbar and Alt+Tab (`ICON_BIG`). #[cfg(target_os = "windows")] fn taskbar_icon(png: &[u8]) -> Option { @@ -486,9 +530,10 @@ fn app_creator<'a>( instance: Option<&'a single_instance::Server>, #[cfg(target_os = "macos")] apple_events: &'a apple_events::AppleEvents, ) -> eframe::AppCreator<'a> { - let Launch { files, options, control, create_images, combine, two_paged, .. } = launch; + let Launch { files, options, control, create_images, combine, frame, lang, two_paged } = launch; Box::new(move |cc| { started.set(true); + install_runtime(cc, frame, lang)?; let ctx = cc.egui_ctx.clone(); let wake = craft::host::Wake::new(move || ctx.request_repaint()); // Files a second launch hands over arrive through the single-instance server. diff --git a/apps/pdfcraft/src/tests.rs b/apps/pdfcraft/src/tests.rs index 284dd03b83..c6da27b3ba 100644 --- a/apps/pdfcraft/src/tests.rs +++ b/apps/pdfcraft/src/tests.rs @@ -11,6 +11,14 @@ fn gpu() -> std::sync::MutexGuard<'static, ()> { GPU.lock().unwrap_or_else(std::sync::PoisonError::into_inner) } +/// Installs the framework runtime on a harness's context, as the app's window does, with a system +/// that opens no OS windows and the interface fonts of English. +fn install_runtime(ctx: &egui::Context) { + use pdfcraft_ui_egui::i18n::Lang; + let spec = craft::ui::InstallSpec::new(craft::host::target::ShellInfo::default(), std::sync::Arc::new(craft::window::system::NoWindows)); + craft::ui::shell::embed::install(ctx, spec, &pdfcraft_ui_egui::fonts::ui_fonts(Lang::EN)).expect("the runtime installs on a fresh context"); +} + #[test] fn opengl_is_the_fallback_unless_a_renderer_was_chosen() { use super::{RendererChoice, native_options, renderer_choice, retry_with_gl}; diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 5abd98f623..fbebce444a 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -176,6 +176,14 @@ impl PdfCraftApp { Self::with_host(host, services) } + /// Installs the framework runtime on a unit test harness's context, as the app's window does, + /// with a system that opens no OS windows and the interface fonts of English. + #[cfg(test)] + pub(crate) fn install_runtime(ctx: &egui::Context) { + let spec = craft::ui::InstallSpec::new(craft::host::target::ShellInfo::default(), Arc::new(craft::window::system::NoWindows)); + craft::ui::shell::embed::install(ctx, spec, &crate::fonts::ui_fonts(i18n::Lang::EN)).expect("the runtime installs on a fresh context"); + } + /// The app over `host` and `services`, with no document open: every queue runs on the host's /// jobs, so a test passes a host over `ManualJobs` and `ScriptedFiles`. pub fn with_host(host: Arc, services: AppServices) -> Self { @@ -417,7 +425,6 @@ impl PdfCraftApp { autosave, recovered_paths: Default::default(), native_menu: None, - fonts_ready: false, window_title: String::new(), page_title: String::new(), control: None, @@ -716,11 +723,12 @@ impl PdfCraftApp { #[cfg(test)] self.injected_panic(crate::frame_guard::FramePart::Ui); let ctx = ui.ctx().clone(); - i18n::set_current(i18n::Lang::from_pref(&self.language)); - // Fonts registered via set_fonts only take effect next frame; named families would panic now. - if !self.fonts_ready { - ctx.request_repaint(); - return; + // The chosen language becomes the current one; when it changes, its fonts are installed with it (the + // runtime installed the starting language's fonts at creation, L-227). + let lang = i18n::Lang::from_pref(&self.language); + if i18n::current().code() != lang.code() { + i18n::set_current(lang); + crate::fonts::ui_fonts(lang).install(&ctx); } // Another tool lets go of a click whose picture is still being found. if self.quick_tool != QuickTool::Select { diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index 748d9d1c77..807402ad6b 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -70,12 +70,8 @@ impl PdfCraftApp { } /// Answers the control requests of this frame and finishes the screenshots that are due. Runs in the - /// logic phase. Before the first real UI pass (`fonts_ready`) the requests wait in the channel, which keeps - /// their deadline. + /// logic phase; the runtime and fonts are installed at creation (L-227), so every frame can answer. pub(crate) fn control_logic(&mut self, ctx: &egui::Context) { - if !self.fonts_ready { - return; - } let Some(mut channel) = self.control.take() else { return }; let host = WindowHost::of(ctx); let document = Some(self.capture_document()); @@ -93,9 +89,6 @@ impl PdfCraftApp { /// Counts the pass that just ran and issues the captures admitted for it. Call at the end of every UI pass. pub(crate) fn control_ui_pass(&mut self, ctx: &egui::Context) { - if !self.fonts_ready { - return; - } // A discarded pass is layout work, not a frame an agent can observe. if ctx.will_discard() { return; diff --git a/crates/ui-egui/src/control/host.rs b/crates/ui-egui/src/control/host.rs index 156eb34165..92fea46f0a 100644 --- a/crates/ui-egui/src/control/host.rs +++ b/crates/ui-egui/src/control/host.rs @@ -47,12 +47,6 @@ impl crate::PdfCraftApp { hasher.finish() } - /// Control requests wait for the first UI pass that installs fonts; a font-gated early - /// return cannot consume injected input or issue a screenshot. - pub(crate) fn accepts_input(&self) -> bool { - self.fonts_ready - } - /// `ui.panel`: the dock's panel request. pub(crate) fn panel(&mut self, reset: bool, params: &Value) -> Result { crate::side_panels::command(self, reset, params) diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 41759bc849..a80713fd1d 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -622,7 +622,6 @@ pub struct PdfCraftApp { recovered_paths: std::collections::HashSet, /// The native menu bar (macOS), while it is installed; the in-window bar shows otherwise. pub(crate) native_menu: Option, - fonts_ready: bool, /// The window title last sent to the platform. pub window_title: String, /// The web page's title last set: the window title, but always with the file's name. diff --git a/crates/ui-egui/tests/it/support/mod.rs b/crates/ui-egui/tests/it/support/mod.rs index 04e914c5b6..22ca612537 100644 --- a/crates/ui-egui/tests/it/support/mod.rs +++ b/crates/ui-egui/tests/it/support/mod.rs @@ -4,14 +4,18 @@ use std::collections::BTreeSet; use std::sync::{Arc, Mutex}; +use craft::host::target::ShellInfo; use craft::host::{AppInfo, Files, Host, InlineJobs, Jobs, ManualJobs, ScriptedFiles, Wake}; use craft::keymap::CommandRef; use craft::platform::clipboard::{Clipboard, ClipboardBackend, ClipboardError, ClipboardImage, Flavor, TEXT}; use craft::platform::open::FileIntake; use craft::platform::watch::FileWatch; +use craft::ui::InstallSpec; use craft::ui::runtime::commands::{FireSource, fire}; use craft::ui::runtime::env::WindowKey; use craft::ui::runtime::toast; +use craft::ui::shell::embed; +use craft::window::system::NoWindows; use egui::FontId; use egui::epaint::text::{Fonts, TextOptions}; use egui_kittest::Harness; @@ -133,6 +137,13 @@ pub fn app_with_clipboard(clipboard: RecordingClipboard) -> PdfCraftApp { /// An app built over `host`, with the tests' services: the one way a test gives the app a host, so /// jobs and picked files run when the test steps. +/// Installs the framework runtime on a harness's context, as the app's window does, with a system +/// that opens no OS windows and the interface fonts of English. Every harness creator calls it. +pub fn install_runtime(ctx: &egui::Context) { + let spec = InstallSpec::new(ShellInfo::default(), Arc::new(NoWindows)); + embed::install(ctx, spec, &pdfcraft_ui_egui::fonts::ui_fonts(i18n::Lang::EN)).expect("the runtime installs on a fresh context"); +} + pub fn host_harness(host: Arc, clipboard: RecordingClipboard) -> Harness<'static, PdfCraftApp> { let app = PdfCraftApp::with_host(host, services(clipboard)); Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| app)