mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 03:17:43 +00:00
Restore field edits before closing modeless dialogs
This commit is contained in:
@@ -51,7 +51,7 @@ pub(crate) fn gesture_gate<C: Component>(comp: &C, cx: &mut Cx<'_, C>) {
|
||||
pub(crate) fn edit_intents<C: Component>(comp: &mut C, cx: &mut Cx<'_, C>) {
|
||||
use crate::contract::cx::Edit as _;
|
||||
if cx.gesture().is_some_and(|phase| matches!(phase, GesturePhase::Begin | GesturePhase::Update)) {
|
||||
let escapes = !cx.core.escape_to_list && !crate::runtime::keys::focus_in_dialog(&cx.core.ctx);
|
||||
let escapes = !cx.core.escape_to_list && !crate::runtime::keys::focus_in_modal_dialog(&cx.core.ctx);
|
||||
let reason = if escapes && cx.core.intents.take(Intent::Cancel) {
|
||||
Some(CancelReason::Escape)
|
||||
} else {
|
||||
|
||||
@@ -371,8 +371,8 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut
|
||||
None => {}
|
||||
}
|
||||
// A clearing field takes Escape while it shows text, with or without a draft, and passes it on once it is empty. In
|
||||
// a dialog a draft leaves Escape to the dialog's Cancel, which discards it with the dialog's other edits.
|
||||
let draft_takes = state.draft.is_some() && !(focused_before && keys::focus_in_dialog(ctx));
|
||||
// a modal dialog a draft leaves Escape to the dialog's Cancel, which discards it with the dialog's other edits.
|
||||
let draft_takes = state.draft.is_some() && !(focused_before && keys::focus_in_modal_dialog(ctx));
|
||||
let takes_escape =
|
||||
if input.options.clear_on_escape { !base.is_empty() || state.preedit.is_some() } else { draft_takes || state.preedit.is_some() };
|
||||
if focused_before && takes_escape && !state.completions.open && !input.list_open && input.intents.take(Intent::Cancel) {
|
||||
|
||||
@@ -44,7 +44,7 @@ pub(crate) use intents::{
|
||||
pub use intents::{with_canvas_window_intents, with_window_intents};
|
||||
pub(crate) use nodes::{redirect_node, register_expanded, register_node};
|
||||
pub use route::focus_holds;
|
||||
pub(crate) use route::{activated_member, begin, focus_in_dialog, layout_of, owner_at, pasted, pasted_text, queue_edit, routed_to, take_shortcuts};
|
||||
pub(crate) use route::{activated_member, begin, focus_in_modal_dialog, layout_of, owner_at, pasted, pasted_text, queue_edit, routed_to, take_shortcuts};
|
||||
|
||||
/// A menu-bar key of an in-window bar (spec 9.7). F10 reaches the bar on every OS; Alt keys only off a Mac.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
|
||||
@@ -244,11 +244,14 @@ pub(crate) fn owner_at(ctx: &Context, target: Option<&StopRec>, scopes: &[Id], b
|
||||
key_owner(layer == overlay::LayerKeys::Blocked, &present)
|
||||
}
|
||||
|
||||
/// Whether the focused stop of the running viewport is in a dialog layer (`OwnerClass::Dialog`): Escape there is the
|
||||
/// dialog's Cancel, which discards a field's draft with the rest of the dialog's edits, so a draft does not hold it.
|
||||
pub(crate) fn focus_in_dialog(ctx: &Context) -> bool {
|
||||
focus::focused_rec(ctx)
|
||||
.is_some_and(|rec| overlay::key_layer(ctx, &focus::scope_chain(ctx, &rec), None) == overlay::LayerKeys::Class(OwnerClass::Dialog))
|
||||
/// A modal dialog's Cancel discards its edits together, so a field's draft does not hold Escape there. A modeless
|
||||
/// window's edits belong to the app, and its field must cancel its own draft first.
|
||||
pub(crate) fn focus_in_modal_dialog(ctx: &Context) -> bool {
|
||||
focus::focused_rec(ctx).is_some_and(|rec| {
|
||||
let scopes = focus::scope_chain(ctx, &rec);
|
||||
overlay::key_layer(ctx, &scopes, None) == overlay::LayerKeys::Class(OwnerClass::Dialog)
|
||||
&& scopes.iter().find_map(|id| overlay::entry(ctx, *id)).is_some_and(|entry| entry.rank.is_modal())
|
||||
})
|
||||
}
|
||||
|
||||
/// The shortcut events the key owner leaves to the app (9.3), and the presses the focused component holds
|
||||
|
||||
@@ -832,3 +832,33 @@ 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.
|
||||
#[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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user