diff --git a/apps/vectorcraft-web/Cargo.toml b/apps/vectorcraft-web/Cargo.toml index 6ea4935711..81330011ad 100644 --- a/apps/vectorcraft-web/Cargo.toml +++ b/apps/vectorcraft-web/Cargo.toml @@ -18,7 +18,7 @@ vectorcraft-engine = { workspace = true, features = ["bundled-fonts", "heif"] } vectorcraft-ui-egui = { workspace = true } vectorcraft-format = { workspace = true } vectorcraft-text = { workspace = true } -craft = { workspace = true, features = ["ui", "keymap", "automation", "platform"] } +craft = { workspace = true, features = ["ui", "keymap", "automation", "platform", "print"] } egui = { workspace = true } log = { workspace = true } @@ -34,4 +34,4 @@ wasm-bindgen = "0.2" serde_json = { workspace = true } wasm-bindgen-futures = "0.4" js-sys = "0.3" -web-sys = { version = "0.3", features = ["Window", "Document", "Element", "HtmlElement", "HtmlCanvasElement", "HtmlInputElement", "KeyboardEvent", "HtmlIFrameElement", "HtmlAnchorElement", "Blob", "BlobPropertyBag", "ClipboardEvent", "DataTransfer", "File", "FileList", "Url", "Location", "CssStyleDeclaration", "Event", "EventTarget", "MouseEvent", "PointerEvent", "TouchEvent", "AddEventListenerOptions", "DomRect", "DragEvent", "Storage", "Navigator", "RequestInit", "Response", "AbortController", "AbortSignal", "Node", "Worker", "MessageEvent", "ErrorEvent", "Performance"] } +web-sys = { version = "0.3", features = ["Window", "Document", "Element", "HtmlElement", "HtmlCanvasElement", "HtmlInputElement", "KeyboardEvent", "HtmlAnchorElement", "Blob", "BlobPropertyBag", "ClipboardEvent", "DataTransfer", "File", "FileList", "Url", "Location", "CssStyleDeclaration", "Event", "EventTarget", "MouseEvent", "PointerEvent", "TouchEvent", "AddEventListenerOptions", "DomRect", "DragEvent", "Storage", "Navigator", "RequestInit", "Response", "AbortController", "AbortSignal", "Node", "Worker", "MessageEvent", "ErrorEvent", "Performance"] } diff --git a/apps/vectorcraft-web/src/web.rs b/apps/vectorcraft-web/src/web.rs index 64a596e5ba..7e9ece12f3 100644 --- a/apps/vectorcraft-web/src/web.rs +++ b/apps/vectorcraft-web/src/web.rs @@ -562,13 +562,9 @@ fn blob_url(bytes: &[u8], mime: &str) -> Result { web_sys::Url::create_object_url_with_blob(&blob).map_err(js_err) } -/// File → Print in the browser: the job's PDF in a hidden frame, printed through the browser's -/// print dialog (which picks the printer). +/// Hands File → Print's composed PDF to the shared browser workflow. struct BrowserPrint; -/// The frame holding the last print job (the next job replaces it). -const PRINT_FRAME_ID: &str = "vectorcraft-print-frame"; - impl PrintService for BrowserPrint { fn printers(&mut self) -> Vec { // Browsers don't tell pages about printers: their print dialog lists them. @@ -576,39 +572,7 @@ impl PrintService for BrowserPrint { } fn print(&mut self, job: &PrintJob) -> Result { - print_pdf(job.pdf)?; - Ok(format!("Printing “{}”: pick the printer in the browser's print dialog", job.title)) + craft::print::browser::print_pdf(job.pdf)?; + Ok(format!("“{}” is ready to print in the browser", job.title)) } } - -fn print_pdf(pdf: &[u8]) -> Result<(), String> { - let document = web_sys::window().and_then(|w| w.document()).ok_or("no document")?; - // The previous job's frame and its blob go first. - if let Some(old) = document.get_element_by_id(PRINT_FRAME_ID) { - if let Some(url) = old.get_attribute("src") { - web_sys::Url::revoke_object_url(&url).ok(); - } - old.remove(); - } - let url = blob_url(pdf, "application/pdf")?; - let frame: web_sys::HtmlIFrameElement = document.create_element("iframe").map_err(js_err)?.dyn_into().map_err(|_| "not a frame")?; - frame.set_id(PRINT_FRAME_ID); - // Out of sight but laid out: browsers don't print a frame that isn't. - let style = frame.style(); - for (k, v) in [("position", "fixed"), ("right", "0"), ("bottom", "0"), ("width", "0"), ("height", "0"), ("border", "0")] { - style.set_property(k, v).map_err(js_err)?; - } - let loaded = frame.clone(); - let onload = wasm_bindgen::closure::Closure::once_into_js(move || { - if let Some(w) = loaded.content_window() { - w.focus().ok(); - if let Err(e) = w.print() { - log::error!("printing failed: {e:?}"); - } - } - }); - frame.set_onload(Some(onload.unchecked_ref())); - frame.set_src(&url); - document.body().ok_or("no body")?.append_child(&frame).map_err(js_err)?; - Ok(()) -} diff --git a/crates/ui-egui/src/dialogs/tests_print.rs b/crates/ui-egui/src/dialogs/tests_print.rs index 4c57342399..3830a60fce 100644 --- a/crates/ui-egui/src/dialogs/tests_print.rs +++ b/crates/ui-egui/src/dialogs/tests_print.rs @@ -171,6 +171,45 @@ fn print_sends_every_page_to_the_chosen_printer_and_keeps_the_settings() { assert_eq!(fields(&app)["copies"], 2); } +/// The browser supplies its own printer picker and can refuse to open it. Retrying prints +/// only the artboards the user selected in the same File → Print window. +#[test] +fn refused_browser_handoff_keeps_the_selected_artboards_for_retry() { + struct Browser { + refuse: bool, + accepted: Jobs, + } + impl PrintService for Browser { + fn printers(&mut self) -> Vec { + Vec::new() + } + + fn print(&mut self, job: &PrintJob) -> Result { + if std::mem::take(&mut self.refuse) { + return Err("The browser could not open the print document".into()); + } + MockPrint(self.accepted.clone()).print(job) + } + } + + let (mut app, jobs) = app(); + app.services.print = Some(Box::new(Browser { refuse: true, accepted: jobs.clone() })); + app.run("file.print", json!({})).unwrap(); + pump(&mut app); + set(&mut app, "artboards", json!("range")); + set(&mut app, "range", json!("2-3")); + confirm(&mut app); + assert!(is_open(&app)); + assert_eq!(fields(&app)["range"], "2-3"); + assert!(jobs.borrow().is_empty()); + confirm(&mut app); + assert!(!is_open(&app)); + let accepted = jobs.borrow(); + assert_eq!(accepted.len(), 1); + assert_eq!(accepted[0].0, None); + assert_eq!(pages(&accepted[0].2), 2); +} + #[test] fn done_keeps_the_settings_without_printing_and_bad_settings_keep_the_dialog() { let (mut app, jobs) = app();