feat(ui-egui): Scale Height/Width and a From origin in Format Shape's Size & Properties

Scale is measured from the size the tab first showed, like the Size and Position
dialog, and runs shape.sizePosition (honouring Lock aspect ratio). From switches
Horizontal/Vertical between the top-left corner and the centre. The lock checkbox
used a fresh id every frame so it never held; it now keeps its state.
This commit is contained in:
Harrison Booth
2026-10-26 21:18:44 -05:00
parent ee875abffa
commit 6fa873dcfc
2 changed files with 85 additions and 3 deletions
+47 -3
View File
@@ -147,9 +147,16 @@ pub(super) fn format_shape(app: &mut SlideApp, ui: &mut Ui) {
}
}
"size" => {
let lock_id = egui::Id::unique("fmt-lock-aspect");
let lock_id = ui.scope_id().with("fmt-lock-aspect");
let mut lock: bool = ui.data_mut(|d| d.get_temp(lock_id).unwrap_or(false));
let locked = lock;
// Scale is measured from the size the shape had when this tab first showed it, like the
// Size and Position dialog; the origin choice is kept for the session.
let base_id = ui.scope_id().with(("fmt-scale-base", uid, sh.id.0));
let (base_w, base_h): (f64, f64) = ui.data_mut(|d| d.get_temp(base_id).unwrap_or((x.w, x.h)));
ui.data_mut(|d| d.insert_temp(base_id, (base_w, base_h)));
let origin_id = ui.scope_id().with("fmt-origin-center");
let mut center: bool = ui.data_mut(|d| d.get_temp(origin_id).unwrap_or(false));
let len = app.session.prefs.units;
let mut fields = |ui: &mut Ui, label: &str, id: &str, v: f64, key: &str, cmd: &str, angle: bool| {
let (unit, suffix) = if angle { (1.0, "°") } else { (len.points_per_unit(), len.suffix()) };
@@ -168,13 +175,50 @@ pub(super) fn format_shape(app: &mut SlideApp, ui: &mut Ui) {
fields(ui, "Height", "fh", x.h, "h", "shape.resize", false);
fields(ui, "Width", "fw", x.w, "w", "shape.resize", false);
fields(ui, "Rotation", "fr", x.rot, "deg", "shape.rotate", true);
for (label, id, cur, base, key) in [("Scale Height", "fsh", x.h, base_h, "h"), ("Scale Width", "fsw", x.w, base_w, "w")] {
ui.horizontal(|ui| {
ui.add_sized(vec2(90.0, 20.0), egui::Label::new(label));
let pct = if base > 0.0 { cur / base * 100.0 } else { 100.0 };
if let Some(nv) = widgets::spinner(ui, id, label, pct, 1.0, "%", 70.0)
&& nv > 0.0
{
let mut p = json!({key: base * nv / 100.0});
if locked {
p["lockAspect"] = json!(true);
}
run(app, "shape.sizePosition", p);
}
});
}
if ui.checkbox(&mut lock, "Lock aspect ratio").changed() {
ui.data_mut(|d| d.insert_temp(lock_id, lock));
}
ui.add_space(6.0);
ui.label(egui::RichText::new("Position").font(theme::bold(13.0)));
fields(ui, "Horizontal", "fx", x.x, "x", "shape.move", false);
fields(ui, "Vertical", "fy", x.y, "y", "shape.move", false);
ui.horizontal(|ui| {
ui.add_sized(vec2(90.0, 20.0), egui::Label::new("From"));
let combo =
egui::ComboBox::from_id_salt("fmt-origin").selected_text(if center { "Center" } else { "Top Left Corner" }).show_ui(ui, |ui| {
ui.selectable_value(&mut center, false, "Top Left Corner");
ui.selectable_value(&mut center, true, "Center");
});
combo.response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ComboBox, true, "From"));
});
ui.data_mut(|d| d.insert_temp(origin_id, center));
let (dx, dy) = if center { (x.w / 2.0, x.h / 2.0) } else { (0.0, 0.0) };
// The spinner shows `value / unit`; `shape.move` takes the top-left corner, so a centre
// reading is shifted by half the size in both directions.
let (px, py) = (x.x + dx, x.y + dy);
let mut pos = |ui: &mut Ui, label: &str, id: &str, v: f64, off: f64, key: &str| {
ui.horizontal(|ui| {
ui.add_sized(vec2(90.0, 20.0), egui::Label::new(label));
if let Some(nv) = widgets::spinner(ui, id, label, v / len.points_per_unit(), 0.1, len.suffix(), 70.0) {
run(app, "shape.move", json!({key: nv * len.points_per_unit() - off}));
}
});
};
pos(ui, "Horizontal", "fx", px, dx, "x");
pos(ui, "Vertical", "fy", py, dy, "y");
ui.add_space(6.0);
ui.label(egui::RichText::new("Alt Text").font(theme::bold(13.0)));
let id = ui.scope_id().with(("alt", uid, sh.id.0));
+38
View File
@@ -211,3 +211,41 @@ fn the_ruler_counts_centimetres_in_halves_and_inches_in_quarters() {
// A hand-edited prefs.json can say points: the ruler stays in inches rather than a tick per point.
assert_eq!(crate::canvas::ruler_steps(Unit::Points), (72.0, 4));
}
fn type_into(h: &mut Harness<'_, SlideApp>, shown: &str, text: &str) {
h.query_all_by_role(Role::TextInput).rfind(|n| n.value().as_deref() == Some(shown)).unwrap().focus();
h.run_steps(1);
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
h.query_all_by_role(Role::TextInput).find(|n| n.is_focused()).unwrap().type_text(text);
h.run_steps(1);
h.key_press(egui::Key::Enter);
h.run_steps(3);
}
/// Format Shape › Size & Properties: Scale Width/Height and the From origin of Position.
#[test]
fn the_format_pane_scales_and_positions_from_the_centre() {
let mut app = app_with_shape(Unit::default());
app.ui.pane = Some("format".into());
app.ui.format_tab = "size".into();
let mut h = harness(app);
h.run_steps(3);
let shown = text_values(&h);
assert_eq!(shown.iter().filter(|s| *s == "100%").count(), 2, "{shown:?}");
// 50% Scale Width (the last "100%" row) halves the 144 pt width.
type_into(&mut h, "100%", "50");
let x = selected_xfrm(h.state());
assert!((x.w - 72.0).abs() < 1e-6 && (x.h - 108.0).abs() < 1e-6, "{x:?}");
assert!(text_values(&h).iter().any(|s| s == "50%"));
// From Center: the fields read the middle of the box, and typing moves the middle.
h.get_by_role_and_label(Role::ComboBox, "From").click();
h.run_steps(2);
h.get_by_label("Center").click();
h.run_steps(3);
// Box is now (72, 36, 72, 108): its centre is (108, 90) pt = (1.5", 1.25").
let shown = text_values(&h);
assert!(shown.iter().any(|s| s == "1.5\"") && shown.iter().any(|s| s == "1.25\""), "{shown:?}");
type_into(&mut h, "1.5\"", "3");
let x = selected_xfrm(h.state());
assert!((x.x - 180.0).abs() < 1e-6 && (x.w - 72.0).abs() < 1e-6 && (x.y - 36.0).abs() < 1e-6, "{x:?}");
}