diff --git a/crates/ui-contract/src/runtime/egui/areas.rs b/crates/ui-contract/src/runtime/egui/areas.rs index 1a30412836..0e814afa02 100644 --- a/crates/ui-contract/src/runtime/egui/areas.rs +++ b/crates/ui-contract/src/runtime/egui/areas.rs @@ -551,6 +551,12 @@ pub(crate) fn host_popup(ui: &Ui, id: egui::Id, at: Pos2) { egui::Popup::new(id, ui.ctx().clone(), at, ui.layer_id()).open_memory(None).show(|ui| ui.allocate_space(ui.spacing().interact_size)); } +/// A host's own egui window `id` around `body`: its first pass is a sizing pass, in an invisible `Ui`. +#[cfg(feature = "suite")] +pub(crate) fn host_egui_window(ctx: &Context, id: Id, body: impl FnOnce(&mut Ui)) { + egui::Window::new("host").id(id).show(ctx, body); +} + /// The size of one entry of [`host_popup_list`], as an app's popup list entry. #[cfg(feature = "suite")] const HOST_LIST_ENTRY: Vec2 = Vec2::new(104.0, 22.0); diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index c94ed1c6b2..1bac559987 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -257,9 +257,9 @@ pub(crate) fn register_stop(ctx: &Context, stop: Stop) { && let Some(rec) = state.stops.iter_mut().find(|rec| rec.stop.host && rec.stop.id == stop.id) { state.stop_roots.insert(stop.root); + let acts = if held && state.given == Some(stop.root) { vec![nav::focus_act(&stop)] } else { Vec::new() }; rec.stop = stop; - let rec = rec.clone(); - return if held && state.given == Some(rec.stop.root) { state.enter_stop(rec, FocusOrigin::Request) } else { Vec::new() }; + return acts; } state.push_stop(stop) }); @@ -338,8 +338,8 @@ fn take_app_widgets(ctx: &Context, except: Option) { /// egui widget the app draws for it in every such pass (`Sense::click()`, `Sense::drag()`; the bare flags `Sense::CLICK` /// and `Sense::DRAG` are not focusable). egui drops the focus of an id that no such widget of the pass has, so Tab would /// lose the focus at the canvas and start over at the window's first stop. A pass that declares a canvas and draws no -/// such widget reports the finding `canvas-without-widget`, and debug builds log it once. A pass that hides or disables -/// the canvas makes no call. +/// such widget reports the finding `canvas-without-widget`, and debug builds log it once; a canvas drawn in an invisible +/// `Ui` (an egui sizing pass) is not reported as disabled. A pass that hides or disables the canvas makes no call. pub fn host_canvas(ctx: &Context, id: Id, keys: KeyUse) { if !crate::runtime::measure::active(ctx) { with(ctx, |state| state.host_canvases.push(id)); @@ -1127,13 +1127,14 @@ pub(crate) fn surrender_after_scopes(ctx: &Context, widget: Id) { /// Reports each canvas the running pass declared (`host_canvas`) whose id no focusable, enabled egui widget of the pass /// has. Read at the pass's end, since a canvas may be declared before it is drawn. A canvas hidden for one pass and still -/// declared counts: egui drops its focus at the end of that pass. +/// declared counts: egui drops its focus at the end of that pass. Only a visible widget counts as disabled: egui disables +/// every widget of an invisible `Ui`, as the sizing pass of an `Area`, a `Window` or a `Grid` has. fn report_undrawn_canvases(ctx: &Context) { for id in with(ctx, |state| state.host_canvases.clone()) { let fault = match egui_focus::drawn_widget(ctx, id) { None => "no egui widget with the id was drawn", Some(widget) if !widget.sense.is_focusable() => "its widget's sense takes no focus", - Some(widget) if !widget.enabled => "its widget is disabled, which takes no focus", + Some(widget) if widget.visible && !widget.enabled => "its widget is disabled, which takes no focus", Some(_) => continue, }; if cfg!(debug_assertions) { diff --git a/crates/ui-contract/src/runtime/focus/nav.rs b/crates/ui-contract/src/runtime/focus/nav.rs index 3fa62bd52c..d7d299d379 100644 --- a/crates/ui-contract/src/runtime/focus/nav.rs +++ b/crates/ui-contract/src/runtime/focus/nav.rs @@ -1,7 +1,7 @@ //! Focus movement: Tab and F6, roving groups, initial focus, restore, programmatic requests and focus after //! removal. Every movement reads last pass's registrations and returns the egui changes to apply. -use super::{Act, BeginIn, FocusOrigin, FocusTarget, Initial, PickedIn, State, StopKey, StopRec, TabReach}; +use super::{Act, BeginIn, FocusOrigin, FocusTarget, Initial, PickedIn, State, Stop, StopKey, StopRec, TabReach}; use crate::runtime::egui::focus as egui_focus; use crate::runtime::egui::memory::FastMap; use craft_behavior::nav::step; @@ -262,27 +262,16 @@ impl State { self.enter_stop(rec, FocusOrigin::Press) } - pub(super) fn enter_stop(&mut self, rec: StopRec, origin: FocusOrigin) -> Vec { + fn enter_stop(&mut self, rec: StopRec, origin: FocusOrigin) -> Vec { self.note_entry(&rec); let stop = &rec.stop; - let roving = stop.group.is_some(); - let keys = stop.keys; - // The app's canvas (`host_canvas`) reads its Escape from egui, as a stop that claims it does: egui keeps its focus - // on it. Every other widget of the app's own keeps Escape by its own egui filter. - let escape = (stop.host && !stop.automatic()) || keys.claims.iter().any(|chord| chord.key == craft_keymap::Key::Escape); - let filter = egui_focus::filter( - keys.keep == super::KeepKeys::Tab, - roving || matches!(keys.keep, super::KeepKeys::InlineArrows | super::KeepKeys::BlockArrows), - roving || keys.keep == super::KeepKeys::BlockArrows, - escape, - ); let moved = self.focus_now != Some(stop.root) || self.focused.get(&stop.root) != Some(&stop.key()); if moved { self.arrived = Some((self.frame, stop.key(), origin)); } self.focused.insert(stop.root, stop.key()); self.focus_now = Some(stop.root); - vec![Act::Focus(stop.root, filter)] + vec![focus_act(stop)] } /// Focus moving to `rec` from outside a scope that traps nothing and restores focus (a layer that Tab reaches, such as @@ -745,6 +734,22 @@ impl State { } } +/// Focus on the egui widget of `stop`, with the key filter its keys ask for. +pub(super) fn focus_act(stop: &Stop) -> Act { + let roving = stop.group.is_some(); + let keys = stop.keys; + // The app's canvas (`host_canvas`) reads its Escape from egui, as a stop that claims it does: egui keeps its focus + // on it. Every other widget of the app's own keeps Escape by its own egui filter. + let escape = (stop.host && !stop.automatic()) || keys.claims.iter().any(|chord| chord.key == craft_keymap::Key::Escape); + let filter = egui_focus::filter( + keys.keep == super::KeepKeys::Tab, + roving || matches!(keys.keep, super::KeepKeys::InlineArrows | super::KeepKeys::BlockArrows), + roving || keys.keep == super::KeepKeys::BlockArrows, + escape, + ); + Act::Focus(stop.root, filter) +} + fn cycle(entries: &[Entry], current: Option<&StopRec>, top: Option, forward: bool) -> Option { if entries.is_empty() { return None; diff --git a/crates/ui-contract/src/testing/host_key_trials.rs b/crates/ui-contract/src/testing/host_key_trials.rs index 8c44785edf..bf22eedf3c 100644 --- a/crates/ui-contract/src/testing/host_key_trials.rs +++ b/crates/ui-contract/src/testing/host_key_trials.rs @@ -1180,6 +1180,41 @@ fn a_host_canvas_marks_the_window_and_tab_toggles_its_panels() { ); } +/// Focus that Tab moves to the app's canvas in a layer returns to the stop it came from when the layer goes away, also +/// when the group the canvas sits in first appears in the pass of the Tab: declaring the canvas enters nothing. +#[trial] +fn declaring_a_focused_canvas_keeps_the_opener_of_its_layer() { + let (opener, canvas) = (named_id("opener"), named_id("host canvas")); + let layer = Rc::new(Cell::new(true)); + let grouped = Rc::new(Cell::new(false)); + let (shown, group) = (Rc::clone(&layer), Rc::clone(&grouped)); + let mut show = move |ui: &mut Ui| { + widget(ui, opener); + register_stop(ui.ctx(), Stop::new(opener, KeyUse::of(Input::Pointer))); + if !shown.get() { + return; + } + push_scope(ui.ctx(), named_id("layer"), FocusPolicy::TAB_LAYER, None); + if group.get() { + push_scope(ui.ctx(), named_id("group"), FocusPolicy::NONE, None); + } + widget(ui, canvas); + host_canvas(ui.ctx(), canvas, KeyUse::CANVAS); + if group.get() { + pop_scope(ui.ctx()); + } + pop_scope(ui.ctx()); + }; + let mut frames = Frames::new(); + frames.idle(2, &mut show); + focus_stop(&mut frames, &mut show, opener); + grouped.set(true); + assert_eq!(tab_with(&mut frames, &mut show, Modifiers::NONE), Some(canvas), "Tab goes from the stop into the layer"); + layer.set(false); + frames.idle(2, &mut show); + assert_eq!(egui_focused(&frames.ctx), Some(opener), "the layer gives the focus back to the stop it came from"); +} + /// How an app's window shows the canvas of `host_canvas_findings`. #[derive(Clone, Copy, Debug)] enum Shown { @@ -1188,6 +1223,8 @@ enum Shown { /// Declared, and drawn with a focusable widget that senses clicks or drags. Click, Drag, + /// Declared, and drawn with a focusable widget in an egui window, whose first pass sizes it invisibly. + Window, /// Declared, and drawn with a widget that senses drags and is not focusable. Unfocusable, /// Declared, and drawn in a disabled `Ui`. @@ -1209,6 +1246,7 @@ fn host_canvas_findings(shown: Shown) -> (bool, Vec) { Shown::Hidden | Shown::Undrawn => {} Shown::Click => _ = fixture::host_button(ui, canvas, rect), Shown::Drag => _ = fixture::host_drag_area(ui, canvas, rect), + Shown::Window => areas::host_egui_window(ui.ctx(), named_id("host window"), |ui| _ = widget(ui, canvas)), Shown::Unfocusable => fixture::host_unfocusable_area(ui, canvas, rect), Shown::Disabled => fixture::host_disabled_button(ui, canvas, rect), } @@ -1225,7 +1263,7 @@ fn host_canvas_findings(shown: Shown) -> (bool, Vec) { /// focusable egui widget of the pass has. #[trial] fn a_host_canvas_no_focusable_widget_draws_is_a_finding() { - for shown in [Shown::Click, Shown::Drag] { + for shown in [Shown::Click, Shown::Drag, Shown::Window] { assert_eq!(host_canvas_findings(shown), (true, Vec::new()), "{shown:?}: Tab stays at a canvas with a focusable widget"); } assert_eq!(host_canvas_findings(Shown::Hidden).1, Vec::::new(), "a hidden canvas makes no call");