Files
craft-libs/crates/ui-widgets/tests/it/text/fields.rs
T
aiongg 21b71e19d1 Completion list: a mark before each candidate and the highlighted one's description under the list; Enter accepts only where the field completes on Enter (ctl:72, ctl:73)
- Completion carries a mark and a description; the unread detail line goes.
- FocusPolicy::list_enter: a text opener of a KeepAnchor layer keeps Enter when its layer turns it off, and Tab alone
  takes the lead. TextField::complete_on_enter turns it on; the review composer's mentions use it, as before.
2026-11-12 14:32:43 +00:00

160 lines
7.0 KiB
Rust

//! Typing runs, submit and escape, undo, completion, the number field and the caret blink.
use super::kit::Rig;
use craft_ui::contract::{Bind, Props as _, Show as _};
use craft_ui::primitives::{Completer, Completion};
use craft_ui::testing::input::key_press;
use craft_ui::testing::text::{caret, painted};
use craft_ui::testing::trial;
use craft_ui::text::{NumberField, TextField};
use egui::{Event, Key, Modifiers};
use std::ops::Range;
struct Prefixes;
impl Completer for Prefixes {
fn complete(&self, text: &str, caret: usize, out: &mut Vec<Completion>) -> Option<Range<usize>> {
let before = text.get(..caret)?;
let hits: Vec<_> = ["alpha", "alps", "beta"].into_iter().filter(|word| !before.is_empty() && word.starts_with(before)).collect();
out.extend(hits.iter().map(|word| Completion::new(*word, *word)));
(!hits.is_empty()).then_some(0..caret)
}
}
/// The completion list gets Up, Down and Tab from the field while it is open, and the caret does not move.
#[trial]
fn completion_list_takes_arrows_and_tab_while_open_and_the_caret_does_not_move() {
let mut rig = Rig::new("");
rig.focus();
let frame = |rig: &mut Rig, events: Vec<Event>| {
let (text, seen) = (&mut rig.text, &mut rig.seen);
rig.frames.frame(events, |ui| {
let out = TextField::new("Name", Bind::bound(&mut *text)).complete(Prefixes).id_salt("name").show(ui);
seen.rect = out.response.rect;
seen.id = out.response.id;
});
};
frame(&mut rig, vec![Event::Text("al".into())]);
for _ in 0..4 {
frame(&mut rig, vec![]);
}
let shapes = {
let (text, seen) = (&mut rig.text, &mut rig.seen);
rig.frames.frame_shapes(|ui| {
let out = TextField::new("Name", Bind::bound(&mut *text)).complete(Prefixes).id_salt("name").show(ui);
seen.id = out.response.id;
})
};
let shown = painted(&shapes);
assert!(shown.iter().any(|t| t == "alpha") && shown.iter().any(|t| t == "alps"), "typing opens the list: {shown:?}");
let widget = rig.widget();
let before = caret(&rig.frames.ctx, widget);
for key in [Key::ArrowDown, Key::ArrowDown, Key::ArrowUp, Key::ArrowDown] {
frame(&mut rig, vec![key_press(key, Modifiers::NONE)]);
frame(&mut rig, vec![]);
}
assert_eq!(caret(&rig.frames.ctx, widget), before, "Up and Down go to the list and do not also move the caret");
let focus = craft_ui::testing::focus::egui_focused(&rig.frames.ctx);
frame(&mut rig, vec![key_press(Key::Tab, Modifiers::NONE)]);
for _ in 0..4 {
frame(&mut rig, vec![]);
}
assert_eq!(craft_ui::testing::focus::egui_focused(&rig.frames.ctx), focus, "Tab went to the list, not to the next stop");
assert_eq!(rig.text, "alps", "Tab accepted the highlighted candidate");
}
/// A value set from outside while a draft is open ends the run: the field shows the new value at once and Enter
/// commits that value, not the draft.
#[trial]
fn a_value_set_outside_ends_the_typing_run() {
let value = std::rc::Rc::new(std::cell::RefCell::new(Some(1.0f64)));
let shown = value.clone();
let mut fixture = craft_ui::testing::Fixture::new(move |ui| {
NumberField::new("Size", Bind::bound(&mut *shown.borrow_mut())).show(ui);
});
fixture.settle();
fixture.key(Key::Tab, Modifiers::NONE);
fixture.type_text("12");
fixture.idle(1);
*value.borrow_mut() = Some(7.0);
fixture.idle(1);
assert!(fixture.texts().iter().any(|run| run.text.contains('7')), "the new value shows in the next pass");
assert!(fixture.texts().iter().all(|run| !run.text.contains("12")), "the stale draft is gone");
fixture.key(Key::Enter, Modifiers::NONE);
fixture.idle(3);
const SET: f64 = 7.0;
assert_eq!(*value.borrow(), Some(SET), "Enter commits the value that was set, not the draft");
}
/// Enter on an open completion list commits the text as typed unless the field completes on Enter; Tab always takes
/// the highlighted candidate.
#[trial]
fn enter_accepts_a_completion_only_where_the_field_completes_on_enter() {
for on_enter in [false, true] {
let mut rig = Rig::new("");
rig.focus();
let frame = |rig: &mut Rig, events: Vec<Event>| {
let (text, seen) = (&mut rig.text, &mut rig.seen);
rig.frames.frame(events, |ui| {
let field = TextField::new("Name", Bind::bound(&mut *text)).complete(Prefixes).id_salt("name");
let out = if on_enter { field.complete_on_enter() } else { field }.show(ui);
seen.rect = out.response.rect;
seen.id = out.response.id;
});
};
frame(&mut rig, vec![Event::Text("al".into())]);
for _ in 0..4 {
frame(&mut rig, vec![]);
}
frame(&mut rig, vec![key_press(Key::Enter, Modifiers::NONE)]);
for _ in 0..4 {
frame(&mut rig, vec![]);
}
let expected = if on_enter { "alpha" } else { "al" };
assert_eq!(rig.text, expected, "Enter with complete_on_enter {on_enter}");
}
}
/// Completes "sum" and "sumif", each marked, with a description.
struct Marked;
impl Completer for Marked {
fn complete(&self, text: &str, caret: usize, out: &mut Vec<Completion>) -> Option<Range<usize>> {
let before = text.get(..caret)?;
let hits: Vec<_> = [("sum", "Adds numbers."), ("sumif", "Adds numbers that meet a condition.")]
.into_iter()
.filter(|(word, _)| !before.is_empty() && word.starts_with(before))
.collect();
out.extend(hits.iter().map(|(word, what)| Completion::new(*word, *word).mark("ƒ").description(*what)));
(!hits.is_empty()).then_some(0..caret)
}
}
/// The list marks each candidate and describes the highlighted one: the description follows the highlight.
#[trial]
fn the_completion_list_describes_the_highlighted_candidate() {
let mut rig = Rig::new("");
rig.focus();
let shown_after = |rig: &mut Rig, events: Vec<Event>| {
let (text, seen) = (&mut rig.text, &mut rig.seen);
let mut show = |ui: &mut egui::Ui| {
let out = TextField::new("Name", Bind::bound(&mut *text)).complete(Marked).id_salt("name").show(ui);
seen.rect = out.response.rect;
seen.id = out.response.id;
};
rig.frames.frame(events, &mut show);
for _ in 0..4 {
rig.frames.frame(vec![], &mut show);
}
painted(&rig.frames.frame_shapes(&mut show))
};
let first = shown_after(&mut rig, vec![Event::Text("su".into())]);
assert!(first.iter().any(|t| t == "ƒ"), "each candidate shows its mark: {first:?}");
assert!(
first.iter().any(|t| t == "Adds numbers.") && !first.iter().any(|t| t == "Adds numbers that meet a condition."),
"the first candidate is described: {first:?}"
);
let second = shown_after(&mut rig, vec![key_press(Key::ArrowDown, Modifiers::NONE)]);
assert!(second.iter().any(|t| t == "Adds numbers that meet a condition."), "the description follows the highlight: {second:?}");
}