diff --git a/crates/text/src/ui/editor.rs b/crates/text/src/ui/editor.rs index fad53f083..8d8a85a0e 100644 --- a/crates/text/src/ui/editor.rs +++ b/crates/text/src/ui/editor.rs @@ -40,7 +40,7 @@ use craft_ui::contract::{ use craft_ui::primitives::{EditorLine, EditorPurpose, EditorRun, EditorState, PaintShape, StrokeWidth}; use craft_ui::runtime::commands::ClipItem; use craft_ui::runtime::env::Label; -use craft_ui::runtime::focus::KeyUse; +use craft_ui::runtime::focus::{FocusPolicy, KeyUse}; use craft_ui::runtime::pending::Pending; use craft_ui::style::{ComponentStyle, StyleColor, StyleCx}; use egui::{Pos2, Rect, Response, Vec2}; @@ -461,6 +461,8 @@ impl Component for TextEditor<'_> { const INPUT: craft_ui::contract::Input = craft_ui::contract::Input::Editor; /// Escape always ends the session, so the editor keeps it from its row and from the shortcuts while focused. const KEYS: KeyUse = KeyUse::CODE.with_claims(&[ESCAPE]); + /// When the session ends, focus goes back to the stop it came from (the canvas), not to the stop before the editor. + const FOCUS_SCOPE: FocusPolicy = FocusPolicy::TAB_LAYER; type Style = TextEditorStyle; type Output = TextEditorOutput; diff --git a/crates/ui-contract/src/contract/mod.rs b/crates/ui-contract/src/contract/mod.rs index 7b44688d6..84cf4a8d7 100644 --- a/crates/ui-contract/src/contract/mod.rs +++ b/crates/ui-contract/src/contract/mod.rs @@ -356,6 +356,12 @@ pub trait Component: Meta + Sized { /// Whether the component or its parts can take key focus: `false` skips the stop registration of step 7, except /// for the roving group of a body the shape declares. const STOPS: bool = true; + /// The focus scope a leaf runs in, as `ContainerProps::focus` gives a container's: the leaf's own stop and its + /// parts lie inside it, and `FocusPolicy::NONE` (the default) opens none. A scope that restores + /// (`FocusPolicy::TAB_LAYER`) makes the stop focus came from its opener, so focus goes back there when the leaf + /// goes away with focus inside (a canvas text editor hands it back to the canvas). Containers and collections take + /// their scope from their props, not from this. + const FOCUS_SCOPE: crate::runtime::focus::FocusPolicy = crate::runtime::focus::FocusPolicy::NONE; /// Keys the focused component keeps from egui and the shortcut dispatcher. const KEYS: crate::runtime::focus::KeyUse = crate::runtime::focus::KeyUse::of(Self::INPUT); /// The keys this instance keeps in its current state. The default is `KEYS`; a component whose key use depends diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index caa428823..2cb5c7bae 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -317,6 +317,10 @@ impl ShapeHooks for LeafHooks { self.scope } + fn focus(&self, _: &C) -> Option { + (C::FOCUS_SCOPE != crate::runtime::focus::FocusPolicy::NONE).then_some(C::FOCUS_SCOPE) + } + fn sized(&self, comp: &C, ui: &Ui, id: Id, size: Vec2) -> Vec2 { use craft_behavior::Axis; let room = crate::layout::leaf_fill_room(ui); diff --git a/docs/contract-api.md b/docs/contract-api.md index 70e62b511..5d27b1e54 100644 --- a/docs/contract-api.md +++ b/docs/contract-api.md @@ -1837,6 +1837,7 @@ The component traits, context types, `PaintUi`, `Looks`, the pipeline and its st - `const OVERLAYS: bool`. Whether the component hosts popups: `false` skips the overlay bookkeeping of steps 9, 10, 12, 15 and 16. - `const PARTS: bool`. Whether the component declares parts, a text part or an editor part: `false` skips the parts step (the parts - `const STOPS: bool`. Whether the component or its parts can take key focus: `false` skips the stop registration of step 7, except + - `const FOCUS_SCOPE: crate::runtime::focus::FocusPolicy`. The focus scope a leaf runs in, as `ContainerProps::focus` gives a container's: the leaf's own stop and its - `const KEYS: crate::runtime::focus::KeyUse`. Keys the focused component keeps from egui and the shortcut dispatcher. - `fn keys(&self) -> crate::runtime::focus::KeyUse`. The keys this instance keeps in its current state. The default is `KEYS`; a component whose key use depends - `const SHOWS_LABEL: bool`. Whether the component paints its label as visible text. `false` for a face that shows only an icon or a