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 <safvanedge@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
committed 2026-11-08 17:53:27 +01:00
1 parent 8500fd729a
commit f5025f8f2a
3 files changed
+105 -4

No files matched your search

+20 -3
View File
@@ -335,13 +335,27 @@ pub fn confirm(app: &mut PhotocraftApp, f: &Map<String, Value>) -> Result<Value,
// Colour is the existing global guide display preference, not document data.
let color = rgb(color).ok_or("invalid guide color")?;
let hex = format!("#{:02x}{:02x}{:02x}", color.r(), color.g(), color.b());
let result = app.run(COMMAND, params_with(Some(app), f))?;
let params = params_with(Some(app), f);
// Lengths are remembered as the pixels they applied, so a bare "5" typed in px still means
// 5 px after the ruler unit changes; reopening shows them in the new unit.
let mut remembered = f.clone();
for (key, along_x, optional) in [("width", true, true), ("height", false, true), ("gutter", true, false), ("rowGutter", false, false)] {
if let Some(v) = f.get(key) {
remembered.insert(key.into(), parse_length_value(Some(v), Some(app), along_x, optional));
}
}
if let Some(Value::Array(m)) = f.get("margin")
&& m.len() == 4
{
let px = m.iter().zip([false, true, false, true]).map(|(v, along_x)| parse_length_value(Some(v), Some(app), along_x, false)).collect();
remembered.insert("margin".into(), Value::Array(px));
}
let result = app.run(COMMAND, params)?;
// The preview is visible even when guides were hidden. Keep a committed layout visible too.
app.ui.view.extras = true;
app.ui.view.show.canvas_guides = true;
app.ui.extras.guides = true;
app.session.prefs.edit(|prefs| prefs.guides_grid_and_slices.guide_color = hex);
let mut remembered = f.clone();
for key in [MARK, "__command", "__label", "__document", "__previewGuides", "__previewParams", "__previewSource"] {
remembered.remove(key);
}
@@ -382,8 +396,11 @@ fn count_field(ui: &mut egui::Ui, f: &mut Map<String, Value>, 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<String, Value>, key: &str, label: egui::Id, optional: bool, along_x: bool) {
@@ -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");
}
+13 -1
View File
@@ -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::<Option<Id>>(pending_id())).flatten() {
let pending = ctx.data_mut(|d| d.remove_temp::<Option<Id>>(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::<Option<Id>>(reached_id())).flatten() == Some(id)
}
/// Start collecting the fields of a dialog body.
pub fn begin(ctx: &Context) {
ctx.data_mut(|d| {