diff --git a/crates/ui-panels/src/docking/panels.rs b/crates/ui-panels/src/docking/panels.rs index a403a80ef..e1069fadd 100644 --- a/crates/ui-panels/src/docking/panels.rs +++ b/crates/ui-panels/src/docking/panels.rs @@ -32,10 +32,10 @@ pub trait DockPanels

{ 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 { None } diff --git a/crates/ui-patterns/src/patterns/dock/area.rs b/crates/ui-patterns/src/patterns/dock/area.rs index 91bc9f913..16e3c09c6 100644 --- a/crates/ui-patterns/src/patterns/dock/area.rs +++ b/crates/ui-patterns/src/patterns/dock/area.rs @@ -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 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 = members diff --git a/crates/ui-patterns/tests/it/docking/group_view.rs b/crates/ui-patterns/tests/it/docking/group_view.rs new file mode 100644 index 000000000..a92f50c55 --- /dev/null +++ b/crates/ui-patterns/tests/it/docking/group_view.rs @@ -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>>, +} + +impl DockPanels 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 { + 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:?}"); +} diff --git a/crates/ui-patterns/tests/it/docking/mod.rs b/crates/ui-patterns/tests/it/docking/mod.rs index e2641c3e1..9c195ac25 100644 --- a/crates/ui-patterns/tests/it/docking/mod.rs +++ b/crates/ui-patterns/tests/it/docking/mod.rs @@ -18,6 +18,7 @@ mod floating; mod floating_area; mod columns; +mod group_view; mod workspace_dialogs; mod native; diff --git a/docs/ui-docking.md b/docs/ui-docking.md index bfcf59803..af57a184b 100644 --- a/docs/ui-docking.md +++ b/docs/ui-docking.md @@ -55,7 +55,7 @@ The host implements `DockPanels

`: | `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. |