From 0fd61ad96865267557e7a557e80e11846855b502 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 21:48:19 -0800 Subject: [PATCH] Let preset host tools lead the toolbar on opt-in --- crates/color-ui/src/ui/swatches/panel.rs | 10 +++++ .../src/patterns/presets/browser.rs | 24 +++++++++--- .../ui-patterns/tests/it/presets/browser.rs | 38 +++++++++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/crates/color-ui/src/ui/swatches/panel.rs b/crates/color-ui/src/ui/swatches/panel.rs index 18c05a3691..85f60fc84b 100644 --- a/crates/color-ui/src/ui/swatches/panel.rs +++ b/crates/color-ui/src/ui/swatches/panel.rs @@ -89,6 +89,7 @@ pub struct SwatchesPanel<'a> { foreground: Srgba, thumb: Option>, tools: Option>, + tools_at_start: bool, drop_zone: Option, footer: Option>>, reveal: Option, @@ -120,6 +121,7 @@ impl<'a> SwatchesPanel<'a> { foreground, thumb: None, tools: None, + tools_at_start: false, drop_zone: None, footer: None, reveal: None, @@ -154,6 +156,12 @@ impl<'a> SwatchesPanel<'a> { self } + /// Places the host's tools before the browser controls in the toolbar. Off by default. + pub fn tools_at_start(mut self, at_start: bool) -> Self { + self.tools_at_start = at_start; + self + } + /// Where a drop onto the panel lands. pub fn drop_zone(mut self, zone: DropZone) -> Self { self.drop_zone = Some(zone); @@ -237,6 +245,7 @@ impl<'a> SwatchesPanel<'a> { foreground, thumb, mut tools, + tools_at_start, drop_zone, footer, reveal, @@ -278,6 +287,7 @@ impl<'a> SwatchesPanel<'a> { let mut browser = PresetBrowser::new(words.title.clone(), Bind::bound(&mut st.browser), &source) .thumb_range(SWATCH_ZOOM) .toolbar(true) + .tools_at_start(tools_at_start) .search(search) .select(select) .reorder(PresetReorder::items_and_groups()) diff --git a/crates/ui-patterns/src/patterns/presets/browser.rs b/crates/ui-patterns/src/patterns/presets/browser.rs index 06ff89253e..cc74027b23 100644 --- a/crates/ui-patterns/src/patterns/presets/browser.rs +++ b/crates/ui-patterns/src/patterns/presets/browser.rs @@ -324,6 +324,10 @@ pub struct PresetBrowser<'a, K: Clone + Eq + Hash + Send + Sync + MemoProps + 's /// The host's controls at the reading end of the toolbar. #[prop(func = "FnMut(&mut Ui)")] tools: Option>, + /// Places the host's tools at the reading start, with the search and view controls at the end. + /// Off by default, preserving the existing toolbar order. + #[prop] + tools_at_start: bool, /// What a secondary click or the context-menu key opens: the standard row menu, the host's `row_items` alone, or /// nothing. #[prop] @@ -398,6 +402,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> PresetBrowser toolbar: false, search: true, tools: None, + tools_at_start: false, row_menu: PresetRowMenu::Standard, row_items: None, drags: false, @@ -725,6 +730,7 @@ impl PresetBrowser<'_, toolbar, search, mut tools, + tools_at_start, row_menu, row_items, drags, @@ -775,11 +781,8 @@ impl PresetBrowser<'_, Entry::new(PresetView::List, words.list_view.clone()).icon(IconId::new("list")), ]; let locked = view_lock.is_some(); - let mut bar = Bar::new(Label::Empty, BarKind::Tool); - if let Some(tools) = tools.as_mut() { - bar = bar.trailing(|ui, _| tools.show(ui)); - } - let _ = bar.show(ui, |ui, _| { + let bar = Bar::new(Label::Empty, BarKind::Tool); + let mut controls = |ui: &mut Ui| { if search { let field = TextField::search(words.search.clone(), Bind::bound(&mut local.filter)).placeholder(words.search.clone()); let field = field.drives(list_id); @@ -789,7 +792,16 @@ impl PresetBrowser<'_, if !locked { Segmented::new(words.view.clone(), Bind::bound(&mut local.view), &modes).show(ui); } - }); + }; + if let Some(tools) = tools.as_mut() { + if tools_at_start { + let _ = bar.trailing(|ui, _| controls(ui)).show(ui, |ui, _| tools.show(ui)); + } else { + let _ = bar.trailing(|ui, _| tools.show(ui)).show(ui, |ui, _| controls(ui)); + } + } else { + let _ = bar.show(ui, |ui, _| controls(ui)); + } } // A hidden search field filters nothing: a search typed before hiding it would otherwise keep presets // out of the list with no field to show why. diff --git a/crates/ui-patterns/tests/it/presets/browser.rs b/crates/ui-patterns/tests/it/presets/browser.rs index 889de8713c..4eb45471a9 100644 --- a/crates/ui-patterns/tests/it/presets/browser.rs +++ b/crates/ui-patterns/tests/it/presets/browser.rs @@ -476,3 +476,41 @@ fn host_row_menu_rows_are_menu_items_and_only_a_checked_row_a_check_box() { assert_eq!(names(Role::MENU_ITEM), ["Options…"], "a plain host row is a menu item"); assert_eq!(names(Role::MENU_ITEM_CHECK_BOX), ["Locked"], "a checked host row is a check box"); } + +/// Host tools can share the header's reading start without changing the default order or view actions. +#[trial] +fn host_toolbar_tools_can_lead_the_view_controls_without_changing_the_default() { + use craft_ui::buttons::Button; + use craft_ui::contract::Show as _; + + for at_start in [false, true] { + let state = Rc::new(RefCell::new(PresetState::::new())); + let shown = Rc::clone(&state); + let rows = PresetSource::new((), PresetFns::new(|_, _| vec![PresetRow::item(ALPHA, "Alpha", 0)])); + let mut fixture = Fixture::new(move |ui| { + ui.scope_builder(UiBuilder::new().max_rect(BROWSER), |ui| { + let _ = PresetBrowser::new("Swatches", Bind::bound(&mut *shown.borrow_mut()), &rows) + .toolbar(true) + .search(false) + .tools_at_start(at_start) + .tools(|ui: &mut egui::Ui| { + Button::new("Host Tool").show(ui); + }) + .show(ui); + }); + }); + fixture.room(vec2(480.0, 320.0)).settle(); + let host = fixture.by_label("Host Tool").expect("host control").center(); + let grid = fixture.by_label("Grid View").expect("grid action").center(); + let list = fixture.by_label("List View").expect("list action").center(); + assert!((host.y - grid.y).abs() < 1.0, "both groups share the toolbar row"); + assert_eq!(host.x < grid.x, at_start, "the default stays trailing; the opt-in leads"); + assert!(grid.x < list.x, "the view choices retain their order"); + let target = fixture.by_label("List View").expect("list action"); + fixture.click(&target).settle(); + assert_eq!(state.borrow().view, PresetView::List); + let target = fixture.by_label("Grid View").expect("grid action"); + fixture.click(&target).settle(); + assert_eq!(state.borrow().view, PresetView::Grid); + } +}