mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
Merge pull request #3941 from dotCooCoo/fix/number-field-target-undo-b
Discard number editor history and composition when its target changes
This commit is contained in:
5 files changed
+173
-6
No files matched your search
@@ -612,11 +612,20 @@ pub(crate) fn run<C: Component>(
|
||||
let mut state = std::mem::take(cx.core.memory.shown::<TextState>(state_id));
|
||||
// A part that was not shown last pass lost its focus, and its typing run with it: the run is forgotten (step 4
|
||||
// ended its gesture without putting back the value from before it), and the part starts afresh from its value.
|
||||
if !crate::runtime::memory::shown_last_pass(&ctx, id) {
|
||||
state = TextState::default();
|
||||
} else if state.edit_target != part.options.edit_target {
|
||||
// The old target's draft and gesture must never reach the new target's binding.
|
||||
cx.cancel(CancelReason::HostChanged);
|
||||
let shown_last_pass = crate::runtime::memory::shown_last_pass(&ctx, id);
|
||||
if !shown_last_pass || state.edit_target != part.options.edit_target {
|
||||
// The old target's draft and gesture must never reach the new target's binding. A hidden part's gesture
|
||||
// already ended, but its widget may still retain history and composition.
|
||||
if shown_last_pass {
|
||||
cx.cancel(CancelReason::HostChanged);
|
||||
}
|
||||
let widget = adapter::text_id(id, part.key);
|
||||
crate::runtime::egui::text_edit::clear_history(&ctx, widget);
|
||||
crate::runtime::ime::request_interrupt(&ctx, widget);
|
||||
// Input was folded before this part ran; discard events already queued for the old target too.
|
||||
let _ = crate::runtime::ime::take(&ctx, widget, false);
|
||||
// A focus change may already have handed the old composition to this adapter.
|
||||
let _ = crate::runtime::ime::take_leaving(&ctx, widget);
|
||||
state = TextState::default();
|
||||
}
|
||||
state.edit_target = part.options.edit_target;
|
||||
|
||||
@@ -68,6 +68,7 @@ pub struct TextOptions {
|
||||
/// A parsable draft applies as typed whatever the codec says.
|
||||
pub live: bool,
|
||||
/// The owner of the current draft, when the same field can edit different values.
|
||||
/// A change retires the old draft, editor history and composition while preserving the field's focus.
|
||||
pub edit_target: Option<egui::IdSalt>,
|
||||
/// Escape clears the text and commits the empty text while text shows, and goes on to the host once it is empty.
|
||||
pub clear_on_escape: bool,
|
||||
|
||||
@@ -357,6 +357,14 @@ pub(crate) fn select(ctx: &Context, id: Id, anchor: usize, caret: usize) {
|
||||
state.store(ctx, id);
|
||||
}
|
||||
|
||||
/// Discards Undo and Redo belonging to the previous target while keeping the widget's caret and focus.
|
||||
pub(crate) fn clear_history(ctx: &Context, id: Id) {
|
||||
if let Some(mut state) = TextEditState::load(ctx, id) {
|
||||
state.set_undoer(Default::default());
|
||||
state.store(ctx, id);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the widget's field has an undo step and a redo step, as `(undo, redo)`, against its current text `text`
|
||||
/// (egui's undoer compares its steps with the text, which the widget's state does not hold). It reads the undoer and
|
||||
/// steps nothing, so it agrees with what [`undo`] would do now.
|
||||
|
||||
@@ -353,7 +353,8 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
}
|
||||
|
||||
/// Identifies the target of this field's edit, for a control reused across drawings or selections.
|
||||
/// Changing the key discards the previous draft even when the two bound values are equal.
|
||||
/// Changing the key discards the previous draft, editor Undo/Redo and input-method composition even when the
|
||||
/// two bound values are equal. Late composition commits cannot enter the new target's draft.
|
||||
/// The field keeps its identity and focus.
|
||||
#[must_use]
|
||||
pub fn edit_target(mut self, key: impl egui::AsIdSalt) -> Self {
|
||||
|
||||
@@ -836,3 +836,151 @@ fn changing_a_number_fields_edit_target_clears_the_old_draft_with_equal_values()
|
||||
assert_eq!((values[0].get(), values[1].get()), (Some(START), Some(13.0)));
|
||||
}
|
||||
}
|
||||
|
||||
/// The editor's Undo and Redo belong to the object being edited, even when the next object has the same value.
|
||||
#[trial]
|
||||
fn changing_a_number_fields_edit_target_discards_the_old_objects_undo_history() {
|
||||
use craft_ui::testing::Fixture;
|
||||
use std::rc::Rc;
|
||||
|
||||
let target = Rc::new(Cell::new(0));
|
||||
let values = Rc::new([Cell::new(Some(START)), Cell::new(Some(START))]);
|
||||
let (shown_target, shown_values) = (target.clone(), values.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let current = shown_target.get();
|
||||
let model = &shown_values[current];
|
||||
let mut value = model.get();
|
||||
NumberField::new("Size", Bind::bound(&mut value)).id_salt("size").edit_target(current).show(ui);
|
||||
model.set(value);
|
||||
});
|
||||
fixture.settle();
|
||||
let field = fixture.by_label("Size").expect("the number field");
|
||||
fixture.click(&field).key(Key::A, Modifiers::COMMAND).type_text("9+9").settle();
|
||||
assert!(fixture.texts().iter().any(|text| text.text == "9+9"));
|
||||
// Advance the fixture's manual clock so the editor seals this typing group in its history.
|
||||
fixture.advance(2.0).settle();
|
||||
fixture.type_text("+1").advance(2.0).settle();
|
||||
assert!(fixture.texts().iter().any(|text| text.text == "9+9+1"));
|
||||
target.set(1);
|
||||
fixture.settle();
|
||||
assert!(!fixture.texts().iter().any(|text| text.text == "9+9"));
|
||||
|
||||
for modifiers in [Modifiers::COMMAND, Modifiers::COMMAND | Modifiers::SHIFT, Modifiers::COMMAND] {
|
||||
fixture.key(Key::Z, modifiers).settle();
|
||||
assert!(!fixture.texts().iter().any(|text| text.text.starts_with("9+9")), "Undo and Redo cannot restore another object's draft");
|
||||
fixture.key(Key::Enter, Modifiers::NONE).settle();
|
||||
assert_eq!((values[0].get(), values[1].get()), (Some(START), Some(START)), "neither object adopts the old draft");
|
||||
}
|
||||
fixture.click(&field).key(Key::A, Modifiers::COMMAND).type_text("5+8").key(Key::Enter, Modifiers::NONE).settle();
|
||||
assert_eq!((values[0].get(), values[1].get()), (Some(START), Some(13.0)), "the new object still accepts a fresh edit");
|
||||
}
|
||||
|
||||
/// Reappearing after a hidden pass forgets the old object's editor history and composition, even for equal values.
|
||||
#[trial]
|
||||
fn a_hidden_number_field_discards_the_old_objects_history_when_switching_targets() {
|
||||
use craft_ui::testing::Fixture;
|
||||
use std::rc::Rc;
|
||||
|
||||
for composing in [false, true] {
|
||||
let visible = Rc::new(Cell::new(true));
|
||||
let target = Rc::new(Cell::new(0));
|
||||
let values = Rc::new([Cell::new(Some(START)), Cell::new(Some(START))]);
|
||||
let id = Rc::new(Cell::new(egui::Id::NULL));
|
||||
let (shown_visible, shown_target, shown_values, shown_id) = (visible.clone(), target.clone(), values.clone(), id.clone());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
if !shown_visible.get() {
|
||||
return;
|
||||
}
|
||||
let current = shown_target.get();
|
||||
let model = &shown_values[current];
|
||||
let mut value = model.get();
|
||||
let out = NumberField::new("Size", Bind::bound(&mut value)).id_salt("size").edit_target(current).show(ui);
|
||||
shown_id.set(out.response.id);
|
||||
model.set(value);
|
||||
});
|
||||
fixture.settle();
|
||||
let field = fixture.by_label("Size").expect("the number field");
|
||||
fixture.click(&field).key(Key::A, Modifiers::COMMAND).type_text("9+9").settle();
|
||||
// Seal separate typing groups using only the fixture's manual clock.
|
||||
fixture.advance(2.0).settle();
|
||||
fixture.type_text("+1").advance(2.0).settle();
|
||||
assert!(fixture.texts().iter().any(|text| text.text == "9+9+1"));
|
||||
if composing {
|
||||
fixture.frame(vec![Event::Ime(egui::ImeEvent::Preedit { text: "99".into(), active_range_chars: None })]).settle();
|
||||
assert!(fixture.texts().iter().any(|text| text.text.contains("99")), "A has a live composition");
|
||||
}
|
||||
assert_eq!((values[0].get(), values[1].get()), (Some(START), Some(START)));
|
||||
|
||||
let stable = id.get();
|
||||
visible.set(false);
|
||||
fixture.idle(1);
|
||||
assert!(fixture.by_label("Size").is_none(), "the field is hidden for a pass");
|
||||
target.set(1);
|
||||
visible.set(true);
|
||||
fixture.settle();
|
||||
let field = fixture.by_label("Size").expect("the reappearing number field");
|
||||
assert_eq!(id.get(), stable, "the same widget edits B");
|
||||
fixture.click(&field).settle();
|
||||
assert!(fixture.focused_stop().is_some_and(|(stop, _)| stop == stable), "B can be refocused");
|
||||
assert!(!fixture.texts().iter().any(|text| text.text.starts_with("9+9") || text.text.contains("99")));
|
||||
if composing {
|
||||
fixture.frame(vec![Event::Ime(egui::ImeEvent::Commit("99".into()))]).settle();
|
||||
}
|
||||
fixture.key(Key::Enter, Modifiers::NONE).settle();
|
||||
assert_eq!((values[0].get(), values[1].get()), (Some(START), Some(START)), "Enter cannot adopt A's draft or composition");
|
||||
|
||||
for modifiers in [Modifiers::COMMAND, Modifiers::COMMAND | Modifiers::SHIFT, Modifiers::COMMAND] {
|
||||
fixture.key(Key::Z, modifiers).settle();
|
||||
assert!(
|
||||
!fixture.texts().iter().any(|text| text.text.starts_with("9+9") || text.text.contains("99")),
|
||||
"Undo and Redo cannot restore A's draft"
|
||||
);
|
||||
fixture.key(Key::Enter, Modifiers::NONE).settle();
|
||||
assert_eq!((values[0].get(), values[1].get()), (Some(START), Some(START)), "neither object adopts A's draft");
|
||||
}
|
||||
fixture.key(Key::A, Modifiers::COMMAND).type_text("5+8").key(Key::Enter, Modifiers::NONE).settle();
|
||||
assert_eq!((values[0].get(), values[1].get()), (Some(START), Some(13.0)), "B still accepts fresh input");
|
||||
}
|
||||
}
|
||||
|
||||
/// A composition begun for one object cannot commit into another object through the same field.
|
||||
#[trial]
|
||||
fn changing_a_number_fields_edit_target_discards_the_old_objects_composition() {
|
||||
use craft_ui::testing::Fixture;
|
||||
use std::rc::Rc;
|
||||
|
||||
for (leave, queued) in [(false, true), (false, false), (true, false)] {
|
||||
let target = Rc::new(Cell::new(0));
|
||||
let values = Rc::new([Cell::new(Some(START)), Cell::new(Some(START))]);
|
||||
let (shown_target, shown_values) = (target.clone(), values.clone());
|
||||
let mut other = String::new();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let current = shown_target.get();
|
||||
let model = &shown_values[current];
|
||||
let mut value = model.get();
|
||||
NumberField::new("Size", Bind::bound(&mut value)).id_salt("size").edit_target(current).show(ui);
|
||||
model.set(value);
|
||||
craft_ui::text::TextField::new("Other", Bind::bound(&mut other)).show(ui);
|
||||
});
|
||||
fixture.settle();
|
||||
let field = fixture.by_label("Size").expect("the number field");
|
||||
fixture.click(&field).key(Key::A, Modifiers::COMMAND).settle();
|
||||
fixture.frame(vec![Event::Ime(egui::ImeEvent::Preedit { text: "99".into(), active_range_chars: None })]).settle();
|
||||
assert!(fixture.texts().iter().any(|text| text.text.contains("99")), "the old object has a live preedit");
|
||||
target.set(1);
|
||||
if queued {
|
||||
fixture.frame(vec![Event::Ime(egui::ImeEvent::Preedit { text: "88".into(), active_range_chars: None })]).settle();
|
||||
assert!(!fixture.texts().iter().any(|text| text.text.contains("88")), "preedit queued in the switch frame belongs to the old target");
|
||||
}
|
||||
if leave {
|
||||
fixture.key(Key::Tab, Modifiers::NONE).settle();
|
||||
} else {
|
||||
fixture.settle();
|
||||
fixture.frame(vec![Event::Ime(egui::ImeEvent::Commit("99".into()))]).settle();
|
||||
fixture.key(Key::Enter, Modifiers::NONE).settle();
|
||||
}
|
||||
assert_eq!((values[0].get(), values[1].get()), (Some(START), Some(START)), "the old composition cannot reach the new target (leave={leave})");
|
||||
fixture.click(&field).key(Key::A, Modifiers::COMMAND).type_text("13").key(Key::Enter, Modifiers::NONE).settle();
|
||||
assert_eq!(values[1].get(), Some(13.0), "the new target still accepts fresh input");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user