diff --git a/crates/text/src/ui/editor.rs b/crates/text/src/ui/editor.rs index c58cd14b6..3f8c784f2 100644 --- a/crates/text/src/ui/editor.rs +++ b/crates/text/src/ui/editor.rs @@ -432,6 +432,12 @@ pub struct TextEditor<'a> { /// Whether Esc commits (Photoshop's default) or cancels the session. #[prop] esc_commits: bool, + /// Whether focus goes back to the stop it came from when the session ends (on by default: the canvas text editor + /// hands it back to the canvas). Off, the editor opens no focus scope, and an ended session leaves focus where the + /// removal of a stop leaves it: for a form field whose own part handed it the focus, returning there would open the + /// field again. + #[prop] + returns_focus: bool, /// What the editor is: the canvas text editor, or a one-line or multi-line form field. #[prop] mode: EditorMode, @@ -455,6 +461,7 @@ impl<'a> TextEditor<'a> { selection, smart_quotes: None, esc_commits: true, + returns_focus: true, mode: EditorMode::Canvas, masked: false, changed: false, @@ -625,6 +632,10 @@ impl Component for TextEditor<'_> { type Style = TextEditorStyle; type Output = TextEditorOutput; + fn focus_scope(&self) -> FocusPolicy { + if self.returns_focus { Self::FOCUS_SCOPE } else { FocusPolicy::NONE } + } + fn style(cx: &StyleCx<'_>, _: TextEditorStyleKey) -> TextEditorStyle { let overlay = &cx.tokens.color.overlay; TextEditorStyle { diff --git a/crates/text/tests/it/editor.rs b/crates/text/tests/it/editor.rs index fc89944a8..0797b7bdb 100644 --- a/crates/text/tests/it/editor.rs +++ b/crates/text/tests/it/editor.rs @@ -654,3 +654,49 @@ fn an_ended_session_returns_the_focus_to_the_canvas_and_the_view_stays() { assert_eq!(view.get(), before, "rulers {rulers}: the view stays"); } } + +/// An editor shown with `returns_focus(false)` opens no focus scope: when its session ends, focus does not go back to +/// the stop that handed it over (a form field's part, which would open the field again), while the default editor +/// hands it back there. +#[trial] +fn an_editor_that_does_not_return_focus_leaves_its_opener_alone() { + const EDITOR: craft_behavior::DocKey = craft_behavior::DocKey::new("trial.field-editor"); + const OPENER: craft_behavior::DocKey = craft_behavior::DocKey::new("trial.field-opener"); + use craft_ui::runtime::focus::{FocusTarget, request}; + use craft_ui::testing::access::{focused_key, name}; + + for returns in [true, false] { + let editing = Rc::new(Cell::new(false)); + let shown_editing = Rc::clone(&editing); + let mut host = Host { text: "abc".to_owned(), ..Host::default() }; + let mut selection = TextSelection::caret_at(3); + let mut fixture = Fixture::new(move |ui| { + drop(Button::new("Opener").doc_key(OPENER).show(ui)); + if shown_editing.get() { + let bounds = Rect::from_min_size(ui.cursor().min, vec2(200.0, 20.0)); + drop( + TextEditor::new("Field", &mut host, Bind::bound(&mut selection)) + .mode(EditorMode::FieldLine) + .returns_focus(returns) + .doc_key(EDITOR) + .show_at(ui, bounds), + ); + } + ui.add_space(30.0); + drop(Button::new("After").show(ui)); + }); + fixture.room(vec2(400.0, 300.0)).settle(); + let focused = |fixture: &Fixture| name(fixture.tree(), focused_key(fixture.tree())).map(str::to_owned); + request(fixture.ctx(), FocusTarget::Doc(OPENER)); + fixture.settle(); + assert_eq!(focused(&fixture).as_deref(), Some("Opener"), "returns {returns}: the opener has the focus"); + editing.set(true); + fixture.settle(); + request(fixture.ctx(), FocusTarget::Doc(EDITOR)); + fixture.settle(); + assert_eq!(focused(&fixture).as_deref(), Some("Field"), "returns {returns}: the editor took the focus"); + editing.set(false); + fixture.settle(); + assert_eq!(focused(&fixture).as_deref() == Some("Opener"), returns, "returns {returns}: the focus went back to the opener only when asked"); + } +} diff --git a/crates/ui-contract/src/contract/mod.rs b/crates/ui-contract/src/contract/mod.rs index 4bc45500a..9be33abd0 100644 --- a/crates/ui-contract/src/contract/mod.rs +++ b/crates/ui-contract/src/contract/mod.rs @@ -364,6 +364,11 @@ pub trait Component: Meta + Sized { /// 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; + /// The focus scope this instance runs in. The default is `FOCUS_SCOPE`; a component whose scope is a choice of its + /// caller (an editor that hands focus back to where it came from, or not) overrides it. + fn focus_scope(&self) -> crate::runtime::focus::FocusPolicy { + Self::FOCUS_SCOPE + } /// 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 01a27e71b..5fd0627bb 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -317,8 +317,9 @@ 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 focus(&self, comp: &C) -> Option { + let scope = comp.focus_scope(); + (scope != crate::runtime::focus::FocusPolicy::NONE).then_some(scope) } fn sized(&self, comp: &C, ui: &Ui, id: Id, size: Vec2) -> Vec2 {