diff --git a/crates/ui-panels/src/docking/dock_column.rs b/crates/ui-panels/src/docking/dock_column.rs index b44039f5f..2e5b9558a 100644 --- a/crates/ui-panels/src/docking/dock_column.rs +++ b/crates/ui-panels/src/docking/dock_column.rs @@ -37,6 +37,30 @@ pub use sizing::{GroupSize, fit_widths, group_heights, scale_widths, stacked, wi const DIVIDER: PartKey = PartKey::new("divider"); const EDGE: PartKey = PartKey::new("edge"); +/// Where a column's bar is. A dock column and a floating window are both one column; the window's title row is the +/// bar of the column it holds. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[non_exhaustive] +pub enum ColumnBar { + /// The column shows its own bar (the double arrow and the band that moves the column and opens its menu), the + /// divider of its inner edge, its frame and the drop bands of its two edges: a column of a side dock. + #[default] + Own, + /// The column fills a floating window, whose frame, edges and title row carry what `Own` shows: the column shows + /// no bar, no edge divider and no frame, and registers no edge bands, since nothing goes beside a floating column. + Window, +} + +impl ColumnBar { + /// The height of the column's bar: the dock's bar height for `Own`, none for `Window`. + pub(super) fn height(self, sizes: &DockSizes) -> f32 { + match self { + ColumnBar::Own => sizes.bar, + ColumnBar::Window => 0.0, + } + } +} + /// The caller's view of one group: the group, the rect it takes and the body step's context. The dock passes the /// group to `DockPanels::group_view` or shows a `DockGroup` in it. pub type GroupView<'a, P> = Box, Rect, &mut Ui, &mut BodyCx<'_>) + 'a>; @@ -85,6 +109,9 @@ pub struct DockColumnStyle { /// /// The column registers the drop bands of its two edges (`DockSpot::column`) and of the seams between its groups /// (`DockSpot::seam`) with the dock `dock` every pass. A dock hidden by the shell shows nothing. +/// +/// In a floating window ([`ColumnBar::Window`]) the window's title row is the column's bar and the window's edges +/// size it: the column shows its groups and their dividers only, and registers its seams. #[derive(crate::contract::Component, crate::contract::Props)] #[component(container, probe(DockColumn<'static, u8>))] pub struct DockColumn<'a, P: Value + Hash + 'static> { @@ -101,6 +128,9 @@ pub struct DockColumn<'a, P: Value + Hash + 'static> { /// The window edge the column's dock sits on; the column's inner edge faces away from it. #[prop] edge: DockEdge, + /// Where the column's bar is: its own (a dock column) or its floating window's title row. + #[prop] + bar: ColumnBar, /// The rect the dock may grow within, which bounds how wide its inner edge can make the column. #[prop] within: Rect, @@ -124,6 +154,7 @@ impl<'a, P: Value + Hash + 'static> DockColumn<'a, P> { spec, auto_collapse: false, edge: DockEdge::Start, + bar: ColumnBar::Own, within: Rect::NOTHING, laid: None, resized: Vec::new(), @@ -151,19 +182,22 @@ impl<'a, P: Value + Hash + 'static> DockColumn<'a, P> { at: Option, mut view: impl FnMut(&Group

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

{ - let (column, spec, auto_collapse, edge) = (self.column, self.spec, self.auto_collapse, self.edge); + let (column, spec, auto_collapse, edge, own) = (self.column, self.spec, self.auto_collapse, self.edge, self.bar); let mut bar = BarRecord::default(); let shown = &mut bar; let body = move |ui: &mut Ui, body: &mut BodyCx<'_>| { - if body.shell().is_some_and(|shell| !shell.dock_visible()) { + // A floating window shows where the area shows it; the shell hides docks only. + if own == ColumnBar::Own && body.shell().is_some_and(|shell| !shell.dock_visible()) { return; } let Some(sizes) = body.style::>().map(|style| style.sizes) else { return }; // 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, &sizes, drags); - if let Some(grip) = column_grip(column, spec, body) { + let plan = column_plan(column, spec, body.rect, own, &sizes, drags); + if own == ColumnBar::Own + && let Some(grip) = column_grip(column, spec, body) + { *shown = show_bar(ui, body, plan.bar, true, auto_collapse, edge, grip); } for (group, rect) in column.groups.iter().zip(&plan.groups) { @@ -200,15 +234,17 @@ impl<'a, P: Value + Hash + 'static> DockColumn<'a, P> { } /// The bar and body of the column in `rect`, and the groups stacked in the body, with the drags `drags` makes of -/// the dividers between open groups (the divider's part index and the divider as laid out before the drag). +/// the dividers between open groups (the divider's part index and the divider as laid out before the drag). A +/// column in a floating window has an empty bar on top. fn column_plan( column: &Column

, spec: &dyn Fn(&P) -> PanelSpec, rect: Rect, + own: ColumnBar, sizes: &DockSizes, drags: impl Fn(u64, &Divider) -> Option, ) -> Plan { - let bar = Rect::from_min_max(rect.min, Pos2::new(rect.right(), (rect.top() + sizes.bar).min(rect.bottom()))); + let bar = Rect::from_min_max(rect.min, Pos2::new(rect.right(), (rect.top() + own.height(sizes)).min(rect.bottom()))); let body = Rect::from_min_max(Pos2::new(rect.left(), bar.bottom()), rect.max); body_plan(column, spec, bar, body, sizes, drags) } @@ -297,27 +333,33 @@ impl Component for DockColumn<'_, P> { Sense::empty() } - /// The chevron at the bar's outer end, and a divider between every two open groups. + /// 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, &sizes, |_, _| None); - let (chevron, _) = bar_slots(plan.bar, self.edge, cx.flow.is_rtl()); - let name = chevron_name(&cx.words::(), true); - let _ = cx.part(CHEVRON, chevron).sense(Sense::CLICK | Sense::FOCUS).focusable().tooltip(name); + let plan = column_plan(self.column, self.spec, rect, self.bar, &sizes, |_, _| None); + if self.bar == ColumnBar::Own { + let (chevron, _) = bar_slots(plan.bar, self.edge, cx.flow.is_rtl()); + let name = chevron_name(&cx.words::(), true); + let _ = cx.part(CHEVRON, chevron).sense(Sense::CLICK | Sense::FOCUS).focusable().tooltip(name); + } let dividers = plan.dividers(rect, sizes.reach); for (key, _, divider) in dividers { divider::declare(cx, rect, DIVIDER, key, ÷r); } - divider::declare(cx, rect, EDGE, 0, &self.edge_divider(rect, &sizes)); + if self.bar == ColumnBar::Own { + divider::declare(cx, rect, EDGE, 0, &self.edge_divider(rect, &sizes)); + } self.laid = Some(plan); } fn update(&mut self, cx: &mut Cx<'_, Self>) { let sizes = cx.style.sizes; let (rect, rtl, ctx) = (cx.rect, cx.flow.is_rtl(), cx.core.ctx.clone()); - self.collapse = cx.part_activated(CHEVRON).is_some(); + let own = self.bar == ColumnBar::Own; + self.collapse = own && cx.part_activated(CHEVRON).is_some(); let Some(base) = self.laid.take() else { return }; - let mut plan = column_plan(self.column, self.spec, rect, &sizes, |key, divider| divider::pointer(cx, DIVIDER, key, divider)); + let mut plan = column_plan(self.column, self.spec, rect, 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) @@ -333,12 +375,14 @@ impl Component for DockColumn<'_, P> { self.resized.push((anchor.clone(), plan.heights[group])); } } - let edge = self.edge_divider(rect, &sizes); - if let Some(Drag::To(coord)) = divider::pointer(cx, EDGE, 0, &edge) { - self.move_edge(rect, &edge, coord); - } - if let Some((_, coord)) = divider::keys(cx, EDGE, &[(0, edge)], sizes.step, sizes.jump) { - self.move_edge(rect, &edge, coord); + if own { + let edge = self.edge_divider(rect, &sizes); + if let Some(Drag::To(coord)) = divider::pointer(cx, EDGE, 0, &edge) { + self.move_edge(rect, &edge, coord); + } + if let Some((_, coord)) = divider::keys(cx, EDGE, &[(0, edge)], sizes.step, sizes.jump) { + self.move_edge(rect, &edge, coord); + } } self.register(&ctx, rect, rtl, &plan, &sizes); self.laid = Some(plan); @@ -348,13 +392,21 @@ impl Component for DockColumn<'_, P> { 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); - ui.draw().surface(plan.bar, SurfaceKind::Background, Corners::None); - paint_chevron(ui, self.edge, true, cx.flow.is_rtl(), chevron_name(&cx.words::(), true)); + if self.bar == ColumnBar::Own { + ui.draw().surface(plan.bar, SurfaceKind::Background, Corners::None); + paint_chevron(ui, self.edge, true, cx.flow.is_rtl(), chevron_name(&cx.words::(), true)); + } } - /// The column's frame, then the dividers over it. + /// The column's frame, then the dividers over it. The frame of a column in a floating window is the window's. fn paint_over(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { let Some(plan) = self.laid.as_ref() else { return }; + if self.bar == ColumnBar::Window { + for pair in plan.open.windows(2) { + ui.face(DIVIDER.at(pair[0] as u64), Splitter::new(Axis::Y)); + } + return; + } 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)); @@ -372,9 +424,11 @@ impl Component for DockColumn<'_, P> { for (key, _, divider) in plan.dividers(cx.rect, sizes.reach) { divider::describe(access, DIVIDER, key, ÷r, words.resize.as_str(), sizes.step); } - let edge = self.edge_divider(cx.rect, &sizes); - divider::describe(access, EDGE, 0, &edge, words.resize.as_str(), sizes.step); - describe_chevron(access, &chevron_name(&words, true)); + if self.bar == ColumnBar::Own { + let edge = self.edge_divider(cx.rect, &sizes); + divider::describe(access, EDGE, 0, &edge, words.resize.as_str(), sizes.step); + describe_chevron(access, &chevron_name(&words, true)); + } } fn finish(self, cx: Cx<'_, Self>) -> DockColumnOutput

{ @@ -413,10 +467,12 @@ pub(super) fn register_edges(ctx: &Context, dock: Id, } impl DockColumn<'_, P> { - /// Registers the drop bands of the column's two edges and of the seams between its groups. + /// Registers the drop bands of the column's two edges, for a dock column, and of the seams between its groups. fn register(&self, ctx: &Context, rect: Rect, rtl: bool, plan: &Plan, sizes: &DockSizes) { let Some(first) = self.anchor(0) else { return }; - register_edges(ctx, self.dock, first, rect, rtl, sizes); + if self.bar == ColumnBar::Own { + register_edges(ctx, self.dock, first, rect, rtl, sizes); + } let (mut before, half) = (first, sizes.seam_band * 0.5); for (at, group) in plan.groups.iter().enumerate().skip(1) { let Some(after) = self.anchor(at) else { continue }; diff --git a/crates/ui-panels/src/docking/floating.rs b/crates/ui-panels/src/docking/floating.rs index 3e819847a..9d753dc97 100644 --- a/crates/ui-panels/src/docking/floating.rs +++ b/crates/ui-panels/src/docking/floating.rs @@ -4,15 +4,18 @@ //! caller places it with the rect the dock model keeps and takes the rect the output reports after each step of //! a gesture. The title row carries the active panel's title and the Menu, Float and Close actions; the Menu action //! opens the `menu` rows. The body step supplies the move band, the edge grips and the keyboard Move and Size modes. +//! A dock window's title row is also the bar of the column the window holds, with the column's double arrow. +use super::dock_column::{CHEVRON, chevron_name, describe_chevron, paint_chevron}; use super::drop::{DragUnit, start_group_drag}; use super::{DockDropTarget, DockDropped, DockWords, GroupDrag, PanelDrag, PanelKey, native}; use crate::contract::BodyCx; use crate::contract::SizeLimits; use crate::contract::overlay::{PlacementChange, PlacementKind, Popup}; use crate::contract::{ - Bind, BindMut, Common, Component, Container, ContainerProps, Cx, DragBand, HeaderRegion, Inner, Mount, NoValue, OsFiles, Outcome, Overlay, Pad, - Pointer as _, Props as _, Role, Show as _, ShowContainer as _, Sizing, WordSetCx as _, + Access, Bind, BindMut, Common, Component, Container, ContainerProps, Cx, DragBand, HeaderEdge, HeaderParts, HeaderRegion, Inner, MeasureCx, Mount, + NoValue, OsFiles, Outcome, Overlay, Pad, PaintUi, PartsCx, Pointer as _, Props as _, Role, Show as _, ShowContainer as _, Sizing, + WordSetCx as _, }; use crate::runtime::drag::OsFile; use crate::runtime::env::{Env, Label}; @@ -20,9 +23,10 @@ use crate::runtime::overlay::CloseReason; use crate::runtime::windows::WindowSpec; use crate::sections::{HeaderAction, HeaderStrength, MenuRows, TitleRow}; use crate::style::{ComponentStyle, StyleCx, SurfaceKind}; +use craft_behavior::dock::DockEdge; use craft_behavior::{GesturePhase, Metrics, Sense}; use craft_ui_widgets::popover::WholeWindow; -use egui::{Id, Rect, Response, Ui, Vec2}; +use egui::{Id, Pos2, Rect, Response, Ui, Vec2}; use std::cell::RefCell; use std::hash::Hash; @@ -40,6 +44,36 @@ pub struct FloatingGroupStyle { metrics: Metrics, } +/// The column a dock's floating window holds. The window's title row is the column's bar: it shows the column's +/// double arrow at the end on `edge`, and a collapsed column shows against that edge of the window's rect. +#[derive(Clone, Copy, Debug, PartialEq)] +#[non_exhaustive] +pub struct WindowColumn { + /// The edge of the float area, in reading order, that the window keeps to: the one nearer the window. + pub edge: DockEdge, + /// The size of the collapsed column under the title row; `None` while the column is expanded. + pub collapsed: Option, +} + +impl WindowColumn { + /// An expanded column that keeps to `edge`. + pub fn expanded(edge: DockEdge) -> Self { + WindowColumn { edge, collapsed: 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) } + } + + /// The edge of `bounds`, in reading order, nearer the window at `rect`: the start edge when the window's centre is + /// in the half of the bounds there or `bounds` is empty. + pub fn nearer_edge(rect: Rect, bounds: Rect, rtl: bool) -> DockEdge { + let at_left = !bounds.is_positive() || rect.center().x - bounds.left() <= bounds.right() - rect.center().x; + if at_left != rtl { DockEdge::Start } else { DockEdge::End } + } +} + /// What a floating group reports in a pass. #[derive(Debug)] #[non_exhaustive] @@ -52,8 +86,12 @@ pub struct FloatingGroupOutput { /// The title row's action part taken this pass, or Close when the OS asked the group's window to close. Float and /// Close are the host's: the dock model docks or closes the group. Menu opens the `menu` rows. pub action: Option, - /// Whether a double click on the title band asks to minimize the group, or restore it when it is minimized. + /// Whether a double click on the title band asks to minimize the group, or restore it when it is minimized. A + /// group with a [`WindowColumn`] reports that click in `iconic` instead. pub minimized_toggled: bool, + /// The user asked to collapse the window's column to icons (`true`) or expand it (`false`): a click on the double + /// arrow, or a double click on the title band. Only a group with a [`WindowColumn`] reports it. + pub iconic: Option, /// The files dropped on the group's window that no target there took. pub files: Vec, } @@ -76,6 +114,9 @@ impl Outcome for FloatingGroupOutput { /// the body; with another sizing the rect is the size. The user's resizes are reported either way. /// - Minimized: only the title row shows, at the width of the rect, and the edges do not resize. The rect given to /// `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. /// - 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 @@ -103,6 +144,9 @@ pub struct FloatingGroup<'a> { /// Whether only the title row shows. #[prop] minimized: bool, + /// The column the window holds, whose bar the title row is. + #[prop] + column: Option, /// Whether the height follows the body (`Sizing::Content`) or the rect gives it. #[prop] sized: Sizing, @@ -118,6 +162,12 @@ pub struct FloatingGroup<'a> { taken: Option, menu_opener: Option, toggled: bool, + /// The double arrow was declared this pass, and whether it was clicked. + chevron_declared: bool, + chevron: bool, + /// The title row's height and the layout direction, read when the group shows. + header: f32, + rtl: bool, placed: Option, /// Starts the window-unit drag; set by `window_of`. start_drag: Option>, @@ -149,6 +199,7 @@ impl<'a> FloatingGroup<'a> { limits: SizeLimits::default(), opacity: 1.0, minimized: false, + column: None, sized: Sizing::Content, native: None, menu: None, @@ -157,6 +208,10 @@ impl<'a> FloatingGroup<'a> { taken: None, menu_opener: None, toggled: false, + chevron_declared: false, + chevron: false, + header: 0.0, + rtl: false, placed: None, start_drag: None, take_drop: None, @@ -203,7 +258,9 @@ impl<'a> FloatingGroup<'a> { /// Shows the group; `body` fills the area under the title row unless the group is minimized. pub fn show_group(mut self, ui: &mut Ui, mut body: impl FnMut(&mut Ui, &mut BodyCx<'_>) -> R) -> Inner { - let header = Env::of(ui).tokens.size.panel_header; + let env = Env::of(ui); + let header = env.tokens.size.panel_header; + (self.header, self.rtl) = (header, env.direction.is_rtl()); let mount = self.mount(header); self.props = self.props.mount(mount); let minimized = self.minimized; @@ -218,8 +275,12 @@ impl<'a> FloatingGroup<'a> { fn mount(&self, header: f32) -> Mount { // The frame's limits are the body's with the title row on top. let frame = self.limits.around(Vec2::new(0.0, header)); - let mut overlay = Overlay::floating().movable(true).resizable(!self.minimized).opacity(self.opacity).body_scroll(false); - overlay = if self.minimized { + let collapsed = self.collapsed_rect(); + let mut overlay = + 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 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))) } else if self.sized == Sizing::Content { @@ -236,12 +297,25 @@ impl<'a> FloatingGroup<'a> { None => Mount::Layer(overlay), } } + + /// Where the group shows while its column is collapsed: the collapsed column under the title row, at the top of + /// the rect and against its edge the window keeps to. `None` while the column is expanded. + fn collapsed_rect(&self) -> Option { + let column = self.column?; + let size = column.collapsed? + Vec2::new(0.0, self.header); + let at_left = (column.edge == DockEdge::Start) != self.rtl; + let left = if at_left { self.rect.left() } else { self.rect.right() - size.x }; + Some(Rect::from_min_size(Pos2::new(left, self.rect.top()), size)) + } + + /// The double arrow's square at the end of the title row the window keeps to. + fn chevron_edge(&self) -> Option { + self.column.map(|column| if column.edge == DockEdge::End { HeaderEdge::End } else { HeaderEdge::Start }) + } } impl Component for FloatingGroup<'_> { const ROLE: Role = Role::GROUP; - const PARTS: bool = false; - const STOPS: bool = false; type Style = FloatingGroupStyle; type Output = FloatingGroupOutput; @@ -253,7 +327,17 @@ impl Component for FloatingGroup<'_> { Sense::empty() } + /// The column's double arrow, in its square of the title row. + fn parts(&mut self, _: Rect, cx: &mut PartsCx<'_, Self>) { + let (Some(edge), Some(column)) = (self.chevron_edge(), self.column) else { return }; + let Some(slot) = cx.header_slot(edge) else { return }; + let name = chevron_name(&cx.words::(), column.collapsed.is_none()); + let _ = cx.header_part(edge, CHEVRON, slot).sense(Sense::CLICK | Sense::FOCUS).focusable().tooltip(name); + self.chevron_declared = true; + } + fn update(&mut self, cx: &mut Cx<'_, Self>) { + self.chevron = self.chevron_declared && cx.part_activated(CHEVRON).is_some(); if let Some(mut take) = self.take_drop.take() { take(cx); self.take_drop = Some(take); @@ -267,6 +351,19 @@ impl Component for FloatingGroup<'_> { } } + /// The double arrow, over the title row's surface. + fn paint_over(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { + let Some(column) = self.column.filter(|_| self.chevron_declared) else { return }; + let expanded = column.collapsed.is_none(); + paint_chevron(ui, column.edge, expanded, cx.flow.is_rtl(), chevron_name(&cx.words::(), expanded)); + } + + fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) { + if let Some(column) = self.column.filter(|_| self.chevron_declared) { + describe_chevron(access, &chevron_name(&cx.words::(), column.collapsed.is_none())); + } + } + fn finish(mut self, mut cx: Cx<'_, Self>) -> FloatingGroupOutput { if self.start_drag.is_some() { native::keeps_app_drag(&cx.core.ctx, cx.core.id); @@ -279,11 +376,15 @@ impl Component for FloatingGroup<'_> { if let Some(opener) = self.menu_opener { Popup::toggle(opener, &mut cx); } + // A dock window's band double click collapses or expands its column, as a dock column's bar does. + let collapsed = self.column.map(|column| column.collapsed.is_some()); + let iconic = collapsed.filter(|_| self.chevron || self.toggled).map(|collapsed| !collapsed); let output = FloatingGroupOutput { response: cx.response.clone(), placed: self.placed, action: self.taken, - minimized_toggled: self.toggled, + minimized_toggled: self.toggled && self.column.is_none(), + iconic, files: self.files, }; native::raise(&cx.core.ctx, &output); @@ -293,6 +394,15 @@ impl Component for FloatingGroup<'_> { } impl Container for FloatingGroup<'_> { + /// The double arrow's square at the end of the title row the window keeps to. + fn header_parts(&self, _: &MeasureCx<'_, Self>) -> HeaderParts { + match self.chevron_edge() { + Some(HeaderEdge::End) => HeaderParts::NONE.end(self.header), + Some(_) => HeaderParts::NONE.start(self.header), + None => HeaderParts::NONE, + } + } + fn open(&mut self) -> Option> { Some(self.open.reborrow()) } @@ -315,7 +425,10 @@ impl Container for FloatingGroup<'_> { fn placement_changed(&mut self, change: &PlacementChange) { let mut change = *change; - if self.minimized { + 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 self.minimized { change.rect = Rect::from_min_size(change.rect.min, Vec2::new(change.rect.width(), self.rect.height())); } self.moving |= change.kind == PlacementKind::Move && change.phase == GesturePhase::Begin; diff --git a/crates/ui-panels/src/docking/floating/stories.rs b/crates/ui-panels/src/docking/floating/stories.rs index 541f4c0cb..61fc81fb7 100644 --- a/crates/ui-panels/src/docking/floating/stories.rs +++ b/crates/ui-panels/src/docking/floating/stories.rs @@ -1,11 +1,12 @@ //! Stories of `FloatingGroup`. -use super::FloatingGroup; +use super::{FloatingGroup, WindowColumn}; use crate::buttons::Button; use crate::contract::Show as _; use crate::menu::MenuItem; use crate::testing::{Drive, StoryData, StoryOutput, story}; -use egui::{Pos2, Rect, Ui}; +use craft_behavior::dock::DockEdge; +use egui::{Pos2, Rect, Ui, Vec2}; const START: Rect = Rect { min: Pos2::new(40.0, 40.0), max: Pos2::new(280.0, 220.0) }; @@ -24,3 +25,37 @@ fn floating_group(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { } shown.output.into() } + +/// A dock window: its title row is its column's bar, with the double arrow at the end the window keeps to. The +/// arrow and a double click on the band collapse the column to a strip of icons against that edge. +#[story(FloatingGroup<'static>, covers = [Open], drive = Drive::Never)] +fn floating_dock_window(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let rect = *data.value("rect", START); + let collapsed = *data.value("collapsed", false); + // A strip of three icon cells, as an iconic column of three panels is. + let column = if collapsed { WindowColumn::collapsed(DockEdge::Start, Vec2::new(40.0, 120.0)) } else { WindowColumn::expanded(DockEdge::Start) }; + let shown = FloatingGroup::new("Layers", rect).column(column).sized(crate::contract::Sizing::Fill).show_group(ui, |ui, _| { + Button::new(if collapsed { "L" } else { "Add layer" }).show(ui); + }); + if let Some(change) = shown.output.placed { + *data.value("rect", START) = change.rect; + } + if let Some(iconic) = shown.output.iconic { + *data.value("collapsed", false) = iconic; + } + shown.output.into() +} + +/// A dock window whose column is collapsed to icons, against the float area's end edge. +#[story(FloatingGroup<'static>, covers = [Mixed], drive = Drive::Never)] +fn floating_collapsed_window(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let rect = *data.value("rect", START); + let column = WindowColumn::collapsed(DockEdge::End, Vec2::new(40.0, 120.0)); + let shown = FloatingGroup::new("Layers", rect).column(column).sized(crate::contract::Sizing::Fill).show_group(ui, |ui, _| { + Button::new("L").show(ui); + }); + if let Some(change) = shown.output.placed { + *data.value("rect", START) = change.rect; + } + shown.output.into() +} diff --git a/crates/ui-panels/src/docking/iconic.rs b/crates/ui-panels/src/docking/iconic.rs index 39bf60718..900d85c5a 100644 --- a/crates/ui-panels/src/docking/iconic.rs +++ b/crates/ui-panels/src/docking/iconic.rs @@ -1,7 +1,9 @@ //! `IconicColumn`: a column collapsed to one icon per panel, and the flyouts its icons open. use super::divider::{self, Divider, Drag}; -use super::dock_column::{BarRecord, CHEVRON, bar_slots, chevron_name, column_grip, describe_chevron, paint_chevron, register_edges, show_bar}; +use super::dock_column::{ + BarRecord, CHEVRON, ColumnBar, bar_slots, chevron_name, column_grip, describe_chevron, paint_chevron, register_edges, show_bar, +}; use super::{DockSizes, DockSpot, DockTargets, DockWords, DragUnit, Flyout, GroupAt, PanelKey, PanelSpec, Splitter, start_group_drag}; use crate::buttons::IconButton; use crate::contract::overlay::Anchor; @@ -62,6 +64,18 @@ impl IconicSizes { labels: size.rail.width * LABELS_IN_RAIL_WIDTHS, } } + + /// The sizes in the root scope's style, for code outside a style pass: + /// `StyleCx::with_root(ctx, IconicSizes::style)`. + pub fn of(ctx: &egui::Context) -> Self { + StyleCx::with_root(ctx, IconicSizes::style) + } + + /// The height of the icons of `column` under its bar: a cell per panel and the room between every two groups. + pub fn icons_height

(&self, column: &Column

) -> f32 { + let cells: usize = column.groups.iter().map(|group| group.panels.len()).sum(); + cells as f32 * self.cell + column.groups.len().saturating_sub(1) as f32 * self.separator + } } /// What an iconic column reports. @@ -143,6 +157,9 @@ pub struct IconicColumnStyle { /// size. The flyout's top is level with its group's icons, shifted by the group's `flyout_offset`, inside `within`. /// The user resizes it by its top and bottom edges and the edge away from the column; each step comes back in /// [`IconicColumnOutput::flyout`], which the host keeps in the group (`flyout_width`, `height`, `flyout_offset`). +/// +/// In a floating window ([`ColumnBar::Window`]) the window's title row is the column's bar: the column shows its +/// icons under it and keeps the divider of its inner edge, which sets its width, and registers no edge bands. #[derive(crate::contract::Component, crate::contract::Props)] #[component(container, overlay, probe(IconicColumn<'static, u8>))] pub struct IconicColumn<'a, P: Value + Hash + 'static> { @@ -156,6 +173,9 @@ pub struct IconicColumn<'a, P: Value + Hash + 'static> { /// The window edge the column sits on, which the flyout opens away from. #[prop] edge: DockEdge, + /// Where the column's bar is: its own (a dock column) or its floating window's title row. + #[prop] + bar: ColumnBar, /// Whether Auto-Collapse Iconic Panels is on. #[prop] auto_collapse: bool, @@ -164,7 +184,8 @@ pub struct IconicColumn<'a, P: Value + Hash + 'static> { within: Rect, open: Bind<'a, Option

>, view: Option>, - bar: Rect, + /// The bar's rect this pass. + bar_rect: Rect, record: BarRecord, shown: Option, width: Option, @@ -213,11 +234,12 @@ impl<'a, P: Value + Hash + 'static> IconicColumn<'a, P> { column, spec, edge: DockEdge::Start, + bar: ColumnBar::Own, auto_collapse: false, within: Rect::NOTHING, open, view: None, - bar: Rect::NOTHING, + bar_rect: Rect::NOTHING, record: BarRecord::default(), shown: None, width: None, @@ -277,7 +299,7 @@ impl<'a, P: Value + Hash + 'static> IconicColumn<'a, P> { } fn plan(&self, rect: Rect, sizes: &IconicSizes, rtl: bool) -> Plan { - let bar = Rect::from_min_max(rect.min, Pos2::new(rect.right(), (rect.top() + sizes.column.bar).min(rect.bottom()))); + let bar = Rect::from_min_max(rect.min, Pos2::new(rect.right(), (rect.top() + self.bar.height(&sizes.column)).min(rect.bottom()))); let body = Rect::from_min_max(Pos2::new(rect.left(), bar.bottom()), rect.max); let wide = body.width() >= sizes.labels; let (mut y, mut cells, mut names, mut grips, mut groups) = (body.top(), Vec::new(), Vec::new(), Vec::new(), Vec::new()); @@ -386,10 +408,12 @@ impl Component for IconicColumn<'_, P> { fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) { let sizes = cx.style.sizes; let plan = self.plan(rect, &sizes, cx.flow.is_rtl()); - self.bar = plan.bar; - let (chevron, _) = bar_slots(plan.bar, self.edge, cx.flow.is_rtl()); - let name = chevron_name(&cx.words::(), false); - let _ = cx.header_part(chevron_edge(self.edge), CHEVRON, chevron).sense(Sense::CLICK | Sense::FOCUS).focusable().tooltip(name); + self.bar_rect = plan.bar; + if self.bar == ColumnBar::Own { + let (chevron, _) = bar_slots(plan.bar, self.edge, cx.flow.is_rtl()); + let name = chevron_name(&cx.words::(), false); + let _ = cx.header_part(chevron_edge(self.edge), CHEVRON, chevron).sense(Sense::CLICK | Sense::FOCUS).focusable().tooltip(name); + } for (at, (_, _, cell)) in plan.cells.iter().enumerate() { let Some(panel) = self.flat().get(at).and_then(|(group, panel)| self.panel(*group, *panel)) else { continue }; let label = (self.spec)(panel).label; @@ -417,7 +441,7 @@ impl Component for IconicColumn<'_, P> { fn update(&mut self, cx: &mut Cx<'_, Self>) { let sizes = cx.style.sizes; let (rect, rtl, ctx) = (cx.rect, cx.flow.is_rtl(), cx.core.ctx.clone()); - self.expand = cx.part_activated(CHEVRON).is_some(); + self.expand = self.bar == ColumnBar::Own && cx.part_activated(CHEVRON).is_some(); let Some(plan) = self.laid.take() else { return }; let flat = self.flat(); let popup = cx.popup("flyout"); @@ -491,8 +515,10 @@ impl Component for IconicColumn<'_, P> { fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { // Painted here, not as the container's surface, which lies over what `paint` draws. ui.draw().surface(cx.rect, SurfaceKind::Panel, Corners::All); - ui.draw().surface(self.bar, SurfaceKind::Background, Corners::None); - paint_chevron(ui, self.edge, false, cx.flow.is_rtl(), chevron_name(&cx.words::(), false)); + if self.bar == ColumnBar::Own { + ui.draw().surface(self.bar_rect, SurfaceKind::Background, Corners::None); + paint_chevron(ui, self.edge, false, cx.flow.is_rtl(), chevron_name(&cx.words::(), false)); + } let flat = self.flat(); for (at, (group, panel)) in flat.iter().enumerate() { let Some(panel) = self.panel(*group, *panel) else { continue }; @@ -527,7 +553,9 @@ impl Component for IconicColumn<'_, P> { access.node(GRIP.at(*group as u64), Role::BUTTON).name = Name::Text(name); } divider::describe(access, EDGE, 0, &self.edge_divider(cx.rect, &sizes.column), words.resize.as_str(), sizes.column.step); - describe_chevron(access, &chevron_name(&words, false)); + if self.bar == ColumnBar::Own { + describe_chevron(access, &chevron_name(&words, false)); + } } /// The flyout of every cell whose popup is open, beside the column on the canvas side. A resize the user makes @@ -591,11 +619,13 @@ impl Component for IconicColumn<'_, P> { } impl IconicColumn<'_, P> { - /// Registers the drop bands of the column's edges, and the upper and lower half of every icon group: a drop on + /// Registers the drop bands of a dock column's edges, and the upper and lower half of every icon group: a drop on /// the upper half stacks above the group, one on the lower half below it. fn register(&self, ctx: &egui::Context, rect: Rect, rtl: bool, plan: &Plan, sizes: &IconicSizes) { let Some(first) = self.column.groups.first().and_then(|group| group.panels.first()) else { return }; - register_edges(ctx, self.dock, first, rect, rtl, &sizes.column); + if self.bar == ColumnBar::Own { + register_edges(ctx, self.dock, first, rect, rtl, &sizes.column); + } let half = sizes.separator * 0.5; let last = plan.groups.len().saturating_sub(1); for (at, group) in &plan.groups { @@ -629,19 +659,22 @@ fn chevron_edge(edge: DockEdge) -> HeaderEdge { } impl Container for IconicColumn<'_, P> { - /// The chevron's square at the bar's outer end. + /// The chevron's square at the bar's outer end; nothing in a floating window, whose title row is the bar. fn header_parts(&self, cx: &MeasureCx<'_, Self>) -> HeaderParts { + if self.bar == ColumnBar::Window { + return HeaderParts::NONE; + } let side = cx.style.sizes.column.bar; if chevron_edge(self.edge) == HeaderEdge::End { HeaderParts::NONE.end(side) } else { HeaderParts::NONE.start(side) } } /// The column bar, over the icons. What the user did on it is read in `finish`. fn header(&mut self, ui: &mut Ui, cx: &mut BodyCx<'_>) { - if cx.shell().is_some_and(|shell| !shell.dock_visible()) { + if self.bar == ColumnBar::Window || cx.shell().is_some_and(|shell| !shell.dock_visible()) { return; } if let Some(grip) = column_grip(self.column, self.spec, cx) { - self.record = show_bar(ui, cx, self.bar, false, self.auto_collapse, self.edge, grip); + self.record = show_bar(ui, cx, self.bar_rect, false, self.auto_collapse, self.edge, grip); } } } diff --git a/crates/ui-panels/src/docking/mod.rs b/crates/ui-panels/src/docking/mod.rs index 306939775..2e42fa853 100644 --- a/crates/ui-panels/src/docking/mod.rs +++ b/crates/ui-panels/src/docking/mod.rs @@ -56,7 +56,7 @@ mod flyout; mod iconic; pub use dock_column::{ - 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, width_bounds, }; pub use flyout::{Flyout, FlyoutOutput, FlyoutShown, FlyoutStyle, GroupAt}; pub use iconic::{FlyoutResize, IconicColumn, IconicColumnOutput, IconicColumnStyle, IconicSizes};