Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Report refused print cancellation accurately to automation

skylar taylor-barrickcc902c681b15
diff --git a/crates/engine/src/jobs.rs b/crates/engine/src/jobs.rs
index d0349300c..5ea48f943 100644
--- a/crates/engine/src/jobs.rs
+++ b/crates/engine/src/jobs.rs
@@ -819,11 +819,17 @@ pub fn specs() -> Vec<crate::commands::CommandSpec> {
None | Some(Value::Null) => {
let n = s.jobs.running.len();
s.cancel_all_jobs();
- Ok(json!({"cancelled": n}))
+ Ok(json!({"cancelled": n.saturating_sub(s.jobs.running.len())}))
}
Some(v) => {
let id = v.as_u64().ok_or_else(|| EngineError::BadParams { cmd: "jobs.cancel".into(), msg: "`job` must be a job id".into() })?;
- if s.cancel_job(JobId(id)) { Ok(json!({"cancelled": 1})) } else { Err(EngineError::Other(format!("no running job {id}"))) }
+ if s.cancel_job(JobId(id)) {
+ Ok(json!({"cancelled": 1}))
+ } else if s.job(JobId(id)).is_some() {
+ Err(EngineError::Other("printing cannot be cancelled here; use the system print queue".into()))
+ } else {
+ Err(EngineError::Other(format!("no running job {id}")))
+ }
}
},
journal: false,
diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs
index 23ca04a99..061e7538d 100644
--- a/crates/engine/src/print_cmds/tests.rs
+++ b/crates/engine/src/print_cmds/tests.rs
@@ -250,6 +250,8 @@ fn spool_transport_streams_pdf_and_propagates_refusal() {
panic!("desktop printing must start on a worker");
};
assert!(!s.cancel_job(job), "queue side effects must not be reported as cancelled");
+ assert_eq!(s.execute("jobs.cancel", json!({})).unwrap()["cancelled"], 0);
+ assert!(s.execute("jobs.cancel", json!({"job": job.0})).unwrap_err().to_string().contains("system print queue"));
s.execute("file.new", json!({"width": 40, "height": 10})).unwrap();
let viewed = s.active().unwrap().doc.id;
let out = s.wait_job(job).unwrap();
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs
index b2a48cf6d..7bba3e086 100644
--- a/crates/ui-egui/src/control.rs
+++ b/crates/ui-egui/src/control.rs
@@ -381,16 +381,14 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)
let all = p.get("job").is_none_or(Value::is_null);
match p.get("job").and_then(Value::as_u64) {
Some(j) if app.session.job(photocraft_engine::jobs::JobId(j)).is_some() => {
- crate::jobs_ui::cancel(app, photocraft_engine::jobs::JobId(j));
- ok(json!({"cancelled": 1}))
+ let cancelled = crate::jobs_ui::cancel(app, photocraft_engine::jobs::JobId(j));
+ ok(json!({"cancelled": usize::from(cancelled)}))
}
Some(j) => err(format!("no running job {j}")),
None if all => {
let ids: Vec<_> = app.session.jobs().into_iter().map(|j| j.id).collect();
- for j in &ids {
- crate::jobs_ui::cancel(app, *j);
- }
- ok(json!({"cancelled": ids.len()}))
+ let cancelled = ids.into_iter().filter(|j| crate::jobs_ui::cancel(app, *j)).count();
+ ok(json!({"cancelled": cancelled}))
}
None => err("`job` must be a job id"),
}
diff --git a/crates/ui-egui/src/jobs_ui.rs b/crates/ui-egui/src/jobs_ui.rs
index f0d10c3de..f15a464a8 100644
--- a/crates/ui-egui/src/jobs_ui.rs
+++ b/crates/ui-egui/src/jobs_ui.rs
@@ -391,7 +391,7 @@ pub fn job_in_view(app: &PhotocraftApp) -> Option<JobInfo> {
}
/// Cancel job `id` (the status bar's ×, the dialog's Cancel, Esc, closing an opening tab).
-pub fn cancel(app: &mut PhotocraftApp, id: JobId) {
+pub fn cancel(app: &mut PhotocraftApp, id: JobId) -> bool {
let label = app.session.job(id).map(|j| j.label);
// A save that has started replacing the file runs to the end: cancelling it now would leave
// the file written but the document marked unsaved.
@@ -400,15 +400,17 @@ pub fn cancel(app: &mut PhotocraftApp, id: JobId) {
{
app.ui.status = crate::i18n::fmt(tl!("{label} is writing the file and can no longer be cancelled"), &[("label", label.as_deref().unwrap_or_default())]);
app.ui.status_error = false;
- return;
+ return false;
}
- if app.session.cancel_job(id) {
+ let cancelled = app.session.cancel_job(id);
+ if cancelled {
// The event (and its status text) arrives with the next poll; say so now as well.
if let Some(l) = label {
app.ui.status = crate::i18n::fmt(tl!("Cancelled {label}"), &[("label", &l)]);
app.ui.status_error = false;
}
}
+ cancelled
}
fn on_event(app: &mut PhotocraftApp, ctx: &egui::Context, e: JobEvent) {
diff --git a/crates/ui-egui/src/jobs_ui_tests.rs b/crates/ui-egui/src/jobs_ui_tests.rs
index 2e6114293..2ab8f7e94 100644
--- a/crates/ui-egui/src/jobs_ui_tests.rs
+++ b/crates/ui-egui/src/jobs_ui_tests.rs
@@ -615,6 +615,10 @@ 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 (request, _) = crate::control::ControlRequest::new("jobs.cancel", json!({"job": id.0}));
+ let ctx = h.ctx.clone();
+ let crate::control::Outcome::Done(reply) = crate::control::handle(h.state_mut(), &ctx, &request) else { panic!("cancel replies immediately") };
+ assert_eq!(reply["result"]["cancelled"], 0, "control must not falsely report cancellation: {reply}");
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");