Sketch dimension: typing replaces the proposed value (#11204) (#11214)

* claim: Sketch dimension: typing replaces the proposed value (#11204)

* Sketch dimensions: typing replaces the proposed value until you move the caret (#11204)

The value box selected the proposed value only on its first frame. A frame
in which the field misses the keyboard collapses egui's selection to its
caret, so typing 400 over a proposed 40 mm could give 40040 mm (seen in a
WebView2 host). Now every frame starts with the whole value selected, before
the field reads its state, until the person takes the value over: typing, a
completion or reference, a key in the box, or a press into the box after it
stayed in place. The click that placed or pinned the value does not count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
kaltenbrunnermarkus
2026-11-03 18:47:40 +00:00
committed by GitHub
parent 267d8d646e
commit 160ee037cc
2 changed files with 137 additions and 9 deletions
+42 -9
View File
@@ -58,6 +58,13 @@ struct Edit {
/// dimension, and its text follows the pointer until a click pins it or Enter places it.
live: Option<Live>,
selection: Option<egui::text::CCursorRange>,
/// The person took the value over: typed into it, pressed a key in it, inserted a completion
/// or a reference, or pressed into the field. Until then every frame starts with the whole
/// value selected, so the first key replaces it whatever else moved the caret (#11204).
owned: bool,
/// Where the box sat last frame: a press only takes the value over once the box stayed put
/// under it, so the click that placed or pinned the value does not.
shown: Option<Pos2>,
/// The last dimension a click referenced, and when: the second click of a double-click on
/// it must not insert its name again.
picked: Option<(String, f64)>,
@@ -168,6 +175,8 @@ pub fn begin_dimension(app: &mut SolveApp, entities: Vec<String>, at: Vec2) -> R
draft: Some(draft),
live: None,
selection: None,
owned: false,
shown: None,
picked: None,
sketch: None,
driven: None,
@@ -200,6 +209,8 @@ pub fn begin_live(app: &mut SolveApp, entities: Vec<String>, pointer: Option<Pos
draft: Some(draft),
live,
selection: None,
owned: false,
shown: None,
picked: None,
sketch: None,
driven: None,
@@ -243,6 +254,7 @@ fn move_text(app: &SolveApp, e: &mut Edit, at: Vec2) {
if expr != e.expr {
// Keep the new value selected, so typing still replaces it.
e.fresh = FRESH;
e.owned = false;
}
(e.param, e.expr, *draft) = (param, expr, redone);
return;
@@ -429,6 +441,8 @@ pub fn model_double_click(app: &mut SolveApp, pos: Pos2) -> bool {
draft: None,
live: None,
selection: None,
owned: false,
shown: None,
picked: None,
sketch: Some(d.sketch),
driven: None,
@@ -528,6 +542,8 @@ fn open_editor(app: &mut SolveApp, id: Option<String>) -> bool {
draft: None,
live: None,
selection: None,
owned: false,
shown: None,
picked: None,
sketch: None,
driven,
@@ -864,6 +880,8 @@ pub fn edit_param(app: &SolveApp, param: &str) {
draft: None,
live: None,
selection: None,
owned: false,
shown: None,
picked: None,
sketch: None,
driven: None,
@@ -913,6 +931,7 @@ fn insert_reference(ctx: &egui::Context, edit: &mut Edit, name: &str) {
state.store(ctx, id);
ctx.memory_mut(|m| m.request_focus(id));
edit.fresh = FRESH - 1;
edit.owned = true;
if let Some(live) = edit.live.as_mut() {
live.typed = true;
}
@@ -1000,7 +1019,26 @@ fn edit_box(app: &mut SolveApp, ctx: &egui::Context) {
ui.set_max_width(popup_width);
crate::ui_kit::text(ui, &crate::i18n::format("dialog.dimension_name", &[("name", &e.param)]), crate::ui_kit::Text::Inline)
.on_hover_text(crate::i18n::tr("dialog.dimension_insert_hint"));
if !e.owned {
// The whole value is selected before the field reads its state, every frame
// until the person takes it over, so typing replaces it (#11204). Only on the
// first frame was not enough: a frame without the keyboard collapses the
// field's selection to its caret, and the first digit went in beside the value.
let n = e.expr.chars().count();
let mut s = egui::text_edit::TextEditState::load(ui.ctx(), id).unwrap_or_default();
s.cursor.set_char_range(Some(egui::text_selection::CCursorRange::two(egui::text::CCursor::new(0), egui::text::CCursor::new(n))));
e.selection = s.cursor.char_range();
s.store(ui.ctx(), id);
// A key this frame acts on the whole value (a digit replaces it, Home puts the
// caret before it); from then on the caret is the person's.
let focused = ui.memory(|m| m.has_focus(id));
e.owned = focused && ui.input(|i| i.events.iter().any(|ev| matches!(ev, egui::Event::Key { pressed: true, .. })));
}
let r = crate::ui_kit::cell_editor(ui, id, &mut e.expr, "", false);
// A press into the field puts the caret where it landed; not the press that placed
// or pinned the value, which began before the box sat there.
let settled = e.shown == Some(anchor);
e.owned |= settled && r.hovered() && ui.input(|i| i.pointer.any_pressed());
crate::theme::TabAccent::number_focus(ui, &r);
if r.has_focus() {
e.selection = egui::TextEdit::load_state(ctx, id).and_then(|s| s.cursor.char_range());
@@ -1012,17 +1050,10 @@ fn edit_box(app: &mut SolveApp, ctx: &egui::Context) {
if let Some(live) = e.live.as_mut() {
live.typed |= r.changed() || completed;
}
e.owned |= r.changed() || completed;
if let Some(error) = e.draft.as_ref().and_then(|d| d.error.as_ref()) {
crate::ui_kit::text(ui, error, crate::ui_kit::Text::Report("dimension_error", true));
}
if e.fresh == FRESH {
// The whole value is selected, so typing replaces it.
let n = e.expr.chars().count();
let mut s = egui::text_edit::TextEditState::load(ui.ctx(), id).unwrap_or_default();
s.cursor.set_char_range(Some(egui::text_selection::CCursorRange::two(egui::text::CCursor::new(0), egui::text::CCursor::new(n))));
e.selection = s.cursor.char_range();
s.store(ui.ctx(), id);
}
let enter = ui.input(|i| i.key_pressed(egui::Key::Enter));
if r.lost_focus() && enter {
done = Some(true);
@@ -1042,6 +1073,7 @@ fn edit_box(app: &mut SolveApp, ctx: &egui::Context) {
});
});
e.fresh = e.fresh.saturating_sub(1);
e.shown = Some(anchor);
match done {
Some(true) => {
let result = if let Some(draft) = &e.draft {
@@ -1060,7 +1092,8 @@ fn edit_box(app: &mut SolveApp, ctx: &egui::Context) {
match result {
Err(err) => {
app.set_status(err, true);
EDIT.with(|x| *x.borrow_mut() = Some(Edit { fresh: FRESH, ..e }));
// The value is selected again, so typing replaces it.
EDIT.with(|x| *x.borrow_mut() = Some(Edit { fresh: FRESH, owned: false, ..e }));
}
Ok(reply) => {
clear_picks(app);
@@ -865,6 +865,101 @@ fn type_first_keeps_the_place_first_flow() {
crate::dim_view::cancel();
}
/// Put the value field's caret at `at` with nothing selected, as the field itself does when it
/// misses the keyboard for a frame (a WebView2 showed the caret before the value, #11204).
fn caret_without_the_person(h: &Harness, at: usize) {
let id = egui::Id::new("sc_dim_edit");
let mut state = egui::TextEdit::load_state(&h.ctx, id).unwrap_or_default();
state.cursor.set_char_range(Some(egui::text::CCursorRange::one(egui::text::CCursor::new(at))));
state.store(&h.ctx, id);
}
/// A primary click at sketch point `at`, the button held down for `frames` frames.
fn held_click(h: &mut Harness, at: [f64; 2], frames: usize) {
h.step(&json!({"move":{"world":[at[0],at[1],0]}})).unwrap();
let [x, y] = h.at(&json!({"world":[at[0],at[1],0]})).unwrap();
let pos = egui::pos2(x as f32, y as f32);
let (button, modifiers) = (egui::PointerButton::Primary, egui::Modifiers::NONE);
h.app.synthetic.push(egui::Event::PointerButton { pos, button, pressed: true, modifiers });
h.frames(frames);
h.app.synthetic.push(egui::Event::PointerButton { pos, button, pressed: false, modifiers });
h.settle();
}
/// The value field's caret and selection end (characters).
fn caret(h: &Harness) -> Option<(usize, usize)> {
egui::TextEdit::load_state(&h.ctx, egui::Id::new("sc_dim_edit"))
.and_then(|s| s.cursor.char_range())
.map(|r| (r.primary.index.0, r.secondary.index.0))
}
/// Until the person takes the proposed value over, the first digit they type replaces all of
/// it (#11204): however long the click that placed it was held, frames later, and whatever moved
/// the caret without them. Typed without Ctrl+A, as a person does.
#[test]
fn typed_digits_replace_the_proposed_value_whatever_moved_the_caret() {
for held in [1usize, 4, 12] {
let mut h = sketch_with(&[("sketch.line", json!({"points":[[0,0],[20,0]],"infer":false}))], [10., 0.]);
let dims = dimension_count(&h);
h.step(&json!({"click":{"world":[10,0,0]}})).unwrap();
assert_live(&h);
// A click held `held` frames: what a person's 60 to 150 ms click is at 60 Hz and beyond.
held_click(&mut h, [10., -7.], held);
assert_eq!(crate::dim_view::live(), None, "held {held}: the text is pinned");
h.frames(12);
caret_without_the_person(&h, 0);
h.frames(2);
assert_eq!(h.ctx.memory(|m| m.focused()), Some(egui::Id::new("sc_dim_edit")), "held {held}");
keys(&mut h, "400");
assert_eq!(crate::dim_view::draft_preview().map(|d| d.0).as_deref(), Some("400"), "held {held}");
h.step(&json!({"key":"Enter"})).unwrap();
let new = added(&h, dims);
assert_eq!(new.len(), 1, "held {held}");
assert!((new[0].kind.value().unwrap() - 400.).abs() < 1e-6, "held {held}: {:?}", new[0].kind);
crate::dim_view::cancel();
}
}
/// A caret the person put somewhere stays there: Home then 1 writes in front of the value, and a
/// click into the field types where it landed (#11204).
#[test]
fn a_caret_the_person_moved_stays_where_they_put_it() {
let mut h = sketch_with(&[("sketch.line", json!({"points":[[0,0],[20,0]],"infer":false}))], [10., 0.]);
let dims = dimension_count(&h);
h.step(&json!({"click":{"world":[10,0,0]}})).unwrap();
assert_live(&h);
h.step(&json!({"click":{"world":[10,-7,0]}})).unwrap();
h.frames(4);
h.step(&json!({"key":"Home"})).unwrap();
h.frames(4);
assert_eq!(caret(&h), Some((0, 0)), "Home put the caret before the value");
keys(&mut h, "1");
h.frames(4);
assert_eq!(caret(&h), Some((1, 1)), "the caret stays after what was typed");
assert_eq!(crate::dim_view::draft_preview().map(|d| d.0).as_deref(), Some("120 mm"));
h.step(&json!({"key":"Enter"})).unwrap();
let new = added(&h, dims);
assert_eq!(new.len(), 1);
assert!((new[0].kind.value().unwrap() - 120.).abs() < 1e-6, "{:?}", new[0].kind);
crate::dim_view::cancel();
// A click into the field puts the caret where it landed: typing goes there.
let mut h = sketch_with(&[("sketch.line", json!({"points":[[0,0],[20,0]],"infer":false}))], [10., 0.]);
h.step(&json!({"click":{"world":[10,0,0]}})).unwrap();
h.step(&json!({"click":{"world":[10,-7,0]}})).unwrap();
h.frames(4);
let [x, y] = h.widget("20 mm").expect("the value field");
h.step(&json!({"click":[x, y]})).unwrap();
h.frames(4);
assert!(crate::dim_view::editing(), "a click into the field keeps the value open");
let placed = caret(&h);
assert!(placed.is_some_and(|(a, b)| a == b), "the click placed a caret: {placed:?}");
keys(&mut h, "4");
let expr = crate::dim_view::draft_preview().map(|d| d.0).unwrap_or_default();
assert!(expr.len() == "20 mm4".len() && expr.contains('4') && expr != "4", "typing went in at the click, not over the value: {expr:?}");
crate::dim_view::cancel();
}
/// Type-first, the text follows the pointer: moving it from inside the box the picks span (the
/// distance between them) to above or beside it switches to the horizontal or vertical distance and
/// back. Until something is typed the value follows the dimension, so the preview measures the