Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(ui): reduced suffix spacing width in the value field to prevent label overflow (#3040) (#3084)

Giuseppe2d2e4993a133
diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs
index 303e79b6d..049d26a92 100644
--- a/crates/ui-egui/src/widgets.rs
+++ b/crates/ui-egui/src/widgets.rs
@@ -304,7 +304,10 @@ fn value_field_box(
if !t.bevel {
ui.painter().rect_stroke(rect, t.radius_sm, Stroke::new(1.0, t.field_border), StrokeKind::Inside);
}
- let suffix_w = if suffix.is_empty() { 0.0 } else { 16.0 };
+ // The number's slot ends 1 pt left of the suffix, by its measured width (the suffix is drawn
+ // 6 pt from the right edge), so a one-character unit like `%` leaves the number room (#3040).
+ let suffix_font = theme::mono(11.0);
+ let suffix_w = if suffix.is_empty() { 0.0 } else { ui.painter().layout_no_wrap(suffix.to_owned(), suffix_font.clone(), t.text_faint).size().x + 3.0 };
// 1 pt higher than centred: digits have no descenders, so centred text looks low. The slot
// is at least one text row tall, so a slim box's number isn't pushed out of it (#2823).
let font = theme::mono(12.0);
@@ -341,7 +344,7 @@ fn value_field_box(
};
ui.data_mut(|d| d.insert_temp(slot.id, resp.id));
if !suffix.is_empty() {
- ui.painter().text(pos2(rect.right() - 6.0 - trailing, rect.center().y), Align2::RIGHT_CENTER, suffix, theme::mono(11.0), t.text_faint);
+ ui.painter().text(pos2(rect.right() - 6.0 - trailing, rect.center().y), Align2::RIGHT_CENTER, suffix, suffix_font, t.text_faint);
}
if step != 0.0 {
// Round to the step before adding it, so whole-number fields drop decimals (55.4 + 1 = 56).
@@ -1411,6 +1414,30 @@ mod tests {
}
}
+ /// The number's slot ends just left of its unit, by the unit's measured width: a one-character
+ /// `%` (the status bar zoom) leaves the number more room than the old fixed 16 pt, and a longer
+ /// unit never sits under the number (#3040).
+ #[test]
+ fn value_field_number_slot_stops_at_its_measured_suffix() {
+ for suffix in ["%", "px", "°", "×"] {
+ let mut h = Harness::new_ui_state(
+ move |ui, s: &mut (f32, egui::Rect, f32)| {
+ let before = ui.cursor().min;
+ super::compact_value_field(ui, &mut s.0, 0.0..=3200.0, suffix, 64.0, 18.0);
+ s.1 = egui::Rect::from_min_size(before, egui::vec2(64.0, 18.0));
+ s.2 = ui.painter().layout_no_wrap(suffix.to_owned(), crate::theme::mono(11.0), egui::Color32::WHITE).size().x;
+ },
+ (33.33, egui::Rect::NOTHING, 0.0),
+ );
+ h.run();
+ let number = h.get_by_role(egui::accesskit::Role::SpinButton).rect();
+ let (_, field, suffix_w) = *h.state();
+ let suffix_left = field.right() - 6.0 - suffix_w;
+ assert!(number.right() <= suffix_left, "{suffix}: number {number:?} runs under the suffix at {suffix_left}");
+ assert!(suffix_left - number.right() <= 1.5, "{suffix}: {:.1} pt wasted before the suffix", suffix_left - number.right());
+ }
+ }
+
#[test]
fn arrow_keys_step_a_focused_field_by_one_and_shift_by_ten() {
let mut h = focused(100.0, 0.0..=1000.0);