Skip to content

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

fix(ui): one eliding label for fixed-width chrome, so translations can't overrun (#3580) (#3616)

Scott C Hughes6ca04e5afab5
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index ad543786e..4bda4fcde 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -1449,10 +1449,10 @@ fn label(ui: &mut egui::Ui, s: &str) {
/// Photoshop's scrubby Opacity/Fill labels: drag horizontally to change the percentage without
/// hitting the small number field. The unique drag ID coalesces every frame into one undo step.
-fn scrub_pct_label(ui: &mut egui::Ui, text: &str, percent: &mut f32) -> widgets::PopupFieldResponse {
- let t = Tokens::get(ui.ctx());
- let resp =
- ui.add(egui::Label::new(RichText::new(text).color(t.text_dim)).sense(Sense::click_and_drag())).on_hover_cursor(egui::CursorIcon::ResizeHorizontal);
+fn scrub_pct_label(ui: &mut egui::Ui, text: &str, percent: &mut f32, max_w: f32) -> widgets::PopupFieldResponse {
+ // Elide to the room left in the row (full text on hover): a longer translation shrinks here
+ // instead of running over the fixed controls beside it (#3580).
+ let resp = widgets::elided_label_sensed(ui, text, max_w, Sense::click_and_drag()).on_hover_cursor(egui::CursorIcon::ResizeHorizontal);
let key = resp.id.with("percent-label-drag");
if resp.drag_started() {
let stamp = ui.ctx().cumulative_pass_nr();
@@ -2114,7 +2114,11 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
let mut o = l.opacity * 100.0;
let field = widgets::popup_value_field(ui, opacity_label, &mut o, 0.0..=100.0, "%", LAYER_PCT_W);
// On a narrow dock the (scrubby) label yields its room to the controls (#2309).
- let scrub = if show_opacity_label { scrub_pct_label(ui, opacity_label, &mut o) } else { widgets::PopupFieldResponse::default() };
+ let scrub = if show_opacity_label {
+ scrub_pct_label(ui, opacity_label, &mut o, ui.available_width())
+ } else {
+ widgets::PopupFieldResponse::default()
+ };
if field.changed || scrub.changed {
actions.push(pct_action(l, "opacity", o, scrub.drag.or(field.drag)));
}
@@ -2165,7 +2169,7 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
let mut f = l.fill_opacity * 100.0;
let field = widgets::popup_value_field(ui, fill_label, &mut f, 0.0..=100.0, "%", LAYER_PCT_W);
- let scrub = scrub_pct_label(ui, fill_label, &mut f);
+ let scrub = scrub_pct_label(ui, fill_label, &mut f, ui.available_width());
if field.changed || scrub.changed {
actions.push(pct_action(l, "fill", f, scrub.drag.or(field.drag)));
}
diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs
index 378678459..876ed82e7 100644
--- a/crates/ui-egui/src/widgets.rs
+++ b/crates/ui-egui/src/widgets.rs
@@ -246,6 +246,27 @@ pub fn value_field(ui: &mut Ui, value: &mut f32, range: std::ops::RangeInclusive
value_field_in(ui, value, range, suffix, width, 0.0, FieldOpts::default()).0
}
+/// A single-line label for a row that also holds fixed-width controls: laid out no wider than
+/// `max_w`, cut with '…' when the text (often a longer translation) does not fit, with the full
+/// string on hover. Fixed chrome must never let a label overrun its row (#3580), so an *essential*
+/// label beside a fixed control should use this rather than a show/hide fit check; a *redundant*
+/// label (one whose control already carries the meaning, e.g. an icon row's caption) may be dropped.
+pub fn elided_label_sensed(ui: &mut Ui, text: &str, max_w: f32, sense: Sense) -> Response {
+ let t = Tokens::get(ui.ctx());
+ let font = egui::TextStyle::Body.resolve(ui.style());
+ let galley = crate::tab_strip::elided(ui, text, font, t.text_dim, max_w);
+ let elided = galley.elided;
+ let (rect, resp) = ui.allocate_exact_size(galley.size(), sense);
+ ui.painter().galley(rect.min, galley, t.text_dim);
+ resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Label, ui.is_enabled(), text));
+ if elided { resp.on_hover_text(text) } else { resp }
+}
+
+/// [`elided_label_sensed`] with hover only (a plain, non-interactive label).
+pub fn elided_label(ui: &mut Ui, text: &str, max_w: f32) -> Response {
+ elided_label_sensed(ui, text, max_w, Sense::hover())
+}
+
/// Height of a [`value_field`]'s box.
pub const VALUE_FIELD_H: f32 = 24.0;
@@ -1594,6 +1615,20 @@ mod tests {
);
}
+ /// #3580: the shared eliding label never lays out wider than the room it is given, so a longer
+ /// translation shrinks with '…' instead of overrunning its row; a short label is untouched.
+ #[test]
+ fn elided_label_never_exceeds_its_room() {
+ let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(240.0, 60.0)).build_ui(|ui| {
+ let long = super::elided_label(ui, "Preenchimento:", 24.0); // pt-br Fill:, the longest catalog
+ assert!(long.rect.width() <= 25.0, "elided to the room, got {}", long.rect.width());
+ let short = super::elided_label(ui, "Fill", 200.0);
+ assert!(short.rect.width() < 60.0, "a short label is not padded: {}", short.rect.width());
+ });
+ crate::PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::Studio);
+ h.run_steps(2);
+ }
+
#[test]
fn spectrum_popup_style_matches_main_menu_and_preserves_studio() {
for kind in [crate::theme::ThemeKind::Pro, crate::theme::ThemeKind::ProMedium, crate::theme::ThemeKind::Studio, crate::theme::ThemeKind::StudioLight] {
diff --git a/docs/ui-design.md b/docs/ui-design.md
index f3193b268..91fac9337 100644
--- a/docs/ui-design.md
+++ b/docs/ui-design.md
@@ -26,7 +26,8 @@ The toolbar's foreground and background colour chips follow the `Tokens::round_c
## Rules
- **Colours and radii come from tokens.** Read them with `Tokens::get(ctx)`; never hard-code a colour in a widget.
-- **Shared widgets live in `widgets.rs`:** `card` (panel group; Pro renders a Photoshop tab strip), `value_field`, `slider`/`slider_row`, `toggle`/`checkbox`, `primary_button`/`secondary_button`, `dropdown` (with a chevron icon), `hairline`/`vline`.
+- **Shared widgets live in `widgets.rs`:** `card` (panel group; Pro renders a Photoshop tab strip), `value_field`, `slider`/`slider_row`, `toggle`/`checkbox`, `primary_button`/`secondary_button`, `dropdown` (with a chevron icon), `hairline`/`vline`, `elided_label`.
+- **A label never overflows its row.** Wrap by default. Beside a fixed-width control, or anywhere the row cannot reflow, use `widgets::elided_label(ui, text, max_w)`: a longer translation shrinks with `…` and shows the full string on hover instead of running over its neighbours (#3580). A *redundant* caption may be dropped on a narrow dock where its control already carries the meaning (e.g. the Layers lock icons); an *essential* one elides. This mirrors Photoshop: a fixed layout with elision and tooltips, not per-locale reflow.
- **Icons:** Lucide SVGs in `assets/icons` (ISC licence), embedded via `icon_data.rs`. Regenerate that file when you add icons.
- **Fonts:** Inter (UI) and JetBrains Mono (numbers), both OFL. Named families `medium` and `semibold` are available via `theme::medium()` and `theme::semibold()`.
- **Photoshop layout grammar (Pro):**