Merge pull request #3881 from dotCooCoo/feat/preset-toolbar-leading-tools

Let preset host tools lead the toolbar on opt-in
This commit is contained in:
BFlat authored and GitHub committed 2026-11-27 14:14:33 +07:00
commit c8aa3daad4
3 files changed
+66 -6

No files matched your search

+10
View File
@@ -89,6 +89,7 @@ pub struct SwatchesPanel<'a> {
foreground: Srgba,
thumb: Option<HostThumb<'a>>,
tools: Option<ToolsFn<'a>>,
tools_at_start: bool,
drop_zone: Option<DropZone>,
footer: Option<Option<PresetFooterOptions<'a>>>,
reveal: Option<SwatchKey>,
@@ -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())
@@ -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<BrowserTools<'a>>,
/// 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,
@@ -726,6 +731,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> PresetBrowser<'_,
toolbar,
search,
mut tools,
tools_at_start,
row_menu,
row_items,
drags,
@@ -776,11 +782,8 @@ impl<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> 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);
@@ -790,7 +793,16 @@ impl<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> 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.
@@ -511,3 +511,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::<u32>::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);
}
}