diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 6d4a58bea..e673a5eae 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -324,6 +324,9 @@ impl SlideApp { if !Self::root_viewport_admitted(ctx) { return; } + if WindowHost::of(ctx).injected_idle() { + self.drain_control(ctx); + } #[cfg(not(target_arch = "wasm32"))] if let Some(intake) = &self.intake { let dropped = std::mem::take(&mut raw.dropped_files) @@ -373,7 +376,6 @@ impl SlideApp { } self.tick_hidden_show(ctx, web_time::Instant::now()); if WindowHost::of(ctx).injected_idle() { - self.drain_control(ctx); self.drain_intake(ctx); self.poll_opening(); #[cfg(not(target_arch = "wasm32"))] diff --git a/crates/ui-egui/src/app/control.rs b/crates/ui-egui/src/app/control.rs index 8d1ecf1c7..e356112bc 100644 --- a/crates/ui-egui/src/app/control.rs +++ b/crates/ui-egui/src/app/control.rs @@ -5,8 +5,8 @@ use super::SlideApp; use crate::control; impl SlideApp { - /// Drains requests between injected gestures; each pass takes only its own available work. - /// A rerun sees the requests still in the channel, never a replay of the first pass's drain. + /// Drains requests between injected gestures, once a frame before its input: a request is input, which enters a + /// frame's first pass only, and the command states the frame's passes show are taken after it ran. pub(crate) fn drain_control(&mut self, ctx: &egui::Context) { let Some(mut channel) = self.control.take() else { return }; let host = WindowHost::of(ctx); diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index eabf07a3a..76302671d 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -1094,6 +1094,48 @@ mod tests { Ok(()) } + /// A pass shows the command states of every request taken before it: a diagram inserted in the same frame as the + /// switch to its tab leaves Change Colors enabled there, and a labelled click opens its colour menu. + #[test] + fn requests_run_before_the_frame_takes_its_command_states() -> Result<(), Box> { + let (sender, receiver) = mpsc::channel(); + let mut session = deckcraft_engine::Session::with_new(); + deckcraft_engine::sample::open_sample(&mut session)?; + let mut harness = harness_with( + session, + crate::Services::default(), + egui_kittest::Harness::builder().with_size(egui::vec2(1440.0, 900.0)).with_step_dt(1.0 / 60.0), + |platform| platform.control = Some(ControlChannel::new(receiver)), + ); + harness.run_steps(3); + let input = |harness: &mut egui_kittest::Harness<'static, SlideApp>, method: &str, params: Value| -> Result> { + let (request, reply) = ControlRequest::new(method, params); + sender.send(request).map_err(|_| "control channel closed")?; + for _ in 0..30 { + harness.run_steps(1); + if let Ok(answer) = reply.try_recv() { + return Ok(answer); + } + } + Err(format!("{method} was not answered").into()) + }; + // Both requests are in the channel when the frame starts, so both are taken before its passes. + let smartart = json!({"command": "insert.smartArt", "params": {"kind": "process", "items": ["Plan", "Shoot", "Edit"]}}); + let (insert, inserted) = ControlRequest::new("engine.execute", smartart); + let (tab, shown) = ControlRequest::new("ui.set", json!({"tab": "SmartArt Design"})); + sender.send(insert).map_err(|_| "control channel closed")?; + sender.send(tab).map_err(|_| "control channel closed")?; + harness.run_steps(1); + for reply in [inserted.try_recv()?, shown.try_recv()?] { + assert_eq!(reply["ok"], true, "{reply}"); + } + let reply = input(&mut harness, "ui.click", json!({"label": "Change Colors"}))?; + assert_eq!(reply["ok"], true, "{reply}"); + harness.run_steps(3); + assert!(harness.query_by_label("Colorful").is_some(), "the colour menu is open"); + Ok(()) + } + /// A request from a client's own thread is answered by the UI thread's frame: the connection thread only /// forwards it and asks for a repaint, so it never touches a running pass's state. #[cfg(not(target_arch = "wasm32"))] diff --git a/crates/ui-egui/src/tests.rs b/crates/ui-egui/src/tests.rs index 93179f575..3eb981138 100644 --- a/crates/ui-egui/src/tests.rs +++ b/crates/ui-egui/src/tests.rs @@ -1254,7 +1254,7 @@ fn show_start_refuses_a_from_that_is_not_a_slide() { assert!(h.state_mut().run("show.start", json!({"from": from})).is_err(), "{from}"); let (req, reply) = craft::automation::control::ControlRequest::new("show.start", json!({"from": from})); tx.send(req).unwrap(); - h.state_mut().logic(&ctx); + h.state_mut().raw_input_hook(&ctx, &mut egui::RawInput::default()); let answer = reply.recv_timeout(std::time::Duration::from_secs(1)).unwrap(); assert_eq!(answer["ok"], false, "{from}: {answer}"); assert!(h.state().show.is_none()); @@ -1268,7 +1268,7 @@ fn show_start_refuses_a_from_that_is_not_a_slide() { h.state_mut().show = None; let (req, reply) = craft::automation::control::ControlRequest::new("show.start", json!({"from": 2, "reading": true})); tx.send(req).unwrap(); - h.state_mut().logic(&ctx); + h.state_mut().raw_input_hook(&ctx, &mut egui::RawInput::default()); let answer = reply.recv_timeout(std::time::Duration::from_secs(1)).unwrap(); assert_eq!(answer["ok"], true, "{answer}"); assert_eq!(h.state().show.as_ref().map(|s| (s.state.slide, s.reading)), Some((2, true))); @@ -1339,6 +1339,8 @@ fn logic_drains_control_without_a_ui_pass() { tx.send(req).unwrap(); let mut raw = egui::RawInput::default(); raw.viewports.entry(egui::ViewportId::ROOT).or_default().minimized = Some(true); + // As eframe's logic-only iteration does: the input hook, then logic, with no UI pass. + h.state_mut().raw_input_hook(&ctx, &mut raw); let _ = ctx.run_logic(&raw, |ctx| h.state_mut().logic(ctx)); let answer = reply.recv_timeout(std::time::Duration::from_secs(1)).unwrap(); assert_eq!(answer["ok"], true, "{answer}");