PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(ui): prevent Layers opacity controls from overflowing narrow docks (#2325)
Nigel Ihza Firdausy817678d6a0ae
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex 13881c5bd..d06b08727 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -1804,6 +1804,18 @@ fn layer_drag_edge_scroll(pointer: Option<Pos2>, viewport: Rect, dragging: bool,direction.signum() * velocity * dt.clamp(0.0, 0.05)}+/// Fit blend mode + Opacity into the available Layers-panel width (#2309).+/// Preserve the normal translated label when there is room; on a narrow dock,+/// keep the field interactive and give the dropdown the remaining width.+fn opacity_row_layout(available: f32, label_width: f32, gap: f32) -> (f32, bool) {+ let full_right = label_width + LAYER_PCT_W + 2.0 * gap + 16.0;+ // The label shows while the blend dropdown keeps a usable width (at the default dock width+ // with an English label it does; a long translated label on a narrow dock gives way).+ let show_label = available >= full_right + 80.0;+ let reserved = LAYER_PCT_W + gap + if show_label { label_width + gap + 16.0 } else { 0.0 };+ ((available - reserved).max(40.0), show_label)+}+fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {// A new active layer opens its parent groups and is scrolled into view (#152).let reveal = crate::layer_reveal::track(app, ui.ctx());@@ -1853,12 +1865,13 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {ui.horizontal(|ui| {ui.add_enabled_ui(!bg, |ui| {let mut m = l.blend;- // Leave room for the Opacity label and field: a translated label ("Непрозрачность:")- // can be much wider than the English one, and must not slide under the dropdown.+ // Reserve space for the numeric field first. On a narrow dock, hide the+ // redundant visible Opacity label rather than force the dropdown beyond+ // the panel's right edge (#2309). The numeric field keeps its name.let opacity_label = if t.pro { tl!("Opacity:") } else { tl!("Opacity") };- let right = (body_text_width(ui, opacity_label) + LAYER_PCT_W + 2.0 * ui.spacing().item_spacing.x + 16.0).max(150.0);- let w = ui.available_width() - right;- let (chosen, hovered) = widgets::dropdown_wheel_hovered(ui, "blend", &mut m, &blend_options(l.is_group(), l.blend), w.max(100.0));+ let (blend_width, show_opacity_label) =+ opacity_row_layout(ui.available_width(), body_text_width(ui, opacity_label), ui.spacing().item_spacing.x);+ let (chosen, hovered) = widgets::dropdown_wheel_hovered(ui, "blend", &mut m, &blend_options(l.is_group(), l.blend), blend_width);// One step per choice: a click, an arrow key or each wheel notch (#1747).for m in &chosen {actions.push(("layer.setProps".into(), json!({"layer": l.id.0, "blend": m.label()})));@@ -1868,7 +1881,8 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |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);- let scrub = scrub_pct_label(ui, opacity_label, &mut o);+ // 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() };if field.changed || scrub.changed {actions.push(pct_action(l, "opacity", o, scrub.drag.or(field.drag)));}@@ -4385,6 +4399,29 @@ mod group_drag_selection_tests {}}+#[cfg(test)]+mod opacity_row_layout_tests {+ use super::*;++ #[test]+ fn opacity_label_yields_to_controls_in_narrow_layers_panel() {+ let gap = 2.0;+ let label = 62.0;+ let (wide, visible) = opacity_row_layout(360.0, label, gap);+ assert!(visible);+ assert!(wide >= 100.0);+ // The default dock width keeps the English label (the Layers dock at its 250 px minimum).+ assert!(opacity_row_layout(255.0, 46.0, 8.0).1);+ let (medium, visible) = opacity_row_layout(180.0, label, gap);+ assert!(!visible, "the long label must not overlap the blend field");+ assert!(medium + LAYER_PCT_W + gap <= 180.0);+ let (narrow, visible) = opacity_row_layout(135.0, label, gap);+ assert!(!visible);+ assert!(narrow >= 40.0);+ assert!(narrow + LAYER_PCT_W + gap <= 135.0);+ }+}+#[cfg(test)]#[path = "mask_props_ui_tests.rs"]mod mask_properties_tests;