Dock area: a group view lays out in a Ui held to its group's rect and clipped to it

This commit is contained in:
aiongg
2026-11-16 16:00:29 +00:00
parent 310439e6b4
commit e86e041e13
5 changed files with 74 additions and 7 deletions
+4 -4
View File
@@ -32,10 +32,10 @@ pub trait DockPanels<P> {
fn body(&mut self, panel: &P, ui: &mut Ui, cx: &mut BodyCx<'_>);
/// Takes the group holding `panels`, whose tab `active` is selected, in place of its tab strip and body: at
/// `GroupAt::Show(rect)` it shows its view in `rect`, at `GroupAt::Measure(width)` it measures that view at `width`
/// the way it shows it, and it returns the height the view takes (at `Show`, the rect's). `None` (the default)
/// leaves the group to the dock, at both. A canvas group uses the panels to tell which document it holds and draws
/// it at the group's rect.
/// `GroupAt::Show(rect)` it shows its view in `ui`, which the dock holds to `rect` and clips to it; at
/// `GroupAt::Measure(width)` it measures that view at `width` the way it shows it. It returns the height the view
/// takes (at `Show`, the rect's). `None` (the default) leaves the group to the dock, at both. A canvas group uses
/// the panels to tell which document it holds and draws it in the room `ui` has.
fn group_view(&mut self, _panels: &[P], _active: usize, _ui: &mut Ui, _at: GroupAt, _cx: &mut BodyCx<'_>) -> Option<f32> {
None
}
+14 -2
View File
@@ -24,7 +24,7 @@ use craft_behavior::dock::{
Action, Column, DockEdge, DockError, Group, Layout, Location, Node, PanelPlace, Permissions, SplitSize, Workspace, node_at,
};
use craft_behavior::{CancelReason, DockSide, EditEvent, EditEvents, Flow, GestureId, GesturePhase, Mixed, Proposal};
use egui::{Context, Id, Pos2, Rect, Response, Ui, Vec2, ViewportId};
use egui::{Context, Id, Pos2, Rect, Response, Ui, UiBuilder, Vec2, ViewportId};
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::fmt::Debug;
@@ -758,7 +758,19 @@ impl<P: Clone + Eq + Hash + Debug + Send + Sync + 'static> Pass<'_, P> {
let scene = self.scene;
let anchor = members.first()?;
let active = active.min(members.len() - 1);
if let Some(height) = self.panels.group_view(members, active, ui, at, body) {
let viewed = match at {
// The view draws in a child `Ui` held to the group's rect and clipped to it: it lays out in the group's
// room, never in the room the area has left.
GroupAt::Show(rect) => {
ui.scope_builder(UiBuilder::new().max_rect(rect), |ui| {
ui.shrink_clip_rect(rect);
self.panels.group_view(members, active, ui, at, body)
})
.inner
}
GroupAt::Measure(_) => self.panels.group_view(members, active, ui, at, body),
};
if let Some(height) = viewed {
return matches!(at, GroupAt::Measure(_)).then_some(height);
}
let titles: Vec<PanelTitle> = members
@@ -0,0 +1,54 @@
//! A group the host takes with `group_view` lays out in the group's rect, not in the room the dock area has left.
use craft_behavior::Axis;
use craft_behavior::dock::{Layout, Node, SplitSize, Workspace};
use craft_ui::contract::{Bind, BodyCx};
use craft_ui::docking::{DockConfig, DockPanels, GroupAt, PanelSpec};
use craft_ui::patterns::dock::DockArea;
use craft_ui::testing::{Fixture, trial};
use egui::{Rect, Ui, Vec2};
use std::cell::Cell;
use std::rc::Rc;
/// Panel 0 is a canvas the host draws in its group's place; it records the group's rect and the `Ui` it was given.
struct Panels {
seen: Rc<Cell<Option<(Rect, Rect, Rect)>>>,
}
impl DockPanels<usize> for Panels {
fn spec(&self, panel: &usize) -> PanelSpec {
let spec = PanelSpec::new(format!("Panel {panel}"));
if *panel == 0 { spec.canvas() } else { spec }
}
fn body(&mut self, _: &usize, _: &mut Ui, _: &mut BodyCx<'_>) {}
fn group_view(&mut self, panels: &[usize], _: usize, ui: &mut Ui, at: GroupAt, _: &mut BodyCx<'_>) -> Option<f32> {
if !panels.contains(&0) {
return None;
}
match at {
GroupAt::Measure(_) => Some(0.0),
GroupAt::Show(rect) => {
self.seen.set(Some((rect, ui.max_rect(), ui.clip_rect())));
Some(rect.height())
}
}
}
}
#[trial]
fn a_group_view_lays_out_and_clips_to_its_group_rect() {
let seen = Rc::new(Cell::new(None));
let root = Node::split(Axis::X, SplitSize::Ratio(0.5), Node::tabs(vec![0], 0), Node::tabs(vec![1], 0));
let mut workspace = Workspace::new(Layout::new(Some(root), Vec::new()));
let mut panels = Panels { seen: seen.clone() };
let mut fixture = Fixture::new(move |ui| {
DockArea::new("Dock", Bind::bound(&mut workspace), &mut panels, DockConfig::default()).show(ui);
});
fixture.room(Vec2::new(1200.0, 600.0)).settle();
let (rect, room, clip) = seen.get().expect("the canvas group was shown");
assert!(rect.width() < 1000.0, "the canvas group takes part of the area: {rect:?}");
assert!(rect.contains_rect(room), "the view's room {room:?} lies in its group's rect {rect:?}");
assert!(rect.contains_rect(clip), "the view's clip {clip:?} lies in its group's rect {rect:?}");
}
@@ -18,6 +18,7 @@ mod floating;
mod floating_area;
mod columns;
mod group_view;
mod workspace_dialogs;
mod native;
+1 -1
View File
@@ -55,7 +55,7 @@ The host implements `DockPanels<P>`:
| `spec` | The panel's `PanelSpec`, read every pass: label, icon, `SizeLimits`, `DockHeights`, float size, `allow_native`, `window_kind`, `canvas`. |
| `permissions` | The panel's `Permissions` (default `ALL`). |
| `body` | Shows the panel's body. |
| `group_view` | Takes a whole group in place of its tab strip and body; a canvas group draws its document here. Default `None`. |
| `group_view` | Takes a whole group in place of its tab strip and body, in a `Ui` held to the group's rect; a canvas group draws its document here. Default `None`. |
| `group_header` | The host's controls in a group header, before the tab strip. |
| `panel_rows` | The host's rows of a panel's menu, before the dock's own. |