diff --git a/crates/ui-egui/src/dialogs/guide_layout_ui.rs b/crates/ui-egui/src/dialogs/guide_layout_ui.rs index 5c55773a49..4e269bb348 100644 --- a/crates/ui-egui/src/dialogs/guide_layout_ui.rs +++ b/crates/ui-egui/src/dialogs/guide_layout_ui.rs @@ -335,13 +335,27 @@ pub fn confirm(app: &mut PhotocraftApp, f: &Map) -> Result, key: &str, name: & } } +/// Tab selects the text even while a mouse button is held; a click places the caret. fn should_select_all(response: &egui::Response, ui: &egui::Ui) -> bool { - response.gained_focus() && !response.clicked() && !response.dragged() && !ui.input(|i| i.pointer.any_click() || i.pointer.any_down()) + response.gained_focus() + && (crate::field_tab::reached(ui.ctx(), response.id) + || (!response.clicked() && !response.dragged() && !ui.input(|i| i.pointer.any_click() || i.pointer.any_down()))) } fn length_field(app: &PhotocraftApp, ui: &mut egui::Ui, f: &mut Map, key: &str, label: egui::Id, optional: bool, along_x: bool) { diff --git a/crates/ui-egui/src/dialogs/guide_layout_ui/tests.rs b/crates/ui-egui/src/dialogs/guide_layout_ui/tests.rs index e434ea237b..40e217b392 100644 --- a/crates/ui-egui/src/dialogs/guide_layout_ui/tests.rs +++ b/crates/ui-egui/src/dialogs/guide_layout_ui/tests.rs @@ -433,3 +433,75 @@ fn tabbing_through_untouched_fields_keeps_the_preset() { assert_eq!(f["__preset"], "eight"); assert_eq!(f["width"], Value::Null); } + +fn gutter_dialog(gutter: &str) -> egui_kittest::Harness<'static, PhotocraftApp> { + let mut app = app(); + let id = open(&mut app); + let f = &mut app.ui.dialog_mut(id).unwrap().fields; + f.insert("columns".into(), json!(2)); + f.insert("__rowsEnabled".into(), json!(false)); + f.insert("__marginEnabled".into(), json!(false)); + f.insert("gutter".into(), json!(gutter)); + let mut h = harness(app); + h.run_steps(3); + h +} + +/// Tab from the dialog's start to Spacing (gutter): Number, Width, then Spacing. +fn tab_to_gutter(h: &mut egui_kittest::Harness<'static, PhotocraftApp>) { + for _ in 0..3 { + h.key_press(egui::Key::Tab); + h.run_steps(2); + } +} + +/// Enter straight after typing a bare number applies it in the unit the field showed: "5" typed +/// over "2 mm" is 5 mm, not 5 px. +#[test] +fn enter_after_a_bare_number_applies_the_unit_the_field_showed() { + let mut h = gutter_dialog("2 mm"); + tab_to_gutter(&mut h); + h.event(egui::Event::Text("5".into())); + h.run_steps(2); + h.key_press(egui::Key::Enter); + h.run_steps(3); + let app = h.state(); + assert!(app.ui.dialogs.is_empty(), "Enter applies the layout"); + // Two automatic columns: the gutter is the gap between the second and third guide. + let v = &app.session.active().unwrap().doc.guides.vertical; + let gutter = v[2] - v[1]; + assert!((gutter - 5.0 / 25.4 * 72.0).abs() < 1e-2, "gutter {gutter} px in {v:?}"); +} + +/// A unitless length set over automation is remembered as the length it applied, so a later +/// ruler unit change does not reinterpret it. +#[test] +fn remembered_lengths_keep_their_size_when_the_ruler_unit_changes() { + use photocraft_engine::prefs::Unit; + let mut app = app(); + let id = open(&mut app); + let f = &mut app.ui.dialog_mut(id).unwrap().fields; + f.insert("gutter".into(), json!("5")); + f.insert("margin".into(), json!(["5", "5", "5", "5"])); + crate::dialogs::confirm(&mut app, id).unwrap(); + app.session.prefs.edit(|p| p.units_and_rulers.rulers = Unit::Millimeters); + let reopened = open(&mut app); + let rf = &app.ui.dialog_mut(reopened).unwrap().fields; + assert_eq!(rf["gutter"], "1.764 mm"); + assert_eq!(rf["margin"][0], "1.764 mm"); +} + +/// Tab selects the field it reaches even while a mouse button is held, so typing replaces the +/// value instead of appending to it. +#[test] +fn tab_selects_the_field_while_a_mouse_button_is_held() { + let mut h = gutter_dialog("2 mm"); + let at = egui_kittest::kittest::Queryable::get_all_by_label(&h, "Number").next().unwrap().rect().center(); + let at = egui::pos2(at.x as f32, at.y as f32); + h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed: true, modifiers: egui::Modifiers::NONE }); + h.run_steps(2); + tab_to_gutter(&mut h); + h.event(egui::Event::Text("5".into())); + h.run_steps(2); + assert_eq!(h.state().ui.dialogs.first().unwrap().fields["gutter"], "5"); +} diff --git a/crates/ui-egui/src/widgets/field_tab.rs b/crates/ui-egui/src/widgets/field_tab.rs index 79d8217029..e670e8098e 100644 --- a/crates/ui-egui/src/widgets/field_tab.rs +++ b/crates/ui-egui/src/widgets/field_tab.rs @@ -23,6 +23,10 @@ fn pending_id() -> Id { Id::unique("pc-field-tab-pending") } +fn reached_id() -> Id { + Id::unique("pc-field-tab-reached") +} + /// Give `id` the focus from the next frame on. Focusing a widget after it was laid out would /// never read as "gained focus" to it (egui compares with the focus at the start of the frame), /// and that is what selects a field's text; so the move waits for [`take_step`] next frame. @@ -33,9 +37,11 @@ pub fn focus(ctx: &Context, id: Id) { /// Tab (+1) or ⇧Tab (−1) pressed this frame, taken from the input so nothing else acts on it; /// 0 when neither was. Also applies a pending [`focus`]. Call before the dialog is laid out. pub fn take_step(ctx: &Context) -> i32 { - if let Some(id) = ctx.data_mut(|d| d.remove_temp::>(pending_id())).flatten() { + let pending = ctx.data_mut(|d| d.remove_temp::>(pending_id())).flatten(); + if let Some(id) = pending { ctx.memory_mut(|m| m.request_focus(id)); } + ctx.data_mut(|d| d.insert_temp(reached_id(), pending)); let step = ctx.input_mut(|i| { if i.consume_key(Modifiers::SHIFT, Key::Tab) { -1 @@ -52,6 +58,12 @@ pub fn take_step(ctx: &Context) -> i32 { step } +/// True in the frame a field gets the focus [`take_step`] handed it (Tab, ⇧Tab or [`focus`]), +/// whatever the mouse is doing meanwhile. +pub fn reached(ctx: &Context, id: Id) -> bool { + ctx.data(|d| d.get_temp::>(reached_id())).flatten() == Some(id) +} + /// Start collecting the fields of a dialog body. pub fn begin(ctx: &Context) { ctx.data_mut(|d| {