From 08ada730d33ad253e3bfdce1689f4833cf96da34 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 17:11:22 +0700 Subject: [PATCH 1/5] ui-panels: an edited tool rail takes the drops off its buttons: beside the nearest button, and on a blank toolbar as its last slot --- crates/ui-panels/src/rails/tool.rs | 68 +++++++++++++++++------ crates/ui-panels/src/rails/tool/drag.rs | 39 +++++++++++++ crates/ui-panels/src/rails/tool/trials.rs | 26 ++++++++- 3 files changed, 115 insertions(+), 18 deletions(-) diff --git a/crates/ui-panels/src/rails/tool.rs b/crates/ui-panels/src/rails/tool.rs index f9e092b9f..c5051bfcc 100644 --- a/crates/ui-panels/src/rails/tool.rs +++ b/crates/ui-panels/src/rails/tool.rs @@ -5,8 +5,8 @@ use super::flyout::{FlyoutButton, SlotEdit}; use super::words::RailWords; use crate::buttons::IconButton; use crate::contract::{ - Access, Common, Component, Container, ContainerProps, Cx, FooterRegion, Gap, NoValue, Outcome, Pad, Props as _, Role, Scroll, Show as _, - ShowContainer as _, Sizing, WordSetCx as _, + Access, Common, Component, Container, ContainerProps, Cx, FooterRegion, Gap, NoValue, Outcome, Pad, Pointer as _, Props as _, Role, Scroll, + Show as _, ShowContainer as _, Sizing, WordSetCx as _, }; use crate::contract::{BodyCx, BodyLayout, MemoProps, MemoView, Outbox, PlainData, ThemeScope, Tracked}; use crate::display::{Elide, Shade, TargetMark, Text}; @@ -21,7 +21,9 @@ use crate::tabs::TornOff; use craft_behavior::overlay::Side; use craft_behavior::{IconId, Metrics, NavShape, Proposal, Roving, Sense, Value}; use craft_theme::ControlSize; -use egui::{Id, Response, Ui, Vec2}; +use egui::{Id, Rect, Response, Ui, Vec2}; +use std::cell::{Cell, RefCell}; +use std::rc::Rc; mod data; mod drag; @@ -32,6 +34,7 @@ pub(crate) mod trials; use data::row_groups; pub use data::{LastUsed, RailTool, SlotId, ToolSlot, ToolTips, remembered, shown_tool, visible_slots}; +use drag::RailDrop; pub(crate) use drag::SlotDrop; pub use drag::{DropAt, ToolDrag, ToolDropped, drop_in_slot}; @@ -67,7 +70,7 @@ pub struct ToolRailOutput { pub edit_toolbar: Option, /// The slot whose flyout is open. pub flyout: Option, - /// A tool dropped on a slot while the toolbar is edited, and where it goes. + /// A tool dropped on the rail while the toolbar is edited, and where it goes. pub dropped: Option, /// A group torn off the rail: `keys` and `grabbed` the slot, `pointer` where it tore off. pub torn_off: Option>, @@ -106,7 +109,9 @@ type FooterFn<'a> = Box; /// an overlay bar in the end padding that shows only under the pointer (`Scroll::overlay`), and its /// buttons show the rail's glyphs. The rail is exactly as tall as its rows and footer at the width it gets /// ([`measure_rail`](ToolRail::measure_rail)). The buttons are one roving group: arrow keys move between them by the -/// grid. While the toolbar is edited the buttons are dragged and take drops, and report the drop in the output. +/// grid. While the toolbar is edited the buttons are dragged and the rail takes drops, and reports the drop in the +/// output: on a button by where over it the drop falls ([`drop_in_slot`]), elsewhere on the rail as a slot of its own +/// beside the nearest button, and on a rail with no buttons as the last slot. /// /// With `captions` the caption of the first slot of a row group that has one ([`ToolSlot::captioned`]) stands above /// the group in the caption type, cut at the rail's width; without it a caption takes no room. The grid's width @@ -153,6 +158,8 @@ pub struct ToolRail<'a, T: Value + MemoProps + PlainData + 'static> { footer: Option>, /// The columns the rail lays out in this pass. columns: usize, + /// The host's index and rect of each button the grid showed in this pass. + shown: Rc>>, } impl<'a, T: Value + MemoProps + PlainData + 'static> ToolRail<'a, T> { @@ -185,6 +192,7 @@ impl<'a, T: Value + MemoProps + PlainData + 'static> ToolRail<'a, T> { tips: None, footer: None, columns: 1, + shown: Rc::default(), } .label(label) } @@ -289,8 +297,8 @@ struct Draw<'a, T: PlainData> { impl Draw<'_, T> { /// Shows the grid as a memoized view; what the buttons report goes into `events` (nothing on a replay). Returns the - /// Edit Toolbar button's id while the rail shows it. - fn run(&self, ui: &mut Ui, events: &mut Vec>) -> Option { + /// Edit Toolbar button's id while the rail shows it, and the host's index and rect of each slot button. + fn run(&self, ui: &mut Ui, events: &mut Vec>) -> (Option, Vec<(usize, Rect)>) { let tips = self.tips.map(HostTips); let props = GridProps { slots: self.slots, @@ -308,13 +316,14 @@ impl Draw<'_, T> { reveal: self.reveal, sent: Outbox::new(events), }; - MemoView::new("slots", props, grid::).show(ui).flatten() + MemoView::new("slots", props, grid::).show(ui).unwrap_or_default() } } /// The grid of buttons in rows of `columns`, each row group under its caption while the rail shows captions. The result -/// is the Edit Toolbar button's id, which the rail's output turns into its response. -fn grid(props: &mut GridProps<'_, T>, ui: &mut Ui) -> Option { +/// is the Edit Toolbar button's id, which the rail's output turns into its response, and the host's index and rect of +/// each slot button, around which the rail takes drops. +fn grid(props: &mut GridProps<'_, T>, ui: &mut Ui) -> (Option, Vec<(usize, Rect)>) { let GridProps { slots, current, last_used, hidden, editing, columns, opens, captions, tear_off, tips, mark, reveal, .. } = *props; let tips = tips.map(|tips| tips.0); let visible = visible_slots(slots, hidden_in(hidden)); @@ -328,7 +337,7 @@ fn grid(props: &mut GridProps<'_, T>, ui: &mut let end = group.last().map_or(first, |last| (last + columns).min(cells)); visible.get(first..end.min(visible.len()))?.iter().find_map(|(index, _)| slots[*index].caption.clone()) }; - let mut edit_toolbar = None; + let (mut edit_toolbar, mut shown_at) = (None, Vec::with_capacity(visible.len())); let sent = &mut props.sent; // The grid of buttons sits at the centre of the rail's width. Row::new().fill_width().align(RowAlign::Center).show(ui, |ui, _| { @@ -374,6 +383,7 @@ fn grid(props: &mut GridProps<'_, T>, ui: &mut Some(tips) => button.show_with_tip(ui, |ui| tips.show(&shown.id, ui)), None => button.show(ui), }; + shown_at.push((*index, out.response.rect)); // The slot wears the mark; while its flyout is open the tool's row does. if marked.is_some() && !out.opened { TargetMark::new(out.response.rect).id_salt(("mark", slot.id)).show_mark(ui); @@ -408,7 +418,7 @@ fn grid(props: &mut GridProps<'_, T>, ui: &mut } }); }); - edit_toolbar + (edit_toolbar, shown_at) } impl<'a, T: Value + MemoProps + PlainData + 'static> ToolRail<'a, T> { @@ -435,8 +445,12 @@ impl<'a, T: Value + MemoProps + PlainData + 'static> ToolRail<'a, T> { remembered(draw.slots, draw.current, draw.last_used) }); let last = last.as_ref().clone(); - let (mut events, mut edit_toolbar) = (Vec::new(), None); - let shown = self.show(ui, |ui, _| edit_toolbar = draw.run(ui, &mut events)); + let (mut events, mut edit_toolbar, laid) = (Vec::new(), None, Rc::clone(&self.shown)); + let shown = self.show(ui, |ui, _| { + let (edit, slots) = draw.run(ui, &mut events); + edit_toolbar = edit; + *laid.borrow_mut() = slots; + }); let (mut select, mut double_clicked, mut flyout, mut dropped, mut torn_off, mut ran) = (None, None, None, None, None, None); for event in events { match event { @@ -454,7 +468,7 @@ impl<'a, T: Value + MemoProps + PlainData + 'static> ToolRail<'a, T> { // A replayed grid sends nothing: its button is shown again, and its response is read back. edit_toolbar: ran.or_else(|| edit_toolbar.and_then(|id| crate::contract::response_of(ui, id))), flyout, - dropped, + dropped: dropped.or(shown.output.dropped), torn_off, last_used: last, ..shown.output @@ -494,20 +508,40 @@ impl Component for ToolRail<'_, T> { } } - fn finish(self, cx: Cx<'_, Self>) -> ToolRailOutput { + /// While the toolbar is edited the rail takes the drops its buttons do not: a blank toolbar's, and those in the room + /// around the buttons. It registers after them, and declines over them. + fn finish(self, mut cx: Cx<'_, Self>) -> ToolRailOutput { + let dropped = self.editing.then(|| self.rail_drop(cx.rect, cx.flow.is_rtl())).and_then(|target| cx.drop_target::(&target)); ToolRailOutput { response: cx.response.clone(), select: None, double_clicked: None, edit_toolbar: None, flyout: None, - dropped: None, + dropped, torn_off: None, last_used: None, } } } +impl ToolRail<'_, T> { + /// The rail `rect` as the target of the drops around the buttons the grid showed in this pass. + fn rail_drop(&self, rect: Rect, rtl: bool) -> RailDrop { + let vertical = self.columns == 1; + let slots = self + .shown + .take() + .into_iter() + .map(|(index, rect)| { + let slot = &self.slots[index]; + SlotDrop { rect, slot: slot.id, index, tools: slot.tools.len(), vertical, rtl } + }) + .collect(); + RailDrop { rect, slots, count: self.slots.len(), beside: Cell::new(None) } + } +} + impl Container for ToolRail<'_, T> { /// The host's footer. fn footer(&mut self, ui: &mut Ui, _: &mut BodyCx<'_>) { diff --git a/crates/ui-panels/src/rails/tool/drag.rs b/crates/ui-panels/src/rails/tool/drag.rs index a46767af3..06c74cfe8 100644 --- a/crates/ui-panels/src/rails/tool/drag.rs +++ b/crates/ui-panels/src/rails/tool/drag.rs @@ -4,6 +4,7 @@ use super::data::SlotId; use crate::contract::{DropMark, DropTarget}; use craft_behavior::DropOperation; use egui::{Pos2, Rect}; +use std::cell::Cell; /// The payload of the drag session between the Edit Toolbar drawer and the rail. Positions, not tools: each side /// resolves them against the slots it was given. @@ -107,6 +108,7 @@ pub fn drop_in_slot(fraction: f32, slot: SlotId, index: usize, tools: usize) -> } /// A slot's button as a drop target: the drop's place comes from where over the button it falls. +#[derive(Clone, Copy)] pub(crate) struct SlotDrop { pub rect: Rect, pub slot: SlotId, @@ -143,3 +145,40 @@ impl DropTarget for SlotDrop { } } } + +/// The rail around its slot buttons as a drop target, for a blank toolbar and the room after the last slot. A drop off +/// the buttons makes a slot of its own beside the nearest button: before it when above it, or level with it and on its +/// leading half; else after it. On a rail with no buttons it goes after the host's `count` slots. +pub(crate) struct RailDrop { + pub rect: Rect, + pub slots: Vec, + pub count: usize, + /// The button the last accepted drop goes beside, whose edge the mark lines. + pub beside: Cell>, +} + +impl DropTarget for RailDrop { + type Drop = ToolDropped; + + /// Over a button the button's own target takes the drop. + fn accept(&self, payload: &ToolDrag, at: Pos2, _: DropOperation) -> Option { + if self.slots.iter().any(|slot| slot.rect.contains(at)) { + return None; + } + let nearest = self.slots.iter().min_by(|a, b| a.rect.distance_sq_to_pos(at).total_cmp(&b.rect.distance_sq_to_pos(at))).copied(); + self.beside.set(nearest); + let before = match nearest { + Some(slot) => { + let leading = if slot.rtl { at.x > slot.rect.center().x } else { at.x < slot.rect.center().x }; + let before = at.y < slot.rect.top() || (at.y <= slot.rect.bottom() && leading); + slot.index + usize::from(!before) + } + None => self.count, + }; + Some(ToolDropped { drag: *payload, at: DropAt::new_slot(before) }) + } + + fn mark(&self, drop: &ToolDropped) -> DropMark { + self.beside.get().map_or(DropMark::Into(self.rect), |slot| slot.mark(drop)) + } +} diff --git a/crates/ui-panels/src/rails/tool/trials.rs b/crates/ui-panels/src/rails/tool/trials.rs index 3a973eccb..5669e5964 100644 --- a/crates/ui-panels/src/rails/tool/trials.rs +++ b/crates/ui-panels/src/rails/tool/trials.rs @@ -4,7 +4,7 @@ use super::super::FlyoutButton; use super::stories::tools; -use super::{LastUsed, ToolRail, ToolSlot}; +use super::{DropAt, LastUsed, ToolRail, ToolSlot}; use crate::buttons::Button; use crate::contract::Props as _; use crate::contract::Show as _; @@ -297,6 +297,30 @@ fn a_click_on_a_slot_selects_its_tool_once() { assert_eq!(seen.borrow().selected, [1], "the click selected the brush once"); } +/// While the toolbar is edited a drop off the buttons lands on the rail: on a blank toolbar as its first slot, and under +/// the last button after the last slot. +#[trial] +fn an_edited_rail_takes_a_drop_off_its_buttons_beside_the_nearest_one() { + let dropped = Rc::new(RefCell::new(Vec::new())); + let seen = Rc::clone(&dropped); + let (slots, blank, last) = (Tracked::new(slots()), Tracked::new(Vec::new()), Tracked::new(LastUsed::default())); + let mut fixture = Fixture::new(move |ui| { + Stack::new().show(ui, |ui, _| { + for (label, slots, edit) in [("Tools", &slots, "Edit Tools"), ("Blank", &blank, "Edit Blank")] { + let out = ToolRail::new(label, slots, &0, &last).edit_toolbar(edit).editing().show_column(ui, |_, _| {}); + seen.borrow_mut().extend(out.dropped.map(|drop| (label, drop.at))); + } + }); + }); + fixture.room(Vec2::new(200.0, 800.0)).settle(); + let pencil = rect(&fixture, "Pencil").center(); + fixture.drag_at(pencil, rect(&fixture, "Blank").center() - pencil).settle(); + assert_eq!(dropped.borrow().as_slice(), [("Blank", DropAt::new_slot(0))], "a drop on a blank toolbar makes its first slot"); + dropped.borrow_mut().clear(); + fixture.drag_at(pencil, rect(&fixture, "Edit Tools").center() - pencil).settle(); + assert_eq!(dropped.borrow().as_slice(), [("Tools", DropAt::new_slot(LABELS.len()))], "under the last button it goes after the last slot"); +} + fn near(a: f32, b: f32) -> bool { (a - b).abs() < 1.0 } From 879b8f82a80b2e2f4c144416e910c3669eefb413 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 17:11:22 +0700 Subject: [PATCH 2/5] ui-canvas: Fit Artboard on Screen leaves 30 pt around the board on every side --- crates/ui-canvas/src/canvas/tools.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/ui-canvas/src/canvas/tools.rs b/crates/ui-canvas/src/canvas/tools.rs index 158f9817a..eea676962 100644 --- a/crates/ui-canvas/src/canvas/tools.rs +++ b/crates/ui-canvas/src/canvas/tools.rs @@ -23,9 +23,8 @@ use egui::Pos2; /// The rotation step of the Rotate View commands, in degrees. const ROTATE_STEP_DEG: f64 = 15.0; -/// The room Fit Artboard on Screen leaves around the board, in points: at the sides, and above and below (the board's -/// name sits above it). -const ARTBOARD_MARGIN: Vec2 = Vec2::new(30.0, 40.0); +/// The room Fit Artboard on Screen leaves around the board on each side, in points. +const ARTBOARD_MARGIN: Vec2 = Vec2::new(30.0, 30.0); /// The room Fit Layers on Screen leaves around the layers' bounds, in points. const LAYERS_MARGIN: Vec2 = Vec2::new(20.0, 20.0); From 80e46cf00e9a7bb53276cd7a0c2df3be5ae538c9 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 17:11:23 +0700 Subject: [PATCH 3/5] ui-widgets: IconButton::choice reads as a combo box named by its label, valued by the picked row, expanded while the container hung from it is open (contract::overlay::opened_from) --- crates/ui-contract/src/contract/overlay.rs | 2 +- .../src/contract/overlay/container_open.rs | 7 ++-- crates/ui-widgets/src/buttons/icon_button.rs | 34 ++++++++++++++++--- crates/ui-widgets/src/buttons/stories.rs | 28 +++++++++++++-- crates/ui-widgets/tests/it/buttons.rs | 27 ++++++++++++++- 5 files changed, 86 insertions(+), 12 deletions(-) diff --git a/crates/ui-contract/src/contract/overlay.rs b/crates/ui-contract/src/contract/overlay.rs index ae2654a2a..5974d4d2c 100644 --- a/crates/ui-contract/src/contract/overlay.rs +++ b/crates/ui-contract/src/contract/overlay.rs @@ -16,7 +16,7 @@ mod props; mod toggles; mod ui; -pub use container_open::Popup; +pub use container_open::{Popup, opened_from}; pub(crate) use container_open::{opener_open, set_opener_open}; pub use host::paint_frame_layers; pub(crate) use host::{Layer, forget_layer, host, host_window}; diff --git a/crates/ui-contract/src/contract/overlay/container_open.rs b/crates/ui-contract/src/contract/overlay/container_open.rs index c5223b1a9..232ac6d6e 100644 --- a/crates/ui-contract/src/contract/overlay/container_open.rs +++ b/crates/ui-contract/src/contract/overlay/container_open.rs @@ -61,8 +61,11 @@ pub(crate) fn opener_open(stage: &mut MemoryStage, first: &Opener) -> bool { open } -/// Whether the container hung from the opener `opener` (the id of its response) is open, as its slot holds it. -pub(crate) fn opened_from(ctx: &egui::Context, opener: Id) -> bool { +/// Whether the container hung from the opener `opener` (the id of its response, [`Overlay::opened_by`]) is open, as +/// its slot holds it: an opener reports its expanded state by it. +/// +/// [`Overlay::opened_by`]: super::Overlay::opened_by +pub fn opened_from(ctx: &egui::Context, opener: Id) -> bool { MemoryStage::peek_session::(ctx, sub(opener, "opens")).open } diff --git a/crates/ui-widgets/src/buttons/icon_button.rs b/crates/ui-widgets/src/buttons/icon_button.rs index 68c6455d4..903cfe236 100644 --- a/crates/ui-widgets/src/buttons/icon_button.rs +++ b/crates/ui-widgets/src/buttons/icon_button.rs @@ -6,6 +6,7 @@ use crate::contract::{Access, Common, Component, Cx, MeasureCx, PaintUi, PopupKi use crate::controls::disclosure::DisclosureGlyph; use crate::primitives::{IconSpec, PaintShape, StrokeWidth}; use crate::runtime::commands::CommandRef; +use crate::runtime::env::Label; use crate::style::{ComponentStyle, FocusLook, IconLook, StyleColor, StyleCx, SurfaceLook}; use craft_behavior::{Metrics, Sense}; use craft_i18n::Direction; @@ -65,7 +66,9 @@ pub enum PopupMark { /// A square button showing only an icon. Its label is its accessible name; a tooltip is the caller's. A selected /// button (`selected(true)`) shows the on look: the host owns the state and a click is reported in the response. -/// [`ButtonOutput::held`] reports the press from its start to its end. +/// [`ButtonOutput::held`] reports the press from its start to its end. A button whose menu picks one of its rows (an +/// Align To button) gives the picked row as its [`choice`](Self::choice): it then reads as a combo box named by its +/// label, with the choice as its value. #[derive(crate::contract::Component, crate::contract::Props)] pub struct IconButton<'a> { #[common] @@ -82,15 +85,27 @@ pub struct IconButton<'a> { /// that opens a menu have one. #[prop] popup_mark: PopupMark, - /// Whether the popup the button opens is open, as the host knows it. + /// Whether the popup the button opens is open, as the host knows it. A button with a [`choice`](Self::choice) + /// reports, without it, whether the container hung from it is open. #[prop(into)] expanded: Option, + /// The row the menu it opens has picked, for a button that picks one of its menu's rows. + #[prop(into)] + choice: Option