focus: a leaf can open a focus scope; the canvas text editor returns the focus to the canvas

Component::FOCUS_SCOPE gives a leaf the scope ContainerProps::focus gives a container, through the leaf
shape's focus hook. TextEditor opens a scope that restores (FocusPolicy::TAB_LAYER), so a session that
ends hands the focus back to the stop it came from, the canvas, instead of the stop before the editor
(the rulers' corner, whose arrows moved the zero point after a commit; photocraft#15555). Regenerates
docs/contract-api.md.
This commit is contained in:
aiongg
2026-11-21 18:49:26 +00:00
parent 9de310681e
commit e826abef73
4 changed files with 14 additions and 1 deletions
+3 -1
View File
@@ -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;
+6
View File
@@ -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
+4
View File
@@ -317,6 +317,10 @@ impl<C: Component> ShapeHooks<C> for LeafHooks {
self.scope
}
fn focus(&self, _: &C) -> Option<crate::runtime::focus::FocusPolicy> {
(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);
+1
View File
@@ -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