fw sync: TextEditor::returns_focus (on by default): an editor its caller opens from a part can leave the opener alone

Upstream's editor runs in a restoring focus scope (FocusPolicy::TAB_LAYER), so an ended
session hands focus back to the stop it came from. A form field's editor takes focus
from the field's own page part, and focus arriving back there opens the field again:
Esc, Enter and picks no longer closed PdfCraft's fields. Component::focus_scope (default
FOCUS_SCOPE) lets an instance choose its scope; returns_focus(false) opens none.
Upstream's default is unchanged. Trial: an_editor_that_does_not_return_focus_leaves_its_opener_alone.
This commit is contained in:
Dylan M. Taylor
2026-11-23 07:12:37 -05:00
parent fbd0bd096c
commit d132f01012
4 changed files with 65 additions and 2 deletions
+11
View File
@@ -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 {
+46
View File
@@ -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");
}
}
+5
View File
@@ -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
+3 -2
View File
@@ -317,8 +317,9 @@ 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 focus(&self, comp: &C) -> Option<crate::runtime::focus::FocusPolicy> {
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 {