mirror of
https://github.com/storytold/deckcraft.git
synced 2026-12-01 10:13:09 -06:00
control: take requests once a frame before its input, so its command states include them
eframe runs App::logic in every pass of a frame, but the frame's command states
are taken once, in raw_input_hook. A request drained in a frame's second pass
(insert.smartArt makes one) ran after that snapshot, so the pass drew the
SmartArt Design tab with Change Colors disabled and a labelled click found it
'not clickable'. A request also waited a pass between admission and its first
injected step, and a late layout change in that pass withdrew the click before
it ran ('input cancelled').
drain_control now runs in raw_input_hook, before the command states and the
injected input hook: every pass of the frame shows what the requests did, and a
request's first step enters the frame that admitted it. eframe's logic-only
iteration calls raw_input_hook too, so a hidden window still answers.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
c57351bf5e
commit
a5fa8ed3b4
@@ -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"))]
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<dyn Error>> {
|
||||
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<Value, Box<dyn Error>> {
|
||||
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"))]
|
||||
|
||||
@@ -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}");
|
||||
|
||||
Reference in New Issue
Block a user