From b9ae41c9956e737dba975d7d5541d3dcc1babc9c Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 17 Nov 2026 03:48:35 +0000 Subject: [PATCH] Dock UI: stepped columns: whole content columns wide, as tall as their content docked and floating, edge drags and the double arrow set steps --- crates/ui-panels/src/docking/dock_column.rs | 7 +- .../src/docking/dock_column/sizing.rs | 10 +- crates/ui-panels/src/docking/floating.rs | 24 +++- crates/ui-panels/src/docking/mod.rs | 5 +- crates/ui-panels/src/docking/spec.rs | 63 ++++++++++ crates/ui-panels/src/docking/targets.rs | 37 ++++-- crates/ui-patterns/src/patterns/dock/area.rs | 94 +++++++++++++-- .../ui-patterns/tests/it/docking/columns.rs | 109 +++++++++++++++++- 8 files changed, 316 insertions(+), 33 deletions(-) diff --git a/crates/ui-panels/src/docking/dock_column.rs b/crates/ui-panels/src/docking/dock_column.rs index 2e5b9558a..7ea58c8de 100644 --- a/crates/ui-panels/src/docking/dock_column.rs +++ b/crates/ui-panels/src/docking/dock_column.rs @@ -32,7 +32,7 @@ use bar::panel_names; pub(super) use bar::{BarRecord, CHEVRON, bar_name, bar_slots, chevron_name, describe_chevron, paint_chevron, show_bar}; use sizing::group_sizes; -pub use sizing::{GroupSize, fit_widths, group_heights, scale_widths, stacked, width_bounds}; +pub use sizing::{GroupSize, fit_widths, group_heights, scale_widths, stacked, stepped, width_bounds}; const DIVIDER: PartKey = PartKey::new("divider"); const EDGE: PartKey = PartKey::new("edge"); @@ -214,9 +214,10 @@ impl<'a, P: Value + Hash + 'static> DockColumn<'a, P> { output } - /// The divider on the column's inner edge, which moves between the width bounds of its panels. + /// The divider on the column's inner edge, which moves between the width bounds of its panels, or of its steps + /// for a stepped column ([`stepped`](super::stepped)), whose host takes the width as whole steps. fn edge_divider(&self, rect: Rect, sizes: &DockSizes) -> Divider { - let (least, most) = sizes.width_bounds(self.column, self.spec); + let (least, most) = sizes.shown_bounds(self.column, self.spec); Divider::column_edge(rect, self.edge, (least, most.min((self.within.width() - sizes.least_pane).max(rect.width()))), sizes.reach) } diff --git a/crates/ui-panels/src/docking/dock_column/sizing.rs b/crates/ui-panels/src/docking/dock_column/sizing.rs index 868ed67ae..879a7afce 100644 --- a/crates/ui-panels/src/docking/dock_column/sizing.rs +++ b/crates/ui-panels/src/docking/dock_column/sizing.rs @@ -1,7 +1,7 @@ //! The widths of dock columns and the heights of the groups stacked in one. use crate::contract::SizeLimits; -use crate::docking::{DockHeights, DockSizes, PanelSpec}; +use crate::docking::{DockGrid, DockHeights, DockSizes, PanelSpec}; use craft_behavior::dock::Column; use egui::{Pos2, Rect}; @@ -29,6 +29,14 @@ pub fn fit_widths(columns: &[(f32, Option)], room: f32) -> Vec { scale_widths(&bounded, room) } +/// The grid and steps of a stepped column: its [`Column::steps`] and the [`DockGrid`] of its first panel that +/// declares a valid one. `None` for a column without steps or whose panels declare none, which sizes as any other. +pub fn stepped

(column: &Column

, spec: &dyn Fn(&P) -> PanelSpec) -> Option<(DockGrid, u16)> { + let steps = column.steps?; + let grid = column.panels().find_map(|panel| spec(panel).grid.filter(|grid| grid.valid()))?; + Some((grid, grid.clamp(steps))) +} + /// Column widths that add up to `total` as far as their bounds allow. Each entry is `(width, (lo, hi))`. The /// columns that can still move towards `total` scale by one factor; a column that reaches a bound stops there and /// the others take the rest, so a total past the bounds ends at their sum. diff --git a/crates/ui-panels/src/docking/floating.rs b/crates/ui-panels/src/docking/floating.rs index 40b357b5f..855ef7337 100644 --- a/crates/ui-panels/src/docking/floating.rs +++ b/crates/ui-panels/src/docking/floating.rs @@ -53,17 +53,27 @@ pub struct WindowColumn { pub edge: DockEdge, /// The size of the collapsed column under the title row; `None` while the column is expanded. pub collapsed: Option, + /// The size of an expanded column whose content sets it (a column with steps) under the title row; `None` for a + /// column the rect sizes. + pub fitted: Option, } impl WindowColumn { /// An expanded column that keeps to `edge`. pub fn expanded(edge: DockEdge) -> Self { - WindowColumn { edge, collapsed: None } + WindowColumn { edge, collapsed: None, fitted: None } } /// A column collapsed to icons, `size` under the title row, that keeps to `edge`. pub fn collapsed(edge: DockEdge, size: Vec2) -> Self { - WindowColumn { edge, collapsed: Some(size) } + WindowColumn { edge, collapsed: Some(size), fitted: None } + } + + /// An expanded column whose content is `size` under the title row, that keeps to `edge`. The window shows at + /// that size at its rect's place; a resize the user makes is reported for the host to turn into the column's + /// steps, and a move keeps the rect's size. + pub fn fitted(edge: DockEdge, size: Vec2) -> Self { + WindowColumn { edge, collapsed: None, fitted: Some(size) } } /// The edge of `bounds`, in reading order, nearer the window at `rect`: the start edge when the window's centre is @@ -134,7 +144,9 @@ impl Outcome for FloatingGroupOutput { /// `new` keeps the height the group has when restored. /// - Column: a dock window holds one column ([`column`](FloatingGroup::column)); its title row is the column's bar, /// with the double arrow at the end the window keeps to. While the column is collapsed the group shows at the -/// collapsed size against that edge of the rect, the edges do not resize, and a move keeps the rect's size. +/// collapsed size against that edge of the rect, the edges do not resize, and a move keeps the rect's size. A +/// column its content sizes ([`WindowColumn::fitted`]) shows at that size at the rect's place; its resizes are +/// reported as the user makes them. /// - Docking: with [`window_of`](FloatingGroup::window_of) set, a move by the title starts a window-unit drag /// (`GroupDrag`) for the dock's `DockDropTarget`, which shows the drop mark and docks the window on release. /// - Window: with `native` set the group opens in an OS window of that kind, with the layer as the fallback where @@ -298,6 +310,8 @@ impl<'a> FloatingGroup<'a> { Overlay::floating().movable(true).resizable(!self.minimized && collapsed.is_none()).opacity(self.opacity).body_scroll(false); overlay = if let Some(shown) = collapsed { overlay.placed(shown).size_limits(SizeLimits::new(shown.size(), shown.size())) + } else if let Some(size) = self.column.and_then(|column| column.fitted) { + overlay.placed(Rect::from_min_size(self.rect.min, size + Vec2::new(0.0, header))) } else if self.minimized { let width = self.rect.width(); overlay.placed_at(self.rect.min).size_limits(SizeLimits::new(Vec2::new(width, 0.0), Vec2::new(width, f32::INFINITY))) @@ -443,9 +457,13 @@ impl Container for FloatingGroup<'_> { fn placement_changed(&mut self, change: &PlacementChange) { let mut change = *change; + let fitted = self.column.is_some_and(|column| column.fitted.is_some()); if let Some(shown) = self.collapsed_rect() { // The collapsed window moves; the rect keeps its expanded size. change.rect = self.rect.translate(change.rect.min - shown.min); + } else if fitted && change.kind == PlacementKind::Move { + // A window its content sizes moves; the rect keeps its size. + change.rect = Rect::from_min_size(change.rect.min, self.rect.size()); } else if self.minimized { change.rect = Rect::from_min_size(change.rect.min, Vec2::new(change.rect.width(), self.rect.height())); } diff --git a/crates/ui-panels/src/docking/mod.rs b/crates/ui-panels/src/docking/mod.rs index f1ca68ce7..2fdcc16d8 100644 --- a/crates/ui-panels/src/docking/mod.rs +++ b/crates/ui-panels/src/docking/mod.rs @@ -41,7 +41,7 @@ pub use panels::{DockPanels, request_actions}; #[doc(hidden)] pub use panels::{close_requests, open_requests}; pub use root::{DockRoot, DockRootOutput, DockRootStyle}; -pub use spec::{DockHeights, PanelSpec}; +pub use spec::{DockGrid, DockHeights, PanelSpec}; pub use splitter::{Splitter, SplitterStyle}; pub use targets::{DockSizes, DockSpot, DockTargets, GroupDrag, ZoneRule}; pub use words::DockWords; @@ -56,7 +56,8 @@ mod flyout; mod iconic; pub use dock_column::{ - ColumnBar, DockColumn, DockColumnOutput, DockColumnStyle, GroupSize, GroupView, fit_widths, group_heights, scale_widths, stacked, width_bounds, + ColumnBar, DockColumn, DockColumnOutput, DockColumnStyle, GroupSize, GroupView, fit_widths, group_heights, scale_widths, stacked, stepped, + width_bounds, }; pub use flyout::{Flyout, FlyoutOutput, FlyoutShown, FlyoutStyle, GroupAt}; pub use iconic::{FlyoutResize, IconicColumn, IconicColumnOutput, IconicColumnStyle, IconicSizes}; diff --git a/crates/ui-panels/src/docking/spec.rs b/crates/ui-panels/src/docking/spec.rs index a888703a7..b395cd6f0 100644 --- a/crates/ui-panels/src/docking/spec.rs +++ b/crates/ui-panels/src/docking/spec.rs @@ -48,6 +48,58 @@ impl DockHeights { } } +/// How a panel that lays its content in a grid of equal columns (a toolbar) sizes its dock column. The column is a +/// whole number of content columns wide, its steps ([`Column::steps`](craft_behavior::dock::Column::steps)), and as +/// tall as the content measures at that width: docked from the top of the dock, floating under the title row. A drag +/// of the column's edge sets whole steps; the content lays out as many columns as fit the width it gets. +#[derive(Clone, Copy, Debug, PartialEq)] +#[non_exhaustive] +pub struct DockGrid { + /// The content's width at one column: a cell and the content's own padding on both sides. + pub first: f32, + /// The width each further column adds: a cell and the gap before it. + pub step: f32, + /// The most columns the content fills, its cell count: more steps show it no wider. + pub most: u16, +} + +impl DockGrid { + /// A grid `first` wide at one column, `step` wider for each further column, up to `most` columns. + pub const fn new(first: f32, step: f32, most: u16) -> Self { + DockGrid { first, step, most } + } + + /// Whether the widths are finite and positive and the grid has a column. + pub fn valid(self) -> bool { + self.first.is_finite() && self.first > 0.0 && self.step.is_finite() && self.step > 0.0 && self.most > 0 + } + + /// `steps` kept between one column and [`most`](Self::most). + pub fn clamp(self, steps: u16) -> u16 { + steps.clamp(1, self.most.max(1)) + } + + /// The content's width at `steps` columns, kept within the grid's columns. + pub fn width(self, steps: u16) -> f32 { + self.first + f32::from(self.clamp(steps) - 1) * self.step + } + + /// The most columns whose width fits `width`, at least one. + pub fn steps_within(self, width: f32) -> u16 { + (1..=self.most.max(1)).rev().find(|steps| self.width(*steps) <= width + WIDTH_SLACK).unwrap_or(1) + } + + /// The fewest columns at which `height`, the content's height at a width ([`width`](Self::width)), fits `room`; + /// the most columns when none fits. + pub fn steps_for_height(self, room: f32, mut height: impl FnMut(f32) -> f32) -> u16 { + let most = self.most.max(1); + (1..=most).find(|steps| height(self.width(*steps)) <= room + WIDTH_SLACK).unwrap_or(most) + } +} + +/// How far a width or height may pass the room and still fit it: the rounding of laid-out sizes. +const WIDTH_SLACK: f32 = 0.5; + /// What the dock reads of a panel: its name and icon, its size policy and how its floating window behaves. The /// permissions of a panel (movable, floatable, closable, takes tabs, takes splits) belong to the dock model, not /// to this spec. @@ -75,6 +127,9 @@ pub struct PanelSpec { /// The panel is a document canvas: its group shows it without the tab and side drops of a panel group, and the /// host draws it with `DockPanels::group_view`. pub canvas: bool, + /// The grid the panel lays its content in, which makes a column with steps whole content columns wide and as + /// tall as its content. `None` for a panel that fills the width it gets. + pub grid: Option, } impl PanelSpec { @@ -89,6 +144,7 @@ impl PanelSpec { allow_native: true, window_kind: WindowKind::Tool, canvas: false, + grid: None, } } @@ -140,4 +196,11 @@ impl PanelSpec { self.canvas = true; self } + + /// The grid the panel lays its content in. + #[must_use] + pub fn grid(mut self, grid: DockGrid) -> Self { + self.grid = Some(grid); + self + } } diff --git a/crates/ui-panels/src/docking/targets.rs b/crates/ui-panels/src/docking/targets.rs index aa3fb5beb..164390f63 100644 --- a/crates/ui-panels/src/docking/targets.rs +++ b/crates/ui-panels/src/docking/targets.rs @@ -4,7 +4,7 @@ //! previous pass's registry, because a drop is decided against what was on screen when the pointer moved. Every //! zone is a rect. A spot names a group by an anchor panel, as the dock model does. -use super::{DragUnit, PanelKey, PanelSpec, scale_widths, width_bounds}; +use super::{DragUnit, PanelKey, PanelSpec, scale_widths, stepped, width_bounds}; use crate::contract::DropMark; use crate::runtime::frame::{self, PassStep, PerViewport, Stamped, Stored}; use crate::style::StyleCx; @@ -100,18 +100,25 @@ impl DockSizes { } /// The width an expanded column's panels draw at, whether the column is expanded or iconic (its flyout and a - /// floated iconic column use it): the stored width kept within the panels' limits. + /// floated iconic column use it): the stored width kept within the panels' limits, or a stepped column's width + /// at its steps ([`stepped`]). pub fn expanded_width

(&self, column: &Column

, spec: &dyn Fn(&P) -> PanelSpec) -> f32 { + if let Some((grid, steps)) = stepped(column, spec) { + return grid.width(steps); + } let (lo, hi) = self.width_bounds(column, spec); column.width.unwrap_or(self.column).clamp(lo, hi) } /// The bounds [`width`](Self::width) keeps a column in: an expanded column's [`width_bounds`](Self::width_bounds); - /// an iconic column's rail width up to the width it expands to. + /// a stepped one's width at one step up to its width at all its grid's columns; an iconic column's rail width up + /// to the width it expands to. pub fn shown_bounds

(&self, column: &Column

, spec: &dyn Fn(&P) -> PanelSpec) -> (f32, f32) { if column.iconic { // Widening an iconic column stops at the width it expands to. (self.iconic_least, self.expanded_width(column, spec).max(self.iconic_least)) + } else if let Some((grid, _)) = stepped(column, spec) { + (grid.width(1), grid.width(grid.most)) } else { self.width_bounds(column, spec) } @@ -129,7 +136,8 @@ impl DockSizes { /// The actions that give the dock on `side` of `layout` the total width `total`, its columns scaled within /// their [`shown_bounds`](Self::shown_bounds) ([`scale_widths`]): [`Action::ResizeColumn`] for an expanded column, - /// [`Action::ResizeIconic`] for an iconic one, which keeps its expanded width. Apply them together + /// [`Action::ResizeIconic`] for an iconic one, which keeps its expanded width. A stepped column keeps its width, + /// which only its steps set. Apply them together /// ([`Workspace::apply_all`](craft_behavior::dock::Workspace::apply_all)). Empty when the side has no dock or the /// columns already add up to the width; a column without panels keeps its width. pub fn resize_side( @@ -143,12 +151,19 @@ impl DockSizes { Some(Node::Dock { columns, .. }) => columns, _ => return Vec::new(), }; - let current: Vec<(f32, (f32, f32))> = columns.iter().map(|column| (self.width(column, spec), self.shown_bounds(column, spec))).collect(); + let fixed = |column: &Column

| !column.iconic && stepped(column, spec).is_some(); + let current: Vec<(f32, (f32, f32))> = columns + .iter() + .map(|column| { + let width = self.width(column, spec); + (width, if fixed(column) { (width, width) } else { self.shown_bounds(column, spec) }) + }) + .collect(); let widths = scale_widths(¤t, total); columns .iter() .zip(current.iter().zip(widths)) - .filter(|(_, ((width, _), scaled))| (scaled - width).abs() >= 0.01) + .filter(|(column, ((width, _), scaled))| !fixed(column) && (scaled - width).abs() >= 0.01) .filter_map(|(column, (_, width))| { let panel = column.groups.iter().find_map(|group| group.panels.first())?.clone(); Some(if column.iconic { Action::resize_iconic(panel, Some(width)) } else { Action::resize_column(panel, width) }) @@ -157,9 +172,15 @@ impl DockSizes { } /// The `(width, floor)` of every column, for [`fit_widths`](super::fit_widths): an expanded column shrinks down to - /// its least width, an iconic one keeps its width. + /// its least width, an iconic or stepped one keeps its width. pub fn natural

(&self, columns: &[Column

], spec: &dyn Fn(&P) -> PanelSpec) -> Vec<(f32, Option)> { - columns.iter().map(|column| (self.width(column, spec), (!column.iconic).then(|| self.width_bounds(column, spec).0.max(1.0)))).collect() + columns + .iter() + .map(|column| { + let keeps = column.iconic || stepped(column, spec).is_some(); + (self.width(column, spec), (!keeps).then(|| self.width_bounds(column, spec).0.max(1.0))) + }) + .collect() } } diff --git a/crates/ui-patterns/src/patterns/dock/area.rs b/crates/ui-patterns/src/patterns/dock/area.rs index 041524db9..3f24d0b90 100644 --- a/crates/ui-patterns/src/patterns/dock/area.rs +++ b/crates/ui-patterns/src/patterns/dock/area.rs @@ -2,7 +2,7 @@ //! the parts of `docking`, takes the drops of tab and group drags, and applies their actions to the workspace. use super::hide::{self, Shown}; -use crate::contract::overlay::PlacementChange; +use crate::contract::overlay::{PlacementChange, PlacementKind}; use crate::contract::{ Bind, BindMut, BodyCx, Cx, Entry, Lazy, NoValue, Outcome, Pad, Pointer as _, Props as _, ShowContainer as _, Sizing, WordSetCx as _, }; @@ -13,7 +13,7 @@ use crate::docking::{Arrangement, close_requests, cross_window, end_replaced_dra use crate::docking::{ ColumnBar, DockColumn, DockCommands, DockConfig, DockDropTarget, DockDropped, DockEvent, DockGroup, DockPanels, DockRoot, DockScene, DockSizes, DockSpot, DockTargets, DockTo, DockWords, DragUnit, FloatSpace, FloatingGroup, FlyoutResize, GroupAt, GroupDrag, HidePanels, IconicColumn, - IconicSizes, PanelDrag, PanelKey, PanelSpec, PanelTitle, SplitArea, WindowColumn, actions, dock_states, fit_widths, frame, move_actions, + IconicSizes, PanelDrag, PanelKey, PanelSpec, PanelTitle, SplitArea, WindowColumn, actions, dock_states, fit_widths, frame, move_actions, stepped, }; use crate::menu::{GroupItem, MenuWords, PanelMenu, PanelRow}; use crate::runtime::commands::{CommandRef, CommandSet as _, Commands as _, focus_within}; @@ -913,14 +913,47 @@ impl Pass<'_, P> { for (column, width) in plan.columns.iter().zip(widths) { let span = if start { (cursor, cursor + width) } else { (cursor - width, cursor) }; cursor = if start { span.1 } else { span.0 }; - let cell = plan.flow.to_physical(Rect::from_x_y_ranges(span.0..=span.1, reading.y_range())); + let mut cell = plan.flow.to_physical(Rect::from_x_y_ranges(span.0..=span.1, reading.y_range())); let Some(first) = column.groups.first().and_then(|group| group.panels.first()) else { continue }; + // A stepped column is as tall as its bar and content, from the top of the dock; content taller than the + // dock gets the dock's height and scrolls inside. The rest of its slot is the dock's empty room. + if let Some(content) = self.stepped_height(ui, body, column, cell.width(), false) { + cell.max.y = (cell.top() + scene.sizes.bar + content).min(cell.bottom()); + } self.report.placed.push((first.clone(), cell)); let at = ColumnAt { cell, edge: plan.edge, bar: ColumnBar::Own, within: body.rect, open: plan.open.clone() }; self.column(ui, body, column, at); } } + /// The height of the groups of an expanded stepped column ([`stepped`]) measured at `width`, the width of its + /// steps: what the column shows under its bar. `None` for any other column. + fn stepped_height(&mut self, ui: &mut Ui, body: &mut BodyCx<'_>, column: &Column

, width: f32, in_window: bool) -> Option { + let scene = self.scene; + let spec = |panel: &P| scene.spec(panel); + if column.iconic || stepped(column, &spec).is_none() { + return None; + } + let mut height = 0.0; + for group in &column.groups { + height += self.group_of(ui, body, group, GroupAt::Measure(width), in_window).unwrap_or(0.0); + } + Some(height) + } + + /// What a click on the double arrow of `column`, whose first panel is `first`, asks for when it reports + /// `iconic`: an expanded stepped column whose panels do not collapse switches between one and two steps; any + /// other column collapses to icons or expands. + fn arrow(&self, column: &Column

, first: &P, iconic: bool) -> Action

{ + let scene = self.scene; + let spec = |panel: &P| scene.spec(panel); + let collapses = column.panels().all(|panel| scene.permissions(panel).collapses); + match stepped(column, &spec) { + Some((_, steps)) if !collapses && !column.iconic => Action::set_column_steps(first.clone(), if steps == 1 { 2 } else { 1 }), + _ => Action::set_iconic(first.clone(), iconic), + } + } + /// One column, docked or floating: its icons and their flyouts while it is collapsed, else its stacked groups, /// each group shown by `group_of`. What the user did on it becomes actions. fn column(&mut self, ui: &mut Ui, body: &mut BodyCx<'_>, column: &Column

, at: ColumnAt

) { @@ -959,7 +992,8 @@ impl Pass<'_, P> { fn column_work(&mut self, column: &Column

, work: ColumnWork

) { let Some(first) = column.groups.first().and_then(|group| group.panels.first()) else { return }; if let Some(iconic) = work.iconic { - self.report.actions.push(Action::set_iconic(first.clone(), iconic)); + let action = self.arrow(column, first, iconic); + self.report.actions.push(action); } if let Some(item) = work.picked { self.pick(first, item); @@ -967,12 +1001,23 @@ impl Pass<'_, P> { for (anchor, height) in work.resized { self.report.actions.push(Action::resize_group(anchor, Some(height))); } + let scene = self.scene; + let spec = |panel: &P| scene.spec(panel); if let Some(width) = work.width { - self.report.actions.push(if column.iconic { - Action::resize_iconic(first.clone(), Some(width)) - } else { - Action::resize_column(first.clone(), width) - }); + match stepped(column, &spec) { + // The edge of a stepped column moves in whole steps: the most that fit where it is dragged. + Some((grid, steps)) if !column.iconic => { + let to = grid.steps_within(width); + if to != steps { + self.report.actions.push(Action::set_column_steps(first.clone(), to)); + } + } + _ => self.report.actions.push(if column.iconic { + Action::resize_iconic(first.clone(), Some(width)) + } else { + Action::resize_column(first.clone(), width) + }), + } } if let Some(resize) = work.flyout { let panel = resize.panel; @@ -999,7 +1044,11 @@ impl Pass<'_, P> { /// collapsed column shows against that edge of its rect. A move or resize keeps the edge the window had when it /// began, so nothing jumps while the window crosses the middle; a collapsed window's move ends with the rect /// that keeps its strip where it was dropped ([`WindowColumn::anchored`]). - fn windows(&mut self, ui: &mut Ui, body: &BodyCx<'_>, open: &Option

, started: Option<&Layout

>) -> Option<(P, PlacementChange)> { + /// + /// A window whose column has steps ([`stepped`]) is as wide as its steps and as tall as its content at that + /// width, at its rect's place: a drag of its side sets the most steps that fit the width, one of its top or + /// bottom the fewest whose content fits the height, and the window keeps its place. + fn windows(&mut self, ui: &mut Ui, body: &mut BodyCx<'_>, open: &Option

, started: Option<&Layout

>) -> Option<(P, PlacementChange)> { let scene = self.scene; let ctx = ui.ctx().clone(); let policy = |panel: &P| scene.permissions(panel); @@ -1037,9 +1086,12 @@ impl Pass<'_, P> { let began = started.and_then(|layout| layout.floating.iter().find(|began| began.id == window.id)).map_or(opening.rect, |began| began.rect); let edge = WindowColumn::nearer_edge(began, scene.bounds, scene.rtl); + let width = scene.sizes.expanded_width(&window.column, &spec_of); let shape = if window.column.iconic { let size = Vec2::new(scene.sizes.width(&window.column, &spec_of), IconicSizes::of(&ctx).icons_height(&window.column)); WindowColumn::collapsed(edge, size) + } else if let Some(content) = self.stepped_height(ui, body, &window.column, width, true) { + WindowColumn::fitted(edge, Vec2::new(width, content)) } else { WindowColumn::expanded(edge) }; @@ -1090,6 +1142,25 @@ impl Pass<'_, P> { } else if let Some(mut change) = output.placed { if change.phase == GesturePhase::Commit { change.rect = shape.anchored(change.rect, scene.bounds, scene.rtl); + } + if !matches!(change.kind, PlacementKind::Move) + && let Some(fitted) = shape.fitted + && let Some((grid, steps)) = stepped(&window.column, &spec_of) + { + let column = &window.column; + let to = if (change.rect.width() - fitted.x).abs() > 0.5 { + grid.steps_within(change.rect.width()) + } else { + let room = change.rect.height() - scene.sizes.strip; + grid.steps_for_height(room, |width| self.stepped_height(ui, body, column, width, true).unwrap_or(0.0)) + }; + if to != steps { + self.report.actions.push(Action::set_column_steps(anchor.clone(), to)); + } + // The window keeps its place; its steps set its size. + change.rect = Rect::from_min_size(change.rect.min, opening.rect.size()); + } + if change.phase == GesturePhase::Commit { self.report.events.push(DockEvent::FloatPlaced { panel: anchor.clone(), rect: change.rect }); } moved = Some((anchor.clone(), change)); @@ -1117,7 +1188,8 @@ impl Pass<'_, P> { } // The title row is the column's bar: its double arrow collapses or expands the column. if let Some(iconic) = output.iconic { - self.report.actions.push(Action::set_iconic(anchor.clone(), iconic)); + let action = self.arrow(&window.column, &anchor, iconic); + self.report.actions.push(action); } } moved diff --git a/crates/ui-patterns/tests/it/docking/columns.rs b/crates/ui-patterns/tests/it/docking/columns.rs index e31b78cab..f5d4e3071 100644 --- a/crates/ui-patterns/tests/it/docking/columns.rs +++ b/crates/ui-patterns/tests/it/docking/columns.rs @@ -1,11 +1,15 @@ //! Dock columns: room sharing between groups, and a dock side's total width shared between its columns. use craft_behavior::Axis; -use craft_behavior::dock::{Column, DockEdge, Group, Layout, Node, Permissions, SplitSize, Workspace, node_at}; -use craft_ui::contract::SizeLimits; -use craft_ui::docking::{DockHeights, GroupSize, PanelSpec, group_heights}; -use craft_ui::testing::trial; -use egui::Vec2; +use craft_behavior::dock::{Action, Column, DockEdge, Group, Layout, Node, Permissions, SplitSize, Workspace, node_at}; +use craft_ui::contract::{Bind, BodyCx, Role, Show as _, SizeLimits}; +use craft_ui::display::Text; +use craft_ui::docking::{DockConfig, DockGrid, DockHeights, DockPanels, GroupSize, PanelSpec, group_heights}; +use craft_ui::patterns::dock::DockArea; +use craft_ui::testing::{Fixture, trial}; +use egui::{Rect, Ui, Vec2}; +use std::cell::RefCell; +use std::rc::Rc; const STRIP: f32 = 20.0; const MIN: f32 = 60.0; @@ -103,3 +107,98 @@ fn a_dock_sides_total_width_scales_its_columns_within_their_bounds() { assert!(sizes.resize_side(&live.layout, DockEdge::End, spec, least).is_empty(), "the width it has asks for nothing"); assert!(sizes.resize_side(&live.layout, DockEdge::Start, spec, 500.0).is_empty(), "a side without a dock asks for nothing"); } + +/// The panel that lays its content in a grid, as a toolbar does. +const TOOLS: u32 = 5; + +fn grid() -> DockGrid { + DockGrid::new(40.0, 32.0, 6) +} + +/// A stepped column keeps its width, which only its steps set, while the side's other columns scale; a width that +/// its edge is dragged to takes the most whole steps that fit it. +#[trial] +fn a_stepped_column_keeps_its_width_while_the_others_scale() { + let sizes = super::sizes(4.0, 100.0, STRIP); + let with_tools = |panel: &u32| if *panel == TOOLS { PanelSpec::new("Tools").grid(grid()) } else { spec(panel) }; + let spec: &dyn Fn(&u32) -> PanelSpec = &with_tools; + let columns = vec![Column::new(vec![Group::new(vec![2], 0)]).with_width(250.0), Column::new(vec![Group::new(vec![TOOLS], 0)]).with_steps(2)]; + let root = Node::split(Axis::X, SplitSize::Ratio(0.7), Node::tabs(vec![1], 0), Node::dock(DockEdge::End, columns)); + let mut live = Workspace::new(Layout::new(Some(root), Vec::new())); + let widths = |live: &Workspace| end_columns(&live.layout).iter().map(|column| sizes.width(column, spec)).collect::>(); + let before = widths(&live); + assert_eq!(before[1], grid().width(2), "a stepped column is as wide as its steps"); + let actions = sizes.resize_side(&live.layout, DockEdge::End, spec, sum(&before) + 80.0); + let touches_tools = |action: &Action| matches!(action, Action::ResizeColumn { panel: TOOLS, .. } | Action::SetColumnSteps { .. }); + assert!(!actions.iter().any(touches_tools), "the side's width leaves the stepped column alone: {actions:?}"); + live.apply_all(actions, false, |_| Permissions::ALL).expect("the side resizes"); + let after = widths(&live); + assert_eq!(after[1], before[1], "the stepped column keeps its width"); + assert!(after[0] > before[0], "the other column took the room: {after:?}"); + + let grid = grid(); + assert_eq!(grid.steps_within(grid.width(3) - 1.0), 2, "a width short of three steps takes two"); + assert_eq!(grid.steps_within(grid.width(3)), 3, "the width of three steps takes three"); + assert_eq!(grid.steps_within(0.0), 1, "a column keeps one step"); + assert_eq!(grid.steps_within(f32::MAX), grid.most, "no more steps than the grid has columns"); +} + +/// A toolbar: a panel whose two lines of content lay out in a grid and never collapse. +struct Tools; + +impl DockPanels for Tools { + fn spec(&self, panel: &u32) -> PanelSpec { + if *panel == TOOLS { PanelSpec::new("Tools").grid(grid()) } else { PanelSpec::new("Canvas") } + } + + fn permissions(&self, panel: &u32) -> Permissions { + if *panel == TOOLS { Permissions::ALL.collapses(false) } else { Permissions::ALL } + } + + fn body(&mut self, panel: &u32, ui: &mut Ui, _: &mut BodyCx<'_>) { + if *panel == TOOLS { + Text::new("One").show(ui); + Text::new("Two").show(ui); + } + } +} + +/// A docked stepped column is only as tall as its content, not the dock's height; the double arrow of a column that +/// does not collapse switches it between one and two steps. +#[trial] +fn a_docked_stepped_column_fits_its_content_and_its_double_arrow_switches_one_and_two_steps() { + let dock = Node::dock(DockEdge::Start, vec![Column::new(vec![Group::new(vec![TOOLS], 0)]).with_steps(1)]); + let root = Node::split(Axis::X, SplitSize::FixedFirst(120.0), dock, Node::tabs(vec![1], 0)); + let workspace = Rc::new(RefCell::new(Workspace::new(Layout::new(Some(root), Vec::new())))); + let placed: Rc>> = Rc::default(); + let (held, seen) = (Rc::clone(&workspace), Rc::clone(&placed)); + let mut fixture = Fixture::new(move |ui| { + let mut workspace = held.borrow().clone(); + let out = DockArea::new("Stepped dock", Bind::bound(&mut workspace), &mut Tools, DockConfig::default()).show(ui); + *held.borrow_mut() = workspace; + *seen.borrow_mut() = out.columns; + }); + fixture.settle(); + let tools = placed.borrow().iter().find(|(panel, _)| *panel == TOOLS).map(|(_, rect)| *rect).expect("the stepped column shows"); + let canvas = + fixture.by_role(Role::TAB_PANEL).into_iter().map(|group| group.rect).find(|rect| !tools.contains_rect(*rect)).expect("the canvas shows"); + assert!(tools.bottom() < canvas.bottom() - 10.0, "the column ends below its content, above the dock's foot: {tools:?}, canvas {canvas:?}"); + + let steps = |workspace: &Workspace| match &workspace.layout.root { + Some(Node::Split { first, .. }) => match first.as_ref() { + Node::Dock { columns, .. } => columns[0].steps, + other => panic!("{other:?}"), + }, + other => panic!("{other:?}"), + }; + for expected in [2, 1] { + let tree = fixture.tree(); + let arrow = fixture + .by_role(Role::BUTTON) + .into_iter() + .find(|button| craft_ui::testing::access::name(tree, button.node) == Some("Collapse column to icons")) + .expect("the column bar shows its double arrow"); + fixture.click(&arrow).settle(); + assert_eq!(steps(&workspace.borrow()), Some(expected), "the double arrow switches the steps"); + } +}