From 1175004b65f310a21d0d49631d13e0c3c0f4d939 Mon Sep 17 00:00:00 2001 From: safvanedge <241307593+safvanedge@users.noreply.github.com> Date: Thu, 15 Oct 2026 16:11:55 +0530 Subject: [PATCH 1/9] New Guide Layout: format in ruler unit, support arbitrary units, and normalize on lost focus (#6082) In Photoshop, the New Guide Layout dialog spacing, margin, and size fields format in the document's active ruler unit, accept any unit (cm, in, px, pt, mm) or bare numbers without converting immediately while typing, append the ruler unit for bare numbers on lost focus, and insert a space for unspaced units (e.g. 10cm -> 10 cm). Furthermore, tabbing into a field selects all text for quick replacement, whereas clicking with the mouse places the cursor without selecting all text. * Format initial spacing, width/height, and margins in the active ruler unit. * Replace fixed "px" value_field widgets with singleline text edits. * Convert arbitrary typed units into document pixels for live canvas previews and execution. * Normalize on lost_focus: append active ruler unit for bare values, ensure space between value and unit. * Guard select_all to run only on Tab/keyboard focus, not on mouse clicks. * Add unit tests for unit conversion, reopen formatting, and normalization. Fixes #6082 --- crates/ui-egui/src/guide_layout_ui.rs | 413 +++++++++++++++++--- crates/ui-egui/src/guide_layout_ui/tests.rs | 51 ++- 2 files changed, 409 insertions(+), 55 deletions(-) diff --git a/crates/ui-egui/src/guide_layout_ui.rs b/crates/ui-egui/src/guide_layout_ui.rs index 894535e6875..82491f50f44 100644 --- a/crates/ui-egui/src/guide_layout_ui.rs +++ b/crates/ui-egui/src/guide_layout_ui.rs @@ -2,6 +2,7 @@ use egui::{Color32, Stroke}; use photocraft_doc::{Document, Guides}; +use photocraft_engine::prefs::Unit; use serde_json::{Map, Value, json}; use crate::{PhotocraftApp, theme::Tokens, widgets}; @@ -31,6 +32,110 @@ pub fn owns(f: &Map) -> bool { f.contains_key(MARK) } +fn ruler_unit(app: &PhotocraftApp) -> Unit { + app.session.prefs().units_and_rulers.rulers +} + +fn with_unit(value: &str, unit: Unit) -> String { + format!("{value} {}", unit.suffix()) +} + +/// Document pixels in the active document's ruler unit: "14.11 cm", "0.5 in", "20 px". +pub(crate) fn in_ruler_unit(app: &PhotocraftApp, px: f64, along_x: bool) -> String { + let units = &app.session.prefs().units_and_rulers; + let (dpi, extent) = app.session.active().map_or((72.0, 1.0), |st| { + let d = &st.doc; + ( + f64::from(d.resolution_dpi.max(1.0)), + f64::from(if along_x { d.size.width } else { d.size.height }), + ) + }); + with_unit(&units.format(px, dpi, extent), units.rulers) +} + +fn normalize_field_text(text: &str, app: &PhotocraftApp, along_x: bool, optional: bool) -> Option { + let t = text.trim(); + if t.is_empty() { + if optional { + return Some(String::new()); + } else { + return Some(in_ruler_unit(app, 0.0, along_x)); + } + } + let ruler = ruler_unit(app); + if let Some((v, unit)) = Unit::parse_length(t) { + let u = unit.unwrap_or(ruler); + let s = photocraft_engine::prefs::fmt_decimals(v, u.decimals()); + Some(with_unit(&s, u)) + } else if let Some(v) = widgets::parse_num(t) { + let s = photocraft_engine::prefs::fmt_decimals(v, ruler.decimals()); + Some(with_unit(&s, ruler)) + } else { + None + } +} + +fn parse_length_str(text: &str, app: &PhotocraftApp, along_x: bool) -> Option { + let t = text.trim(); + if t.is_empty() { + return None; + } + let units = &app.session.prefs().units_and_rulers; + let (dpi, extent) = app.session.active().map_or((72.0, 1.0), |st| { + let d = &st.doc; + ( + f64::from(d.resolution_dpi.max(1.0)), + f64::from(if along_x { d.size.width } else { d.size.height }), + ) + }); + let ppi = units.point_size.per_inch(); + let ruler = units.rulers; + + if let Some((n, unit)) = Unit::parse_length(t) { + Some(unit.unwrap_or(ruler).to_px(n, dpi, extent, ppi)) + } else { + widgets::parse_num(t).map(|n| ruler.to_px(n, dpi, extent, ppi)) + } +} + +fn format_stored_length(app: &PhotocraftApp, val: &Value, along_x: bool, optional: bool) -> Value { + match val { + Value::Number(n) => { + let px = n.as_f64().unwrap_or(0.0); + if optional && px <= 0.0 { + Value::Null + } else { + json!(in_ruler_unit(app, px, along_x)) + } + } + Value::String(s) => { + let t = s.trim(); + if t.is_empty() { + if optional { + Value::Null + } else { + json!(in_ruler_unit(app, 0.0, along_x)) + } + } else if let Some(px) = parse_length_str(t, app, along_x) { + if optional && px <= 0.0 { + Value::Null + } else { + json!(in_ruler_unit(app, px, along_x)) + } + } else { + json!(s) + } + } + _ => { + if optional { + Value::Null + } else { + json!(in_ruler_unit(app, 0.0, along_x)) + } + } + } +} + /// Alt+Cancel (Reset): factory New Guide Layout values, not the last-used remembered layout. pub fn reset(app: &PhotocraftApp, f: &mut Map) { if !owns(f) { @@ -51,10 +156,17 @@ pub fn reset(app: &PhotocraftApp, f: &mut Map) { f.insert("__preset".into(), json!("custom")); f.insert("width".into(), Value::Null); f.insert("height".into(), Value::Null); - let margin = f.get("margin").cloned().unwrap_or(json!(0)); - if !margin.is_array() { - f.insert("margin".into(), json!([margin.clone(), margin.clone(), margin.clone(), margin])); - } + f.insert("gutter".into(), json!(in_ruler_unit(app, 20.0, true))); + f.insert("rowGutter".into(), json!(in_ruler_unit(app, 0.0, false))); + f.insert( + "margin".into(), + json!([ + in_ruler_unit(app, 0.0, false), + in_ruler_unit(app, 0.0, true), + in_ruler_unit(app, 0.0, false), + in_ruler_unit(app, 0.0, true) + ]), + ); f.insert("__preview".into(), json!(true)); } @@ -80,18 +192,114 @@ pub fn open(app: &mut PhotocraftApp) -> u64 { f.entry("__preset").or_insert_with(|| json!("custom")); f.entry("width").or_insert(Value::Null); f.entry("height").or_insert(Value::Null); - let margin = f.get("margin").cloned().unwrap_or(json!(0)); - if !margin.is_array() { - f.insert("margin".into(), json!([margin.clone(), margin.clone(), margin.clone(), margin])); + + // Format fields in active ruler unit when opening the dialog + if let Some(gutter) = f.get("gutter").cloned() { + f.insert("gutter".into(), format_stored_length(app, &gutter, true, false)); + } else { + f.insert("gutter".into(), json!(in_ruler_unit(app, 20.0, true))); } + if let Some(row_gutter) = f.get("rowGutter").cloned() { + f.insert("rowGutter".into(), format_stored_length(app, &row_gutter, false, false)); + } else { + f.insert("rowGutter".into(), json!(in_ruler_unit(app, 0.0, false))); + } + if let Some(width) = f.get("width").cloned() { + f.insert("width".into(), format_stored_length(app, &width, true, true)); + } + if let Some(height) = f.get("height").cloned() { + f.insert("height".into(), format_stored_length(app, &height, false, true)); + } + + let margin = f.get("margin").cloned().unwrap_or(json!(0)); + let mut margins = if let Value::Array(a) = margin { + if a.len() == 4 { a } else { vec![json!(0); 4] } + } else { + vec![margin.clone(); 4] + }; + margins[0] = format_stored_length(app, &margins[0], false, false); + margins[1] = format_stored_length(app, &margins[1], true, false); + margins[2] = format_stored_length(app, &margins[2], false, false); + margins[3] = format_stored_length(app, &margins[3], true, false); + f.insert("margin".into(), Value::Array(margins)); + for key in ["__previewGuides", "__previewParams", "__previewSource"] { f.remove(key); } app.ui.open_dialog(crate::state::DialogKind::Command, f) } -/// Preserve legacy command parameter names for automation and remembered defaults. +fn parse_length_value(val: Option<&Value>, app: Option<&PhotocraftApp>, along_x: bool, optional: bool) -> Value { + match val { + Some(Value::Number(n)) => { + if let Some(num) = n.as_f64() { + if optional && num <= 0.0 { + Value::Null + } else { + json!(num) + } + } else { + Value::Null + } + } + Some(Value::String(s)) => { + let t = s.trim(); + if t.is_empty() { + if optional { + Value::Null + } else { + json!(0.0) + } + } else { + let (dpi, extent, ppi, ruler) = match app { + Some(app) => { + let units = &app.session.prefs().units_and_rulers; + let (dpi, extent) = app.session.active().map_or((72.0, 1.0), |st| { + let d = &st.doc; + ( + f64::from(d.resolution_dpi.max(1.0)), + f64::from(if along_x { d.size.width } else { d.size.height }), + ) + }); + (dpi, extent, units.point_size.per_inch(), units.rulers) + } + None => (72.0, 1.0, 72.0, Unit::Pixels), + }; + + if let Some((n, unit)) = Unit::parse_length(t) { + let px = unit.unwrap_or(ruler).to_px(n, dpi, extent, ppi); + if optional && px <= 0.0 { + Value::Null + } else { + serde_json::Number::from_f64(px).map(Value::Number).unwrap_or(Value::Null) + } + } else if let Some(n) = widgets::parse_num(t) { + let px = ruler.to_px(n, dpi, extent, ppi); + if optional && px <= 0.0 { + Value::Null + } else { + serde_json::Number::from_f64(px).map(Value::Number).unwrap_or(Value::Null) + } + } else { + json!(s) + } + } + } + _ => { + if optional { + Value::Null + } else { + json!(0.0) + } + } + } +} + pub fn params(f: &Map) -> Value { + params_with(None, f) +} + +pub fn params_with(app: Option<&PhotocraftApp>, f: &Map) -> Value { let mut p: Map = ["columns", "rows", "width", "height", "gutter", "rowGutter", "margin", "centerColumns", "clearExisting", "target"] .into_iter() .filter_map(|k| f.get(k).cloned().map(|v| (k.to_string(), v))) @@ -103,17 +311,28 @@ pub fn params(f: &Map) -> Value { } if !on(f, "__marginEnabled", true) { p.insert("margin".into(), json!(0)); + } else if let Some(margin_val) = f.get("margin") { + let parsed = match margin_val { + Value::Array(arr) if arr.len() == 4 => { + let top = parse_length_value(Some(&arr[0]), app, false, false); + let left = parse_length_value(Some(&arr[1]), app, true, false); + let bottom = parse_length_value(Some(&arr[2]), app, false, false); + let right = parse_length_value(Some(&arr[3]), app, true, false); + json!([top, left, bottom, right]) + } + Value::Array(_) => json!(0), + other => { + let m = parse_length_value(Some(other), app, false, false); + json!([m.clone(), m.clone(), m.clone(), m]) + } + }; + p.insert("margin".into(), parsed); } - for key in ["width", "height"] { - if let Some(Value::String(s)) = p.get(key) { - let value = if s.trim().is_empty() { - Value::Null - } else { - widgets::parse_num(s).and_then(serde_json::Number::from_f64).map(Value::Number).unwrap_or_else(|| json!(s)) - }; - p.insert(key.into(), value); - } - } + p.insert("width".into(), parse_length_value(f.get("width"), app, true, true)); + p.insert("height".into(), parse_length_value(f.get("height"), app, false, true)); + p.insert("gutter".into(), parse_length_value(f.get("gutter"), app, true, false)); + p.insert("rowGutter".into(), parse_length_value(f.get("rowGutter"), app, false, false)); + // A disabled axis must not be rejected for a stale width or gap typed before disabling it. for (key, size, gap) in [("columns", "width", "gutter"), ("rows", "height", "rowGutter")] { if p.get(key).and_then(Value::as_u64) == Some(0) { @@ -133,7 +352,7 @@ fn calculate(app: &PhotocraftApp, f: &Map) -> Result) -> Result, key: &str, label: &str, value } -fn number(ui: &mut egui::Ui, f: &mut Map, key: &str, count: bool) { +fn count_field(ui: &mut egui::Ui, f: &mut Map, key: &str) { let mut value = f.get(key).and_then(Value::as_f64).unwrap_or(0.0) as f32; - if widgets::value_field(ui, &mut value, (if count { 1.0 } else { 0.0 })..=if count { 1000.0 } else { 300_000.0 }, if count { "" } else { "px" }, 68.0) - .changed() + if widgets::value_field(ui, &mut value, 1.0..=1000.0, "", 68.0).changed() // A disabled field still clamps its value into range (a zero count to 1); writing that // back would switch the axis on through its `positive` fallback (#5625). && ui.is_enabled() { - f.insert(key.into(), if count { json!(value.round() as u64) } else { json!(value) }); + f.insert(key.into(), json!(value.round() as u64)); } } -fn optional(ui: &mut egui::Ui, f: &mut Map, key: &str) { - let mut value = match f.get(key) { +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()) +} + +fn length_field( + app: &PhotocraftApp, + ui: &mut egui::Ui, + f: &mut Map, + key: &str, + width: f32, + optional: bool, + along_x: bool, +) -> egui::Response { + let mut text = match f.get(key) { Some(Value::String(s)) => s.clone(), - Some(v) if v.is_number() => v.to_string(), + Some(Value::Number(n)) => { + let px = n.as_f64().unwrap_or(0.0); + if optional && px <= 0.0 { + String::new() + } else { + in_ruler_unit(app, px, along_x) + } + } _ => String::new(), }; let t = Tokens::get(ui.ctx()); - let response = ui.add(egui::TextEdit::singleline(&mut value).id_salt(key).desired_width(60.0).font(crate::theme::mono(12.0)).background_color(t.field)); + let response = ui.add( + egui::TextEdit::singleline(&mut text) + .id_salt(key) + .desired_width(width) + .font(crate::theme::mono(12.0)) + .background_color(t.field), + ); if ui.is_enabled() { crate::field_tab::register(ui.ctx(), response.id); } - if response.gained_focus() { - crate::field_tab::select_all(ui.ctx(), response.id, &value); + if should_select_all(&response, ui) { + crate::field_tab::select_all(ui.ctx(), response.id, &text); } if response.changed() { - f.insert(key.into(), json!(value)); + f.insert(key.into(), json!(text)); } - response.on_hover_text(tl!("Leave empty for automatic size")); + if response.lost_focus() + && let Some(normalized) = normalize_field_text(&text, app, along_x, optional) + { + text = normalized; + f.insert(key.into(), json!(text)); + } + if optional { + response.on_hover_text(tl!("Leave empty for automatic size")) + } else { + response + } +} + +fn margin_field( + app: &PhotocraftApp, + ui: &mut egui::Ui, + id_salt: &str, + value: &mut Value, + width: f32, + along_x: bool, +) -> egui::Response { + let mut text = match value { + Value::String(s) => s.clone(), + Value::Number(n) => { + let px = n.as_f64().unwrap_or(0.0); + in_ruler_unit(app, px, along_x) + } + _ => in_ruler_unit(app, 0.0, along_x), + }; + let t = Tokens::get(ui.ctx()); + let response = ui.add( + egui::TextEdit::singleline(&mut text) + .id_salt(id_salt) + .desired_width(width) + .font(crate::theme::mono(12.0)) + .background_color(t.field), + ); + if ui.is_enabled() { + crate::field_tab::register(ui.ctx(), response.id); + } + if should_select_all(&response, ui) { + crate::field_tab::select_all(ui.ctx(), response.id, &text); + } + if response.changed() { + *value = json!(text); + } + if response.lost_focus() + && let Some(normalized) = normalize_field_text(&text, app, along_x, false) + { + text = normalized; + *value = json!(text); + } + response } fn group(ui: &mut egui::Ui, content: impl FnOnce(&mut egui::Ui) -> R) -> R { @@ -227,7 +522,7 @@ fn group(ui: &mut egui::Ui, content: impl FnOnce(&mut egui::Ui) -> R) -> R { egui::Frame::new().stroke(Stroke::new(1.0, t.field_border)).inner_margin(8).show(ui, |ui| ui.vertical(content).inner).inner } -fn axis(ui: &mut egui::Ui, f: &mut Map, rows: bool) { +fn axis(app: &PhotocraftApp, ui: &mut egui::Ui, f: &mut Map, rows: bool) { let (toggle, count, size, gap, title, size_label) = if rows { ("__rowsEnabled", "rows", "height", "rowGutter", tl!("Rows"), tl!("Height")) } else { @@ -242,13 +537,13 @@ fn axis(ui: &mut egui::Ui, f: &mut Map, rows: bool) { ui.add_enabled_ui(enabled, |ui| { egui::Grid::new(count).spacing([8.0, 6.0]).show(ui, |ui| { ui.label(tl!("Number")); - number(ui, f, count, true); + count_field(ui, f, count); ui.end_row(); ui.label(size_label); - optional(ui, f, size); + length_field(app, ui, f, size, 68.0, true, !rows); ui.end_row(); ui.label(tl!("Spacing")); - number(ui, f, gap, false); + length_field(app, ui, f, gap, 68.0, false, !rows); ui.end_row(); }); }); @@ -271,18 +566,26 @@ fn main_fields(app: &PhotocraftApp, ui: &mut egui::Ui, f: &mut Map { let (cols, rows, gap) = match preset.as_str() { - "five" => (5, 3, 0), - "eight" => (8, 0, 20), - _ => (12, 0, 24), + "five" => (5, 3, 0.0), + "eight" => (8, 0, 20.0), + _ => (12, 0, 24.0), }; for (k, v) in [ ("columns", json!(cols)), ("rows", json!(rows)), - ("gutter", json!(gap)), - ("rowGutter", json!(0)), + ("gutter", json!(in_ruler_unit(app, gap, true))), + ("rowGutter", json!(in_ruler_unit(app, 0.0, false))), ("width", Value::Null), ("height", Value::Null), - ("margin", json!([0, 0, 0, 0])), + ( + "margin", + json!([ + in_ruler_unit(app, 0.0, false), + in_ruler_unit(app, 0.0, true), + in_ruler_unit(app, 0.0, false), + in_ruler_unit(app, 0.0, true) + ]), + ), ("__columnsEnabled", json!(true)), ("__rowsEnabled", json!(rows > 0)), ("__marginEnabled", json!(true)), @@ -324,24 +627,30 @@ fn main_fields(app: &PhotocraftApp, ui: &mut egui::Ui, f: &mut Map, interactive: bool) -> Option { - let before = (params(f), on(f, "__preview", true), f.get("color").cloned()); + let before = (params_with(Some(app), f), on(f, "__preview", true), f.get("color").cloned()); let mut outcome = None; ui.horizontal_top(|ui| { ui.vertical(|ui| { @@ -386,7 +695,7 @@ pub fn body(app: &PhotocraftApp, ui: &mut egui::Ui, f: &mut Map, match calculate(app, f) { Ok(guides) => { f.insert("__previewGuides".into(), json!(guides)); - f.insert("__previewParams".into(), params(f)); + f.insert("__previewParams".into(), params_with(Some(app), f)); f.insert("__previewSource".into(), source(app)); } Err(error) => { @@ -397,7 +706,7 @@ pub fn body(app: &PhotocraftApp, ui: &mut egui::Ui, f: &mut Map, outcome = Some(true); } // The canvas paints before the dialog. Schedule the frame that displays this draft edit. - if before != (params(f), on(f, "__preview", true), f.get("color").cloned()) { + if before != (params_with(Some(app), f), on(f, "__preview", true), f.get("color").cloned()) { ui.ctx().request_repaint(); } outcome diff --git a/crates/ui-egui/src/guide_layout_ui/tests.rs b/crates/ui-egui/src/guide_layout_ui/tests.rs index 673f1663bf0..94a118922ba 100644 --- a/crates/ui-egui/src/guide_layout_ui/tests.rs +++ b/crates/ui-egui/src/guide_layout_ui/tests.rs @@ -73,7 +73,7 @@ fn tab_includes_optional_sizes_and_shift_tab_selects_their_text() { assert_eq!((f["columns"].as_u64(), f["rows"].as_u64()), (Some(4), Some(2))); assert_eq!(params(f)["width"], 120.0); assert_eq!(params(f)["height"], 90.0); - assert_eq!(f["gutter"], 12.0); + assert_eq!(params(f)["gutter"], 12.0); h.key_press_modifiers(egui::Modifiers::SHIFT, egui::Key::Tab); h.run_steps(2); h.event(egui::Event::Text("3".into())); @@ -101,7 +101,7 @@ fn tab_skips_disabled_rows_and_margins() { let f = &h.state().ui.dialogs.first().unwrap().fields; assert_eq!(f["columns"], 6, "Tab wraps after column spacing, skipping disabled row and margin fields"); assert_eq!(params(f)["width"], 100.0); - assert_eq!(f["gutter"], 4.0); + assert_eq!(params(f)["gutter"], 4.0); } /// The disabled Number field must not clamp a zero count to 1 and so switch its axis on (#5625). @@ -173,7 +173,8 @@ fn alt_reset_restores_factory_layout() { app.ui.dialog_mut(id).unwrap().fields = fields; let f = &app.ui.dialog_mut(id).unwrap().fields; assert_eq!(f["columns"], json!(8)); - assert_eq!(f["gutter"], json!(20)); + assert_eq!(params(f)["gutter"], 20.0); + assert_eq!(f["gutter"], "20 px"); assert_eq!(f["__preset"], json!("custom")); assert!(app.ui.dialogs.iter().any(|d| d.id == id)); } @@ -324,3 +325,47 @@ fn preview_cache_tracks_document_revision() { app.run(COMMAND, json!({"margin":23})).unwrap(); assert!(preview(&app, &app.session.active().unwrap().doc).unwrap().0.vertical.contains(&23.0)); } + +#[test] +fn units_can_be_typed_and_convert_on_reopen() { + let mut app = app(); + let id = open(&mut app); + let f = { + let dialog = app.ui.dialog_mut(id).unwrap(); + dialog.fields.insert("gutter".into(), json!("1 in")); + dialog.fields.insert("margin".into(), json!(["2 cm", "10 mm", "0.5 in", "72 pt"])); + dialog.fields.clone() + }; + let p = params_with(Some(&app), &f); + assert_eq!(p["gutter"], 72.0); + let m = p["margin"].as_array().unwrap(); + assert!((m[0].as_f64().unwrap() - 2.0 / 2.54 * 72.0).abs() < 1e-3); + assert!((m[1].as_f64().unwrap() - 1.0 / 2.54 * 72.0).abs() < 1e-3); + assert_eq!(m[2], 36.0); + assert_eq!(m[3], 72.0); + + crate::dialogs::confirm(&mut app, id).unwrap(); + + app.session.prefs.edit(|p| p.units_and_rulers.rulers = photocraft_engine::prefs::Unit::Inches); + + let reopened = open(&mut app); + let rf = &app.ui.dialog_mut(reopened).unwrap().fields; + assert_eq!(rf["gutter"], "1 in"); + assert_eq!(rf["margin"][2], "0.5 in"); + assert_eq!(rf["margin"][3], "1 in"); +} + +#[test] +fn normalization_appends_ruler_unit_and_ensures_space() { + let mut app = app(); + assert_eq!(normalize_field_text("10", &app, true, false), Some("10 px".into())); + assert_eq!(normalize_field_text("10px", &app, true, false), Some("10 px".into())); + assert_eq!(normalize_field_text("10cm", &app, true, false), Some("10 cm".into())); + assert_eq!(normalize_field_text("2in", &app, true, false), Some("2 in".into())); + assert_eq!(normalize_field_text("5mm", &app, false, false), Some("5 mm".into())); + + app.session.prefs.edit(|p| p.units_and_rulers.rulers = photocraft_engine::prefs::Unit::Centimeters); + assert_eq!(normalize_field_text("10", &app, true, false), Some("10 cm".into())); + assert_eq!(normalize_field_text("2.5", &app, true, false), Some("2.5 cm".into())); + assert_eq!(normalize_field_text("20px", &app, true, false), Some("20 px".into())); +} From 81cdb16ccabba2e5c1719daebbf790df345d8d5c Mon Sep 17 00:00:00 2001 From: safvanedge <241307593+safvanedge@users.noreply.github.com> Date: Thu, 15 Oct 2026 16:19:19 +0530 Subject: [PATCH 2/9] style: cargo fmt --- crates/ui-egui/src/guide_layout_ui.rs | 117 ++++---------------------- 1 file changed, 18 insertions(+), 99 deletions(-) diff --git a/crates/ui-egui/src/guide_layout_ui.rs b/crates/ui-egui/src/guide_layout_ui.rs index 82491f50f44..18a939f1a83 100644 --- a/crates/ui-egui/src/guide_layout_ui.rs +++ b/crates/ui-egui/src/guide_layout_ui.rs @@ -45,10 +45,7 @@ pub(crate) fn in_ruler_unit(app: &PhotocraftApp, px: f64, along_x: bool) -> Stri let units = &app.session.prefs().units_and_rulers; let (dpi, extent) = app.session.active().map_or((72.0, 1.0), |st| { let d = &st.doc; - ( - f64::from(d.resolution_dpi.max(1.0)), - f64::from(if along_x { d.size.width } else { d.size.height }), - ) + (f64::from(d.resolution_dpi.max(1.0)), f64::from(if along_x { d.size.width } else { d.size.height })) }); with_unit(&units.format(px, dpi, extent), units.rulers) } @@ -83,10 +80,7 @@ fn parse_length_str(text: &str, app: &PhotocraftApp, along_x: bool) -> Option { let px = n.as_f64().unwrap_or(0.0); - if optional && px <= 0.0 { - Value::Null - } else { - json!(in_ruler_unit(app, px, along_x)) - } + if optional && px <= 0.0 { Value::Null } else { json!(in_ruler_unit(app, px, along_x)) } } Value::String(s) => { let t = s.trim(); if t.is_empty() { - if optional { - Value::Null - } else { - json!(in_ruler_unit(app, 0.0, along_x)) - } + if optional { Value::Null } else { json!(in_ruler_unit(app, 0.0, along_x)) } } else if let Some(px) = parse_length_str(t, app, along_x) { - if optional && px <= 0.0 { - Value::Null - } else { - json!(in_ruler_unit(app, px, along_x)) - } + if optional && px <= 0.0 { Value::Null } else { json!(in_ruler_unit(app, px, along_x)) } } else { json!(s) } @@ -160,12 +142,7 @@ pub fn reset(app: &PhotocraftApp, f: &mut Map) { f.insert("rowGutter".into(), json!(in_ruler_unit(app, 0.0, false))); f.insert( "margin".into(), - json!([ - in_ruler_unit(app, 0.0, false), - in_ruler_unit(app, 0.0, true), - in_ruler_unit(app, 0.0, false), - in_ruler_unit(app, 0.0, true) - ]), + json!([in_ruler_unit(app, 0.0, false), in_ruler_unit(app, 0.0, true), in_ruler_unit(app, 0.0, false), in_ruler_unit(app, 0.0, true)]), ); f.insert("__preview".into(), json!(true)); } @@ -212,11 +189,7 @@ pub fn open(app: &mut PhotocraftApp) -> u64 { } let margin = f.get("margin").cloned().unwrap_or(json!(0)); - let mut margins = if let Value::Array(a) = margin { - if a.len() == 4 { a } else { vec![json!(0); 4] } - } else { - vec![margin.clone(); 4] - }; + let mut margins = if let Value::Array(a) = margin { if a.len() == 4 { a } else { vec![json!(0); 4] } } else { vec![margin.clone(); 4] }; margins[0] = format_stored_length(app, &margins[0], false, false); margins[1] = format_stored_length(app, &margins[1], true, false); margins[2] = format_stored_length(app, &margins[2], false, false); @@ -233,11 +206,7 @@ fn parse_length_value(val: Option<&Value>, app: Option<&PhotocraftApp>, along_x: match val { Some(Value::Number(n)) => { if let Some(num) = n.as_f64() { - if optional && num <= 0.0 { - Value::Null - } else { - json!(num) - } + if optional && num <= 0.0 { Value::Null } else { json!(num) } } else { Value::Null } @@ -245,21 +214,14 @@ fn parse_length_value(val: Option<&Value>, app: Option<&PhotocraftApp>, along_x: Some(Value::String(s)) => { let t = s.trim(); if t.is_empty() { - if optional { - Value::Null - } else { - json!(0.0) - } + if optional { Value::Null } else { json!(0.0) } } else { let (dpi, extent, ppi, ruler) = match app { Some(app) => { let units = &app.session.prefs().units_and_rulers; let (dpi, extent) = app.session.active().map_or((72.0, 1.0), |st| { let d = &st.doc; - ( - f64::from(d.resolution_dpi.max(1.0)), - f64::from(if along_x { d.size.width } else { d.size.height }), - ) + (f64::from(d.resolution_dpi.max(1.0)), f64::from(if along_x { d.size.width } else { d.size.height })) }); (dpi, extent, units.point_size.per_inch(), units.rulers) } @@ -268,18 +230,10 @@ fn parse_length_value(val: Option<&Value>, app: Option<&PhotocraftApp>, along_x: if let Some((n, unit)) = Unit::parse_length(t) { let px = unit.unwrap_or(ruler).to_px(n, dpi, extent, ppi); - if optional && px <= 0.0 { - Value::Null - } else { - serde_json::Number::from_f64(px).map(Value::Number).unwrap_or(Value::Null) - } + if optional && px <= 0.0 { Value::Null } else { serde_json::Number::from_f64(px).map(Value::Number).unwrap_or(Value::Null) } } else if let Some(n) = widgets::parse_num(t) { let px = ruler.to_px(n, dpi, extent, ppi); - if optional && px <= 0.0 { - Value::Null - } else { - serde_json::Number::from_f64(px).map(Value::Number).unwrap_or(Value::Null) - } + if optional && px <= 0.0 { Value::Null } else { serde_json::Number::from_f64(px).map(Value::Number).unwrap_or(Value::Null) } } else { json!(s) } @@ -424,35 +378,17 @@ 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()) } -fn length_field( - app: &PhotocraftApp, - ui: &mut egui::Ui, - f: &mut Map, - key: &str, - width: f32, - optional: bool, - along_x: bool, -) -> egui::Response { +fn length_field(app: &PhotocraftApp, ui: &mut egui::Ui, f: &mut Map, key: &str, width: f32, optional: bool, along_x: bool) -> egui::Response { let mut text = match f.get(key) { Some(Value::String(s)) => s.clone(), Some(Value::Number(n)) => { let px = n.as_f64().unwrap_or(0.0); - if optional && px <= 0.0 { - String::new() - } else { - in_ruler_unit(app, px, along_x) - } + if optional && px <= 0.0 { String::new() } else { in_ruler_unit(app, px, along_x) } } _ => String::new(), }; let t = Tokens::get(ui.ctx()); - let response = ui.add( - egui::TextEdit::singleline(&mut text) - .id_salt(key) - .desired_width(width) - .font(crate::theme::mono(12.0)) - .background_color(t.field), - ); + let response = ui.add(egui::TextEdit::singleline(&mut text).id_salt(key).desired_width(width).font(crate::theme::mono(12.0)).background_color(t.field)); if ui.is_enabled() { crate::field_tab::register(ui.ctx(), response.id); } @@ -468,21 +404,10 @@ fn length_field( text = normalized; f.insert(key.into(), json!(text)); } - if optional { - response.on_hover_text(tl!("Leave empty for automatic size")) - } else { - response - } + if optional { response.on_hover_text(tl!("Leave empty for automatic size")) } else { response } } -fn margin_field( - app: &PhotocraftApp, - ui: &mut egui::Ui, - id_salt: &str, - value: &mut Value, - width: f32, - along_x: bool, -) -> egui::Response { +fn margin_field(app: &PhotocraftApp, ui: &mut egui::Ui, id_salt: &str, value: &mut Value, width: f32, along_x: bool) -> egui::Response { let mut text = match value { Value::String(s) => s.clone(), Value::Number(n) => { @@ -492,13 +417,7 @@ fn margin_field( _ => in_ruler_unit(app, 0.0, along_x), }; let t = Tokens::get(ui.ctx()); - let response = ui.add( - egui::TextEdit::singleline(&mut text) - .id_salt(id_salt) - .desired_width(width) - .font(crate::theme::mono(12.0)) - .background_color(t.field), - ); + let response = ui.add(egui::TextEdit::singleline(&mut text).id_salt(id_salt).desired_width(width).font(crate::theme::mono(12.0)).background_color(t.field)); if ui.is_enabled() { crate::field_tab::register(ui.ctx(), response.id); } From 835259929469c647dd8cdd94800e8763721787ef Mon Sep 17 00:00:00 2001 From: safvanedge <241307593+safvanedge@users.noreply.github.com> Date: Mon, 19 Oct 2026 15:18:34 +0530 Subject: [PATCH 3/9] test(ui): update new_guide_layout_remembers_successful_values test for unit formatted strings (#6082) --- crates/ui-egui/src/view_cmds/tests.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/view_cmds/tests.rs b/crates/ui-egui/src/view_cmds/tests.rs index 407d8e6824b..87c2fee6190 100644 --- a/crates/ui-egui/src/view_cmds/tests.rs +++ b/crates/ui-egui/src/view_cmds/tests.rs @@ -325,8 +325,8 @@ fn new_guide_layout_remembers_successful_values_not_cancelled_edits() { let (mut app, ctx) = app_with(1); let first = menu(&mut app, &ctx, "view.newGuideLayout", json!({})).unwrap()["dialog"].as_u64().unwrap(); assert_eq!(app.ui.dialog_mut(first).unwrap().fields["columns"], 8); - assert_eq!(app.ui.dialog_mut(first).unwrap().fields["gutter"], 20); - for (key, value) in [("columns", json!(2)), ("gutter", json!(0)), ("rows", json!(2)), ("rowGutter", json!(0))] { + assert_eq!(app.ui.dialog_mut(first).unwrap().fields["gutter"], "20 px"); + for (key, value) in [("columns", json!(2)), ("gutter", json!("0 px")), ("rows", json!(2)), ("rowGutter", json!("0 px"))] { app.ui.dialog_mut(first).unwrap().fields.insert(key.into(), value); } crate::dialogs::confirm(&mut app, first).unwrap(); @@ -334,9 +334,9 @@ fn new_guide_layout_remembers_successful_values_not_cancelled_edits() { let second = menu(&mut app, &ctx, "view.newGuideLayout", json!({})).unwrap()["dialog"].as_u64().unwrap(); let fields = &app.ui.dialog_mut(second).unwrap().fields; assert_eq!(fields["columns"], 2); - assert_eq!(fields["gutter"], 0); + assert_eq!(fields["gutter"], "0 px"); assert_eq!(fields["rows"], 2); - assert_eq!(fields["rowGutter"], 0); + assert_eq!(fields["rowGutter"], "0 px"); assert_eq!(fields["clearExisting"], false); // A cancelled edit must not replace the last successfully used values. app.ui.dialog_mut(second).unwrap().fields.insert("columns".into(), json!(9)); From 4591ad552deb6c324ab90aea46762c6db19be4d3 Mon Sep 17 00:00:00 2001 From: David Mann Date: Sat, 7 Nov 2026 16:44:15 +0100 Subject: [PATCH 4/9] New Guide Layout (#6083): keep three decimals and the field's unit The fields rounded a typed length to the unit's readout precision on lost focus ("12.5 px" became "13 px"), and a bare number always took the ruler unit. The reference application keeps three decimals in every unit (20 px at 72 ppi opens as "0.706 cm", "13.5 px" stays) and gives a bare number the unit the field showed (after "2 mm", "5" becomes "5 mm"). Also merges main (dialog moved to dialogs/, accessible names kept) and folds the two length field widgets into one. Co-Authored-By: safvanedge <241307593+safvanedge@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/dialogs/guide_layout_ui.rs | 100 ++++++++---------- .../src/dialogs/guide_layout_ui/tests.rs | 59 +++++++++-- 2 files changed, 94 insertions(+), 65 deletions(-) diff --git a/crates/ui-egui/src/dialogs/guide_layout_ui.rs b/crates/ui-egui/src/dialogs/guide_layout_ui.rs index 873e91aeaa0..b2940c7f8bb 100644 --- a/crates/ui-egui/src/dialogs/guide_layout_ui.rs +++ b/crates/ui-egui/src/dialogs/guide_layout_ui.rs @@ -40,17 +40,29 @@ fn with_unit(value: &str, unit: Unit) -> String { format!("{value} {}", unit.suffix()) } -/// Document pixels in the active document's ruler unit: "14.11 cm", "0.5 in", "20 px". +/// Decimals the reference application's guide layout fields show in every unit, trailing zeros +/// dropped: 20 px at 72 ppi opens as "0.706 cm", a typed "13.5 px" stays "13.5 px". +const FIELD_DECIMALS: usize = 3; + +/// Document pixels in the active document's ruler unit: "0.706 cm", "0.278 in", "20 px". pub(crate) fn in_ruler_unit(app: &PhotocraftApp, px: f64, along_x: bool) -> String { let units = &app.session.prefs().units_and_rulers; let (dpi, extent) = app.session.active().map_or((72.0, 1.0), |st| { let d = &st.doc; (f64::from(d.resolution_dpi.max(1.0)), f64::from(if along_x { d.size.width } else { d.size.height })) }); - with_unit(&units.format(px, dpi, extent), units.rulers) + let v = units.rulers.from_px(px, dpi, extent, units.point_size.per_inch()); + with_unit(&photocraft_engine::prefs::fmt_decimals(v, FIELD_DECIMALS), units.rulers) } -fn normalize_field_text(text: &str, app: &PhotocraftApp, along_x: bool, optional: bool) -> Option { +/// The unit a field's text names, or `fallback` when it names none. +fn text_unit(text: &str, fallback: Unit) -> Unit { + Unit::parse_length(text).and_then(|(_, unit)| unit).unwrap_or(fallback) +} + +/// The text a field settles on when it loses focus. A bare number takes `unit`, the unit the +/// field showed when it got focus (after "2 mm", a typed "5" becomes "5 mm"). +fn normalize_field_text(text: &str, app: &PhotocraftApp, along_x: bool, optional: bool, unit: Unit) -> Option { let t = text.trim(); if t.is_empty() { if optional { @@ -59,17 +71,11 @@ fn normalize_field_text(text: &str, app: &PhotocraftApp, along_x: bool, optional return Some(in_ruler_unit(app, 0.0, along_x)); } } - let ruler = ruler_unit(app); - if let Some((v, unit)) = Unit::parse_length(t) { - let u = unit.unwrap_or(ruler); - let s = photocraft_engine::prefs::fmt_decimals(v, u.decimals()); - Some(with_unit(&s, u)) - } else if let Some(v) = widgets::parse_num(t) { - let s = photocraft_engine::prefs::fmt_decimals(v, ruler.decimals()); - Some(with_unit(&s, ruler)) - } else { - None - } + let (v, unit) = match Unit::parse_length(t) { + Some((v, typed)) => (v, typed.unwrap_or(unit)), + None => (widgets::parse_num(t)?, unit), + }; + Some(with_unit(&photocraft_engine::prefs::fmt_decimals(v, FIELD_DECIMALS), unit)) } fn parse_length_str(text: &str, app: &PhotocraftApp, along_x: bool) -> Option { @@ -380,65 +386,49 @@ 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()) } -#[allow(clippy::too_many_arguments)] -fn length_field(app: &PhotocraftApp, ui: &mut egui::Ui, f: &mut Map, key: &str, label: egui::Id, width: f32, optional: bool, along_x: bool) -> egui::Response { - let mut text = match f.get(key) { - Some(Value::String(s)) => s.clone(), - Some(Value::Number(n)) => { - let px = n.as_f64().unwrap_or(0.0); - if optional && px <= 0.0 { String::new() } else { in_ruler_unit(app, px, along_x) } - } - _ => String::new(), - }; - let t = Tokens::get(ui.ctx()); - let response = ui - .add(egui::TextEdit::singleline(&mut text).id_salt(key).desired_width(width).font(crate::theme::mono(12.0)).background_color(t.field)) - .labelled_by(label); - if ui.is_enabled() { - crate::field_tab::register(ui.ctx(), response.id); +fn length_field(app: &PhotocraftApp, ui: &mut egui::Ui, f: &mut Map, key: &str, label: egui::Id, optional: bool, along_x: bool) { + let mut value = f.get(key).cloned().unwrap_or(Value::Null); + let response = unit_edit(app, ui, key, &mut value, optional, along_x, 68.0).labelled_by(label); + if f.get(key).unwrap_or(&Value::Null) != &value { + f.insert(key.into(), value); } - if should_select_all(&response, ui) { - crate::field_tab::select_all(ui.ctx(), response.id, &text); + if optional { + response.on_hover_text(tl!("Leave empty for automatic size")); } - if response.changed() { - f.insert(key.into(), json!(text)); - } - if response.lost_focus() - && let Some(normalized) = normalize_field_text(&text, app, along_x, optional) - { - text = normalized; - f.insert(key.into(), json!(text)); - } - if optional { response.on_hover_text(tl!("Leave empty for automatic size")) } else { response } } -fn margin_field(app: &PhotocraftApp, ui: &mut egui::Ui, id_salt: &str, value: &mut Value, name: &str, width: f32, along_x: bool) -> egui::Response { +/// A length field that takes any unit. Its text is stored as typed and settles on lost focus. +fn unit_edit(app: &PhotocraftApp, ui: &mut egui::Ui, id_salt: &str, value: &mut Value, optional: bool, along_x: bool, width: f32) -> egui::Response { let mut text = match value { Value::String(s) => s.clone(), Value::Number(n) => { let px = n.as_f64().unwrap_or(0.0); - in_ruler_unit(app, px, along_x) + if optional && px <= 0.0 { String::new() } else { in_ruler_unit(app, px, along_x) } } + _ if optional => String::new(), _ => in_ruler_unit(app, 0.0, along_x), }; let t = Tokens::get(ui.ctx()); - let response = ui - .add(egui::TextEdit::singleline(&mut text).id_salt(id_salt).desired_width(width).font(crate::theme::mono(12.0)).background_color(t.field)) - .accessible_name(name); + let response = ui.add(egui::TextEdit::singleline(&mut text).id_salt(id_salt).desired_width(width).font(crate::theme::mono(12.0)).background_color(t.field)); if ui.is_enabled() { crate::field_tab::register(ui.ctx(), response.id); } + let ruler = ruler_unit(app); + if response.gained_focus() { + let unit = text_unit(&text, ruler); + ui.ctx().data_mut(|d| d.insert_temp(response.id, unit)); + } if should_select_all(&response, ui) { crate::field_tab::select_all(ui.ctx(), response.id, &text); } if response.changed() { *value = json!(text); } - if response.lost_focus() - && let Some(normalized) = normalize_field_text(&text, app, along_x, false) - { - text = normalized; - *value = json!(text); + if response.lost_focus() { + let unit = ui.ctx().data_mut(|d| d.remove_temp::(response.id)).unwrap_or(ruler); + if let Some(normalized) = normalize_field_text(&text, app, along_x, optional, unit) { + *value = json!(normalized); + } } response } @@ -466,10 +456,10 @@ fn axis(app: &PhotocraftApp, ui: &mut egui::Ui, f: &mut Map, rows count_field(ui, f, count, "Number"); ui.end_row(); let size_label_resp = ui.label(size_label); - length_field(app, ui, f, size, size_label_resp.id, 68.0, true, !rows); + length_field(app, ui, f, size, size_label_resp.id, true, !rows); ui.end_row(); let gap_label_resp = ui.label(tl!("Spacing")); - length_field(app, ui, f, gap, gap_label_resp.id, 68.0, false, !rows); + length_field(app, ui, f, gap, gap_label_resp.id, false, !rows); ui.end_row(); }); }); @@ -577,7 +567,7 @@ fn main_fields(app: &PhotocraftApp, ui: &mut egui::Ui, f: &mut Map Date: Sun, 8 Nov 2026 01:49:24 +0100 Subject: [PATCH 5/9] New Guide Layout (#6083): tabbing past a field keeps the preset Leaving a length field without typing rewrote its value on focus loss: an empty automatic Width turned from null into "", which made a named preset such as "8 Columns" switch to Custom. The field now writes back only when normalising changes the text shown. Co-Authored-By: safvanedge Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/dialogs/guide_layout_ui.rs | 6 ++++- .../src/dialogs/guide_layout_ui/tests.rs | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/dialogs/guide_layout_ui.rs b/crates/ui-egui/src/dialogs/guide_layout_ui.rs index b2940c7f8bb..5c55773a49c 100644 --- a/crates/ui-egui/src/dialogs/guide_layout_ui.rs +++ b/crates/ui-egui/src/dialogs/guide_layout_ui.rs @@ -426,7 +426,11 @@ fn unit_edit(app: &PhotocraftApp, ui: &mut egui::Ui, id_salt: &str, value: &mut } if response.lost_focus() { let unit = ui.ctx().data_mut(|d| d.remove_temp::(response.id)).unwrap_or(ruler); - if let Some(normalized) = normalize_field_text(&text, app, along_x, optional, unit) { + // Leaving a field untouched keeps its stored value (an automatic size stays empty), so + // tabbing past it does not turn a named preset into Custom. + if let Some(normalized) = normalize_field_text(&text, app, along_x, optional, unit) + && normalized != text + { *value = json!(normalized); } } 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 637dde6d8cf..e434ea237b3 100644 --- a/crates/ui-egui/src/dialogs/guide_layout_ui/tests.rs +++ b/crates/ui-egui/src/dialogs/guide_layout_ui/tests.rs @@ -408,3 +408,28 @@ fn a_bare_number_takes_the_unit_the_field_showed() { h.run_steps(2); assert_eq!(h.state().ui.dialogs.first().unwrap().fields["gutter"], "5 mm"); } + +/// Tabbing past fields without typing keeps a named preset: an empty Width stays automatic. +#[test] +fn tabbing_through_untouched_fields_keeps_the_preset() { + let mut app = app(); + let id = open(&mut app); + let f = &mut app.ui.dialog_mut(id).unwrap().fields; + f.insert("__preset".into(), json!("eight")); + f.insert("columns".into(), json!(8)); + f.insert("rows".into(), json!(0)); + f.insert("__rowsEnabled".into(), json!(false)); + f.insert("__marginEnabled".into(), json!(false)); + f.insert("width".into(), Value::Null); + f.insert("gutter".into(), json!(20)); + let mut h = harness(app); + h.run_steps(3); + // Number, Width, Spacing, then back to Number. + for _ in 0..4 { + h.key_press(egui::Key::Tab); + h.run_steps(2); + } + let f = &h.state().ui.dialogs.first().unwrap().fields; + assert_eq!(f["__preset"], "eight"); + assert_eq!(f["width"], Value::Null); +} From f5025f8f2a8e68029b702ab40f8cee4e4c8545ea Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 8 Nov 2026 17:53:27 +0100 Subject: [PATCH 6/9] New Guide Layout (#6083): Tab selects while a button is held, lengths remembered in px CodeRabbit found two more gaps on the PR's latest head: - Tab into a length field while a mouse button was held did not select its text, so typing appended ("2 mm5"). field_tab now marks the field it hands focus to, and the dialog selects that field whatever the mouse does. - A unitless length set over automation ("5") was remembered as typed, so a later ruler unit change reread it in the new unit. Confirm now remembers each length as the pixels it applied; reopening shows them in the new unit. Also adds a test that Enter straight after a bare number applies it in the unit the field showed. Co-Authored-By: safvanedge Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/dialogs/guide_layout_ui.rs | 23 +++++- .../src/dialogs/guide_layout_ui/tests.rs | 72 +++++++++++++++++++ crates/ui-egui/src/widgets/field_tab.rs | 14 +++- 3 files changed, 105 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/dialogs/guide_layout_ui.rs b/crates/ui-egui/src/dialogs/guide_layout_ui.rs index 5c55773a49c..4e269bb3489 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 e434ea237b3..40e217b3920 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 79d82170291..e670e8098ec 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| { From 1e4b8816fef725da9d9882f5acac4989c6602778 Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 8 Nov 2026 18:10:21 +0100 Subject: [PATCH 7/9] New Guide Layout (#6083): drop a same-type cast in a test Co-Authored-By: safvanedge Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/dialogs/guide_layout_ui/tests.rs | 1 - 1 file changed, 1 deletion(-) 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 40e217b3920..c54a649ce12 100644 --- a/crates/ui-egui/src/dialogs/guide_layout_ui/tests.rs +++ b/crates/ui-egui/src/dialogs/guide_layout_ui/tests.rs @@ -497,7 +497,6 @@ fn remembered_lengths_keep_their_size_when_the_ruler_unit_changes() { 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); From 08a908d3f507e4ab36a54ba398b7d990ad639092 Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 9 Nov 2026 19:16:12 +0100 Subject: [PATCH 8/9] New Guide Layout (#6083): percent of each artboard, Tab marker per pass Percent typed in Width, Spacing or a margin was turned into pixels of the document before preview and OK, so with Selected Artboards a board smaller than the document got the document's share. It now goes to the engine as percent, which takes it of each target, and is remembered as percent. The field Tab reached stayed marked as reached while a popup kept the dialog from taking the next Tab step, so a later click into that field selected all its text. The marker now holds only for the pass that handed the focus. Co-Authored-By: safvanedge Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/dialogs/guide_layout_ui.rs | 20 ++++++++++++- .../src/dialogs/guide_layout_ui/tests.rs | 22 +++++++++++++++ crates/ui-egui/src/widgets/field_tab.rs | 28 +++++++++++++++++-- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/dialogs/guide_layout_ui.rs b/crates/ui-egui/src/dialogs/guide_layout_ui.rs index 4e269bb3489..9fc3c72dd5c 100644 --- a/crates/ui-egui/src/dialogs/guide_layout_ui.rs +++ b/crates/ui-egui/src/dialogs/guide_layout_ui.rs @@ -98,6 +98,19 @@ fn parse_length_str(text: &str, app: &PhotocraftApp, along_x: bool) -> Option Option { + match Unit::parse_length(text) { + Some((n, Some(Unit::Percent))) => Some(n), + Some((n, None)) if ruler == Unit::Percent => Some(n), + Some(_) => None, + None if ruler == Unit::Percent => widgets::parse_num(text), + None => None, + } +} + fn format_stored_length(app: &PhotocraftApp, val: &Value, along_x: bool, optional: bool) -> Value { match val { Value::Number(n) => { @@ -108,6 +121,8 @@ fn format_stored_length(app: &PhotocraftApp, val: &Value, along_x: bool, optiona let t = s.trim(); if t.is_empty() { if optional { Value::Null } else { json!(in_ruler_unit(app, 0.0, along_x)) } + } else if let Some(pct) = percent_of(t, ruler_unit(app)) { + if optional && pct <= 0.0 { Value::Null } else { json!(with_unit(&photocraft_engine::prefs::fmt_decimals(pct, FIELD_DECIMALS), Unit::Percent)) } } else if let Some(px) = parse_length_str(t, app, along_x) { if optional && px <= 0.0 { Value::Null } else { json!(in_ruler_unit(app, px, along_x)) } } else { @@ -221,6 +236,8 @@ fn parse_length_value(val: Option<&Value>, app: Option<&PhotocraftApp>, along_x: let t = s.trim(); if t.is_empty() { if optional { Value::Null } else { json!(0.0) } + } else if let Some(pct) = percent_of(t, app.map_or(Unit::Pixels, ruler_unit)) { + if optional && pct <= 0.0 { Value::Null } else { json!(format!("{pct}%")) } } else { let (dpi, extent, ppi, ruler) = match app { Some(app) => { @@ -337,7 +354,8 @@ pub fn confirm(app: &mut PhotocraftApp, f: &Map) -> Result i32 { if let Some(id) = pending { ctx.memory_mut(|m| m.request_focus(id)); } - ctx.data_mut(|d| d.insert_temp(reached_id(), pending)); + // Tagged with this pass: `take_step` is skipped while a popup is open, and a marker left + // over from an earlier pass would make a later click into the field select all its text. + let pass = ctx.cumulative_pass_nr(); + ctx.data_mut(|d| d.insert_temp(reached_id(), pending.map(|id| (id, pass)))); let step = ctx.input_mut(|i| { if i.consume_key(Modifiers::SHIFT, Key::Tab) { -1 @@ -61,7 +64,7 @@ pub fn take_step(ctx: &Context) -> i32 { /// 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) + ctx.data(|d| d.get_temp::>(reached_id())).flatten() == Some((id, ctx.cumulative_pass_nr())) } /// Start collecting the fields of a dialog body. @@ -227,6 +230,27 @@ mod tests { assert_eq!((d.name.as_str(), d.size.width, d.size.height), ("Flyer", 512, 310)); } + /// The field a Tab reached is "reached" only in the pass that handed it focus: while a popup + /// is open `take_step` is skipped, and a later click into the field must place the caret. + #[test] + fn reached_lasts_only_the_pass_that_applied_the_focus() { + let ctx = egui::Context::default(); + let id = egui::Id::new("field"); + let mut seen = Vec::new(); + for apply in [true, false, false] { + ctx.run_ui(Default::default(), |ui| { + if apply { + super::focus(ui.ctx(), id); + super::take_step(ui.ctx()); + } + seen.push(super::reached(ui.ctx(), id)); + }) + .textures_delta + .clear(); + } + assert_eq!(seen, [true, false, false]); + } + /// Tab is the dialog's: the modal underneath never sees it, and a dialog with no fields swallows it. #[test] fn tab_in_a_dialog_without_fields_does_nothing() { From 49bf44ce59f5e8a94cba2392a0b25f30b4a4d0de Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 9 Nov 2026 19:33:04 +0100 Subject: [PATCH 9/9] New Guide Layout (#6083): use Id::unique in the Tab marker test Co-Authored-By: safvanedge Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/widgets/field_tab.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ui-egui/src/widgets/field_tab.rs b/crates/ui-egui/src/widgets/field_tab.rs index e2cd9402fc6..e50c009f566 100644 --- a/crates/ui-egui/src/widgets/field_tab.rs +++ b/crates/ui-egui/src/widgets/field_tab.rs @@ -235,7 +235,7 @@ mod tests { #[test] fn reached_lasts_only_the_pass_that_applied_the_focus() { let ctx = egui::Context::default(); - let id = egui::Id::new("field"); + let id = egui::Id::unique("field"); let mut seen = Vec::new(); for apply in [true, false, false] { ctx.run_ui(Default::default(), |ui| {