From 9d192a1df1ca0843cf9c0e55ad57600c0b6fe8df Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 2 Nov 2026 17:33:55 +0700 Subject: [PATCH 1/3] Slider number fields keep what is typed and change nothing when left unchanged --- crates/ui-egui/src/widgets.rs | 34 +++++++++++++---- crates/ui-egui/tests/effect_sliders.rs | 52 ++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index 96d8a7c8..3d10259e 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -36,18 +36,36 @@ fn slider_inner(ui: &mut Ui, label: &str, value: &mut f32, range: std::ops::Rang ui.horizontal(|ui| { ui.label(RichText::new(label).color(tk.text)); ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - let mut text = format!("{:.*}", decimals, value); - let r = ui.add(egui::TextEdit::singleline(&mut text).desired_width(44.0).align(egui::Align2::RIGHT_CENTER).frame(egui::Frame::NONE)); - if r.lost_focus() - && let Ok(v) = text.trim().parse::() - && v.is_finite() - { - *value = v.clamp(lo, hi); - edit = Some(false); + let id = ui.next_auto_id(); + let shown = format!("{:.*}", decimals, value); + // What is typed lives here while the field has focus (`value` is re-read every frame). + let mut text = ui.data(|d| d.get_temp::(id)).unwrap_or_else(|| shown.clone()); + let r = ui.add(egui::TextEdit::singleline(&mut text).id(id).desired_width(44.0).align(egui::Align2::RIGHT_CENTER).frame(egui::Frame::NONE)); + if r.has_focus() { + ui.data_mut(|d| d.insert_temp(id, text.clone())); + } else { + ui.data_mut(|d| d.remove_temp::(id)); + // Escape unfocuses the field before the app's shortcuts may consume the key, so + // look at the raw input. + let escape = ui.input(|i| i.raw.events.iter().any(|e| matches!(e, egui::Event::Key { key: egui::Key::Escape, pressed: true, .. }))); + // Leaving the field unchanged (or with Escape) keeps the value as it is. + if r.lost_focus() + && !escape + && text != shown + && let Ok(v) = text.trim().parse::() + && v.is_finite() + { + *value = v.clamp(lo, hi); + edit = Some(false); + } } }); }); let (rect, mut resp) = ui.allocate_exact_size(vec2(w, 14.0), Sense::click_and_drag()); + if edit.is_some() { + // A typed value is a change for `slider`'s callers too. + resp.mark_changed(); + } if (resp.dragged() || resp.clicked()) && let Some(p) = resp.interact_pointer_pos() { diff --git a/crates/ui-egui/tests/effect_sliders.rs b/crates/ui-egui/tests/effect_sliders.rs index 8c1bb8a7..c4300730 100644 --- a/crates/ui-egui/tests/effect_sliders.rs +++ b/crates/ui-egui/tests/effect_sliders.rs @@ -110,3 +110,55 @@ fn clicking_the_effect_opacity_applies_it() { h.run_steps(3); assert!(opacity(&h, effect) < 0.5, "a click on the track applies: {}", opacity(&h, effect)); } + +/// The number field in the row of the slider labelled `label`. +fn number_field<'a>(h: &'a Harness<'_, TextureCraftApp>, label: &str) -> egui_kittest::Node<'a> { + let row = h.get_by_label(label).rect().center().y; + h.get_all(by().role(Role::TextInput)).find(|n| (n.rect().center().y - row).abs() < 4.0).unwrap() +} + +/// Focus the opacity field and press `keys`, then type `text`, one key per frame like a person. +fn type_opacity(h: &mut Harness<'_, TextureCraftApp>, keys: &[egui::Key], text: &str) { + number_field(h, "Opacity").focus(); + h.run_steps(2); + for k in keys { + h.key_press(*k); + h.run_steps(1); + } + for c in text.chars() { + number_field(h, "Opacity").type_text(&c.to_string()); + h.run_steps(1); + } +} + +#[test] +fn a_value_typed_in_the_number_field_applies() { + let (mut h, effect) = harness(); + let steps = h.state().session.history_labels().len(); + type_opacity(&mut h, &[egui::Key::End, egui::Key::Backspace, egui::Key::Backspace, egui::Key::Backspace, egui::Key::Backspace], "0.25"); + h.key_press(egui::Key::Enter); + h.run_steps(3); + assert_eq!(opacity(&h, effect), 0.25); + assert_eq!(h.state().session.history_labels().len(), steps + 1); +} + +/// Leaving the number field without typing, or with Escape, keeps the value (not the rounded +/// text the field shows) and makes no undo step. +#[test] +fn leaving_the_number_field_unchanged_keeps_the_value() { + let (mut h, effect) = harness(); + texturecraft_engine::execute(&mut h.state_mut().session, "effect.set", &json!({"effect": effect.0, "opacity": 0.333})).unwrap(); + h.run_steps(2); + let steps = h.state().session.history_labels().len(); + type_opacity(&mut h, &[], ""); + h.key_press(egui::Key::Enter); + h.run_steps(3); + assert_eq!(opacity(&h, effect), 0.333, "left without typing"); + type_opacity(&mut h, &[egui::Key::End, egui::Key::Backspace], "9"); + assert_eq!(number_field(&h, "Opacity").value().as_deref(), Some("0.39")); + assert!(number_field(&h, "Opacity").is_focused()); + h.key_press(egui::Key::Escape); + h.run_steps(3); + assert_eq!(opacity(&h, effect), 0.333); + assert_eq!(h.state().session.history_labels().len(), steps); +} From 366f52b8887b56c5b72c43d22bba983958f4d9f7 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 2 Nov 2026 18:08:07 +0700 Subject: [PATCH 2/3] Using a slider's track drops text typed in its number field --- crates/ui-egui/src/widgets.rs | 63 +++++++++++++++----------- crates/ui-egui/tests/effect_sliders.rs | 23 ++++++++++ 2 files changed, 59 insertions(+), 27 deletions(-) diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index 3d10259e..782fcd29 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -33,35 +33,44 @@ fn slider_inner(ui: &mut Ui, label: &str, value: &mut f32, range: std::ops::Rang let (lo, hi) = (*range.start(), *range.end()); let w = ui.available_width(); let mut edit = None; - ui.horizontal(|ui| { - ui.label(RichText::new(label).color(tk.text)); - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - let id = ui.next_auto_id(); - let shown = format!("{:.*}", decimals, value); - // What is typed lives here while the field has focus (`value` is re-read every frame). - let mut text = ui.data(|d| d.get_temp::(id)).unwrap_or_else(|| shown.clone()); - let r = ui.add(egui::TextEdit::singleline(&mut text).id(id).desired_width(44.0).align(egui::Align2::RIGHT_CENTER).frame(egui::Frame::NONE)); - if r.has_focus() { - ui.data_mut(|d| d.insert_temp(id, text.clone())); - } else { - ui.data_mut(|d| d.remove_temp::(id)); - // Escape unfocuses the field before the app's shortcuts may consume the key, so - // look at the raw input. - let escape = ui.input(|i| i.raw.events.iter().any(|e| matches!(e, egui::Event::Key { key: egui::Key::Escape, pressed: true, .. }))); - // Leaving the field unchanged (or with Escape) keeps the value as it is. - if r.lost_focus() - && !escape - && text != shown - && let Ok(v) = text.trim().parse::() - && v.is_finite() - { - *value = v.clamp(lo, hi); - edit = Some(false); + let field = ui + .horizontal(|ui| { + ui.label(RichText::new(label).color(tk.text)); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + let id = ui.next_auto_id(); + let shown = format!("{:.*}", decimals, value); + // What is typed lives here while the field has focus (`value` is re-read every frame). + let mut text = ui.data(|d| d.get_temp::(id)).unwrap_or_else(|| shown.clone()); + let r = ui.add(egui::TextEdit::singleline(&mut text).id(id).desired_width(44.0).align(egui::Align2::RIGHT_CENTER).frame(egui::Frame::NONE)); + if r.has_focus() { + ui.data_mut(|d| d.insert_temp(id, text.clone())); + } else { + ui.data_mut(|d| d.remove_temp::(id)); + // Escape unfocuses the field before the app's shortcuts may consume the key, so + // look at the raw input. + let escape = ui.input(|i| i.raw.events.iter().any(|e| matches!(e, egui::Event::Key { key: egui::Key::Escape, pressed: true, .. }))); + // Leaving the field unchanged (or with Escape) keeps the value as it is. + if r.lost_focus() + && !escape + && text != shown + && let Ok(v) = text.trim().parse::() + && v.is_finite() + { + *value = v.clamp(lo, hi); + edit = Some(false); + } } - } - }); - }); + id + }) + .inner + }) + .inner; let (rect, mut resp) = ui.allocate_exact_size(vec2(w, 14.0), Sense::click_and_drag()); + if resp.is_pointer_button_down_on() { + // Using the track drops what was typed in the field (a drag does not take its focus). + ui.data_mut(|d| d.remove_temp::(field)); + ui.memory_mut(|m| m.surrender_focus(field)); + } if edit.is_some() { // A typed value is a change for `slider`'s callers too. resp.mark_changed(); diff --git a/crates/ui-egui/tests/effect_sliders.rs b/crates/ui-egui/tests/effect_sliders.rs index c4300730..4e86f243 100644 --- a/crates/ui-egui/tests/effect_sliders.rs +++ b/crates/ui-egui/tests/effect_sliders.rs @@ -142,6 +142,29 @@ fn a_value_typed_in_the_number_field_applies() { assert_eq!(h.state().session.history_labels().len(), steps + 1); } +/// Dragging the track while the number field has focus drops what was typed there: leaving the +/// field afterwards does not undo the drag. +#[test] +fn a_track_drag_drops_text_typed_in_the_number_field() { + let (mut h, effect) = harness(); + type_opacity(&mut h, &[egui::Key::End, egui::Key::Backspace], "5"); + let t = track(&h, "Opacity"); + h.drag_at(egui::pos2(t.right(), t.center().y)); + h.run_steps(2); + h.hover_at(egui::pos2(t.left() + 20.0, t.center().y)); + h.run_steps(2); + h.drop_at(egui::pos2(t.left() + 20.0, t.center().y)); + h.run_steps(2); + let dragged = opacity(&h, effect); + assert!(dragged < 0.5, "the drag applies: {dragged}"); + // Leave whatever has focus: a click on empty space. + h.drag_at(egui::pos2(5.0, 1390.0)); + h.drop_at(egui::pos2(5.0, 1390.0)); + h.key_press(egui::Key::Enter); + h.run_steps(3); + assert_eq!(opacity(&h, effect), dragged, "the text typed before the drag stays dropped"); +} + /// Leaving the number field without typing, or with Escape, keeps the value (not the rounded /// text the field shows) and makes no undo step. #[test] From 1a703ecfafdf8a5987f2d2597909f2a11124abcf Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 2 Nov 2026 18:33:39 +0700 Subject: [PATCH 3/3] A typed value equal to the current one is no edit --- crates/ui-egui/src/widgets.rs | 1 + crates/ui-egui/tests/effect_sliders.rs | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index 782fcd29..5e000fa8 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -55,6 +55,7 @@ fn slider_inner(ui: &mut Ui, label: &str, value: &mut f32, range: std::ops::Rang && text != shown && let Ok(v) = text.trim().parse::() && v.is_finite() + && v.clamp(lo, hi) != *value { *value = v.clamp(lo, hi); edit = Some(false); diff --git a/crates/ui-egui/tests/effect_sliders.rs b/crates/ui-egui/tests/effect_sliders.rs index 4e86f243..c5374fb0 100644 --- a/crates/ui-egui/tests/effect_sliders.rs +++ b/crates/ui-egui/tests/effect_sliders.rs @@ -183,5 +183,9 @@ fn leaving_the_number_field_unchanged_keeps_the_value() { h.key_press(egui::Key::Escape); h.run_steps(3); assert_eq!(opacity(&h, effect), 0.333); + // Typing the value it already has is no edit either. + type_opacity(&mut h, &[egui::Key::End, egui::Key::Backspace, egui::Key::Backspace, egui::Key::Backspace, egui::Key::Backspace], "0.333"); + h.key_press(egui::Key::Enter); + h.run_steps(3); assert_eq!(h.state().session.history_labels().len(), steps); }