PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Dialogs: the first value is focused and selected; values persist (#1757) (#2040)
David Mann7567347b293a
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex 97b2651fa..24e7ce0dc 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -202,7 +202,16 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {// Long parameter lists (Flame, Lighting Effects) scroll, so the title and the// OK / Cancel buttons stay inside a small window.let room = (ctx.content_rect().height() - DIALOG_CHROME).max(120.0);+ // Photoshop opens a value dialog on its first number, selected: typing+ // replaces it and Enter applies it (#1757). Once, on the first laid-out frame.+ let focused = id.with("first-field");+ let first = !ui.is_sizing_pass() && !ctx.data(|m| m.get_temp::<bool>(focused).unwrap_or(false));+ if first {+ ctx.data_mut(|m| m.insert_temp(focused, true));+ }+ crate::widgets::focus_first_field(ctx, first);egui::ScrollArea::vertical().id_salt(id.with("body")).max_height(room).show(ui, |ui| crate::filter_dialog::body(ui, &mut fields));+ crate::widgets::focus_first_field(ctx, false);}DialogKind::Command if fields.contains_key("__form") => crate::view_cmds::form_body(ui, &mut fields),DialogKind::Command => {}@@ -643,4 +652,37 @@ mod tests {let other = h.get_by_label("Motion Blur").rect();assert!(other.min.x > moved.min.x + 100.0, "Motion Blur opened centred: {other:?}");}++ /// #1757: like Photoshop, a value dialog opens on its first number with the text selected, so+ /// typing replaces it and Enter applies it; it reopens with the value applied last time, and a+ /// cancelled edit is not remembered.+ #[test]+ fn a_value_dialog_opens_on_its_first_number_selected_and_remembers_it() {+ use egui_kittest::kittest::Queryable;+ const BLUR: &str = "filter.blur.gaussianBlur";+ let mut h = dialog_harness(egui::vec2(1280.0, 800.0), BLUR);+ let radius = |h: &egui_kittest::Harness<'static, PhotocraftApp>| h.state().ui.dialogs.last().and_then(|d| d.fields["radius"].as_f64());+ assert_eq!(radius(&h), Some(1.0), "the default the first time");+ assert!(h.get_by_role(egui::accesskit::Role::SpinButton).is_focused(), "the Radius field has focus");+ h.event(egui::Event::Text("5".into()));+ h.run_steps(1);+ assert_eq!(radius(&h), Some(5.0), "typing replaced the selected 1");+ h.key_press(egui::Key::Enter);+ h.run_steps(3);+ assert!(h.state().ui.dialogs.is_empty(), "Enter is OK");+ assert_eq!(h.state().session.journal.last(), Some(&(BLUR.to_string(), json!({"radius": 5.0}))));++ crate::filter_dialog::open(h.state_mut(), BLUR).unwrap();+ h.run_steps(4);+ assert_eq!(radius(&h), Some(5.0), "reopens with the last value");+ assert!(h.get_by_role(egui::accesskit::Role::SpinButton).is_focused(), "focused again");+ h.event(egui::Event::Text("9".into()));+ h.run_steps(1);+ h.key_press(egui::Key::Escape);+ h.run_steps(3);+ assert!(h.state().ui.dialogs.is_empty(), "Esc is Cancel");+ crate::filter_dialog::open(h.state_mut(), BLUR).unwrap();+ h.run_steps(2);+ assert_eq!(radius(&h), Some(5.0), "a cancelled 9 is not remembered");+ }}diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rsindex 05c582444..f1371c888 100644--- a/crates/ui-egui/src/widgets.rs+++ b/crates/ui-egui/src/widgets.rs@@ -343,11 +343,31 @@ pub fn popup_value_field(ui: &mut Ui, name: &str, value: &mut f32, range: std::oout}+fn first_field_id() -> egui::Id {+ egui::Id::new("pc-focus-first-field")+}++/// While `on`, the next number field drawn takes the keyboard focus with its text selected (a+/// dialog opening on its first value, as in Photoshop). Turn it off once the body is laid out.+pub fn focus_first_field(ctx: &egui::Context, on: bool) {+ ctx.data_mut(|d| {+ if on {+ d.insert_temp(first_field_id(), true);+ } else {+ d.remove::<bool>(first_field_id());+ }+ });+}+/// The number in a [`value_field`]. A typed number applies as it's typed; arithmetic waits for/// Enter, Tab or click-away, because per keystroke `5/2` would land first and a caller that/// rounds it would cut `5/2*2` short. `changed()` means a new value, not just a keystroke.fn number_edit(ui: &mut Ui, value: &mut f32, range: std::ops::RangeInclusive<f32>, fine: bool) -> Response {let (id, ctx) = (ui.next_auto_id(), ui.ctx().clone());+ // Focused before the DragValue is drawn, so it gains focus this frame and selects its text.+ if ui.data_mut(|d| d.remove_temp::<bool>(first_field_id())).unwrap_or(false) {+ ui.memory_mut(|m| m.request_focus(id));+ }let held = id.with("arithmetic");let math = ui.memory(|m| m.has_focus(id)) && ui.data(|d| d.get_temp(held)).unwrap_or(false);ui.data_mut(|d| d.insert_temp(held, math));