ToolbarDrawer: rows pick without selecting; a click or Enter selects the tool

This commit is contained in:
aiongg
2026-11-12 14:32:42 +00:00
parent 6261e56681
commit d51bbc0738
2 changed files with 36 additions and 19 deletions
+11 -19
View File
@@ -6,8 +6,8 @@ use super::words::RailWords;
use crate::buttons::{Button, IconButton};
use crate::contract::overlay::{Anchor, Dismiss, Overlay, Place};
use crate::contract::{
Bind, CollectionEvent, Gap, ListDrag, NoValue, Outcome, Props as _, SelectMode, Show as _, ShowCollection as _, ShowContainer as _, Sizing,
clicked,
ActivateOn, Bind, CollectionEvent, Gap, ListDrag, NoValue, Outcome, Props as _, SelectMode, Show as _, ShowCollection as _, ShowContainer as _,
Sizing, clicked,
};
use crate::display::{Shade, Text};
use crate::layout_components::{Row, Stack};
@@ -20,7 +20,7 @@ use crate::runtime::windows::os_of;
use crate::sections::DropAction;
use crate::style::TypeRole;
use craft_behavior::overlay::{Align, Side};
use craft_behavior::{DisplayOrder, IconId, ItemSource, SelectionChange, Value};
use craft_behavior::{DisplayOrder, IconId, ItemSource, Value};
use egui::{Rect, Response, Ui};
use std::borrow::Cow;
@@ -117,8 +117,8 @@ impl<T: Value> Outcome for ToolbarDrawerOutput<T> {
/// tool that is on the toolbar is dimmed. The drawer is as tall as its rows, up to the share of the window a dialog
/// takes, and the list scrolls inside that height.
///
/// - **Rows.** A selected row reports its tool as `select`. Enter or a double click on a tool that is not on the
/// toolbar adds it as a slot of its own at the end; on a tool that is on the toolbar it takes it off.
/// - **Rows.** A click on a row, or Enter on the lead row, reports its tool as `select`. The rows pick without
/// selecting: no row keeps a selection fill.
/// - **Drags.** A tool dragged from the rail and dropped on the drawer is taken off the toolbar. A drop on the rail
/// comes back from the rail as a [`ToolDropped`]; hand it to [`ToolbarDrawer::new`] and the drawer turns it into an
/// edit.
@@ -265,7 +265,8 @@ impl<T: Value + 'static> ToolbarDrawer<'_, T> {
.show(ui, |ui, _| {
let list = List::<DrawerKey>::new(words.drawer_tools.clone())
.id(ui.scope_id().with("drawer-tools"))
.select(SelectMode::Single)
.select(SelectMode::None)
.activate(ActivateOn::Click)
.can_drag(|key| match *key {
DrawerKey::Tool { group, tool } => !on_toolbar(toolbar, &groups[group].tools[tool].id),
DrawerKey::Group(_) => false,
@@ -297,19 +298,10 @@ impl<T: Value + 'static> ToolbarDrawer<'_, T> {
DrawerKey::Group(_) => None,
};
for event in out.events {
match event {
CollectionEvent::Selection(SelectionChange::Set(keys)) => {
select = keys.last().and_then(tool_of).map(|tool| tool.id.clone());
}
CollectionEvent::Activate(key) => {
let Some(tool) = tool_of(&key) else { continue };
edits.push(if on_toolbar(toolbar, &tool.id) {
ToolbarEdit::Remove { tool: tool.id.clone() }
} else {
ToolbarEdit::Add { tool: tool.clone(), at: DropAt::new_slot(toolbar.len()) }
});
}
_ => {}
if let CollectionEvent::Activate(key) = event
&& let Some(tool) = tool_of(&key)
{
select = Some(tool.id.clone());
}
}
});
+25
View File
@@ -88,3 +88,28 @@ fn only_a_tool_off_the_toolbar_drags() {
assert!(!drags(&mut fixture, "Brush"), "Brush is on the toolbar");
assert!(drags(&mut fixture, "Eraser"), "Eraser is not");
}
/// A click on a tool reports it as the one to select and leaves no row selected: the drawer's rows pick, they do not
/// hold a selection.
#[trial]
fn a_click_on_a_tool_selects_it_and_no_row_stays_selected() {
use craft_ui::rails::{DrawerGroup, ToolbarDrawer};
use std::cell::Cell;
use std::rc::Rc;
let picked = Rc::new(Cell::new(None::<u8>));
let seen = picked.clone();
let mut fixture = Fixture::new(move |ui| {
let groups = [DrawerGroup::new("Edit", vec![tool(UNDO), tool(CUT), tool(4)])];
let toolbar = [slot(0, &[UNDO])];
let out = ToolbarDrawer::new(ui.max_rect(), &groups, &toolbar, None).show(ui);
if let Some(id) = out.select {
seen.set(Some(id));
}
});
fixture.settle();
let row = fixture.by_label("Cut").expect("the Cut row");
fixture.click_at(row.center()).settle();
assert_eq!(picked.get(), Some(CUT), "the click selects the tool");
let selected = fixture.tree().selected_of(row.node).unwrap_or(false);
assert!(!selected, "the row keeps no selection");
}