diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs index 430fd3bde4..37dee42adc 100644 --- a/crates/engine/src/print_cmds.rs +++ b/crates/engine/src/print_cmds.rs @@ -330,6 +330,7 @@ pub fn layout(doc: &Document, p: &Value, cmd: &str) -> Result { fn do_print(s: &mut Session, p: &Value, cmd: &str) -> Result { let d = s.active().ok_or(EngineError::NoDocument)?; let doc = d.doc.clone(); + let doc_id = doc.id; let params = p.clone(); let worker_params = params.clone(); let command = cmd.to_string(); @@ -347,7 +348,16 @@ fn do_print(s: &mut Session, p: &Value, cmd: &str) -> Result { }, move |s, result| { remember_print(s, ¶ms); - crate::automate_cmds::fire_event(s, "print"); + // A user can switch tabs while printing. Hooks belong to the source document; + // a closed source has no document left to modify, as for background saves. + let viewed = s.active().map(|st| st.doc.id); + if let Some(index) = s.documents().iter().position(|st| st.doc.id == doc_id) { + s.set_active(index); + crate::automate_cmds::fire_event(s, "print"); + if let Some(index) = viewed.and_then(|id| s.documents().iter().position(|st| st.doc.id == id)) { + s.set_active(index); + } + } Ok(result) }, ); diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs index e8e55c8961..23ca04a991 100644 --- a/crates/engine/src/print_cmds/tests.rs +++ b/crates/engine/src/print_cmds/tests.rs @@ -244,11 +244,19 @@ fn spool_transport_streams_pdf_and_propagates_refusal() { const CHILD: &str = "PHOTOCRAFT_TEST_PRINT_TRANSPORT"; if let Ok(dir) = std::env::var(CHILD) { let mut s = session("rgb", 8); + let source = s.active().unwrap().doc.id; + s.execute("file.scripts.scriptEventsManager", json!({"add": {"event": "print", "steps": [["image.imageRotation.90cw", {}]]}})).unwrap(); let crate::jobs::Started::Job(job) = s.start("file.print", json!({"printer": "Office", "copies": 3, "paper": "a4", "scale": 50})).unwrap() else { panic!("desktop printing must start on a worker"); }; assert!(!s.cancel_job(job), "queue side effects must not be reported as cancelled"); + s.execute("file.new", json!({"width": 40, "height": 10})).unwrap(); + let viewed = s.active().unwrap().doc.id; let out = s.wait_job(job).unwrap(); + assert_eq!(s.active().unwrap().doc.id, viewed, "completion keeps the user's tab"); + assert_eq!(s.active().unwrap().doc.size.width, 40, "print hook must not act on the newly viewed document"); + let printed = &s.documents().iter().find(|st| st.doc.id == source).unwrap().doc; + assert_eq!((printed.size.width, printed.size.height), (150, 300), "print hook ran on the source document"); assert_eq!(out["sent"], true); assert_eq!(out["spooler"], "request id is Office-42"); assert!(out["pdf"].is_null(), "spool-only printing leaves no diagnostic PDF"); diff --git a/crates/ui-egui/src/discard_ui.rs b/crates/ui-egui/src/discard_ui.rs index b0b455ec9c..b7ca6ebd60 100644 --- a/crates/ui-egui/src/discard_ui.rs +++ b/crates/ui-egui/src/discard_ui.rs @@ -120,9 +120,9 @@ pub fn guard_window_close(app: &mut PhotocraftApp, ctx: &egui::Context) { ctx.send_viewport_cmd(egui::ViewportCommand::Focus); return; } - // A save still writing in the background finishes first (a saved copy, say, leaves nothing - // unsaved); `jobs_ui` repeats the close when it ends. - if app.saving() { + // A save or print worker must finish its external side effect before the process exits; + // `jobs_ui` repeats the close after completion and keeps errors visible. + if app.saving() || app.printing() { ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose); app.jobs.close_after_saves = true; return; diff --git a/crates/ui-egui/src/jobs_ui.rs b/crates/ui-egui/src/jobs_ui.rs index 4a52e26d48..7b3e0d8a62 100644 --- a/crates/ui-egui/src/jobs_ui.rs +++ b/crates/ui-egui/src/jobs_ui.rs @@ -348,7 +348,7 @@ fn finish_save(app: &mut PhotocraftApp, ctx: &egui::Context, save: SaveJob, labe /// no save worker is left. fn close_after_saves(app: &mut PhotocraftApp, ctx: &egui::Context) { app.jobs.saves.retain(|s| !(s.ended && s.state.finished())); - if !app.saving() && std::mem::take(&mut app.jobs.close_after_saves) { + if !app.saving() && !app.printing() && std::mem::take(&mut app.jobs.close_after_saves) { ctx.send_viewport_cmd(egui::ViewportCommand::Close); } } @@ -363,8 +363,8 @@ pub fn tick(app: &mut PhotocraftApp, ctx: &egui::Context) { if app.jobs.focus.is_some_and(|j| !app.jobs.opens.iter().any(|o| o.job == j)) { app.jobs.focus = app.jobs.opens.last().map(|o| o.job); } - if app.saving() { - close_after_saves(app, ctx); + close_after_saves(app, ctx); + if app.saving() || app.printing() { // Keep checking on a cancelled save's worker until it returns. ctx.request_repaint_after(std::time::Duration::from_millis(50)); } @@ -493,6 +493,9 @@ fn on_event(app: &mut PhotocraftApp, ctx: &egui::Context, e: JobEvent) { app.ui.status_error = false; } JobOutcome::Failed(err) => { + if matches!(e.command.as_str(), "file.print" | "file.printOneCopy") { + app.jobs.close_after_saves = false; + } app.sync_views(); notices::error(app, format!("{}: {err}", e.label)); } diff --git a/crates/ui-egui/src/jobs_ui_tests.rs b/crates/ui-egui/src/jobs_ui_tests.rs index dcf24594b2..2e61142938 100644 --- a/crates/ui-egui/src/jobs_ui_tests.rs +++ b/crates/ui-egui/src/jobs_ui_tests.rs @@ -615,12 +615,17 @@ fn printing_keeps_drawing_and_reports_only_the_final_spooler_result() { h.key_press(egui::Key::Escape); h.run_steps(2); assert!(h.state().session.job(id).is_some(), "Esc cannot retract a submitted job"); + let st = h.state_mut().session.active_mut().unwrap(); + st.saved_revision = st.revision; + assert!(press_window_close(h.state_mut()), "quit waits for print submission"); + assert!(h.state().jobs.close_after_saves); gate.store(true, Ordering::Relaxed); let deadline = Instant::now() + Duration::from_secs(10); while h.state().session.has_jobs() && Instant::now() < deadline { h.step(); } assert!(!h.state().session.has_jobs()); + assert!(!h.state().jobs.close_after_saves, "close repeats on success or is cleared for a visible error"); if success { assert!(h.state().ui.status.contains("Office-42"), "{}", h.state().ui.status); assert!(!h.state().ui.status_error); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index d3dc82a73a..0bac9f2a93 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1378,6 +1378,11 @@ impl PhotocraftApp { self.tiff_options.is_some() || self.targa_options.is_some() } + /// A print worker may still be handing a job to the system queue. + pub(crate) fn printing(&self) -> bool { + self.session.jobs().iter().any(|j| matches!(j.command.as_str(), "file.print" | "file.printOneCopy")) + } + /// A save is running in the background. pub(crate) fn saving(&self) -> bool { !self.jobs.saves.is_empty()