diff --git a/crates/ui-contract/src/runtime/frame.rs b/crates/ui-contract/src/runtime/frame.rs index e810c741b..7c8ce6fa5 100644 --- a/crates/ui-contract/src/runtime/frame.rs +++ b/crates/ui-contract/src/runtime/frame.rs @@ -175,6 +175,8 @@ pub struct FrameOutput { #[derive(Default)] pub(crate) struct FrameState { input: Option, + /// The root pass number when `set_input` was last called, so test drivers know whether a frame's input is set. + input_pass: Option, /// The window of `FrameInput::app_modal` and the root frame its modal first showed in: its place in the modal stack /// (`overlay::modal_viewport`). It stays while the app names the same window frame after frame. app_modal: Option<(ViewportId, u64)>, @@ -413,6 +415,7 @@ pub fn set_input(ctx: &Context, input: FrameInput) { (viewport, _) => viewport.map(|viewport| (viewport, pass::root_frame_nr(ctx))), }; state.input = Some(input); + state.input_pass = Some(root_passes); ran }); commands::set_input(ctx, &commands); @@ -422,6 +425,14 @@ pub fn set_input(ctx: &Context, input: FrameInput) { scale::set_interface_scale(ctx, scale.0); } +/// Whether [`set_input`] has already run for the upcoming root pass. +#[cfg(feature = "testing")] +#[doc(hidden)] +pub fn input_pending(ctx: &Context) -> bool { + let root_passes = pass::pass_nr(ctx, ViewportId::ROOT); + pass_state::with::(ctx, |state| state.input_pass == Some(root_passes)) +} + /// The tokens the app's `sizes` give the default density: the built-in sets and the overrides apply in /// `craft_theme`'s resolve, whichever theme carries them. fn resolved_default(sizes: &AppSizes) -> craft_theme::Tokens { diff --git a/crates/ui-shell/tests/it/shell/embed.rs b/crates/ui-shell/tests/it/shell/embed.rs index c5a6fbb2e..f3c268241 100644 --- a/crates/ui-shell/tests/it/shell/embed.rs +++ b/crates/ui-shell/tests/it/shell/embed.rs @@ -1,17 +1,20 @@ //! An app that drives its own frames hosts the runtime through the `embed` hooks alone. use craft_behavior::Intent; -use craft_ui::contract::{Bind, Role, ShowContainer as _}; +use craft_ui::contract::{Bind, Props as _, Role, ShowContainer as _}; use craft_ui::dialog::Dialog; use craft_ui::fonts::{I18nScripts, UiFonts}; +use craft_ui::runtime::commands::{AppCatalog, Binding, CommandDef, CommandInput, FirePhase, Scope}; use craft_ui::runtime::env::RootEnv; use craft_ui::runtime::frame::FrameInput; use craft_ui::runtime::keys; use craft_ui::sections::{DialogRole, FooterButton}; +use craft_ui::shell::DocumentWindow; use craft_ui::shell::embed::{self, CloseDecision}; use craft_ui::testing::focus::key_release; use craft_ui::testing::input::key_press; use craft_ui::testing::{Frames, install_spec, trial}; +use craft_ui_test_kit::windows::Windows; use egui::{Event, Key, Modifiers}; /// The app's own state: its dialog's open state and the Escapes it read at window level. @@ -71,3 +74,40 @@ fn an_app_with_its_own_frame_loop_hosts_a_dialog_and_reads_the_escape_no_compone escape(&mut frames, &mut app); assert_eq!(app.window_escapes, 1, "the app reads the Escape no component took"); } + +/// A `FrameInput` installed via `embed::before_input` survives `Windows::frame` and reaches a child viewport pass. +#[trial] +fn before_input_survives_windows_frame_and_reaches_child_viewport() { + const CLOSE: &str = "trialEmbed.close"; + let catalog = AppCatalog::new( + vec![CommandDef::new(CLOSE, "Close", Scope::app()).with_default(Binding::parse("Cmd+W").expect("the trial's chord parses"))], + 1, + ) + .expect("the trial's catalog is valid"); + let windows = Windows::new(); + let frame_input = || FrameInput::new(RootEnv::default()).commands(CommandInput::new().catalog(catalog.clone())); + let show = |root: Vec, child: Vec| { + windows.frame(root, child, |ui| { + embed::logic(ui.ctx(), || CloseDecision::Close); + DocumentWindow::new("Document").id_salt("doc").show(ui, |_, _| {}); + }); + }; + + embed::before_input(&windows.ctx, |_| frame_input()); + show(vec![], vec![]); + + embed::before_input(&windows.ctx, |_| frame_input()); + show(vec![], vec![key_press(Key::W, Modifiers::COMMAND)]); + + let mut fired = Vec::new(); + embed::before_input(&windows.ctx, |out| { + fired = out.fired; + frame_input() + }); + show(vec![], vec![key_release(Key::W, Modifiers::COMMAND)]); + + assert!( + fired.iter().any(|item| item.command.id == CLOSE && item.phase == FirePhase::Press), + "the shortcut installed by embed::before_input must survive Windows::frame and fire in the child viewport; got {fired:?}", + ); +} diff --git a/crates/ui-test-kit/src/windows.rs b/crates/ui-test-kit/src/windows.rs index e6514eb36..bdbd816ee 100644 --- a/crates/ui-test-kit/src/windows.rs +++ b/crates/ui-test-kit/src/windows.rs @@ -8,7 +8,7 @@ use craft_host::target::ShellInfo; use craft_ui::runtime::InstallSpec; use craft_ui::runtime::env::RootEnv; -use craft_ui::runtime::frame::{FrameInput, WindowInput, set_input}; +use craft_ui::runtime::frame::{FrameInput, WindowInput, input_pending, set_input}; use craft_ui::testing::Frames; use craft_ui::testing::access::{AccessTree, tree_of}; use craft_ui::testing::windows::FakeSystem; @@ -407,7 +407,9 @@ impl Windows { CHILD_FILES.with(|files| *files.borrow_mut() = child_files); TREES.with(|kept| kept.borrow_mut().clear()); SHAPES.with(|kept| kept.borrow_mut().clear()); - set_input(&self.ctx, FrameInput::new(self.root.borrow().clone()).app_modal(self.app_modal.get()).window_input(self.window_input.get())); + if !input_pending(&self.ctx) { + set_input(&self.ctx, FrameInput::new(self.root.borrow().clone()).app_modal(self.app_modal.get()).window_input(self.window_input.get())); + } let mut input = RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, ROOT_SIZE)), events: root,