From 5a54b17194895f89714a39675b938f19b03937e8 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 09:40:33 -0800 Subject: [PATCH] Gradient panel: rows wrap so the panel stays inside its dock column Rows of side-by-side controls in the Gradient panel did not wrap, so the panel's column grew wider than the panel and controls ran past its right edge. Docked, the Type buttons, Repeats, the gradient slider and the notice reached up to 10.5 pt past the panel in English and 13.9 pt in French. At the narrowest panel width (220 pt), Method, Spread, Repeats, Location and Delete Stop ran 23.5 to 100.5 pt past it. - The header places the type buttons and Edit Gradient beside the thumbnail, level with its top, or under it when the panel is too narrow. - The Type, Method and Spread rows wrap. - The stop row wraps, and each caption stays with its field. A wide panel draws every control where it did before. --- crates/ui-egui/src/panels/gradient.rs | 102 +++++++------ crates/ui-egui/src/panels/gradient/tests.rs | 153 ++++++++++++++++++++ 2 files changed, 212 insertions(+), 43 deletions(-) diff --git a/crates/ui-egui/src/panels/gradient.rs b/crates/ui-egui/src/panels/gradient.rs index 8f9d83a4b1..0a92cde57f 100644 --- a/crates/ui-egui/src/panels/gradient.rs +++ b/crates/ui-egui/src/panels/gradient.rs @@ -25,7 +25,7 @@ //! gradient behind the active proxy, keeping its placement (`gradient.presets.*`). Hide Presets in //! the panel menu leaves the row out. -use craft::behavior::{EditEvent, IconId, Mixed}; +use craft::behavior::{ColumnWidth, EditEvent, IconId, Mixed}; use craft::color_ui::gradient::stops::AltDrop; use craft::color_ui::ui::DropperButton; use craft::color_ui::ui::gradient::{ColorStop, GradientEditor, GradientStops, StopDrop, StopRef, StopRow, StopRows, StopRules}; @@ -35,7 +35,8 @@ use craft::ui::buttons::{Button, IconButton}; use craft::ui::choice::{Checkbox, Radio, RadioGroup, Segmented, Swatch}; use craft::ui::contract::{Bind, EditResponse, Entry, MemoProps, Props as _, Show as _, ShowContainer as _, Trigger}; use craft::ui::display::{Divider, Icon, Shade, Text}; -use craft::ui::layout_components::{Row, Stack}; +use craft::ui::layout::CellAlign; +use craft::ui::layout_components::{Grid, Row, Stack}; use craft::ui::list::ThumbAspect; use craft::ui::menu::{Menu, MenuItem, PanelRow}; use craft::ui::notices::Notice; @@ -66,6 +67,9 @@ use crate::panel_drag::PanelDrag; /// The Angle field's preset menu, degrees. const ANGLE_PRESETS: [f64; 9] = [-180.0, -135.0, -90.0, -45.0, 0.0, 45.0, 90.0, 135.0, 180.0]; +/// The header's columns: the thumbnail with its swatch menu, and the type buttons with Edit Gradient, each as wide +/// as its content. +const BESIDE: [ColumnWidth; 2] = [ColumnWidth::Content, ColumnWidth::Content]; /// The Aspect Ratio field's preset menu, percent. const ASPECT_PRESETS: [f64; 9] = [10.0, 25.0, 50.0, 75.0, 100.0, 150.0, 200.0, 300.0, 400.0]; // PLAN: the sheet drops `HIDE_OPTIONS` and `HIDE_PRESETS`, but each key is read by `show`, `menu_states` and `run`; kept as the op.A5 hide-store keys. @@ -171,7 +175,7 @@ pub(crate) fn type_row(app: &mut VectorcraftApp, ui: &mut Ui) { (GradientKind::Diamond, "dc-grad-diamond", tl!("Diamond Gradient")), (GradientKind::Freeform, "dc-grad-freeform", tl!("Freeform Gradient")), ]); - Row::new().show(ui, |ui, _| { + Row::new().wrap(true).show(ui, |ui, _| { Text::new(tl!("Type:")).shade(Shade::Muted).show(ui); let pick = Segmented::new(tl!("Type"), lit(kind), &entries).show(ui); if let Some(k) = pick.events.committed() { @@ -302,19 +306,23 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui) { let kind = g.gradient.kind; let hidden = part_hidden(app, HIDE_OPTIONS); // Header: the thumbnail and its swatch menu, the type buttons and Edit Gradient. - Row::new().show(ui, |ui, _| { - fill_box(app, ui, &g, is_grad); - if hidden { + if hidden { + Row::new().show(ui, |ui, _| { + fill_box(app, ui, &g, is_grad); super::proxy(app, ui); - return; - } - Stack::new().show(ui, |ui, _| { - type_row(app, ui); - if Button::new(tl!("Edit Gradient")).show(ui).clicked() { - app.select_tool("gradient"); - } }); - }); + } else { + // The type buttons go beside the thumbnail, level with its top, or under it in a narrow panel. + Grid::new(&BESIDE).cells(CellAlign::Top).show(ui, |ui, _| { + Row::new().show(ui, |ui, _| fill_box(app, ui, &g, is_grad)); + Stack::new().show(ui, |ui, _| { + type_row(app, ui); + if Button::new(tl!("Edit Gradient")).show(ui).clicked() { + app.select_tool("gradient"); + } + }); + }); + } let freeform = is_grad && kind == GradientKind::Freeform; if hidden { if !freeform { @@ -442,7 +450,7 @@ fn method_select(app: &mut VectorcraftApp, ui: &mut Ui, g: &GradientPaint, is_gr ] .map(|(m, label)| Entry::new(m, label)); let current = g.gradient.method; - Row::new().show(ui, |ui, _| { + Row::new().wrap(true).show(ui, |ui, _| { Text::new(tl!("Method:")).shade(Shade::Muted).show(ui); let pick = Select::new(tl!("Method"), Bind::given(current), &methods).enabled(is_grad).show(ui); if let Some(m) = pick.events.committed().filter(|m| **m != current) { @@ -459,7 +467,7 @@ fn spread_select(app: &mut VectorcraftApp, ui: &mut Ui, g: &GradientPaint, is_gr .map(|(s, label)| Entry::new(s, label)); let current = g.gradient.spread; let presets = number_presets(&REPEAT_PRESETS, ""); - Row::new().show(ui, |ui, _| { + Row::new().wrap(true).show(ui, |ui, _| { Text::new(tl!("Spread:")).shade(Shade::Muted).show(ui); let pick = Select::new(tl!("Spread"), Bind::given(current), &spreads).enabled(is_grad).show(ui); if let Some(s) = pick.events.committed().filter(|s| **s != current) { @@ -667,34 +675,42 @@ fn host_fields(app: &mut VectorcraftApp, ui: &mut Ui, g: &Gradient, is_grad: boo } }); } - Row::new().show(ui, |ui, _| { - Text::new(tl!("Opacity:")).shade(Shade::Muted).show(ui); - let shown = stop.map_or(100.0, |s| f64::from(s.opacity) * 100.0); - let opacity = NumberField::new(tl!("Opacity"), Bind::given(Some(shown))).range(0.0..=100.0).unit("%").enabled(sel.is_some()).show(ui); - apply_number(app, &opacity.edit, |app, v, phase| { - let Some(i) = sel else { return }; - let mut v2 = stops.clone(); - if let Some(s) = v2.get_mut(i) { - s.opacity = (v / 100.0).clamp(0.0, 1.0) as f32; - } - set_stops(app, &v2, Some(i), phase); + // In a narrow panel the row wraps; each caption stays on the line of its field. + Row::new().wrap(true).show(ui, |ui, _| { + Row::new().show(ui, |ui, _| { + Text::new(tl!("Opacity:")).shade(Shade::Muted).show(ui); + let shown = stop.map_or(100.0, |s| f64::from(s.opacity) * 100.0); + let opacity = NumberField::new(tl!("Opacity"), Bind::given(Some(shown))).range(0.0..=100.0).unit("%").enabled(sel.is_some()).show(ui); + apply_number(app, &opacity.edit, |app, v, phase| { + let Some(i) = sel else { return }; + let mut v2 = stops.clone(); + if let Some(s) = v2.get_mut(i) { + s.opacity = (v / 100.0).clamp(0.0, 1.0) as f32; + } + set_stops(app, &v2, Some(i), phase); + }); }); - Text::new(tl!("Location:")).shade(Shade::Muted).show(ui); - let loc = match (mid, stop) { - (Some(m), _) => stops.get(m).map_or(0.0, |s| percent(s.midpoint)), - (None, Some(s)) => percent(s.offset), - _ => 0.0, - }; - let location = - NumberField::new(tl!("Location"), Bind::given(Some(loc))).range(0.0..=100.0).unit("%").enabled(sel.is_some() || mid.is_some()).show(ui); - apply_number(app, &location.edit, |app, v, phase| { - let v = (v / 100.0) as f32; - if let Some(m) = mid { - set_stops(app, &set_midpoint(stops, m, v), None, phase); - } else if let Some(i) = sel { - let (v2, ni) = move_stop(stops, i, v); - set_stops(app, &v2, Some(ni), phase); - } + Row::new().show(ui, |ui, _| { + Text::new(tl!("Location:")).shade(Shade::Muted).show(ui); + let loc = match (mid, stop) { + (Some(m), _) => stops.get(m).map_or(0.0, |s| percent(s.midpoint)), + (None, Some(s)) => percent(s.offset), + _ => 0.0, + }; + let location = NumberField::new(tl!("Location"), Bind::given(Some(loc))) + .range(0.0..=100.0) + .unit("%") + .enabled(sel.is_some() || mid.is_some()) + .show(ui); + apply_number(app, &location.edit, |app, v, phase| { + let v = (v / 100.0) as f32; + if let Some(m) = mid { + set_stops(app, &set_midpoint(stops, m, v), None, phase); + } else if let Some(i) = sel { + let (v2, ni) = move_stop(stops, i, v); + set_stops(app, &v2, Some(ni), phase); + } + }); }); let can_del = sel.is_some() && stops.len() > MIN_STOPS; let delete = IconButton::new(IconId::new("trash-2")).label(tl!("Delete Stop")).tooltip(tl!("Delete Stop")).enabled(can_del).show(ui); diff --git a/crates/ui-egui/src/panels/gradient/tests.rs b/crates/ui-egui/src/panels/gradient/tests.rs index 4324e20f49..6d02ba22cd 100644 --- a/crates/ui-egui/src/panels/gradient/tests.rs +++ b/crates/ui-egui/src/panels/gradient/tests.rs @@ -335,3 +335,156 @@ fn escape_ends_the_stop_eyedropper_and_returns_to_the_gradient_tool() { assert_eq!(current(&app), paint, "the stop keeps its color"); assert_eq!(app.session.active().unwrap().selection.objects, selected, "the selection stays"); } + +// ---------- the panel in the app window ---------- + +/// Where the panel is shown in the app window. +#[derive(Clone, Copy, Debug)] +enum Place { + /// Docked in its column. + Docked, + /// Floating, this many points wide. + Floating(f32), +} + +/// The Gradient panel as one frame of the app window drew it. +struct Shown { + /// The panel's body. + body: egui::Rect, + /// The name and rect of every named node in the body. + nodes: Vec<(String, egui::Rect)>, + /// Each catalog string asked for, as the panel's language shows it. + names: Vec<(&'static str, String)>, +} + +impl Shown { + /// The rects of the nodes named `name` (a catalog string asked for). + fn rects(&self, name: &str) -> Vec { + let shown = self.names.iter().find(|(asked, _)| *asked == name).map_or(name, |(_, shown)| shown.as_str()); + let found: Vec = self.nodes.iter().filter(|(label, _)| label == shown).map(|(_, r)| *r).collect(); + assert!(!found.is_empty(), "the panel shows no {shown:?}"); + found + } + + /// The nodes that reach past either side of the body, each with its rect. + fn past(&self, nodes: impl IntoIterator) -> Vec { + let outside = |r: &egui::Rect| r.left() < self.body.left() - 0.5 || r.right() > self.body.right() + 0.5; + nodes.into_iter().filter(|(_, r)| outside(r)).map(|(name, r)| format!("{name} at {r:?}")).collect() + } + + /// The vertical middles of the nodes named `names`. + fn middles(&self, names: &[&str]) -> Vec { + names.iter().flat_map(|name| self.rects(name)).map(|r| r.center().y).collect() + } +} + +/// A selected gradient stop: the panel shows its Color row. +fn select_a_stop(app: &mut VectorcraftApp) { + app.run("paint.setFill", json!({"gradient": {}})).unwrap(); + app.run("gradient.selectStop", json!({"index": 1})).unwrap(); +} + +/// The Gradient panel shown at `place` in the whole app window (1440 by 900 pt) over a selected rectangle that +/// `setup` paints, drawn in `lang`, with the catalog strings `names` as `lang` shows them. +fn shown(lang: &str, place: Place, setup: fn(&mut VectorcraftApp), names: &[&'static str]) -> Shown { + use egui::accesskit::{Node, NodeId, Role}; + let mut app = super::super::tests_appearance::app_with_rect(); + setup(&mut app); + app.session.prefs.interface_language = lang.into(); + app.run("window.panel", json!({ "panel": "gradient" })).unwrap(); + match place { + Place::Docked => app.run("window.panel.dock", json!({ "panel": "gradient" })).unwrap(), + Place::Floating(width) => { + app.run("window.panel.float", json!({ "panel": "gradient", "x": 520, "y": 120, "width": width, "height": 700 })).unwrap() + } + }; + app.run("window.panel.activate", json!({ "panel": "gradient" })).unwrap(); + let ctx = crate::testing::ui_context(); + ctx.enable_accesskit(); + let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1440.0, 900.0)); + let mut nodes: Vec<(NodeId, Node)> = Vec::new(); + for at in 1..=8 { + let input = egui::RawInput { time: Some(f64::from(at) * 0.1), screen_rect: Some(screen), ..Default::default() }; + let out = crate::testing::app_frame(&mut app, &ctx, input); + nodes = out.platform_output.accesskit_update.map_or_else(Vec::new, |update| update.nodes); + } + // The frames set the language from the preference; the names are looked up in it before English is put back. + let names: Vec<(&'static str, String)> = names.iter().map(|name| (*name, crate::i18n::t(name).to_owned())).collect(); + let fill = crate::i18n::t("Gradient Fill").to_owned(); + crate::i18n::set_current(crate::i18n::Lang::EN); + let rect = + |node: &Node| node.bounds().map(|b| egui::Rect::from_min_max(egui::pos2(b.x0 as f32, b.y0 as f32), egui::pos2(b.x1 as f32, b.y1 as f32))); + let by_id: std::collections::HashMap = nodes.iter().map(|(id, n)| (*id, n)).collect(); + let under = |root: NodeId| { + let mut found = Vec::new(); + let mut todo = vec![root]; + while let Some(id) = todo.pop() { + let Some(node) = by_id.get(&id) else { continue }; + if let (Some(label), Some(r)) = (node.label(), rect(node)) { + found.push((label.to_owned(), r)); + } + todo.extend(node.children().iter().copied()); + } + found + }; + // The panel's body: the tab panel that holds the Gradient Fill box. + let (body, nodes) = nodes + .iter() + .filter(|(_, n)| n.role() == Role::TabPanel) + .filter_map(|(id, n)| Some((rect(n)?, under(*id)))) + .find(|(_, named)| named.iter().any(|(l, _)| *l == fill)) + .unwrap_or_else(|| panic!("no Gradient panel at {place:?} in {lang}")); + Shown { body, nodes, names } +} + +/// Docked, every control of the panel stays inside its column, with a solid fill (the panel shows its notice) and +/// with a stop selected (its Color row), in languages with longer words. +#[test] +fn the_gradient_panel_stays_inside_its_dock_column() { + for lang in ["en", "fr", "de"] { + for setup in [|_: &mut VectorcraftApp| {}, select_a_stop] { + let panel = shown(lang, Place::Docked, setup, &[]); + let past = panel.past(panel.nodes.clone()); + assert!(past.is_empty(), "past the docked panel in {lang}: {past:?}"); + } + } +} + +/// In a panel at the narrowest panel width the Type, Method, Spread and stop rows wrap: their controls stay inside +/// the panel. +#[test] +fn the_gradient_rows_wrap_in_a_narrow_panel() { + let names = [ + "Linear Gradient", + "Radial Gradient", + "Conic Gradient", + "Diamond Gradient", + "Edit Gradient", + "Method", + "Spread", + "Repeats", + "Opacity", + "Location", + "Delete Stop", + ]; + for lang in ["en", "fr"] { + let panel = shown(lang, Place::Floating(crate::panel_docking::PANEL_MIN_WIDTH), select_a_stop, &names); + let past = panel.past(names.iter().flat_map(|name| panel.rects(name).into_iter().map(|r| (name.to_string(), r)))); + assert!(past.is_empty(), "past the panel in {lang}: {past:?}"); + } +} + +/// In a wide panel the rows stay on one line each, and the thumbnail stays level with the top of the type buttons. +#[test] +fn the_gradient_rows_stay_on_one_line_in_a_wide_panel() { + let rows: [&[&str]; 3] = + [&["Method:", "Method"], &["Spread:", "Spread", "Repeats"], &["Opacity:", "Opacity", "Location:", "Location", "Delete Stop"]]; + let names: Vec<&'static str> = rows.iter().flat_map(|row| row.iter().copied()).chain(["Gradient Fill", "Linear Gradient"]).collect(); + let panel = shown("en", Place::Floating(400.0), select_a_stop, &names); + for row in rows { + let middles = panel.middles(row); + assert!(middles.windows(2).all(|m| (m[0] - m[1]).abs() < 1.0), "{row:?} is not on one line: {middles:?}"); + } + let (thumb, linear) = (panel.rects("Gradient Fill")[0], panel.rects("Linear Gradient")[0]); + assert!((thumb.top() - linear.top()).abs() < 1.0, "the thumbnail {thumb:?} is not level with the type buttons {linear:?}"); +}