diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index e2c9662509..d12c85d005 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -122,6 +122,7 @@ impl PhotocraftApp { synthetic_reinject: false, synthetic_snapshot: None, automation_input: false, + run_depth: 0, paused_playbacks: Default::default(), channel_thumbs: None, channel_views: HashMap::new(), diff --git a/crates/ui-egui/src/app/commands.rs b/crates/ui-egui/src/app/commands.rs index 8c99167b4c..14cce82bb8 100644 --- a/crates/ui-egui/src/app/commands.rs +++ b/crates/ui-egui/src/app/commands.rs @@ -35,6 +35,13 @@ impl PhotocraftApp { } pub(crate) fn run_command(&mut self, id: &str, params: Value) -> Result { + self.run_depth += 1; + let r = self.run_command_at_depth(id, params); + self.run_depth -= 1; + r + } + + fn run_command_at_depth(&mut self, id: &str, params: Value) -> Result { if !vector_ui::cancel_stale(self) { return Err(self.ui.status.clone()); } @@ -121,7 +128,9 @@ impl PhotocraftApp { self.sync_command_side_effects(id, &r, creates_active_mask, creates_active_vector_mask, creates_adjustment_or_fill, path_mask); self.perf.last_command = id.to_string(); self.perf.command_ms = gpu_canvas::now_ms() - t0; - self.perf.stage.command(id, self.perf.command_ms); + if self.run_depth == 1 { + self.perf.stage.command(id, self.perf.command_ms); + } match &r { Ok(_) => { if self.ui.status_error { diff --git a/crates/ui-egui/src/app/frame.rs b/crates/ui-egui/src/app/frame.rs index d7b5a688c5..df0de6ff0b 100644 --- a/crates/ui-egui/src/app/frame.rs +++ b/crates/ui-egui/src/app/frame.rs @@ -167,12 +167,67 @@ impl PhotocraftApp { self.automation_input = false; // No editor was drawn this pass: end its IME context rather than keep a stale owner. type_tool::sync_ime(self, &ctx); + // The frame's stage record was started with its input: close it here too. + self.finish_frame(0.0); return; } let t0 = gpu_canvas::now_ms(); self.perf.stage.ui_start(t0, frame.info().cpu_usage); // A software adapter or CPU mode chosen at startup is every document's reason. crate::render_path::frame(self); + // Every frame that started its stage record closes it, whichever way `ui_body` returns. + let drawn = self.ui_body(ui); + let ui_ms = gpu_canvas::now_ms() - t0; + // A GPU measurement lands a frame or two after the refresh it belongs to. Refreshes + // carry them out, but the last refresh before the app settles has none following it, so + // drain the timer here too or its number is never reported. Drain it either way: a + // measurement left sitting would surface later against an unrelated refresh. Report it + // only while the canvas is still on the GPU, since a CPU refresh clears `gpu_ms` + // precisely so a GPU number is never shown as that refresh's. + if let Some(gpu) = &self.gpu + && let Some(t) = gpu.take_gpu_timing() + && self.perf.last_refresh.starts_with("gpu") + { + self.perf.record_gpu(t); + } + self.perf.frame(ui_ms); + self.finish_frame(ui_ms); + if !drawn { + return; + } + // This pass handled its input: the in-flight synthetic batch is delivered once and the + // next step may be injected on the next frame. + self.finish_synthetic_pass(); + // Synthetic input is injected one press/release step per frame: keep frames coming until + // the queue is empty, then release control replies waiting on it. + // Only a pass that reached the end of the UI and delivered the in-flight batch counts as + // settled input; an early-returning pass keeps the batch (and the waiting replies). + if !self.synthetic.is_empty() || self.synthetic_batch.is_some() { + ctx.request_repaint(); + } else if !self.input_waiters.is_empty() { + for w in self.input_waiters.drain(..) { + 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)); + // The Type tool's IME descriptor is the last writer of this pass: every panel and the + // canvas have finished mutating the model, and an absent/focus-moved editor ends here. + type_tool::sync_ime(self, &ctx); + // Issue any due `ui.screenshot` now that this pass's state is settled: a queued request + // that has waited its passes and animation delay captures exactly this frame. + self.issue_captures(&ctx); + // Last, after everything drawn this frame: a lost window device can't draw it (#3438). + gpu_status::check_window(self, &ctx); + } + + /// The frame's UI, between `ui_start` and `finish_frame`. False when the screen picker's + /// wait modal took the frame (its input, IME and window check already handled). + fn ui_body(&mut self, ui: &mut egui::Ui) -> bool { + let ctx = ui.ctx().clone(); // A text field's right-click menu, before any field is drawn (text_field_menu.rs). text_field_menu::show(&ctx); // The wheel goes to a focused value field before the canvas or a panel sees it (#5473). @@ -203,7 +258,7 @@ impl PhotocraftApp { self.automation_input = false; type_tool::sync_ime(self, &ctx); gpu_status::check_window(self, &ctx); - return; + return false; } if screen_picker::busy(&ctx) { ui.disable(); @@ -298,47 +353,6 @@ impl PhotocraftApp { egui::Modal::new(egui::Id::unique("screen-color-wait")).frame(egui::Frame::NONE).backdrop_color(egui::Color32::TRANSPARENT).show(&ctx, |_| {}); ctx.set_cursor_icon(egui::CursorIcon::Wait); } - let ui_ms = gpu_canvas::now_ms() - t0; - // A GPU measurement lands a frame or two after the refresh it belongs to. Refreshes - // carry them out, but the last refresh before the app settles has none following it, so - // drain the timer here too or its number is never reported. Drain it either way: a - // measurement left sitting would surface later against an unrelated refresh. Report it - // only while the canvas is still on the GPU, since a CPU refresh clears `gpu_ms` - // precisely so a GPU number is never shown as that refresh's. - if let Some(gpu) = &self.gpu - && let Some(t) = gpu.take_gpu_timing() - && self.perf.last_refresh.starts_with("gpu") - { - self.perf.record_gpu(t); - } - self.perf.frame(ui_ms); - self.finish_frame(ui_ms); - // This pass handled its input: the in-flight synthetic batch is delivered once and the - // next step may be injected on the next frame. - self.finish_synthetic_pass(); - // Synthetic input is injected one press/release step per frame: keep frames coming until - // the queue is empty, then release control replies waiting on it. - // Only a pass that reached the end of the UI and delivered the in-flight batch counts as - // settled input; an early-returning pass keeps the batch (and the waiting replies). - if !self.synthetic.is_empty() || self.synthetic_batch.is_some() { - ctx.request_repaint(); - } else if !self.input_waiters.is_empty() { - for w in self.input_waiters.drain(..) { - 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)); - // The Type tool's IME descriptor is the last writer of this pass: every panel and the - // canvas have finished mutating the model, and an absent/focus-moved editor ends here. - type_tool::sync_ime(self, &ctx); - // Issue any due `ui.screenshot` now that this pass's state is settled: a queued request - // that has waited its passes and animation delay captures exactly this frame. - self.issue_captures(&ctx); - // Last, after everything drawn this frame: a lost window device can't draw it (#3438). - gpu_status::check_window(self, &ctx); + true } } diff --git a/crates/ui-egui/src/control/tests.rs b/crates/ui-egui/src/control/tests.rs index b3cf7454d1..1db2ec45ad 100644 --- a/crates/ui-egui/src/control/tests.rs +++ b/crates/ui-egui/src/control/tests.rs @@ -42,6 +42,23 @@ fn perf_output_includes_slow_frames() { for key in ["t", "frameMs", "inputMs", "commandMs", "damageMs", "compositeMs", "uploadMs", "gpuMs", "presentMs", "refresh", "refreshPx", "path"] { assert!(last.get(key).is_some(), "{key} in {last}"); } + // A nested run (`actions.play` → its view steps through `App::run`) is inside the outer + // call's time: the frame counts the outer command once, not the steps again. + app.session.actions.list.push(photocraft_engine::actions_cmds::Action { + name: "Zoom".into(), + steps: vec![("view.zoomIn".into(), json!({})), ("view.zoomOut".into(), json!({}))], + disabled: Default::default(), + ..Default::default() + }); + let played = app.run("actions.play", json!({"action": "Zoom"})).unwrap(); + assert_eq!(played["ran"], 2, "{played}"); + let outer_ms = app.perf.command_ms; + app.finish_frame(1.0); + let frames = call(&mut app, &ctx, "ui.inspect", json!({}))["result"]["perf"]["slowFrames"].clone(); + let last = frames.as_array().and_then(|f| f.last()).expect("a slow frame").clone(); + assert_eq!(last["command"], "actions.play", "{last}"); + let command_ms = last["commandMs"].as_f64().unwrap(); + assert!((command_ms - outer_ms).abs() < 1e-9, "the outer run's {outer_ms} ms, once: {last}"); } #[test] diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index fdac209ca4..22d3565ffa 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -505,6 +505,10 @@ pub struct PhotocraftApp { /// makes shortcut- and dialog-triggered commands pass the same policy as /// direct control calls. pub(crate) automation_input: bool, + /// How many `run_command` calls are on the stack. Only the outermost one writes the frame's + /// command stage: a nested run (`file.save` → `layer.smartObjects.saveContents`, + /// `actions.play` → its steps) is already inside the outer call's time. + run_depth: u32, /// Action playbacks paused at a step dialog, by the token in that dialog's fields /// ([`actions::RESUME`]): what to play next and the automation gate it paused under. pub(crate) paused_playbacks: actions::PausedPlaybacks,