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 <noreply@anthropic.com>
This commit is contained in:
committed 2026-11-07 16:44:15 +01:00
1 parent 58b9630bec
commit 4591ad552d
2 files changed
+94 -65

No files matched your search

+45 -55
View File
@@ -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<String> {
/// 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<String> {
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<f64> {
@@ -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<String, Value>, 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<String, Value>, 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::<Unit>(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<String, Value>, 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<String, Value
for (value, (label, id_salt, along_x)) in margins.iter_mut().zip(items) {
ui.vertical(|ui| {
ui.label(label);
margin_field(app, ui, id_salt, value, label, 64.0, along_x);
unit_edit(app, ui, id_salt, value, false, along_x, 64.0).accessible_name(label);
});
}
});
@@ -356,16 +356,55 @@ fn units_can_be_typed_and_convert_on_reopen() {
}
#[test]
fn normalization_appends_ruler_unit_and_ensures_space() {
fn normalization_appends_the_field_unit_and_ensures_space() {
use photocraft_engine::prefs::Unit;
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()));
let px = Unit::Pixels;
assert_eq!(normalize_field_text("10", &app, true, false, px), Some("10 px".into()));
assert_eq!(normalize_field_text("10px", &app, true, false, px), Some("10 px".into()));
assert_eq!(normalize_field_text("10cm", &app, true, false, px), Some("10 cm".into()));
assert_eq!(normalize_field_text("2in", &app, true, false, px), Some("2 in".into()));
assert_eq!(normalize_field_text("5mm", &app, false, false, px), 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()));
app.session.prefs.edit(|p| p.units_and_rulers.rulers = Unit::Centimeters);
assert_eq!(normalize_field_text("10", &app, true, false, Unit::Centimeters), Some("10 cm".into()));
assert_eq!(normalize_field_text("2.5", &app, true, false, Unit::Centimeters), Some("2.5 cm".into()));
assert_eq!(normalize_field_text("20px", &app, true, false, Unit::Centimeters), Some("20 px".into()));
}
/// The reference application keeps three decimals in every unit: a typed "13.5 px" stays, it is
/// not rounded to whole pixels, and 20 px at 72 ppi opens as "0.706 cm".
#[test]
fn fields_keep_three_decimals_in_every_unit() {
use photocraft_engine::prefs::Unit;
let mut app = app();
assert_eq!(normalize_field_text("13.5 px", &app, true, false, Unit::Pixels), Some("13.5 px".into()));
assert_eq!(normalize_field_text("5.123456", &app, true, false, Unit::Millimeters), Some("5.123 mm".into()));
app.session.prefs.edit(|p| p.units_and_rulers.rulers = Unit::Centimeters);
let dpi = f64::from(app.session.active().unwrap().doc.resolution_dpi);
assert_eq!(in_ruler_unit(&app, dpi * 0.70556 / 2.54, true), "0.706 cm");
}
/// After "2 mm", a bare number typed into the field is in millimetres, not the ruler unit.
#[test]
fn a_bare_number_takes_the_unit_the_field_showed() {
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!("2 mm"));
let mut h = harness(app);
h.run_steps(3);
// Number, Width, then Spacing (gutter).
for _ in 0..3 {
h.key_press(egui::Key::Tab);
h.run_steps(2);
}
h.event(egui::Event::Text("5".into()));
h.run_steps(2);
h.key_press(egui::Key::Tab);
h.run_steps(2);
assert_eq!(h.state().ui.dialogs.first().unwrap().fields["gutter"], "5 mm");
}