PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(ui): keep filter jobs polling on idle Linux (#3961)
McClabebe877c27d0cb
diff --git a/crates/ui-egui/src/jobs_ui.rs b/crates/ui-egui/src/jobs_ui.rsindex 301460fc5..f0164c197 100644--- a/crates/ui-egui/src/jobs_ui.rs+++ b/crates/ui-egui/src/jobs_ui.rs@@ -368,11 +368,9 @@ pub fn tick(app: &mut PhotocraftApp, ctx: &egui::Context) {// Keep checking on a cancelled save's worker until it returns.ctx.request_repaint_after(std::time::Duration::from_millis(50));}- if !app.session.has_jobs() {+ if !keep_polling(app, ctx) {return;}- // Animate progress and poll for results at display rate; the UI thread only draws.- ctx.request_repaint_after(std::time::Duration::from_millis(16));if let Some(job) = job_in_view(app)&& !ctx.text_edit_focused()&& ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape))@@ -381,6 +379,21 @@ pub fn tick(app: &mut PhotocraftApp, ctx: &egui::Context) {}}+/// Keep the event loop awake while a background document job exists.+///+/// Jobs can be started by a menu or dialog while the current frame is being drawn, after+/// [`tick`] has already run. Calling this from the shell after drawing avoids leaving such a job+/// unapplied on an otherwise idle event loop (notably Wayland): its document would remain locked,+/// making Undo and Redo appear unavailable.+pub fn keep_polling(app: &PhotocraftApp, ctx: &egui::Context) -> bool {+ if !app.session.has_jobs() {+ return false;+ }+ // Animate progress and poll for results at display rate; the UI thread only draws.+ ctx.request_repaint_after(std::time::Duration::from_millis(16));+ true+}+/// The job the user is looking at: the open tab in view, the active document's job, or (for/// the status bar) the newest running job.pub fn job_in_view(app: &PhotocraftApp) -> Option<JobInfo> {diff --git a/crates/ui-egui/src/jobs_ui_tests.rs b/crates/ui-egui/src/jobs_ui_tests.rsindex 1893075ac..9d5c15506 100644--- a/crates/ui-egui/src/jobs_ui_tests.rs+++ b/crates/ui-egui/src/jobs_ui_tests.rs@@ -104,6 +104,27 @@ fn the_frame_loop_keeps_running_during_a_slow_job_and_esc_cancels_it() {assert!(h.query_by_label("Esc to cancel").is_none(), "the dialog closed");}+#[test]+fn a_job_started_while_drawing_requests_the_follow_up_poll() {+ let mut h = app_harness();+ let gate = Arc::new(AtomicBool::new(false));+ let job = slow_job(h.state_mut(), &gate);+ let repaints = Arc::new(AtomicUsize::new(0));+ let seen = repaints.clone();+ h.ctx.set_request_repaint_callback(move |_| {+ seen.fetch_add(1, Ordering::Relaxed);+ });++ // A job may start from a menu or dialog after the frame's initial `jobs_ui::tick`.+ // The UI must still schedule a poll, or an idle Wayland event loop leaves the document+ // locked after the worker finishes and blocks Edit > Undo / Redo.+ assert!(crate::jobs_ui::keep_polling(h.state(), &h.ctx));+ assert_eq!(repaints.load(Ordering::Relaxed), 1);++ crate::jobs_ui::cancel(h.state_mut(), job);+ h.run_steps(2);+}+#[test]fn a_finished_job_lands_as_one_undo_step_and_the_dialog_cancel_button_works() {let mut h = app_harness();diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex a16dca00d..4dcc72569 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -1732,6 +1732,9 @@ impl eframe::App for PhotocraftApp {let _ = w.send(serde_json::json!({"ok": true, "result": null}));}}+ // A menu or dialog can start a filter after `jobs_ui::tick` ran in this frame. Keep+ // polling it even when the platform would otherwise put the event loop to sleep.+ jobs_ui::keep_polling(self, &ctx);// Menus and buttons in this frame may have asked for a file dialog.self.poll_file_dialog(&ctx, Some(frame));}