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); 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-contract/src/testing/fixture.rs b/crates/ui-contract/src/testing/fixture.rs index af7fc72ce..69a3f167e 100644 --- a/crates/ui-contract/src/testing/fixture.rs +++ b/crates/ui-contract/src/testing/fixture.rs @@ -587,16 +587,21 @@ impl Fixture { } /// The node named `label`: the interactive node whose name resolves to it (its own, or the node that labels it - /// through `labelled_by`), else the first node that carries the text. + /// through `labelled_by`) and that labels no other node, so a field's control comes before its caption link; + /// else the first node that carries the text. pub fn by_label(&self, label: &str) -> Option { let tree = &self.tree; let reported: HashSet = tree.keys().into_iter().collect(); + let labels: HashSet = reported.iter().flat_map(|key| tree.labelled_by_key(*key)).collect(); let ordered = self.in_tree_order(&reported); let node = ordered .iter() .copied() .find(|key| { - super::access::name(tree, *key) == Some(label) && tree.role_of(*key) != Some(Role::LABEL) && super::access::is_interactive(tree, *key) + super::access::name(tree, *key) == Some(label) + && tree.role_of(*key) != Some(Role::LABEL) + && !labels.contains(key) + && super::access::is_interactive(tree, *key) }) .or_else(|| ordered.iter().copied().find(|key| tree.name_of(*key) == Some(label)))?; self.target(node) diff --git a/crates/ui-panels/src/docking/flyout.rs b/crates/ui-panels/src/docking/flyout.rs index a17f91b56..8b62c56a1 100644 --- a/crates/ui-panels/src/docking/flyout.rs +++ b/crates/ui-panels/src/docking/flyout.rs @@ -1,6 +1,6 @@ //! `Flyout`: the panel an icon of an iconic column opens beside the column. -use crate::contract::overlay::{Anchor, Dismiss, FloatPlace, Place, PlacementChange, ResizeEdges}; +use crate::contract::overlay::{Anchor, Dismiss, FloatPlace, Outside, Place, PlacementChange, ResizeEdges}; use crate::contract::{ BodyCx, Common, Component, Container, ContainerProps, Cx, Mount, NoValue, Outcome, Overlay, OverlayUi, Pad, PopupHandle, Role, SizeLimits, Sizing, }; @@ -48,8 +48,8 @@ impl Outcome for FlyoutOutput { } } -/// A popup beside an iconic column, on the side of the canvas, whose top left the caller places. Escape and a press -/// outside close it; with Auto-Collapse Iconic Panels the focus leaving it closes it too. Its body is measured at the +/// A popup beside an iconic column, on the side of the canvas, whose top left the caller places. Escape closes it; with +/// Auto-Collapse Iconic Panels a press outside it and the focus leaving it close it too. Its body is measured at the /// flyout's width first: the flyout is as tall as the body measures within the room below its top, and at least its /// `size`'s height, and the body then shows in the rect that takes. /// @@ -77,7 +77,7 @@ pub struct Flyout { impl Flyout { /// A flyout at least `size` that opens on the side `opens` of its column, in reading order (`Side::Start` or - /// `Side::End`), closing when the focus leaves it if `auto_collapse`. + /// `Side::End`), closing on a press outside it and when the focus leaves it if `auto_collapse`. pub fn new(opens: Side, size: Vec2, auto_collapse: bool) -> Self { let props = ContainerProps::new().surface(SurfaceKind::Popup).pad(Pad::None).sizing(Sizing::Content); Flyout { common: Common::new(), props, opens, size, auto_collapse, within: Rect::EVERYTHING, least: Vec2::ZERO, placed: None } @@ -96,7 +96,7 @@ impl Flyout { .placed_at(top_left) .size_limits(SizeLimits::at_least(least)) .resize_edges(free.union(ResizeEdges::TOP).union(ResizeEdges::BOTTOM)) - .dismiss(Dismiss::new().focus_left(self.auto_collapse)) + .dismiss(Dismiss::new().outside(if self.auto_collapse { Outside::Close } else { Outside::Keep }).focus_left(self.auto_collapse)) .body_scroll(false); if bounded { overlay.bounds(self.within) } else { overlay } } diff --git a/crates/ui-panels/src/docking/iconic.rs b/crates/ui-panels/src/docking/iconic.rs index 9a602d4e5..d4304070f 100644 --- a/crates/ui-panels/src/docking/iconic.rs +++ b/crates/ui-panels/src/docking/iconic.rs @@ -153,9 +153,8 @@ pub struct IconicColumnStyle { /// icon, cut with an ellipsis to the cell; a click or drag on the name acts as one on the icon. A click or Enter on an icon /// opens the flyout of its panel beside the column, switches it to that panel or closes it; one flyout is open at a /// time. A drag of an icon starts a move of its panel, a drag of the grip on top of a group a move of the group, a -/// drag of the bar beside its button a move of the column; each starts through the drag session, and a press outside -/// the flyout closes it. Escape and a press -/// outside close the flyout; with `auto_collapse` the focus leaving it does too. +/// drag of the bar beside its button a move of the column; each starts through the drag session. Escape closes the +/// flyout; with `auto_collapse` a press outside it and the focus leaving it do too. /// /// The open flyout is the bound `Option

` of [`new`](IconicColumn::new), owned by the host like the layout: a click /// on an icon, Escape and auto-collapse write it, and the host opens a panel (the Window menu) by writing it. The diff --git a/crates/ui-panels/src/docking/iconic/trials.rs b/crates/ui-panels/src/docking/iconic/trials.rs index 12bbcfb5e..65ad0bdbf 100644 --- a/crates/ui-panels/src/docking/iconic/trials.rs +++ b/crates/ui-panels/src/docking/iconic/trials.rs @@ -140,6 +140,11 @@ struct Flyouts { impl Flyouts { /// A column of `groups` on the right window edge when `right`, else the left one; its flyouts stay in `within`. fn new(groups: &[&[u8]], right: bool, within: Rect) -> Self { + Self::with(groups, right, within, false) + } + + /// [`Flyouts::new`] with Auto-Collapse Iconic Panels on when `auto_collapse`. + fn with(groups: &[&[u8]], right: bool, within: Rect, auto_collapse: bool) -> Self { let mut column = Column::new(groups.iter().map(|panels| Group::new(panels.to_vec(), 0)).collect()); column.iconic = true; let edge = if right { DockEdge::End } else { DockEdge::Start }; @@ -159,6 +164,7 @@ impl Flyouts { .edge(edge) .opens(if right { Side::Start } else { Side::End }) .within(within) + .auto_collapse(auto_collapse) .show_flyouts_at(ui, place, |group, at, ui, _| { let GroupAt::Show(rect) = at else { return None }; let panel = *group.active_panel()?; @@ -220,6 +226,18 @@ impl Flyouts { } } +/// A press outside the open flyout leaves it open; with Auto-Collapse Iconic Panels on it closes it. +#[trial] +fn a_press_outside_closes_the_flyout_only_with_auto_collapse() { + for auto_collapse in [false, true] { + let mut d = Flyouts::with(&[&[1, 2]], false, screen(), auto_collapse); + d.open("Brushes"); + let away = d.rect().right_center() + Vec2::new(100.0, 0.0); + d.fixture.click_at(away).settle(); + assert_eq!(d.shown.get().is_none(), auto_collapse, "auto-collapse {auto_collapse}: a press outside closes the flyout only with it on"); + } +} + /// The top edge and the free top corner grow the flyout upward on either window edge: the bottom and the column-side /// edge stay, the column-side edge has no grip, and the new top is kept as an offset from the icons, so it survives /// another group's flyout and reopening. 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 } diff --git a/crates/ui-patterns/src/patterns/dock/area.rs b/crates/ui-patterns/src/patterns/dock/area.rs index 6db392557..5e19b696a 100644 --- a/crates/ui-patterns/src/patterns/dock/area.rs +++ b/crates/ui-patterns/src/patterns/dock/area.rs @@ -130,7 +130,7 @@ pub struct DockArea<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> { /// The panel whose frame shows alone. #[prop] maximized: Option

, - /// Auto-Collapse Iconic Panels: a flyout closes when the focus leaves it. + /// Auto-Collapse Iconic Panels: a flyout closes on a press outside it and when the focus leaves it. #[prop(flag)] auto_collapse: bool, /// The workspace is locked: nothing moves, floats or closes. diff --git a/crates/ui-patterns/tests/it/docking/native.rs b/crates/ui-patterns/tests/it/docking/native.rs index 999412537..8e774f0f3 100644 --- a/crates/ui-patterns/tests/it/docking/native.rs +++ b/crates/ui-patterns/tests/it/docking/native.rs @@ -266,6 +266,8 @@ struct NativeDock { palette: Option, /// The tree the palette window's pass produced in the last frame. palette_tree: Option, + /// Whether Auto-Collapse Iconic Panels is on in the frames of a collapsed palette. + auto_collapse: bool, } impl NativeDock { @@ -287,7 +289,7 @@ impl NativeDock { windows.child_info(info(true, FLOAT_AT)); let workspace = Arc::new(Workspace::new(Layout::new(Some(Node::tabs(vec![2], 0)), vec![palette]))); let specs = Tracked::new(Specs { rights: Permissions::ALL, least: false, stepped }); - let mut dock = NativeDock { windows, workspace, stepped, specs, builder: None, palette: None, palette_tree: None }; + let mut dock = NativeDock { windows, workspace, stepped, specs, builder: None, palette: None, palette_tree: None, auto_collapse: false }; for _ in 0..3 { dock.frame(vec![]); } @@ -1034,10 +1036,10 @@ fn collapsed_palette() -> (NativeDock, Rect) { /// One frame of `dock` with `panels`, `root` events for the main window and `child` events for the palette's window, /// whose OS window follows its commands at `at`: the frame's output. fn collapsed_frame(dock: &mut NativeDock, panels: &mut dyn DockPanels, at: &mut Rect, root: Vec, child: Vec) -> Out { - let workspace = &mut dock.workspace; - let specs = &dock.specs; + let (workspace, specs, auto_collapse) = (&mut dock.workspace, &dock.specs, dock.auto_collapse); let out = dock.windows.frame(root, child, |ui| { - let _ = DockArea::new("Native dock", Bind::bound(workspace), specs, &mut *panels, DockConfig::default()).show(ui); + let area = DockArea::new("Native dock", Bind::bound(workspace), specs, &mut *panels, DockConfig::default()); + let _ = if auto_collapse { area.auto_collapse() } else { area }.show(ui); }); let palette = dock.palette; let commands: Vec = @@ -1222,22 +1224,26 @@ fn a_tooltip_from_a_native_palette_opens_in_a_popup_os_window() { assert!(rect.width() > at.width() && rect.height() < rect.width(), "the tooltip's text runs on one line, wider than the strip: {rect:?}"); } -/// A press in the main window closes the flyout a collapsed native palette opened in a popup window. +/// With Auto-Collapse Iconic Panels on, a press in the main window closes the flyout a collapsed native palette opened +/// in a popup window; with it off the flyout stays open. #[trial] -fn a_press_in_the_main_window_closes_a_native_palettes_flyout() { - let (mut dock, mut at) = collapsed_palette(); - let mut probe = BodyProbe::default(); - let popup = open_flyout(&mut dock, &mut probe, &mut at); - let over = OVER_GROUP - MAIN_AT.min.to_vec2(); - for root in [vec![moved(over)], vec![press(over)], vec![release(over)]] { - collapsed_frame(&mut dock, &mut probe, &mut at, root, vec![]); +fn a_press_in_the_main_window_closes_a_native_palettes_flyout_with_auto_collapse() { + for auto_collapse in [true, false] { + let (mut dock, mut at) = collapsed_palette(); + dock.auto_collapse = auto_collapse; + let mut probe = BodyProbe::default(); + let popup = open_flyout(&mut dock, &mut probe, &mut at); + let over = OVER_GROUP - MAIN_AT.min.to_vec2(); + for root in [vec![moved(over)], vec![press(over)], vec![release(over)]] { + collapsed_frame(&mut dock, &mut probe, &mut at, root, vec![]); + } + // The flyout fades out, then its window closes. + let closed = (0..60).any(|_| { + let out = collapsed_frame(&mut dock, &mut probe, &mut at, vec![], vec![]); + !popups(&dock, &out).contains(&popup) + }); + assert_eq!(closed, auto_collapse, "auto-collapse {auto_collapse}: the flyout's window closed only with it on"); } - // The flyout fades out, then its window closes. - let closed = (0..60).any(|_| { - let out = collapsed_frame(&mut dock, &mut probe, &mut at, vec![], vec![]); - !popups(&dock, &out).contains(&popup) - }); - assert!(closed, "the flyout's window closed"); } /// Palettes no smaller than `PALETTE_LEAST`. 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