diff --git a/crates/ui-panels/src/docking/config.rs b/crates/ui-panels/src/docking/config.rs index 26ecfb20d..9932a8262 100644 --- a/crates/ui-panels/src/docking/config.rs +++ b/crates/ui-panels/src/docking/config.rs @@ -80,6 +80,12 @@ pub struct DockConfig { /// sides ([`FloatingGroup::rounded_body`](super::FloatingGroup)): a floating tool rail reads as one rounded piece. /// Off: the panels and rails inside paint their own square surfaces to the window's edge. pub floating_rounded_body: bool, + /// A docked column of a tool rail (a stepped column whose panel has a [`DockGrid::rail`](super::DockGrid::rail)) + /// shows as a card: inset by `space.gap.stack` on its outer, top and bottom sides, where the dock's background + /// shows, and painted `surface.panel` with `shape.radius.surface` corners and no frame. The column is wider by the + /// inset, so it keeps its steps; its dividers, drop bands and edge handle stay on its outer rect. Off: the column + /// fills its cell. + pub rail_cards: bool, /// Shows a close button on the tabs of closable panels in docked and floating groups, where a middle click also /// closes them, for tabs that act as documents. Off: panels close from their menu. Accordion headers and iconic /// columns never show it. @@ -115,6 +121,7 @@ impl Default for DockConfig { close_button: true, floating_grip: false, floating_rounded_body: false, + rail_cards: false, tab_close_buttons: false, titled_headers: false, wheel_selects_tabs: false, @@ -217,6 +224,13 @@ impl DockConfig { self } + /// Docked tool rails as cards on or off ([`rail_cards`](Self::rail_cards)). + #[must_use] + pub fn rail_cards(mut self, on: bool) -> Self { + self.rail_cards = on; + self + } + /// Close buttons on tabs on or off. #[must_use] pub fn tab_close_buttons(mut self, on: bool) -> Self { diff --git a/crates/ui-panels/src/docking/dock_column.rs b/crates/ui-panels/src/docking/dock_column.rs index 4e487eefd..70511455e 100644 --- a/crates/ui-panels/src/docking/dock_column.rs +++ b/crates/ui-panels/src/docking/dock_column.rs @@ -14,7 +14,7 @@ use crate::contract::{ use crate::menu::GroupItem; use crate::runtime::env::Label; use crate::runtime::focus::KeyUse; -use crate::style::{ComponentStyle, Corners, StyleCx, SurfaceKind}; +use crate::style::{ComponentStyle, Corners, StyleCx, SurfaceEdge, SurfaceKind, SurfaceStyle, UiTokens}; use craft_behavior::divider::{self as push, Segment}; use craft_behavior::dock::{Column, DockEdge, Group}; use craft_behavior::{Axis, DockSide, Flow, Metrics, PartKey, Sense, Value}; @@ -156,6 +156,9 @@ pub struct DockColumnStyle { #[style(metrics)] metrics: Metrics, sizes: DockSizes, + /// The card of a column shown as one ([`card`](DockColumn::card)): `surface.panel` with `shape.radius.surface` + /// corners and no edge. + card: SurfaceStyle, } /// One column of a side dock: the column bar over the stacked groups of `column`. Show it with @@ -195,6 +198,10 @@ pub struct DockColumn<'a, P: Value + Hash + 'static> { /// The rect the dock may grow within, which bounds how wide its inner edge can make the column. #[prop] within: Rect, + /// Whether a column of a tool rail shows as a card ([`DockConfig::rail_cards`](super::DockConfig::rail_cards)): + /// its bar and groups inset by the card's inset ([`DockSizes::card_inset`]) on its outer, top and bottom sides. + #[prop] + card: bool, /// The plan `parts` laid out this pass, which update, paint and access read. laid: Option, resized: Vec<(P, f32)>, @@ -218,6 +225,7 @@ impl<'a, P: Value + Hash + Debug + 'static> DockColumn<'a, P> { bar: ColumnBar::Own, arrow: ColumnArrow::Collapse, within: Rect::NOTHING, + card: false, laid: None, resized: Vec::new(), width: None, @@ -244,15 +252,22 @@ impl<'a, P: Value + Hash + Debug + 'static> DockColumn<'a, P> { at: Option, mut view: impl FnMut(&Group

, Rect, &mut Ui, &mut BodyCx<'_>) + 'a, ) -> DockColumnOutput

{ - let (column, spec, auto_collapse, edge, own, arrow) = (self.column, self.spec, self.auto_collapse, self.edge, self.bar, self.arrow); + let (column, spec, auto_collapse, edge, own, arrow, card) = + (self.column, self.spec, self.auto_collapse, self.edge, self.bar, self.arrow, self.card); let mut bar = BarRecord::default(); let shown = &mut bar; let body = move |ui: &mut Ui, body: &mut BodyCx<'_>| { - let Some(sizes) = body.style::>().map(|style| style.sizes) else { return }; + let Some(style) = body.style::>() else { return }; + let sizes = DockSizes { rail_cards: card, ..style.sizes }; + let inner = card_rect(column, spec, body.rect, edge, body.env.direction().is_rtl(), &sizes); + if inner != body.rect { + // The groups' own surfaces keep to the card's rounded corners. + crate::primitives::frame_surfaces(ui, inner, style.card.radius); + } // The bar and the groups at the heights this pass's drag leaves them, in the measuring run and the real // run alike: the same plan `parts` lays out, so the dividers sit at the seams. let drags = |key: u64, divider: &Divider| divider::drag(body.part_press(DIVIDER.at(key)).phase, body.rect, false, divider); - let plan = column_plan(column, spec, body.rect, own, &sizes, drags); + let plan = column_plan(column, spec, inner, own, &sizes, drags); if own == ColumnBar::Own { let band = bar_band(ui, plan.bar, edge); *shown = show_bar(ui, band, true, auto_collapse, arrow, false, column_grip(column, spec, body)); @@ -286,11 +301,30 @@ impl<'a, P: Value + Hash + Debug + 'static> DockColumn<'a, P> { } } + /// The sizes of `style` with the column's card on or off. + fn sizes(&self, style: &DockColumnStyle) -> DockSizes { + DockSizes { rail_cards: self.card, ..style.sizes } + } + + /// Where the column's bar and groups sit in `rect`: inside its card, if it shows as one. + fn inner(&self, rect: Rect, rtl: bool, sizes: &DockSizes) -> Rect { + card_rect(self.column, self.spec, rect, self.edge, rtl, sizes) + } + fn anchor(&self, group: usize) -> Option<&P> { self.column.groups.get(group).and_then(|group| group.panels.first()) } } +/// The card of `column` in `rect`, the column's rect in a dock on the window edge `edge`: `rect` inset by the card's +/// inset ([`DockSizes::card_inset`]) on its outer side, the one towards `edge`, and on its top and bottom; `rect` for a +/// column that is no card. +fn card_rect

(column: &Column

, spec: &dyn Fn(&P) -> PanelSpec, rect: Rect, edge: DockEdge, rtl: bool, sizes: &DockSizes) -> Rect { + let inset = sizes.card_inset(column, spec); + let card = rect.shrink2(egui::vec2(0.0, inset)); + if (edge == DockEdge::Start) != rtl { card.with_min_x(card.left() + inset) } else { card.with_max_x(card.right() - inset) } +} + /// Where the groups of `column` sit in `rect`, the column's rect with its bar `bar`, as the column lays them out while /// no divider between them is dragged. pub fn group_rects

(column: &Column

, spec: &dyn Fn(&P) -> PanelSpec, rect: Rect, bar: ColumnBar, sizes: &DockSizes) -> Vec { @@ -390,7 +424,13 @@ impl Component for DockColumn<'_, P> { } fn style(cx: &StyleCx<'_>, _: DockColumnStyleKey

) -> DockColumnStyle { - DockColumnStyle { metrics: Metrics::new(), sizes: DockSizes::style(cx) } + let card = SurfaceStyle { + radius: UiTokens::corner(cx.tokens.shape.radius.surface), + edge: SurfaceEdge::None, + shadow: None, + ..cx.surface(SurfaceKind::Panel) + }; + DockColumnStyle { metrics: Metrics::new(), sizes: DockSizes::style(cx), card } } fn sense(&self) -> Sense { @@ -400,8 +440,8 @@ impl Component for DockColumn<'_, P> { /// The chevron at the bar's outer end, a divider between every two open groups and the inner edge's divider; a /// column in a floating window has the group dividers only. fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) { - let sizes = cx.style.sizes; - let plan = column_plan(self.column, self.spec, rect, self.bar, &sizes, |_, _| None); + let sizes = self.sizes(cx.style); + let plan = column_plan(self.column, self.spec, self.inner(rect, cx.flow.is_rtl(), &sizes), self.bar, &sizes, |_, _| None); if self.bar == ColumnBar::Own && self.arrow.shown() { let (chevron, _) = bar_slots(plan.bar, self.edge, cx.flow.is_rtl()); let name = self.arrow.name(&cx.words::(), true); @@ -418,12 +458,13 @@ impl Component for DockColumn<'_, P> { } fn update(&mut self, cx: &mut Cx<'_, Self>) { - let sizes = cx.style.sizes; + let sizes = self.sizes(cx.style); let (rect, rtl, ctx) = (cx.rect, cx.flow.is_rtl(), cx.core.ctx.clone()); let own = self.bar == ColumnBar::Own; self.collapse = own && self.arrow.shown() && cx.part_activated(CHEVRON).is_some(); let Some(base) = self.laid.take() else { return }; - let mut plan = column_plan(self.column, self.spec, rect, self.bar, &sizes, |key, divider| divider::pointer(cx, DIVIDER, key, divider)); + let inner = self.inner(rect, rtl, &sizes); + let mut plan = column_plan(self.column, self.spec, inner, self.bar, &sizes, |key, divider| divider::pointer(cx, DIVIDER, key, divider)); let dividers = base.dividers(rect, sizes.reach); let keyed: Vec<(u64, Divider)> = dividers.iter().map(|(key, _, divider)| (*key, *divider)).collect(); if let Some((key, coord)) = divider::keys(cx, DIVIDER, &keyed, sizes.step, sizes.jump) @@ -455,9 +496,19 @@ impl Component for DockColumn<'_, P> { fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { let Some(plan) = self.laid.as_ref() else { return }; // Painted here, not as the container's surface, which lies over what `paint` draws. - ui.draw().surface(cx.rect, SurfaceKind::Panel, Corners::All); + let card = self.inner(cx.rect, cx.flow.is_rtl(), &self.sizes(cx.style)); + let carded = card != cx.rect; + if carded { + // The card on the dock's background; the bar is part of the card. + ui.draw().surface(cx.rect, SurfaceKind::Background, Corners::None); + ui.draw().surface_styled(card, &cx.style.card); + } else { + ui.draw().surface(cx.rect, SurfaceKind::Panel, Corners::All); + } if self.bar == ColumnBar::Own { - ui.draw().surface(plan.bar, SurfaceKind::Background, Corners::None); + if !carded { + ui.draw().surface(plan.bar, SurfaceKind::Background, Corners::None); + } if self.arrow.shown() { paint_chevron(ui, self.edge, self.arrow, true, cx.flow.is_rtl(), &cx.words::()); } @@ -473,19 +524,22 @@ impl Component for DockColumn<'_, P> { } return; } - ui.draw().surface(cx.rect, SurfaceKind::Frame, Corners::None); + // A card has no frame. + if self.inner(cx.rect, cx.flow.is_rtl(), &self.sizes(cx.style)) == cx.rect { + ui.draw().surface(cx.rect, SurfaceKind::Frame, Corners::None); + } for pair in plan.open.windows(2) { ui.face(DIVIDER.at(pair[0] as u64), Splitter::new(Axis::Y)); } // The edge's line lies on the column's inner edge, over the frame; its handle stays inside the column. - let (rect, edge) = (cx.rect, self.edge_divider(cx.rect, &cx.style.sizes)); + let (rect, edge) = (cx.rect, self.edge_divider(cx.rect, &self.sizes(cx.style))); let x = if cx.flow.is_rtl() { rect.right() - (edge.at - rect.left()) } else { edge.at }; ui.face(EDGE.at(0), Splitter::new(Axis::X).boundary(x)); } fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) { let words = cx.words::(); - let sizes = cx.style.sizes; + let sizes = self.sizes(cx.style); let Some(plan) = self.laid.as_ref() else { return }; for (key, _, divider) in plan.dividers(cx.rect, sizes.reach) { divider::describe(access, DIVIDER, key, ÷r, words.resize.as_str(), sizes.step); diff --git a/crates/ui-panels/src/docking/dock_column/stories.rs b/crates/ui-panels/src/docking/dock_column/stories.rs index 9e7409a50..98b42d0ae 100644 --- a/crates/ui-panels/src/docking/dock_column/stories.rs +++ b/crates/ui-panels/src/docking/dock_column/stories.rs @@ -1,12 +1,19 @@ //! Stories of `DockColumn`. -use crate::contract::ShowContainer as _; -use crate::docking::{ColumnBar, DockColumn, PanelSpec}; +use crate::contract::{ShowContainer as _, Tracked}; +use crate::docking::{ColumnBar, DockColumn, DockGrid, DockSizes, PanelSpec}; +use crate::rails::tool::stories::{BRUSH, tools}; +use crate::rails::{LastUsed, RailDensity, ToolRail, ToolSlot}; +use crate::runtime::env::Env; use crate::sections::Panel; use crate::testing::memory::global; use crate::testing::{Drive, StoryData, StoryOutput, story}; +use craft_behavior::Flow; use craft_behavior::dock::{Column, Group}; -use egui::Ui; +use egui::{Ui, UiBuilder}; + +/// The height of the rail card story's column. +const CARD_HEIGHT: f32 = 160.0; fn spec(panel: &u8) -> PanelSpec { PanelSpec::new(format!("Panel {panel}")) @@ -36,3 +43,29 @@ fn in_a_floating_window(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { }) .into() } + +/// A docked column of a tool rail shown as a card (`DockConfig::rail_cards`), at each rail density and one or two +/// steps: inset on its outer, top and bottom sides, where the dock's background shows, with its bar and its rail on +/// the rounded panel card. The column is wider than its steps by the inset. +#[story(DockColumn<'static, u8>, covers = [], drive = Drive::Never)] +fn a_tool_rail_card(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let density = *data.choice("density", RailDensity::Dense, &[("Regular", RailDensity::Regular), ("Dense", RailDensity::Dense)]); + let steps = *data.ranged("steps", 1_u16, 1.0..=2.0); + let slots = Tracked::new((0..4_u8).map(|slot| ToolSlot::new(u32::from(slot), tools(&[slot]))).collect::>()); + let last = Tracked::new(LastUsed::default()); + let cells = slots.len(); + let spec = move |_: &u8| PanelSpec::new("Tools").grid(DockGrid::rail(cells).density(density)); + let column = Column::new(vec![Group::new(vec![0], 0)]).with_steps(steps); + let sizes = DockSizes { rail_cards: true, ..DockSizes::of_scope(ui) }; + let width = sizes.natural(std::slice::from_ref(&column), &spec).first().map_or(0.0, |(width, _)| *width); + let room = Flow::new(ui.available_rect_before_wrap(), Env::of(ui).direction.is_rtl()).span(0.0, width); + let rect = room.with_max_y(room.top() + CARD_HEIGHT); + // The column takes the cell a dock gives it, as high as the dock, whatever its rail's height. + let shown = + DockColumn::new("Panels", global("dock column", "rail card story"), &column, &spec).card(true).show_groups_at(ui, rect, |_, rect, ui, _| { + let rail = ToolRail::new("Tools", &slots, &BRUSH, &last).density(density); + ui.scope_builder(UiBuilder::new().max_rect(rect), |ui| rail.show_rail(ui, |_, _| {})); + }); + ui.advance_cursor_after_rect(rect); + shown.into() +} diff --git a/crates/ui-panels/src/docking/spec.rs b/crates/ui-panels/src/docking/spec.rs index 2fed17f8f..dbdea4370 100644 --- a/crates/ui-panels/src/docking/spec.rs +++ b/crates/ui-panels/src/docking/spec.rs @@ -107,6 +107,11 @@ impl DockGrid { } } + /// Whether the grid is a [`rail`](Self::rail) grid, before it is [`resolved`](Self::resolved). + pub(crate) fn is_rail(self) -> bool { + matches!(self.widths, GridWidths::Rail(_)) + } + /// The content's width at one column and the width each further column adds; zero for a rail grid not yet /// [`resolved`](Self::resolved). fn first_step(self) -> (f32, f32) { diff --git a/crates/ui-panels/src/docking/targets.rs b/crates/ui-panels/src/docking/targets.rs index 7aac54e64..bd29d87c0 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::{DockGrid, DragUnit, PanelKey, PanelSpec, scale_widths, width_bounds}; use crate::contract::DropMark; use crate::rails::RailDensity; use crate::runtime::frame::{self, PassStep, PerViewport, Stamped, Stored}; @@ -86,6 +86,12 @@ pub struct DockSizes { pub dense_rail_first: f32, /// [`rail_step`](Self::rail_step) with the buttons of a dense rail ([`RailDensity::Dense`]). pub dense_rail_step: f32, + /// The inset of a docked tool rail's column shown as a card ([`rail_cards`](Self::rail_cards)) on its outer, top + /// and bottom sides: `space.gap.stack`. + pub rail_card: f32, + /// Whether the dock shows its docked tool rails as cards ([`DockConfig::rail_cards`](super::DockConfig::rail_cards)): + /// off in the style, set by the dock that shows them. + pub rail_cards: bool, /// The height of a grip strip, the band with the dock's grip marks: above each icon group of a collapsed column, /// and under the column bar of a tool rail (a group whose panel has a [`DockGrid`](super::DockGrid)) that the host /// shows itself: the least hit size, or the theme's grip marks with `space.gap.tight` above and below them. @@ -141,6 +147,8 @@ impl DockSizes { rail_step, dense_rail_first, dense_rail_step, + rail_card: cx.tokens.space.gap.stack, + rail_cards: false, grip: super::group::grip_strip(cx.tokens), } } @@ -188,6 +196,23 @@ impl DockSizes { column.width.unwrap_or(self.column).clamp(lo, hi) } + /// The inset of `column`'s card: [`rail_card`](Self::rail_card) for an expanded stepped column of a tool rail + /// ([`DockGrid::rail`](super::DockGrid::rail)) in a dock that shows [`rail_cards`](Self::rail_cards), else none. + /// A docked column is that much wider than its steps. + pub fn card_inset

(&self, column: &Column

, spec: &dyn Fn(&P) -> PanelSpec) -> f32 { + let rail = || { + column.steps.is_some() + && column.panels().find_map(|panel| spec(panel).grid.filter(|grid| grid.resolved(self).valid())).is_some_and(DockGrid::is_rail) + }; + if self.rail_cards && !column.iconic && rail() { self.rail_card } else { 0.0 } + } + + /// The width of a docked column before the dock's room shrinks it: its [`width`](Self::width) and its card's + /// inset. + fn docked_width

(&self, column: &Column

, spec: &dyn Fn(&P) -> PanelSpec) -> f32 { + self.width(column, spec) + self.card_inset(column, spec) + } + /// The width bounds of a column collapsed to icons, docked or floating: the rail width up to the width it expands /// to. A docked column's inner edge and a floating window's sides resize it within them. pub fn iconic_bounds

(&self, column: &Column

, spec: &dyn Fn(&P) -> PanelSpec) -> (f32, f32) { @@ -201,7 +226,8 @@ impl DockSizes { if column.iconic { self.iconic_bounds(column, spec) } else if let Some((grid, _)) = self.stepped(column, spec) { - (grid.width(1), grid.width(grid.most)) + let inset = self.card_inset(column, spec); + (grid.width(1) + inset, grid.width(grid.most) + inset) } else { self.width_bounds(column, spec) } @@ -241,7 +267,7 @@ impl DockSizes { let current: Vec<(f32, (f32, f32))> = columns .iter() .map(|column| { - let width = self.width(column, spec); + let width = self.docked_width(column, spec); (width, if fixed(column) { (width, width) } else { self.shown_bounds(column, spec) }) }) .collect(); @@ -264,7 +290,7 @@ impl DockSizes { .iter() .map(|column| { let keeps = column.iconic || self.stepped(column, spec).is_some(); - (self.width(column, spec), (!keeps).then(|| self.width_bounds(column, spec).0.max(1.0))) + (self.docked_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 2ebfa1692..7b990f63e 100644 --- a/crates/ui-patterns/src/patterns/dock/area.rs +++ b/crates/ui-patterns/src/patterns/dock/area.rs @@ -254,7 +254,7 @@ impl Drops<'_, P> { /// Takes a group or tab drag released on the root. fn take(&self, cx: &mut Cx<'_, DockRoot<'_, DockDropped

>>) -> Option> { let ctx = cx.core.ctx.clone(); - let sizes = *cx.style.sizes(); + let sizes = area_sizes(*cx.style.sizes(), self.config); let shell = cx.env.get::().0; let bounds = inset_top(shell.as_ref().map_or(cx.rect, ShellCx::float_bounds), self.top_inset); let space = float_space(&ctx, bounds, &sizes, self.config, self.specs); @@ -271,6 +271,13 @@ impl Drops<'_, P> { } } +/// The sizes a dock area lays out with: the dock's style, with its docked tool rails as cards when `config` shows +/// them ([`DockConfig::rail_cards`]). +fn area_sizes(mut sizes: DockSizes, config: &DockConfig) -> DockSizes { + sizes.rail_cards = config.rail_cards; + sizes +} + /// What the area collects while it shows its parts. struct Report

{ actions: Vec>, @@ -526,7 +533,7 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { let mut passed = None; open_requests::

(&ctx); let shown = root.show(ui, |ui, body| { - let Some(sizes) = body.style::>>().map(|style| *style.sizes()) else { return }; + let Some(sizes) = body.style::>>().map(|style| area_sizes(*style.sizes(), &config)) else { return }; let memory = body.memory::>(); memory.window_floor = memory.window_floor.max(current.layout().window_floor()); let input = Input { @@ -1390,7 +1397,8 @@ impl Pass<'_, P> { .bar(bar) .arrow(self.arrow_of(column)) .within(within) - .auto_collapse(scene.auto_collapse); + .auto_collapse(scene.auto_collapse) + .card(scene.sizes.rail_cards && !in_window); let output = shown.show_groups_at(ui, cell, |group, rect, ui, body| { self.group_of(ui, body, group, GroupAt::Show(rect), in_window); }); @@ -1421,9 +1429,10 @@ impl Pass<'_, P> { let spec = |panel: &P| scene.spec(panel); if let Some(width) = work.width { match scene.sizes.stepped(column, &spec) { - // The edge of a stepped column moves in whole steps: the most that fit where it is dragged. + // The edge of a stepped column moves in whole steps: the most that fit where it is dragged, inside its + // card. Some((grid, steps)) if !column.iconic => { - let to = grid.steps_within(width); + let to = grid.steps_within(width - scene.sizes.card_inset(column, &spec)); if to != steps { self.report.actions.push(Action::set_column_steps(first.clone(), to)); } diff --git a/crates/ui-patterns/tests/it/docking/columns.rs b/crates/ui-patterns/tests/it/docking/columns.rs index fdfc36ace..e274aefde 100644 --- a/crates/ui-patterns/tests/it/docking/columns.rs +++ b/crates/ui-patterns/tests/it/docking/columns.rs @@ -4,11 +4,14 @@ use craft_behavior::Axis; use craft_behavior::dock::{Action, Column, DockEdge, Floating, Group, Layout, Node, Permissions, SplitSize, Workspace, node_at}; use craft_ui::contract::{Bind, BodyCx, PlainData, Role, Show as _, SizeLimits, Tracked}; use craft_ui::display::Text; -use craft_ui::docking::{DockConfig, DockGrid, DockHeights, DockPanels, DockSizes, GroupSize, IconicMode, PanelSpec, PanelSpecs, group_heights}; +use craft_ui::docking::{ + DockConfig, DockGrid, DockHeights, DockPanels, DockSizes, GroupAt, GroupSize, IconicMode, PanelSpec, PanelSpecs, group_heights, +}; use craft_ui::patterns::dock::DockArea; +use craft_ui::runtime::env::Direction; use craft_ui::testing::{Fixture, pointer, trial}; use egui::{Pos2, Rect, Ui, Vec2}; -use std::cell::RefCell; +use std::cell::{Cell, RefCell}; use std::rc::Rc; use std::sync::Arc; @@ -470,3 +473,85 @@ fn a_menu_opens_from_the_rail_footer() { fixture.frame(vec![pointer::moved(at), pointer::press(at), pointer::release(at)]).settle(); assert!(fixture.by_label("Rail option").is_some(), "a same-frame click opens the menu"); } + +/// A toolbar of six rail cells beside a side panel. +#[derive(PlainData)] +struct RailTools; + +impl PanelSpecs for RailTools { + fn spec(&self, panel: &u32) -> PanelSpec { + if *panel == TOOLS { PanelSpec::new("Tools").grid(DockGrid::rail(6)) } else { PanelSpec::new("Side") } + } + + fn permissions(&self, panel: &u32) -> Permissions { + if *panel == TOOLS { Permissions::ALL.collapses(false).shares_column(false) } else { Permissions::ALL } + } +} + +/// Panels that record the rect the toolbar's group gets, the rail's stage. +struct RailStage(Rc>>); + +impl DockPanels for RailStage { + fn body(&mut self, _: &u32, _: &mut Ui, _: &mut BodyCx<'_>) {} + + fn group_view(&mut self, panels: &[u32], _: usize, _: &mut Ui, at: GroupAt, _: &mut BodyCx<'_>) -> Option { + if !panels.contains(&TOOLS) { + return None; + } + Some(match at { + GroupAt::Measure(_) => 0.0, + GroupAt::Show(rect) => { + self.0.set(Some(rect)); + rect.height() + } + }) + } +} + +/// A start dock in `direction` of a toolbar column of two rail steps, at the window edge, and a side column beside it, +/// with `DockConfig::rail_cards` set to `cards`: the toolbar's cell, the side column's cell and the rail's stage. +fn rail_dock(cards: bool, direction: Direction) -> (Rect, Rect, Rect) { + let columns = vec![Column::new(vec![Group::new(vec![TOOLS], 0)]).with_steps(2), Column::new(vec![Group::new(vec![2], 0)]).with_width(150.0)]; + let root = Node::split(Axis::X, SplitSize::FixedFirst(300.0), Node::dock(DockEdge::Start, columns), Node::tabs(vec![1], 0)); + let workspace = Rc::new(RefCell::new(Arc::new(Workspace::new(Layout::new(Some(root), Vec::new()))))); + let placed: Rc>> = Rc::default(); + let stage: Rc>> = Rc::default(); + let (held, seen, shown, specs) = (Rc::clone(&workspace), Rc::clone(&placed), Rc::clone(&stage), Tracked::new(RailTools)); + let mut fixture = Fixture::new(move |ui| { + let mut workspace = held.borrow().clone(); + let config = DockConfig::default().rail_cards(cards); + let out = DockArea::new("Rail dock", Bind::bound(&mut workspace), &specs, &mut RailStage(Rc::clone(&shown)), config).show(ui); + *held.borrow_mut() = workspace; + *seen.borrow_mut() = out.columns; + }); + fixture.direction(direction).room(Vec2::new(800.0, 500.0)).settle(); + let cell = |panel: u32| placed.borrow().iter().find(|(at, _)| *at == panel).map(|(_, rect)| *rect).expect("the column shows"); + (cell(TOOLS), cell(2), stage.get().expect("the rail's group shows")) +} + +/// A docked tool rail shown as a card is inset alike on its outer side, the window edge's, and on its top and bottom, +/// its bar and grip on the card above the rail as without one, and flush with its inner edge; the column beside it starts past its cell; and its +/// rail gets the same width as without the card, so it shows the same steps, its column wider by the inset. Right to +/// left mirrors the outer side. +#[trial] +fn a_rail_card_is_inset_alike_on_three_sides_and_keeps_its_steps() { + for direction in [Direction::Ltr, Direction::Rtl] { + let rtl = direction == Direction::Rtl; + let (plain_cell, _, plain) = rail_dock(false, direction); + let (cell, side, card) = rail_dock(true, direction); + let (outer, inner) = + if rtl { (cell.right() - card.right(), card.left() - cell.left()) } else { (card.left() - cell.left(), cell.right() - card.right()) }; + let top = (card.top() - cell.top()) - (plain.top() - plain_cell.top()); + let bottom = (cell.bottom() - card.bottom()) - (plain_cell.bottom() - plain.bottom()); + assert!(outer > 0.5, "{direction:?}: the card is inset from the window edge: {card:?} in {cell:?}"); + assert!((top - outer).abs() <= 0.5 && (bottom - outer).abs() <= 0.5, "{direction:?}: insets outer {outer}, top {top}, bottom {bottom}"); + assert!(inner.abs() <= 0.5, "{direction:?}: the card keeps to the inner edge: {card:?} in {cell:?}"); + let clear = if rtl { side.right() <= cell.left() + 0.5 } else { side.left() >= cell.right() - 0.5 }; + assert!(clear, "{direction:?}: the side column {side:?} overlaps the toolbar {cell:?}"); + assert!((card.width() - plain.width()).abs() <= 0.5, "{direction:?}: the rail's stage {card:?} differs from {plain:?} without the card"); + assert!( + (cell.width() - plain_cell.width() - outer).abs() <= 0.5, + "{direction:?}: the column {cell:?} grows by the inset from {plain_cell:?}" + ); + } +}