mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 07:57:47 +00:00
ui-contract: Escape in a modeless dialog's field closes the dialog
With RootEnv::escape_leaves_fields on, a text field took Escape and gave up the focus unless it sat in a modal dialog. In a modeless dialog whose Escape closes it (one with a footer, such as Find and Replace), the first Escape left nothing focused, and a modeless layer does not take an Escape while the focus is outside it, so no later Escape closed the dialog and the focus never went back to the opener. The field now keeps the focus in any dialog on top whose Escape closes it, modal or modeless. An Escape the field does not take for its typing closes the dialog, which returns the focus to its opener. In a modeless dialog the first Escape still reverts a draft.
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
//! The Find and Replace dialog's protocol: fields set by automation are what the next request carries.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
use craft_text::find::FindOptions;
|
||||
use craft_text::ui::find::{FindCheck, FindReplaceDialog, FindRequest, FindTab, ReplaceButtons};
|
||||
use craft_ui::contract::Role;
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{Outcome as _, Role, Show as _};
|
||||
use craft_ui::dialog::{DialogStack, StackDialog as _};
|
||||
use craft_ui::runtime::env::RootEnv;
|
||||
use craft_ui::runtime::focus::{FocusTarget, request};
|
||||
use craft_ui::testing::access::{focused_key, name};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Key, Modifiers};
|
||||
use serde_json::json;
|
||||
|
||||
/// Presses the button named `name`.
|
||||
@@ -62,3 +67,45 @@ fn find_fields_set_by_automation_reach_the_next_request() {
|
||||
assert_eq!(fields["message"], json!("nothing found"));
|
||||
assert_eq!(fields["backwards"], json!(true));
|
||||
}
|
||||
|
||||
// Escape in Find what closes the dialog and puts the focus back on the control that opened it, also where Escape
|
||||
// otherwise takes a field's focus (`escape_leaves_fields`). Typing in the field is reverted by the first Escape, which
|
||||
// keeps the focus in the field, and the next Escape closes the dialog.
|
||||
#[trial]
|
||||
fn escape_in_find_what_closes_the_dialog_and_returns_the_focus() {
|
||||
for leaves in [false, true] {
|
||||
for typed in [false, true] {
|
||||
let open = Rc::new(Cell::new(false));
|
||||
let opener = Rc::new(Cell::new(None));
|
||||
let (shown, seen) = (Rc::clone(&open), Rc::clone(&opener));
|
||||
let dialog = RefCell::new(FindReplaceDialog::new("craft", FindTab::Find));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let button = Button::new("Find").show(ui);
|
||||
seen.set(Some(button.response().id));
|
||||
if shown.get() && dialog.borrow_mut().show(ui, &DialogStack::default()).is_some() {
|
||||
shown.set(false);
|
||||
}
|
||||
});
|
||||
let mut root = RootEnv::new();
|
||||
root.escape_leaves_fields = leaves;
|
||||
fixture.root(root).settle();
|
||||
request(fixture.ctx(), FocusTarget::Id(opener.get().expect("the opener is shown")));
|
||||
fixture.settle();
|
||||
assert_eq!(fixture.focused(), opener.get(), "the opener has the focus");
|
||||
open.set(true);
|
||||
fixture.settle();
|
||||
let focused_name = |fixture: &Fixture| name(fixture.tree(), focused_key(fixture.tree())).map(str::to_owned);
|
||||
assert_eq!(focused_name(&fixture).as_deref(), Some("Find what:"), "the dialog opens with the focus in Find what");
|
||||
let in_field = fixture.focused();
|
||||
if typed {
|
||||
fixture.type_text("teh").settle();
|
||||
fixture.key(Key::Escape, Modifiers::NONE).settle();
|
||||
assert!(open.get(), "leaves {leaves}: the first Escape reverts the typing and keeps the dialog open");
|
||||
assert_eq!(fixture.focused(), in_field, "leaves {leaves}: the focus stays in Find what");
|
||||
}
|
||||
fixture.key(Key::Escape, Modifiers::NONE).settle();
|
||||
assert!(!open.get(), "leaves {leaves}, typed {typed}: Escape closes the dialog");
|
||||
assert_eq!(fixture.focused(), opener.get(), "leaves {leaves}, typed {typed}: the focus goes back to the opener");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -511,8 +511,9 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut
|
||||
}
|
||||
let draft_takes = state.draft.is_some() && !dialog_escape && !(focused_before && keys::focus_in_modal_dialog(ctx));
|
||||
// With `RootEnv::escape_leaves_fields` a field takes Escape with nothing to revert too, and gives up the focus;
|
||||
// in a dialog that Escape closes, the Escape stays the dialog's.
|
||||
let leaves = !input.options.clear_on_escape && !closes_dialog && crate::runtime::env::Env::of(input.ui).escape_leaves_fields;
|
||||
// in a dialog that Escape closes, modal or modeless, the Escape stays the dialog's and the field keeps the focus.
|
||||
let in_closing_dialog = focused_before && crate::runtime::focus::escape_reaches_dialog(ctx);
|
||||
let leaves = !input.options.clear_on_escape && !in_closing_dialog && crate::runtime::env::Env::of(input.ui).escape_leaves_fields;
|
||||
let takes_escape = keep_on_escape
|
||||
|| if input.options.clear_on_escape { !base.is_empty() || state.preedit.is_some() } else { draft_takes || state.preedit.is_some() || leaves };
|
||||
let list_first = input.list_open && !input.options.clear_on_escape;
|
||||
|
||||
+3
-1
@@ -38,7 +38,9 @@ pub struct RootEnv {
|
||||
/// Whether a bare Tab toggles the window's chrome in place of moving focus where the key stage's gate allows it.
|
||||
pub tab_toggles_chrome: bool,
|
||||
/// Whether Escape in a focused text field also takes the focus from it, with or without typing to revert, so the
|
||||
/// next keys go to the window. A clearing field (a search box) still clears its text first.
|
||||
/// next keys go to the window. A clearing field (a search box) still clears its text first. In a dialog whose Escape
|
||||
/// closes it, modal or modeless, the field keeps the focus, and an Escape it does not take for its typing closes
|
||||
/// the dialog.
|
||||
pub escape_leaves_fields: bool,
|
||||
/// Whether a held key's repeats run the command its chord binds again (`FirePhase::Repeat`), as a held Cmd+= keeps
|
||||
/// zooming in. A key with a hold binding, and a key held down while a modal layer opened or closed, repeat nothing.
|
||||
|
||||
@@ -1311,18 +1311,25 @@ pub fn focus_in_scope(ctx: &Context, scope: Id) -> Option<bool> {
|
||||
/// Only a modal dialog counts: a modeless window's edits belong to the app, so its field reverts its own draft first
|
||||
/// and the next Escape closes the window.
|
||||
pub(crate) fn escape_closes_dialog(ctx: &Context) -> bool {
|
||||
escaping_dialog(ctx).is_some()
|
||||
escaping_dialog(ctx, true).is_some()
|
||||
}
|
||||
|
||||
/// Whether the focused stop's innermost layer-like scope is a dialog on top whose Escape closes it, modal or modeless:
|
||||
/// an Escape that a field there does not take for itself closes the dialog, so the field keeps the focus instead of
|
||||
/// giving it up (`RootEnv::escape_leaves_fields`).
|
||||
pub(crate) fn escape_reaches_dialog(ctx: &Context) -> bool {
|
||||
escaping_dialog(ctx, false).is_some()
|
||||
}
|
||||
|
||||
/// Whether the dialog that [`escape_closes_dialog`] finds keeps the focused field's typing on Escape
|
||||
/// (`FocusPolicy::escape_keeps_typing`): the field then commits its draft instead of cancelling it.
|
||||
pub(crate) fn escape_keeps_typing(ctx: &Context) -> bool {
|
||||
escaping_dialog(ctx).is_some_and(|policy| policy.escape_keeps_typing)
|
||||
escaping_dialog(ctx, true).is_some_and(|policy| policy.escape_keeps_typing)
|
||||
}
|
||||
|
||||
/// The policy of the modal dialog on top whose Escape closes it and which holds the focused stop
|
||||
/// ([`escape_closes_dialog`]).
|
||||
fn escaping_dialog(ctx: &Context) -> Option<FocusPolicy> {
|
||||
/// The policy of the dialog on top whose Escape closes it and which holds the focused stop, when `modal` only a modal
|
||||
/// one ([`escape_closes_dialog`]).
|
||||
fn escaping_dialog(ctx: &Context, modal: bool) -> Option<FocusPolicy> {
|
||||
let rec = focused_rec(ctx)?;
|
||||
let (id, policy) = with(ctx, |state| {
|
||||
let mut at = rec.scope;
|
||||
@@ -1338,9 +1345,9 @@ fn escaping_dialog(ctx: &Context) -> Option<FocusPolicy> {
|
||||
}
|
||||
None
|
||||
})?;
|
||||
let modal_on_top =
|
||||
crate::runtime::overlay::top(ctx) == Some(id) && crate::runtime::overlay::entry(ctx, id).is_some_and(|entry| entry.rank.is_modal());
|
||||
modal_on_top.then_some(policy)
|
||||
let on_top =
|
||||
crate::runtime::overlay::top(ctx) == Some(id) && crate::runtime::overlay::entry(ctx, id).is_some_and(|entry| !modal || entry.rank.is_modal());
|
||||
on_top.then_some(policy)
|
||||
}
|
||||
|
||||
/// Whether the focus is in the focus scope of the layer `layer`, or, for an `Initial::KeepAnchor` scope, on the stop
|
||||
|
||||
@@ -1249,32 +1249,39 @@ fn escape_in_a_text_field_closes_the_form_dialog_typed_or_not() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A modeless form keeps its other edits when Escape cancels an active text edit.
|
||||
/// A modeless form keeps its other edits when Escape cancels an active text edit. The field keeps the focus through
|
||||
/// that Escape, also where Escape otherwise takes a field's focus (`escape_leaves_fields`), so the next Escape closes
|
||||
/// the window.
|
||||
#[trial]
|
||||
fn escape_in_a_modeless_form_restores_the_field_before_closing() {
|
||||
let open = Rc::new(Cell::new(true));
|
||||
let value = Rc::new(RefCell::new(String::from("Original")));
|
||||
let (shown, text) = (Rc::clone(&open), Rc::clone(&value));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut is_open = shown.get();
|
||||
let mut text = text.borrow_mut();
|
||||
craft_ui::patterns::form_dialogs::FormDialog::new("Tool", Bind::bound(&mut *text))
|
||||
.opened(Bind::bound(&mut is_open))
|
||||
.shaped(|dialog| dialog.modeless())
|
||||
.show(ui, |ui, _, text| {
|
||||
craft_ui::text::TextField::new("Name", Bind::bound(text)).show(ui);
|
||||
true
|
||||
});
|
||||
shown.set(is_open);
|
||||
});
|
||||
fixture.idle(10);
|
||||
let field = fixture.by_label("Name").expect("the field is shown");
|
||||
fixture.click(&field).idle(2);
|
||||
fixture.type_text("x").idle(2);
|
||||
assert_ne!(&*value.borrow(), "Original", "typing changed the value");
|
||||
fixture.key(Key::Escape, Modifiers::NONE).idle(2);
|
||||
assert!(open.get(), "the first Escape leaves the window open");
|
||||
assert_eq!(&*value.borrow(), "Original", "the field's edit is restored");
|
||||
fixture.key(Key::Escape, Modifiers::NONE).idle(6);
|
||||
assert!(!open.get(), "the second Escape closes the window");
|
||||
for leaves in [false, true] {
|
||||
let open = Rc::new(Cell::new(true));
|
||||
let value = Rc::new(RefCell::new(String::from("Original")));
|
||||
let (shown, text) = (Rc::clone(&open), Rc::clone(&value));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut is_open = shown.get();
|
||||
let mut text = text.borrow_mut();
|
||||
craft_ui::patterns::form_dialogs::FormDialog::new("Tool", Bind::bound(&mut *text))
|
||||
.opened(Bind::bound(&mut is_open))
|
||||
.shaped(|dialog| dialog.modeless())
|
||||
.show(ui, |ui, _, text| {
|
||||
craft_ui::text::TextField::new("Name", Bind::bound(text)).show(ui);
|
||||
true
|
||||
});
|
||||
shown.set(is_open);
|
||||
});
|
||||
let mut root = craft_ui::runtime::env::RootEnv::new();
|
||||
root.escape_leaves_fields = leaves;
|
||||
fixture.root(root).idle(10);
|
||||
let field = fixture.by_label("Name").expect("the field is shown");
|
||||
fixture.click(&field).idle(2);
|
||||
fixture.type_text("x").idle(2);
|
||||
assert_ne!(&*value.borrow(), "Original", "leaves {leaves}: typing changed the value");
|
||||
fixture.key(Key::Escape, Modifiers::NONE).idle(2);
|
||||
assert!(open.get(), "leaves {leaves}: the first Escape leaves the window open");
|
||||
assert_eq!(&*value.borrow(), "Original", "leaves {leaves}: the field's edit is restored");
|
||||
assert!(fixture.focused().is_some(), "leaves {leaves}: the field keeps the focus");
|
||||
fixture.key(Key::Escape, Modifiers::NONE).idle(6);
|
||||
assert!(!open.get(), "leaves {leaves}: the second Escape closes the window");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user