From 99d8fe537ed7a78b00621d595a167ba03ed6bab1 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 24 Oct 2026 03:03:07 +0000 Subject: [PATCH 1/3] docking: a dragged divider shrinks the groups beyond it in turn A divider dragged past the minimum of the group it shrinks moves on to the next group along the axis, and so on until every group on that side is at its minimum. The line follows same-axis splits up the tree and ends at a split across the axis or beside a dock. Groups touching the divider on the other side grow, up to their maximums, as before. Stacked panels and the groups of a dock column cascade the same way. --- crates/ui/src/docking/dock_tests.rs | 10 ++ crates/ui/src/docking/render/dock.rs | 47 +++-- crates/ui/src/docking/render/floating.rs | 2 +- crates/ui/src/docking/render/mod.rs | 87 ++++++---- crates/ui/src/docking/render/sizes.rs | 210 +++++++++++++++++++---- crates/ui/src/docking/split_tests.rs | 32 +++- crates/ui/src/docking/surface.rs | 2 +- crates/ui/src/docking/tests.rs | 11 ++ docs/ui-docking.md | 11 +- 9 files changed, 318 insertions(+), 94 deletions(-) diff --git a/crates/ui/src/docking/dock_tests.rs b/crates/ui/src/docking/dock_tests.rs index 654b637f5..7819667f8 100644 --- a/crates/ui/src/docking/dock_tests.rs +++ b/crates/ui/src/docking/dock_tests.rs @@ -415,6 +415,16 @@ fn column_and_group_dividers_resize_and_minimized_groups_keep_their_height() { assert_eq!(d.tab(4).top(), 600.0 - 26.0); } +#[test] +fn a_group_divider_dragged_past_the_lower_groups_minimum_shrinks_the_next_group() { + let mut d = Driver::new(docked(Side::Start, &[&[&[2], &[3], &[4]]])); + let divider = d.rect("group-resize", 2).center(); + d.drag(divider, divider + vec2(0.0, 500.0)); + // Groups 3 and 4 are at their minimum: a tab strip and a 40 pt body. + let heights = column(&d.layout, 0).groups.iter().map(|g| g.height).collect::>(); + assert_eq!(heights[1..], [Some(66.0), Some(66.0)], "{heights:?}"); +} + #[test] fn double_click_on_a_docked_tab_minimizes_and_clicking_another_tab_expands() { let mut d = Driver::new(docked(Side::Start, &[&[&[2, 3], &[4]]])); diff --git a/crates/ui/src/docking/render/dock.rs b/crates/ui/src/docking/render/dock.rs index 55336dca1..7ce4e88f0 100644 --- a/crates/ui/src/docking/render/dock.rs +++ b/crates/ui/src/docking/render/dock.rs @@ -8,7 +8,7 @@ use std::{fmt::Debug, hash::Hash}; use egui::{Context, Key, Pos2, Rect, Sense, Stroke, StrokeKind, Ui, UiBuilder, pos2, vec2}; -use super::{GroupKind, Renderer}; +use super::{GroupKind, Renderer, RowItem, row_drag, row_range}; use crate::docking::{ Action, Column, DockEvent, DockStyle, Group, GroupDrag, GroupGesture, Node, Placement, Side, Target, dock_model::Unit, @@ -144,7 +144,15 @@ pub(in crate::docking) fn fit_widths(columns: &[(f32, Option)], room: f32) impl Renderer<'_, P> { /// A horizontal split with a dock child: the dock takes its columns' width on its physical /// edge, the sibling the rest. False when neither child is a dock. - pub(super) fn dock_split(&mut self, ui: &mut Ui, first: &Node

, second: &Node

, rect: Rect, path: &mut Vec) -> bool { + pub(super) fn dock_split( + &mut self, + ui: &mut Ui, + first: &Node

, + second: &Node

, + rect: Rect, + path: &mut Vec, + root: (&Node

, Rect), + ) -> bool { let (dock_first, side, columns, other) = match (first, second) { (Node::Dock { side, columns }, other) => (true, *side, columns, other), (other, Node::Dock { side, columns }) => (false, *side, columns, other), @@ -153,7 +161,7 @@ impl Renderer<'_, P> { let (dock, rest, room) = dock_rects(side, columns, other, rect, self.style, &*self.content); self.dock(ui, side, columns, dock, room); path.push(dock_first); - self.node(ui, other, rest, path); + self.node(ui, other, rest, path, root); path.pop(); true } @@ -360,16 +368,18 @@ impl Renderer<'_, P> { push(&mut self.dock_targets, DockTarget::GroupBand { anchor: last.clone(), below: true }, band, horizontal_line(top, xs)); } // Height dividers between an open group and the next open one, after the groups so - // their hit areas win at the boundary. Both groups' heights are saved. + // their hit areas win at the boundary. The heights of the groups a drag changes are + // saved: the upper group, and the lower groups that give way in turn, or the reverse. let open: Vec = (0..column.groups.len()).filter(|i| column.groups.get(*i).is_some_and(|g| !g.minimized)).collect(); - for pair in open.windows(2) { - let &[i, j] = pair else { continue }; - let (Some(upper), Some(lower), Some(rect)) = (column.groups.get(i), column.groups.get(j), rects.get(i)) else { continue }; - let (Some(a), Some(b), Some(first), Some(second)) = (heights.get(i), heights.get(j), upper.panels.first(), lower.panels.first()) else { - continue; - }; - let (lo, hi) = (self.group_min(upper), a + b - self.group_min(lower)); - let hi = hi.max(lo); + let row: Vec = open + .iter() + .filter_map(|i| Some(RowItem { size: *heights.get(*i)?, min: self.group_min(column.groups.get(*i)?), max: f32::INFINITY })) + .collect(); + for (index, pair) in open.windows(2).enumerate() { + let &[i, _] = pair else { continue }; + let (Some(upper), Some(rect), Some(a)) = (column.groups.get(i), rects.get(i), heights.get(i)) else { continue }; + let Some(first) = upper.panels.first() else { continue }; + let (lo, hi) = row_range(&row, index); let divider = Rect::from_x_y_ranges(rect.x_range(), (rect.bottom() - 1.0)..=(rect.bottom() + 1.0)); let response = ResizeHandle::new( self.id.with(("group-resize", Some(first))), @@ -378,13 +388,16 @@ impl Renderer<'_, P> { &self.style.resize_label, self.handle_style(), ) - .value(*a, lo..=hi) + .value(*a, (a + lo)..=(a + hi)) .show(ui); self.register_interaction(ui.ctx(), response.response.id); - let next = (a + response.delta).clamp(lo, hi); - if response.delta != 0.0 && next != *a { - self.output.actions.push(Action::ResizeGroup { panel: first.clone(), height: Some(next) }); - self.output.actions.push(Action::ResizeGroup { panel: second.clone(), height: Some(a + b - next) }); + if response.delta == 0.0 { + continue; + } + for ((group, item), next) in open.iter().filter_map(|g| column.groups.get(*g)).zip(&row).zip(row_drag(&row, index, response.delta)) { + if let Some(panel) = group.panels.first().filter(|_| next != item.size) { + self.output.actions.push(Action::ResizeGroup { panel: panel.clone(), height: Some(next) }); + } } } } diff --git a/crates/ui/src/docking/render/floating.rs b/crates/ui/src/docking/render/floating.rs index 72ec389c3..a5168cb69 100644 --- a/crates/ui/src/docking/render/floating.rs +++ b/crates/ui/src/docking/render/floating.rs @@ -116,7 +116,7 @@ impl Renderer<'_, P> { (_, true) => {} (root, false) => { let outer = self.tree.replace((key, frame)); - self.node(ui, root, body, &mut Vec::new()); + self.node(ui, root, body, &mut Vec::new(), (root, body)); self.tree = outer; } } diff --git a/crates/ui/src/docking/render/mod.rs b/crates/ui/src/docking/render/mod.rs index f4fbf37ef..2f16a8d05 100644 --- a/crates/ui/src/docking/render/mod.rs +++ b/crates/ui/src/docking/render/mod.rs @@ -27,7 +27,7 @@ mod group; mod preview; mod sizes; -pub(in crate::docking) use sizes::{DROP_MARGIN, Drawn, Sizer}; +pub(in crate::docking) use sizes::{DROP_MARGIN, Drawn, RowItem, Sizer, row_drag, row_range}; pub(super) struct Renderer<'a, P> { pub(super) id: Id, @@ -63,8 +63,6 @@ pub(super) struct Renderer<'a, P> { pub(super) struct DrawnDivider { path: Vec, axis: SplitAxis, - /// The whole split's rect. - rect: Rect, divider: Rect, } @@ -107,23 +105,25 @@ impl Renderer<'_, P> { }); } - pub(in crate::docking) fn node(&mut self, ui: &mut Ui, node: &Node

, rect: Rect, path: &mut Vec) { + /// Draws `node` at view path `path` in `rect`, in the tree `root` (its root node and rect), + /// which a dragged divider resizes beyond `node`. + pub(in crate::docking) fn node(&mut self, ui: &mut Ui, node: &Node

, rect: Rect, path: &mut Vec, root: (&Node

, Rect)) { if !crate::docking::targets::usable(rect) { return; } self.note_edge(path, rect); match node { - Node::Split { axis: SplitAxis::Horizontal, first, second, .. } if self.dock_split(ui, first, second, rect, path) => {} + Node::Split { axis: SplitAxis::Horizontal, first, second, .. } if self.dock_split(ui, first, second, rect, path, root) => {} Node::Split { axis, size, first, second } => { let sizer = self.sizer(); let Some(split) = sizer.split(*axis, *size, first, second, rect) else { return }; - let (lo, hi) = sizer.range(node, rect); + let (lo, hi) = sizer.range(root, path); let mark = self.dividers.len(); path.push(false); - self.node(ui, first, split.first, path); + self.node(ui, first, split.first, path, root); path.pop(); path.push(true); - self.node(ui, second, split.second, path); + self.node(ui, second, split.second, path, root); path.pop(); // The divider's inner-edge hit margin must win over child scrollbars, just // as accordion dividers win over the adjacent panel's content. @@ -140,12 +140,12 @@ impl Renderer<'_, P> { .show(ui); self.register_interaction(ui.ctx(), response.response.id); if response.delta != 0.0 { - let actions = self.divider_actions(node, rect, path, response.delta); + let actions = self.divider_actions(root, path, response.delta); self.output.actions.extend(actions); } - self.intersections(ui, node, rect, path, split.divider, mark); + self.intersections(ui, node, rect, path, split.divider, mark, root); if self.dividers.len() < crate::docking::model::MAX_NODES { - self.dividers.push(DrawnDivider { path: path.clone(), axis: *axis, rect, divider: split.divider }); + self.dividers.push(DrawnDivider { path: path.clone(), axis: *axis, divider: split.divider }); } } Node::Tabs { panels, active } => { @@ -212,21 +212,30 @@ impl Renderer<'_, P> { } // Register dividers after the headers so their narrow hit regions win at the // shared boundary. Closed entries retain their saved sizes and are skipped. + let scale = if total_wanted > 0.0 { f64::from(remaining) / total_wanted } else { 0.0 }; + let body_limits = |panel: &P| { + let mut limits = self.limits(panel); + limits.min.y = limits.min.y.max(self.style.min_pane); + limits.max = limits.max.max(limits.min); + limits + }; + let open: Vec<&crate::docking::StackEntry

> = entries.iter().filter(|entry| entry.open).collect(); + let row: Vec = open + .iter() + .map(|entry| { + let limits = body_limits(&entry.panel); + let size = (f64::from(entry.height.unwrap_or(self.style.min_pane).max(1.0)) * scale) as f32; + RowItem { size, min: limits.min.y, max: limits.max.y } + }) + .collect(); for (index, divider) in dividers { let Some(first) = entries.get(index) else { continue }; - let Some(second) = entries.iter().skip(index.saturating_add(1)).find(|entry| entry.open) else { continue }; - let a = f64::from(first.height.unwrap_or(self.style.min_pane).max(1.0)); - let b = f64::from(second.height.unwrap_or(self.style.min_pane).max(1.0)); - let scale = f64::from(remaining) / total_wanted; - let first_size = (a * scale) as f32; - let pair_size = ((a + b) * scale) as f32; - let body_limits = |panel: &P| { - let mut limits = self.limits(panel); - limits.min.y = limits.min.y.max(self.style.min_pane); - limits.max = limits.max.max(limits.min); - limits - }; - let (lo, hi) = split_bounds(pair_size, SplitAxis::Vertical, body_limits(&first.panel), body_limits(&second.panel)); + let Some(position) = open.iter().position(|entry| std::ptr::eq(*entry, first)) else { continue }; + let Some(first_size) = row.get(position).map(|item| item.size) else { continue }; + if position + 1 >= row.len() { + continue; + } + let (lo, hi) = row_range(&row, position); let response = ResizeHandle::new( self.id.with(("stack-resize", &first.panel)), SplitAxis::Vertical, @@ -234,14 +243,16 @@ impl Renderer<'_, P> { &self.style.resize_label, ResizeHandleStyle { hovered: self.style.accent, dragged: self.style.accent, focused: self.style.accent }, ) - .value(first_size, lo..=hi) + .value(first_size, (first_size + lo)..=(first_size + hi)) .show(ui); self.register_interaction(ui.ctx(), response.response.id); if response.delta != 0.0 && scale > 0.0 { - let delta = f64::from((first_size + response.delta).clamp(lo, hi) - first_size) / scale; - let next_a = (a + delta).clamp((a + b - 1_000_000.0).max(1.0), (a + b - 1.0).min(1_000_000.0)); - self.output.actions.push(Action::ResizeStack { panel: first.panel.clone(), height: Some(next_a as f32) }); - self.output.actions.push(Action::ResizeStack { panel: second.panel.clone(), height: Some((a + b - next_a) as f32) }); + for ((entry, item), next) in open.iter().zip(&row).zip(row_drag(&row, position, response.delta)) { + if next != item.size { + let height = (f64::from(next) / scale).clamp(1.0, 1_000_000.0) as f32; + self.output.actions.push(Action::ResizeStack { panel: entry.panel.clone(), height: Some(height) }); + } + } } } } @@ -252,10 +263,10 @@ impl Renderer<'_, P> { Sizer { style: self.style, content: &*self.content } } - /// `ResizeSplit` actions that move the divider of the split `node`, drawn at view path - /// `path` in `rect`, by `delta` points. - fn divider_actions(&self, node: &Node

, rect: Rect, path: &[bool], delta: f32) -> Vec> { - let sizes = self.sizer().drag(node, rect, delta, &mut path.to_vec()); + /// `ResizeSplit` actions that move the divider of the split at view path `path` in the + /// tree `root` by `delta` points. + fn divider_actions(&self, root: (&Node

, Rect), path: &[bool], delta: f32) -> Vec> { + let sizes = self.sizer().drag(root, path, delta); let resize = |path: Vec, size| match &self.tree { None => Action::ResizeSplit { path, size }, Some((panel, _)) => Action::ResizeWindowSplit { panel: panel.clone(), path, size }, @@ -274,7 +285,7 @@ impl Renderer<'_, P> { /// Four-way handles where a divider drawn inside the split `node` (since `mark`) ends on /// the split's own divider: dragging one moves both dividers. - fn intersections(&mut self, ui: &mut Ui, node: &Node

, rect: Rect, path: &[bool], divider: Rect, mark: usize) { + fn intersections(&mut self, ui: &mut Ui, node: &Node

, rect: Rect, path: &[bool], divider: Rect, mark: usize, root: (&Node

, Rect)) { let Node::Split { axis, .. } = node else { return }; let side = (self.style.gap * 3.0).max(12.0); let mut found = Vec::new(); @@ -286,9 +297,9 @@ impl Renderer<'_, P> { SplitAxis::Horizontal => pos2(divider.center().x, inner.divider.center().y), SplitAxis::Vertical => pos2(inner.divider.center().x, divider.center().y), }; - found.push((inner.path.clone(), inner.rect, Rect::from_center_size(center, Vec2::splat(side)).intersect(rect))); + found.push((inner.path.clone(), Rect::from_center_size(center, Vec2::splat(side)).intersect(rect))); } - for (inner_path, inner_rect, hit) in found { + for (inner_path, hit) in found { let Some(inner) = inner_path.get(path.len()..).and_then(|rest| crate::docking::dock_model::node_at(node, rest)) else { continue }; let (outer, inner_layout) = (self.original_path(path), self.original_path(&inner_path)); let id = match &self.tree { @@ -305,8 +316,8 @@ impl Renderer<'_, P> { if delta != Vec2::ZERO && delta.is_finite() { let along = |axis: SplitAxis| if axis == SplitAxis::Horizontal { delta.x } else { delta.y }; let Node::Split { axis: inner_axis, .. } = inner else { continue }; - let mut actions = self.divider_actions(node, rect, path, along(*axis)); - actions.extend(self.divider_actions(inner, inner_rect, &inner_path, along(*inner_axis))); + let mut actions = self.divider_actions(root, path, along(*axis)); + actions.extend(self.divider_actions(root, &inner_path, along(*inner_axis))); self.output.actions.extend(actions); } } diff --git a/crates/ui/src/docking/render/sizes.rs b/crates/ui/src/docking/render/sizes.rs index bf7fd3dc5..8af0dd5ae 100644 --- a/crates/ui/src/docking/render/sizes.rs +++ b/crates/ui/src/docking/render/sizes.rs @@ -1,5 +1,13 @@ //! Split-tree geometry: where each split draws its children, how far a divider can travel, -//! and the sizes that move a divider while every group away from it keeps its size. +//! and the sizes that move a divider. +//! +//! A dragged divider grows the groups touching it on one side, up to their maximums, and +//! shrinks the groups on the other side in turn: the group touching the divider gives way +//! down to its minimum, then the next group along the axis, and so on, until every group on +//! that side is at its minimum. Groups side by side across the axis shrink together, and the +//! line goes on past them once one is at its minimum. The line follows splits of the drag's +//! axis up the tree and ends at a split across it or beside a dock. Every other group keeps +//! its size. use egui::Rect; @@ -78,8 +86,9 @@ impl

Sizer<'_, P> { } /// How far the end edge (`end`) or start edge of `node` drawn in `rect` can move along - /// `axis`: inward while the groups touching it stay at their minimums, and outward up to - /// their maximums. + /// `axis`: inward until every group along the axis from that edge is at its minimum, and + /// outward while the groups touching it stay below their maximums. Groups side by side + /// across the axis move together, so the first of them to reach its limit stops them. pub(in crate::docking) fn travel(&self, node: &Node

, rect: Rect, axis: SplitAxis, end: bool) -> (f32, f32) { let docked = matches!(node, Node::Split { first, second, .. } if matches!(**first, Node::Dock { .. }) || matches!(**second, Node::Dock { .. })); @@ -89,8 +98,9 @@ impl

Sizer<'_, P> { self.travel(other, rect, axis, end) } (Node::Split { axis: split_axis, .. }, Some([a, b])) if *split_axis == axis => { - let (child, rect) = if end { b } else { a }; - self.travel(child, rect, axis, end) + let (near, far) = if end { (b, a) } else { (a, b) }; + let (near, far) = (self.travel(near.0, near.1, axis, end), self.travel(far.0, far.1, axis, end)); + (near.0 + far.0, near.1) } (_, Some([a, b])) => { let (a, b) = (self.travel(a.0, a.1, axis, end), self.travel(b.0, b.1, axis, end)); @@ -105,7 +115,9 @@ impl

Sizer<'_, P> { } /// The sizes that move the end (`end`) or start edge of `node` drawn in `rect` outward by - /// `grow` points: only the groups touching that edge change. Paths are relative to `path`. + /// `grow` points. Growing, only the groups touching that edge change. Shrinking, the group + /// at the edge gives way down to its minimum, then the next one along the axis, and so on. + /// Paths are relative to `path`. pub(in crate::docking) fn follow( &self, (node, rect): (&Node

, Rect), @@ -118,55 +130,195 @@ impl

Sizer<'_, P> { let Some([a, b]) = self.children(node, rect) else { return }; let Node::Split { axis: split_axis, first, second, .. } = node else { return }; let docked = matches!(**first, Node::Dock { .. }) || matches!(**second, Node::Dock { .. }); - let visit = |child: (&Node

, Rect), second_child: bool, path: &mut Vec, out: &mut Vec<(Vec, SplitSize)>| { + let visit = |child: (&Node

, Rect), second_child: bool, grow: f32, path: &mut Vec, out: &mut Vec<(Vec, SplitSize)>| { path.push(second_child); self.follow(child, axis, end, grow, path, out); path.pop(); }; if docked { let dock_first = matches!(**first, Node::Dock { .. }); - visit(if dock_first { b } else { a }, dock_first, path, out); + visit(if dock_first { b } else { a }, dock_first, grow, path, out); } else if *split_axis == axis { - let available = extent(a.1, axis) + extent(b.1, axis) + grow; - let first_size = extent(a.1, axis) + if end { 0.0 } else { grow }; - if available > 0.0 { - out.push((path.clone(), SplitSize::Ratio((first_size / available).clamp(0.0, 1.0)))); + let (near, far) = if end { (b, a) } else { (a, b) }; + let near_grow = if grow < 0.0 { grow.max(-self.travel(near.0, near.1, axis, end).0) } else { grow }; + let far_grow = grow - near_grow; + let (near_size, far_size) = (extent(near.1, axis) + near_grow, extent(far.1, axis) + far_grow); + let first_size = if end { far_size } else { near_size }; + push_ratio(path, first_size, near_size + far_size, out); + visit(near, end, near_grow, path, out); + if far_grow != 0.0 { + visit(far, !end, far_grow, path, out); } - visit(if end { b } else { a }, end, path, out); } else { - visit(a, false, path, out); - visit(b, true, path, out); + visit(a, false, grow, path, out); + visit(b, true, grow, path, out); } } - /// The divider of the split `node` drawn in `rect` moved by `delta` points, clamped to its - /// travel: the split's own size and those of the splits that keep the groups away from the - /// divider at their sizes. Paths are relative to `path`, the split's own. - pub(in crate::docking) fn drag(&self, node: &Node

, rect: Rect, delta: f32, path: &mut Vec) -> Vec<(Vec, SplitSize)> { + /// The drawn splits from `root` down to the one at `path`, each with its rect. + fn chain<'n>(&self, root: (&'n Node

, Rect), path: &[bool]) -> Option, Rect)>> { + let mut chain = vec![root]; + for &second in path { + let (node, rect) = *chain.last()?; + let [a, b] = self.children(node, rect)?; + chain.push(if second { b } else { a }); + } + Some(chain) + } + + /// The ancestors that continue the line of the split at the end of `chain` along `axis`: + /// each with its children and whether the line runs through its second child, nearest first. + /// The line ends at a split across the axis or beside a dock. + fn line<'n>(&self, chain: &[(&'n Node

, Rect)], path: &[bool], axis: SplitAxis) -> Vec<(usize, [(&'n Node

, Rect); 2], bool)> { + let mut line = Vec::new(); + for depth in (0..path.len()).rev() { + let Some(&(node, rect)) = chain.get(depth) else { break }; + let Node::Split { axis: split_axis, first, second, .. } = node else { break }; + if *split_axis != axis || matches!(**first, Node::Dock { .. }) || matches!(**second, Node::Dock { .. }) { + break; + } + let (Some(children), Some(&second_child)) = (self.children(node, rect), path.get(depth)) else { break }; + line.push((depth, children, second_child)); + } + line + } + + /// The divider of the split at `path` under `root` (a node and the rect it is drawn in) + /// moved by `delta` points, clamped to its travel. The groups touching the divider on the + /// side it moves toward give way, then the groups beyond them along the axis, one after + /// another, as far as the line of same-axis splits reaches; the groups on the other side + /// that touch the divider take the room. Paths are absolute, like `path`. + pub(in crate::docking) fn drag(&self, root: (&Node

, Rect), path: &[bool], delta: f32) -> Vec<(Vec, SplitSize)> { + let Some(chain) = self.chain(root, path) else { return Vec::new() }; + let Some(&(node, rect)) = chain.last() else { return Vec::new() }; let Node::Split { axis, size, first, second } = node else { return Vec::new() }; - let Some(split) = self.split(*axis, *size, first, second, rect) else { return Vec::new() }; - let (lo, hi) = self.range(node, rect); + let axis = *axis; + let Some(split) = self.split(axis, *size, first, second, rect) else { return Vec::new() }; + let (lo, hi) = self.range(root, path); let delta = delta.clamp(lo, hi); if delta == 0.0 || !delta.is_finite() || split.available() <= 0.0 { return Vec::new(); } - let mut out = vec![(path.clone(), SplitSize::Ratio(((split.first_size() + delta) / split.available()).clamp(0.0, 1.0)))]; + // Toward the end edge the second child gives way, toward the start the first. + let forward = delta > 0.0; + let moved = delta.abs(); + let given = if forward { + moved.min(self.travel(second, split.second, axis, false).0) + } else { + moved.min(self.travel(first, split.first, axis, true).0) + }; + let (first_grow, second_grow) = if forward { (moved, -given) } else { (-given, moved) }; + let mut out = Vec::new(); + let mut path = path.to_vec(); + let first_size = split.first_size() + first_grow; + push_ratio(&path, first_size, first_size + extent(split.second, axis) + second_grow, &mut out); path.push(false); - self.follow((first, split.first), *axis, true, delta, path, &mut out); + self.follow((first, split.first), axis, true, first_grow, &mut path, &mut out); path.pop(); path.push(true); - self.follow((second, split.second), *axis, false, -delta, path, &mut out); + self.follow((second, split.second), axis, false, second_grow, &mut path, &mut out); path.pop(); + // What the split's own child could not give, the ancestors' children beyond it give: + // the split grows by `rest` toward them, and so does each ancestor until one absorbs it. + let mut rest = moved - given; + for (depth, [a, b], second_child) in self.line(&chain, &path, axis) { + if rest <= 0.0 { + break; + } + path.truncate(depth); + let (a_size, b_size) = (extent(a.1, axis), extent(b.1, axis)); + let (first_size, second_size) = match (forward, second_child) { + (true, false) => { + let taken = rest.min(self.travel(b.0, b.1, axis, false).0); + path.push(true); + self.follow(b, axis, false, -taken, &mut path, &mut out); + path.pop(); + let sizes = (a_size + rest, b_size - taken); + rest -= taken; + sizes + } + (false, true) => { + let taken = rest.min(self.travel(a.0, a.1, axis, true).0); + path.push(false); + self.follow(a, axis, true, -taken, &mut path, &mut out); + path.pop(); + let sizes = (a_size - taken, b_size + rest); + rest -= taken; + sizes + } + (true, true) => (a_size, b_size + rest), + (false, false) => (a_size + rest, b_size), + }; + push_ratio(&path, first_size, first_size + second_size, &mut out); + } out } - /// How far the divider of the split `node` drawn in `rect` can move: `(lo, hi)`, with - /// `lo <= 0 <= hi`. - pub(in crate::docking) fn range(&self, node: &Node

, rect: Rect) -> (f32, f32) { + /// How far the divider of the split at `path` under `root` can move: `(lo, hi)`, with + /// `lo <= 0 <= hi`. Each way it is the lesser of the room the groups touching the divider + /// have to grow and the room every group beyond the divider, along the line of same-axis + /// splits, has to shrink. + pub(in crate::docking) fn range(&self, root: (&Node

, Rect), path: &[bool]) -> (f32, f32) { + let Some(chain) = self.chain(root, path) else { return (0.0, 0.0) }; + let Some(&(node, rect)) = chain.last() else { return (0.0, 0.0) }; let Node::Split { axis, size, first, second } = node else { return (0.0, 0.0) }; let Some(split) = self.split(*axis, *size, first, second, rect) else { return (0.0, 0.0) }; - let (first_in, first_out) = self.travel(first, split.first, *axis, true); - let (second_in, second_out) = self.travel(second, split.second, *axis, false); + let (mut first_in, first_out) = self.travel(first, split.first, *axis, true); + let (mut second_in, second_out) = self.travel(second, split.second, *axis, false); + for (_, [a, b], second_child) in self.line(&chain, path, *axis) { + if second_child { + first_in += self.travel(a.0, a.1, *axis, true).0; + } else { + second_in += self.travel(b.0, b.1, *axis, false).0; + } + } (-first_in.min(second_out), first_out.min(second_in)) } } + +/// The size of a split at `path` whose first child is `first` of `available` points. +fn push_ratio(path: &[bool], first: f32, available: f32, out: &mut Vec<(Vec, SplitSize)>) { + if available > 0.0 { + out.push((path.to_vec(), SplitSize::Ratio((first / available).clamp(0.0, 1.0)))); + } +} + +/// One group of a flat row, such as the panels of a stack or the groups of a dock column: +/// its size along the row and its limits. +#[derive(Clone, Copy)] +pub(in crate::docking) struct RowItem { + pub(in crate::docking) size: f32, + pub(in crate::docking) min: f32, + pub(in crate::docking) max: f32, +} + +/// How far the divider after `row[index]` can move: `(lo, hi)`, with `lo <= 0 <= hi`, by the +/// rule of [`row_drag`]. +pub(in crate::docking) fn row_range(row: &[RowItem], index: usize) -> (f32, f32) { + let room = |items: &[RowItem]| items.iter().map(|item| (item.size - item.min).max(0.0)).sum::(); + let grow = |item: Option<&RowItem>| item.map_or(0.0, |item| (item.max - item.size).max(0.0)); + let (before, after) = row.split_at((index + 1).min(row.len())); + (-room(before).min(grow(after.first())), room(after).min(grow(before.last()))) +} + +/// The sizes of `row` after the divider after `row[index]` moves by `delta`, clamped to its +/// travel: the item touching the divider on the side it moves toward gives way down to its +/// minimum, then the next one, and so on; the item on the other side takes the room. +pub(in crate::docking) fn row_drag(row: &[RowItem], index: usize, delta: f32) -> Vec { + let (lo, hi) = row_range(row, index); + let delta = if delta.is_finite() { delta.clamp(lo, hi) } else { 0.0 }; + let mut sizes: Vec = row.iter().map(|item| item.size).collect(); + let (grows, givers): (usize, Vec) = + if delta > 0.0 { (index, (index + 1..row.len()).collect()) } else { (index + 1, (0..=index).rev().collect()) }; + let mut rest = delta.abs(); + for giver in givers { + let (Some(size), Some(item)) = (sizes.get_mut(giver), row.get(giver)) else { continue }; + let taken = rest.min((item.size - item.min).max(0.0)); + *size -= taken; + rest -= taken; + } + if let Some(size) = sizes.get_mut(grows) { + *size += delta.abs() - rest; + } + sizes +} diff --git a/crates/ui/src/docking/split_tests.rs b/crates/ui/src/docking/split_tests.rs index 3c7c1c455..ea3283adf 100644 --- a/crates/ui/src/docking/split_tests.rs +++ b/crates/ui/src/docking/split_tests.rs @@ -227,6 +227,24 @@ fn a_divider_resizes_only_the_groups_that_share_it() { assert_eq!(group(&d, PROPERTIES[0]), properties); } +#[test] +fn a_divider_dragged_past_a_groups_minimum_shrinks_the_next_group_along_the_row() { + // A | B | C | D, 357 pt each, as (A | B) | (C | D): the cascade runs on past B's split. + let row = hsplit(0.5, hsplit(0.5, tabs(&[1]), tabs(&[2])), hsplit(0.5, tabs(&[3]), tabs(&[4]))); + let mut d = driver(Layout { root: Some(row), floating: Vec::new() }, Film::default()); + let widths = |d: &Driver| [1, 2, 3, 4].map(|panel| group(d, panel).width().round()); + assert_eq!(widths(&d), [357.0, 357.0, 357.0, 357.0]); + // B gives way down to its 40 pt minimum, then C. + drag_hit(&mut d, divider(&[false]), vec2(400.0, 0.0)); + assert_eq!(widths(&d), [757.0, 40.0, 274.0, 357.0]); + // The drag stops when B, C and D are all at their minimums. + drag_hit(&mut d, divider(&[false]), vec2(670.0, 0.0)); + assert_eq!(widths(&d), [1308.0, 40.0, 40.0, 40.0]); + // Back the other way, A gives way and only B, which touches the divider, grows. + drag_hit(&mut d, divider(&[false]), vec2(-1300.0, 0.0)); + assert_eq!(widths(&d), [40.0, 1308.0, 40.0, 40.0]); +} + #[test] fn an_intersection_moves_both_dividers() { let mut d = film(); @@ -361,12 +379,16 @@ fn assert_sizes(d: &Driver, context: &str) { let room = g.rect.size() - g.limits.min; assert!(room.x >= -0.01 && room.y >= -0.01, "{context}: {:?} is below its minimum {:?}", g.rect, g.limits.min); } - let mut nodes = vec![(root, area)]; - while let Some((node, rect)) = nodes.pop() { - let Some(children) = sizer.children(node, rect) else { continue }; - let (lo, hi) = sizer.range(node, rect); + let mut nodes = vec![(root, area, Vec::new())]; + while let Some((node, rect, path)) = nodes.pop() { + let Some([a, b]) = sizer.children(node, rect) else { continue }; + let (lo, hi) = sizer.range((root, area), &path); assert!(lo < -0.5 && hi > 0.5, "{context}: a divider is stuck ({lo}, {hi}) at {rect:?} in {:?}", d.layout); - nodes.extend(children); + for (second, (child, rect)) in [(false, a), (true, b)] { + let mut path = path.clone(); + path.push(second); + nodes.push((child, rect, path)); + } } } } diff --git a/crates/ui/src/docking/surface.rs b/crates/ui/src/docking/surface.rs index 1deff0205..f133f190f 100644 --- a/crates/ui/src/docking/surface.rs +++ b/crates/ui/src/docking/surface.rs @@ -829,7 +829,7 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { tree: None, }; if let Some(root) = &view.root { - renderer.node(ui, root, rect, &mut Vec::new()); + renderer.node(ui, root, rect, &mut Vec::new(), (root, rect)); ui.allocate_rect(rect, Sense::hover()); } renderer.flyout(ui.ctx()); diff --git a/crates/ui/src/docking/tests.rs b/crates/ui/src/docking/tests.rs index 3f21199a1..2a3d7c56f 100644 --- a/crates/ui/src/docking/tests.rs +++ b/crates/ui/src/docking/tests.rs @@ -653,6 +653,17 @@ fn stack_dividers_resize_adjacent_open_bodies_by_pointer_and_keyboard() { let Node::Stack { entries } = layout.root.as_ref().unwrap() else { panic!() }; assert!(entries[0].height.unwrap() > after_pointer); assert!((entries[0].height.unwrap() + entries[2].height.unwrap() - 300.0).abs() < 0.001); + // Dragged past the next body's minimum, the one after it gives way too, down to the same + // minimum; the closed entry keeps its height. + let start = ctx.read_response(id).unwrap().rect.center(); + step(&ctx, &mut layout, vec![Event::PointerMoved(start), pointer_button(start, true)]); + step(&ctx, &mut layout, vec![Event::PointerMoved(start + vec2(0.0, 600.0))]); + step(&ctx, &mut layout, vec![pointer_button(start + vec2(0.0, 600.0), false)]); + let Node::Stack { entries } = layout.root.as_ref().unwrap() else { panic!() }; + let heights = entries.iter().map(|entry| entry.height.unwrap()).collect::>(); + assert!((heights[2] - heights[3]).abs() < 0.01 && heights[3] < 120.0, "{heights:?}"); + assert!((heights[0] + heights[2] + heights[3] - 420.0).abs() < 0.01, "{heights:?}"); + assert_eq!(heights[1], 91.0); } #[test] diff --git a/docs/ui-docking.md b/docs/ui-docking.md index fb6d0deee..2cac493d9 100644 --- a/docs/ui-docking.md +++ b/docs/ui-docking.md @@ -218,9 +218,14 @@ A flyout fits its content the same way, without the title bar. window side: top and bottom, and left and right with `side_edge_zones`. It takes a quarter of the window and is offered only when every frame keeps its minimum. Past the band, a frame's own side zones apply. -- A divider resizes only the frames that touch it; every other frame keeps its size. It stops - when a frame touching it reaches its minimum. Where a divider ends on another divider, a - four-way handle (`Divider::Intersection`) moves both. +- A dragged divider grows the frames that touch it on one side, up to their maximums. On the + other side the frame touching it shrinks to its minimum, then the next frame along the same + axis, and so on: the divider stops when every frame on that side is at its minimum. Frames + side by side across the axis shrink together, and the next frame gives way once one of them + is at its minimum. The line of frames ends at a split across the axis or at a dock; every + other frame keeps its size. Dividers between stacked panels and between the groups of a dock + column work the same way. Where a divider ends on another divider, a four-way handle + (`Divider::Intersection`) moves both. - Every frame has the same minimum, `DockStyle::min_pane` (plus its tab strip), unless its panels ask for more through `PanelLimits::min`. Frames have no maximum unless every panel in them asks for one through `PanelLimits::max`: a panel without a maximum lifts the maximum of the panels it From cb352b39ffa84cd94060c1ac1b0a756937969854 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 24 Oct 2026 03:06:11 +0000 Subject: [PATCH 2/3] docking: all four drop zones of a group share one depth Each side zone reaches zone_depth into the body, or a quarter of the body's smaller side when that is less. A tall narrow group's top and bottom zones are no deeper than its left and right ones. --- crates/ui/src/docking/surface.rs | 6 +++--- crates/ui/src/docking/targets.rs | 19 ++++++++++--------- crates/ui/src/docking/tests.rs | 17 ++++++++++++----- docs/ui-docking.md | 4 ++-- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/crates/ui/src/docking/surface.rs b/crates/ui/src/docking/surface.rs index f133f190f..64046b323 100644 --- a/crates/ui/src/docking/surface.rs +++ b/crates/ui/src/docking/surface.rs @@ -51,9 +51,9 @@ pub struct DockStyle { /// The outer edge band also runs along the left and right edges, where a drop adds a /// full-height column. Off: only the top and bottom edges add a full-width row. pub side_edge_zones: bool, - /// How far a group's side drop zones reach into its body, below the tab strip. A body less - /// than three times as wide or tall gets zones a third of its extent deep in that axis, so - /// it keeps a center. + /// How far a group's side drop zones reach into its body, below the tab strip. All four + /// zones share one depth: this, or a quarter of the body's smaller side when that is less, + /// so the body keeps a center. pub zone_depth: f32, /// [D] The docked area's left and right edges, where no dock is yet, take a drop as a new /// dock (see [`Self::new_dock_band`]). diff --git a/crates/ui/src/docking/targets.rs b/crates/ui/src/docking/targets.rs index 89e235e0b..82eec3250 100644 --- a/crates/ui/src/docking/targets.rs +++ b/crates/ui/src/docking/targets.rs @@ -1,4 +1,4 @@ -use egui::{Pos2, Rangef, Rect, Vec2, pos2, vec2}; +use egui::{Pos2, Rangef, Rect, Vec2, pos2}; use super::{Placement, Side, Target, Zone}; @@ -32,10 +32,11 @@ pub(super) fn usable(rect: Rect) -> bool { } /// Tab header (the top `tab_height` of `rect`) is the center; otherwise the nearest side zone of -/// the body below it wins. A side zone reaches `depth` into the body, or a third of the body's -/// extent in that axis when that is less, so a body always keeps a center; a `depth` of zero -/// or less leaves only the center. Outside, non-finite, empty and inverted rectangles have no -/// target. +/// the body below it wins. Every side zone reaches the same depth into the body: `depth`, or a +/// quarter of the body's smaller side when that is less, so a body always keeps a center and a +/// narrow body's top and bottom zones are no deeper than its left and right ones. A `depth` of +/// zero or less leaves only the center. Outside, non-finite, empty and inverted rectangles have +/// no target. pub fn drop_zone(rect: Rect, tab_height: f32, depth: f32, pointer: Pos2) -> Option { if !usable(rect) || !pointer.is_finite() || !rect.contains(pointer) || !tab_height.is_finite() || tab_height < 0.0 { return None; @@ -65,10 +66,10 @@ fn body(rect: Rect, tab_height: f32) -> Rect { } /// How deep the side zones of `body` reach in from its left and right edges (`x`) and its top -/// and bottom edges (`y`). +/// and bottom edges (`y`): the same on all four sides. fn side_depths(body: Rect, depth: f32) -> Vec2 { let depth = if depth > 0.0 { depth } else { 0.0 }; - vec2(depth.min(body.width() / 3.0), depth.min(body.height() / 3.0)) + Vec2::splat(depth.min(body.width().min(body.height()) / 4.0)) } /// The outline of a drop zone of the group drawn at `rect`, as [`drop_zone`] finds it with the @@ -152,8 +153,8 @@ pub fn tab_insertion_marker(target: &DropTarget

, before: Option<&P>) - /// Resolve a pointer against visible groups. The frontmost containing group occludes groups /// behind it even if it rejects the drop. A strip reorders by midpoint; the body of the source /// group is a no-op at center. The body's side zones are [`drop_zone`]'s with `zone_depth` (see -/// [`crate::docking::DockStyle::zone_depth`]). Floating groups normally set `allow_splits` to -/// false. +/// [`crate::docking::DockStyle::zone_depth`]): one depth on all four sides, at most a quarter of +/// the body's smaller side. Floating groups normally set `allow_splits` to false. pub fn resolve_drop(panel: &P, pointer: Pos2, targets: &[DropTarget

], zone_depth: f32) -> Option> { if !pointer.is_finite() || targets.len() > super::MAX_PANELS { return None; diff --git a/crates/ui/src/docking/tests.rs b/crates/ui/src/docking/tests.rs index 2a3d7c56f..80c1fb97d 100644 --- a/crates/ui/src/docking/tests.rs +++ b/crates/ui/src/docking/tests.rs @@ -303,23 +303,30 @@ fn pointer_targets_distinguish_reorder_center_split_and_occlusion() { } #[test] -fn side_zones_are_40_pt_deep_or_a_third_of_a_small_body() { +fn side_zones_are_40_pt_deep_or_a_quarter_of_the_bodys_smaller_side() { let depth = DockStyle::default().zone_depth; assert_eq!(depth, 40.0); // The inner corner of the left trapezoid is the depth of both bands. let inner = |rect: Rect| zone_shape(rect, 0.0, depth, Zone::Left).unwrap()[1] - rect.min; - for size in [vec2(600.0, 400.0), vec2(130.0, 130.0)] { + for size in [vec2(600.0, 400.0), vec2(160.0, 160.0)] { let rect = Rect::from_min_size(pos2(10.0, 20.0), size); assert_eq!(inner(rect), vec2(40.0, 40.0), "{size:?}"); let y = rect.center().y; assert_eq!(drop_zone(rect, 0.0, depth, pos2(rect.left() + 39.0, y)), Some(Zone::Left), "{size:?}"); assert_eq!(drop_zone(rect, 0.0, depth, pos2(rect.left() + 41.0, y)), Some(Zone::Center), "{size:?}"); } - // A 90 pt body keeps a 30 pt center. + // A 90 pt body keeps a 45 pt center. let rect = Rect::from_min_size(pos2(10.0, 20.0), vec2(90.0, 90.0)); - assert_eq!(inner(rect), vec2(30.0, 30.0)); + assert_eq!(inner(rect), vec2(22.5, 22.5)); assert_eq!(drop_zone(rect, 0.0, depth, rect.center()), Some(Zone::Center)); - assert_eq!(drop_zone(rect, 0.0, depth, pos2(rect.left() + 29.0, rect.center().y)), Some(Zone::Left)); + assert_eq!(drop_zone(rect, 0.0, depth, pos2(rect.left() + 22.0, rect.center().y)), Some(Zone::Left)); + // A tall narrow body: its top and bottom zones are as deep as its left and right ones. + let rect = Rect::from_min_size(pos2(10.0, 20.0), vec2(120.0, 600.0)); + assert_eq!(inner(rect), vec2(30.0, 30.0)); + let top = zone_shape(rect, 0.0, depth, Zone::Top).unwrap(); + assert_eq!(top[2].y - rect.top(), 30.0); + assert_eq!(drop_zone(rect, 0.0, depth, pos2(rect.center().x, rect.top() + 29.0)), Some(Zone::Top)); + assert_eq!(drop_zone(rect, 0.0, depth, pos2(rect.center().x, rect.top() + 31.0)), Some(Zone::Center)); // The bands start below the tab header. let rect = Rect::from_min_size(pos2(10.0, 20.0), vec2(600.0, 400.0)); assert_eq!(drop_zone(rect, 24.0, depth, pos2(rect.center().x, rect.top() + 60.0)), Some(Zone::Top)); diff --git a/docs/ui-docking.md b/docs/ui-docking.md index 2cac493d9..33b27d092 100644 --- a/docs/ui-docking.md +++ b/docs/ui-docking.md @@ -211,8 +211,8 @@ A flyout fits its content the same way, without the title bar. limits and the screen. A focused resize handle moves its edge 8 pt per arrow key. - Over a frame, the tab strip and the middle join its tabs, and the nearest side splits it. The side zones are trapezoids along the four edges of the body below the tab strip, like a picture - frame: `DockStyle::zone_depth` (40 pt) deep, or a third of the body's width or height when that - is less, so every frame keeps a center. A split that would leave either half without + frame, all four the same depth: `DockStyle::zone_depth` (40 pt), or a quarter of the body's + smaller side when that is less, so every frame keeps a center. A split that would leave either half without `DockStyle::min_pane` joins the frame's tabs instead. The gap between two frames is no target. - The outer edge band (`DockStyle::edge_band`) takes a drop as a new frame across the whole window side: top and bottom, and left and right with `side_edge_zones`. It takes a quarter of From 2cad4d2bec62f80b4bf59e6376504e63ec5a25b2 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 24 Oct 2026 03:13:34 +0000 Subject: [PATCH 3/3] docking: clippy, a type for a split's children and one argument for a node and its rect --- crates/ui/src/docking/render/mod.rs | 4 ++-- crates/ui/src/docking/render/sizes.rs | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/ui/src/docking/render/mod.rs b/crates/ui/src/docking/render/mod.rs index 2f16a8d05..9867b24d6 100644 --- a/crates/ui/src/docking/render/mod.rs +++ b/crates/ui/src/docking/render/mod.rs @@ -143,7 +143,7 @@ impl Renderer<'_, P> { let actions = self.divider_actions(root, path, response.delta); self.output.actions.extend(actions); } - self.intersections(ui, node, rect, path, split.divider, mark, root); + self.intersections(ui, (node, rect), path, split.divider, mark, root); if self.dividers.len() < crate::docking::model::MAX_NODES { self.dividers.push(DrawnDivider { path: path.clone(), axis: *axis, divider: split.divider }); } @@ -285,7 +285,7 @@ impl Renderer<'_, P> { /// Four-way handles where a divider drawn inside the split `node` (since `mark`) ends on /// the split's own divider: dragging one moves both dividers. - fn intersections(&mut self, ui: &mut Ui, node: &Node

, rect: Rect, path: &[bool], divider: Rect, mark: usize, root: (&Node

, Rect)) { + fn intersections(&mut self, ui: &mut Ui, (node, rect): (&Node

, Rect), path: &[bool], divider: Rect, mark: usize, root: (&Node

, Rect)) { let Node::Split { axis, .. } = node else { return }; let side = (self.style.gap * 3.0).max(12.0); let mut found = Vec::new(); diff --git a/crates/ui/src/docking/render/sizes.rs b/crates/ui/src/docking/render/sizes.rs index 8af0dd5ae..1c44cc1ad 100644 --- a/crates/ui/src/docking/render/sizes.rs +++ b/crates/ui/src/docking/render/sizes.rs @@ -15,6 +15,9 @@ use super::{dock::dock_rects, node_limits, split_bounds}; use crate::docking::{DockContent, DockStyle, Node, PanelLimits}; use crate::layout::{SplitAxis, SplitLayout, SplitSize, split_rect}; +/// A split's two children with the rects they are drawn in. +type Children<'n, P> = [(&'n Node

, Rect); 2]; + /// Room a group keeps above its minimum after a drop, so each of its dividers can still /// travel both ways. pub(in crate::docking) const DROP_MARGIN: f32 = 1.0; @@ -169,7 +172,7 @@ impl

Sizer<'_, P> { /// The ancestors that continue the line of the split at the end of `chain` along `axis`: /// each with its children and whether the line runs through its second child, nearest first. /// The line ends at a split across the axis or beside a dock. - fn line<'n>(&self, chain: &[(&'n Node

, Rect)], path: &[bool], axis: SplitAxis) -> Vec<(usize, [(&'n Node

, Rect); 2], bool)> { + fn line<'n>(&self, chain: &[(&'n Node

, Rect)], path: &[bool], axis: SplitAxis) -> Vec<(usize, Children<'n, P>, bool)> { let mut line = Vec::new(); for depth in (0..path.len()).rev() { let Some(&(node, rect)) = chain.get(depth) else { break };