Merge pull request #3518 from dotCooCoo/fix/zoom-field-tab-leaves-focus

ui-contract, ui-widgets: Tab moves on from a zoom field with nothing typed
This commit is contained in:
BFlat
2026-11-26 09:56:58 +07:00
committed by GitHub
6 changed files with 67 additions and 15 deletions
+12 -7
View File
@@ -701,11 +701,12 @@ fn own_sense(e: &dyn ErasedPass<'_>, resolved: &Resolved, inert: bool) -> Sense
}
}
/// The widget that holds the component's text or editor and the keys it keeps, when it has either.
fn text_holder(id: Id, parts: &PartsStep, base: focus::KeyUse) -> (Option<Id>, Option<focus::KeyUse>) {
/// The widget that holds the component's text or editor and the keys it keeps, when it has either. `drafting` is
/// whether the text part holds a draft.
fn text_holder(id: Id, parts: &PartsStep, base: focus::KeyUse, drafting: bool) -> (Option<Id>, Option<focus::KeyUse>) {
let text = parts.text.decl.as_ref().map(|decl| {
let decl = &decl.part;
(crate::primitives::text_adapter::text_id(id, decl.key), crate::primitives::text_adapter::keys_of(base, &decl.options))
(crate::primitives::text_adapter::text_id(id, decl.key), crate::primitives::text_adapter::keys_of(base, &decl.options, drafting))
});
let editor =
parts.text.editor.as_ref().map(|decl| (crate::primitives::ImeSurface::of(id, decl.key).widget(), super::editor::keys_of(base, decl.purpose)));
@@ -716,12 +717,14 @@ fn text_holder(id: Id, parts: &PartsStep, base: focus::KeyUse) -> (Option<Id>, O
}
/// `keys`, those of the component's text part, with the edits it cannot run now inactive (`text_adapter::with_idle`),
/// read after the text step, so they are as the pass's edits left the selection and the undo steps.
fn idle_keys(ctx: &Context, id: Id, parts: &PartsStep, keys: focus::KeyUse, memory: &mut MemoryStage) -> focus::KeyUse {
/// read after the text step, so they are as the pass's edits left the selection and the undo steps. The text part's
/// keys are read again from `base` with the draft the text step left, which decides whether a rewriting Tab is kept.
fn idle_keys(ctx: &Context, id: Id, parts: &PartsStep, keys: focus::KeyUse, base: focus::KeyUse, memory: &mut MemoryStage) -> focus::KeyUse {
let Some(decl) = &parts.text.decl else { return keys };
let decl = &decl.part;
let held = crate::contract::text::held_selection(memory, id);
let draft = crate::contract::text::draft(memory, id);
let keys = crate::primitives::text_adapter::keys_of(base, &decl.options, draft.is_some());
let widget = crate::primitives::text_adapter::text_id(id, decl.key);
crate::primitives::text_adapter::with_idle(ctx, widget, held, draft.as_deref().unwrap_or(&decl.shown), keys)
}
@@ -1008,7 +1011,8 @@ fn sense_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, e
}
steps::check_parts(&ctx, facts.name, &parts);
let keys = e.keys();
let (holder_widget, text_keys) = text_holder(id, &parts, keys);
let drafting = parts.text.decl.is_some() && crate::contract::text::draft(e.core().memory, id).is_some();
let (holder_widget, text_keys) = text_holder(id, &parts, keys, drafting);
let own = own_sense(e, resolved, inert);
// The collection handles a row's click, but its access node must still offer the redirected action.
let unnarrowed = own;
@@ -1185,7 +1189,8 @@ fn update_steps(p: &mut Pass<'_, '_, '_>, env: &Env, h: &HostStep) {
steps::record_context(e.core_ref());
// The stops register after `update`, which says whether the component joins its body's roving group, and after
// the text step, so a Select All of the pass makes Copy active.
let text_keys = h.text_keys.map(|keys| idle_keys(ctx, id, &h.parts, keys, &mut *e.core().memory));
let base = e.keys();
let text_keys = h.text_keys.map(|keys| idle_keys(ctx, id, &h.parts, keys, base, &mut *e.core().memory));
let at = StopsStep { ctx, id, rtl, env, resolved: p.resolved, holder_widget: h.holder_widget, member: h.member, own: h.own, text_keys };
stops_step(at, e, facts, &h.parts);
// The body's group registers after `update`, which may change its rules from what the component knows now.
@@ -98,10 +98,16 @@ pub(crate) fn text_id(id: Id, key: PartKey) -> Id {
crate::runtime::memory::sub(id, ("text", key))
}
/// The keys the text part keeps, from its options.
pub(crate) fn keys_of(base: KeyUse, options: &TextOptions) -> KeyUse {
// A part whose Tab inserts, rewrites or reports keeps Tab, so the focus service never moves focus on it.
let keys = if options.tab != TabKey::Moves {
/// The keys the text part keeps, from its options and whether it holds a draft (`drafting`).
pub(crate) fn keys_of(base: KeyUse, options: &TextOptions, drafting: bool) -> KeyUse {
// A part whose Tab inserts or reports keeps Tab, so the focus service never moves focus on it. A part whose Tab
// rewrites keeps it only while it holds a draft to rewrite; with nothing typed, Tab moves focus on.
let keeps_tab = match options.tab {
TabKey::Moves => false,
TabKey::Rewrites => drafting,
TabKey::Inserts | TabKey::Reports => true,
};
let keys = if keeps_tab {
base.with_keep(KeepKeys::Tab)
} else if options.multiline() {
base.with_keep(KeepKeys::BlockArrows)
@@ -542,7 +548,7 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut
let mut tab = None;
if input.focused && state.preedit.is_none() {
match input.options.tab {
TabKey::Rewrites if !input.read_only => {
TabKey::Rewrites if !input.read_only && state.draft.is_some() => {
if egui_input::take_tab(ctx).is_some() {
state.end = Some(End::Rewrite { refuses: true });
}
@@ -43,7 +43,7 @@ pub enum TabKey {
Moves,
/// Tab inserts a tab.
Inserts,
/// Tab and Shift+Tab rewrite the draft (the text part selects it and refuses).
/// Tab and Shift+Tab rewrite the draft (the text part selects it and refuses); with no draft they move focus.
Rewrites,
/// Tab keeps focus and is handed back as `EditResponse::tab`: `Some(true)` for Tab, `Some(false)` for Shift+Tab.
Reports,
+2 -1
View File
@@ -352,7 +352,8 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
self
}
/// Zoom typing: a Tab rewrites the draft and a press on the field's other parts keeps it unapplied.
/// Zoom typing: a Tab rewrites the draft (with no draft it moves focus) and a press on the field's other parts
/// keeps it unapplied.
#[must_use]
pub(super) fn zoom_typing(mut self) -> Self {
self.options.tab = crate::primitives::TabKey::Rewrites;
+2 -1
View File
@@ -21,7 +21,8 @@ const MESSAGE_DECIMALS: usize = 2;
///
/// - Typing takes digits, `-`, `/` and the decimal separator: `a/b` is the zoom `a ÷ b × 100` %. Non-positive
/// entries are rejected; a second separator applies the number before it and reports [`InvalidZoom`].
/// - Tab writes the typed number in the field's own form and selects it; focus stays and nothing applies.
/// - Tab writes the typed number in the field's own form and selects it; focus stays and nothing applies. With nothing
/// typed, Tab and Shift+Tab move focus as in any field.
/// - A press on a part declared `holds_drafts` (the pasteboard, the title bar) keeps the typed zoom unapplied; the
/// next Enter applies it.
#[derive(crate::contract::Pattern)]
@@ -279,6 +279,45 @@ fn a_press_on_a_draft_holding_part_keeps_the_typed_zoom_and_a_press_elsewhere_ap
);
}
/// With no typed zoom, Tab leaves the field for the next stop and Shift+Tab comes back to it, so the controls after a
/// zoom field are reached by keyboard. With a typed zoom, Tab still rewrites it and keeps the focus.
#[trial]
fn tab_without_a_typed_zoom_moves_to_the_next_stop_and_shift_tab_returns() {
let kept = Kept::new();
let app = kept.clone();
let mut fixture = Fixture::new(move |ui| {
Row::new().show(ui, |ui, _| {
let _ = ZoomField::new(ZOOM, Some(app.view.get()), DOCUMENT).show(ui);
let _ = Button::new("Zoom Out").show(ui);
let _ = Button::new("Zoom In").show(ui);
});
});
fixture.settle();
fixture.key(Key::Tab, Modifiers::NONE).settle();
let field = fixture.by_label(ZOOM).expect("the zoom field").node;
let zoom_out = fixture.by_label("Zoom Out").expect("the first button").node;
assert_eq!(fixture.tree().focused_key(), field, "Tab from nothing focuses the zoom field");
fixture.key(Key::Tab, Modifiers::NONE).settle();
assert_eq!(fixture.tree().focused_key(), zoom_out, "Tab from the zoom field focuses the first button after it");
fixture.key(Key::Tab, Modifiers::SHIFT).settle();
assert_eq!(fixture.tree().focused_key(), field, "Shift+Tab from the button returns to the zoom field");
fixture.key(Key::Tab, Modifiers::SHIFT).settle();
fixture.key(Key::Tab, Modifiers::NONE).settle();
assert_eq!(fixture.tree().focused_key(), field, "Tab from the stop before it returns to the zoom field");
fixture.key(Key::A, Modifiers::COMMAND);
fixture.type_text("045");
fixture.settle();
fixture.key(Key::Tab, Modifiers::NONE).settle();
assert_eq!(fixture.tree().focused_key(), field, "Tab with a typed zoom keeps the focus");
assert_eq!(text(&fixture), "45", "and rewrites the typed zoom");
assert!(close(kept.zoom(), 100.0), "and applies nothing: {}", kept.zoom());
fixture.key(Key::Enter, Modifiers::NONE).settle();
fixture.key(Key::Tab, Modifiers::NONE).settle();
assert_eq!(fixture.tree().focused_key(), zoom_out, "Tab after Enter applied the zoom moves on");
}
/// The ids of the app's Cut, Copy and Delete that the frames since the last call fired.
fn app_edits_fired(fixture: &Fixture) -> Vec<String> {
let edits = [EditCommands::Cut, EditCommands::Copy, EditCommands::Delete].map(|command| CommandRef::from(command).id);