mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
Each dock column (docked, collapsed to icons or in a floating window) shows as a memoized view keyed by the pruned layout, its place, the area's requests and the versions of the panels it shows (DockPanels::content). The docked tree is one view keyed the same way over every panel it shows, so an idle dock whose panels all have versions replays as one record; a column or tree with an unversioned panel runs as before. What a replay must still report (gesture sources, shown groups, placed columns) comes back as the view's value, and the focused group is read from the shown groups' ids after the views. The docks' natural widths and card places are derived per layout, sizes and config, and SplitArea derives its frames, handles and their ranges from the caller's revision of its layout and rules (a required argument) instead of walking the tree five times per run. Spec lookups in the area return references. Two fixes the replay audit found: DockGroup's header drained its command states in a measuring run, so the real run after it published none; and a group reported its rect and sources from measuring runs as well, doubling them in the first pass.
223 lines
9.3 KiB
Rust
223 lines
9.3 KiB
Rust
//! Drops through the drag session: a dock's `DockDropTarget` takes a tab or a group grip drag, shows the mark
|
|
//! and the released drop applies to the layout.
|
|
|
|
use craft_behavior::Axis;
|
|
use craft_behavior::dock::{Layout, Node, Permissions, SplitSize, Workspace};
|
|
use craft_ui::contract::SizeLimits;
|
|
use craft_ui::contract::{
|
|
Bind, BodyCx, Common, Component, Cx, MeasureCx, MemoKey, PlainData, Pointer as _, Role, Show as _, ShowContainer as _, Tracked,
|
|
};
|
|
use craft_ui::docking::tree::{self, FrameRules};
|
|
use craft_ui::docking::{
|
|
DockConfig, DockDropTarget, DockDropped, DockGroup, DockPanels, DockSizes, GroupDrag, PanelDrag, PanelKey, PanelSpec, PanelSpecs, PanelTitle,
|
|
SplitArea, Titles,
|
|
};
|
|
use craft_ui::patterns::dock::DockArea;
|
|
use craft_ui::runtime::drag;
|
|
use craft_ui::style::{ComponentStyle, StyleCx};
|
|
use craft_ui::testing::pointer::{moved, press, release};
|
|
use craft_ui::testing::{Fixture, trial};
|
|
use egui::{Id, Pos2, Rect, Response, Ui, Vec2};
|
|
use std::cell::RefCell;
|
|
use std::rc::Rc;
|
|
use std::sync::Arc;
|
|
|
|
/// The docking sizes with a one-point divider, a 40-point least pane and a 24-point strip.
|
|
fn sizes() -> DockSizes {
|
|
super::sizes(1.0, 40.0, 24.0)
|
|
}
|
|
const AREA: Vec2 = Vec2::new(400.0, 300.0);
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq, ComponentStyle)]
|
|
#[non_exhaustive]
|
|
pub struct HostStyle {
|
|
#[style(metrics)]
|
|
metrics: craft_behavior::Metrics,
|
|
}
|
|
|
|
/// The dock area: it registers the dock's drop target over the whole area, as a `DockArea` does.
|
|
#[derive(Component)]
|
|
struct Host<'a> {
|
|
#[common]
|
|
common: Common,
|
|
target: &'a DockDropTarget<'a, String>,
|
|
seen: &'a RefCell<Vec<DockDropped<String>>>,
|
|
}
|
|
|
|
impl Component for Host<'_> {
|
|
const ROLE: Role = Role::GROUP;
|
|
type Style = HostStyle;
|
|
type Output = Response;
|
|
|
|
fn style(_: &StyleCx<'_>, _: HostStyleKey) -> HostStyle {
|
|
HostStyle::default()
|
|
}
|
|
|
|
fn measure(&mut self, _: &MeasureCx<'_, Self>) -> Vec2 {
|
|
AREA
|
|
}
|
|
|
|
fn update(&mut self, cx: &mut Cx<'_, Self>) {
|
|
if let Some(dropped) = cx.drop_target::<GroupDrag<String>, _>(self.target) {
|
|
self.seen.borrow_mut().push(dropped);
|
|
} else if let Some(dropped) = cx.drop_target::<PanelDrag, _>(self.target) {
|
|
self.seen.borrow_mut().push(dropped);
|
|
}
|
|
}
|
|
|
|
fn finish(self, cx: Cx<'_, Self>) -> Response {
|
|
cx.response.clone()
|
|
}
|
|
}
|
|
|
|
fn dock_id() -> Id {
|
|
Id::NULL.with("session dock")
|
|
}
|
|
|
|
fn panels(names: &[&str]) -> Vec<String> {
|
|
names.iter().map(|name| (*name).to_owned()).collect()
|
|
}
|
|
|
|
/// Two groups whose left one is wide enough to leave empty room in its header, which is its drag handle.
|
|
fn wide_left_group() -> Layout<String> {
|
|
let root = Node::split(Axis::X, SplitSize::Ratio(0.5), Node::tabs(panels(&["Layers", "Channels"]), 0), Node::tabs(panels(&["Properties"]), 0));
|
|
Layout::new(Some(root), Vec::new())
|
|
}
|
|
|
|
/// The panels of each group, in reading order.
|
|
fn groups(layout: &Layout<String>) -> Vec<Vec<String>> {
|
|
let limits = |_: &String| SizeLimits::default();
|
|
let rules = FrameRules::new(&limits);
|
|
let Some(root) = layout.root.as_ref() else { return Vec::new() };
|
|
let area = Rect::from_min_size(Pos2::ZERO, AREA);
|
|
tree::frames(root, area, &sizes(), &rules, false)
|
|
.groups
|
|
.iter()
|
|
.map(|group| match group.node {
|
|
Node::Tabs { panels, .. } => panels.clone(),
|
|
_ => Vec::new(),
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// The anchor, panels and rect of each group of the layout in an area at `origin`.
|
|
fn placed_groups(layout: &Layout<String>, origin: Pos2) -> Vec<(String, Vec<String>, Rect)> {
|
|
let limits = |_: &String| SizeLimits::default();
|
|
let rules = FrameRules::new(&limits);
|
|
let Some(root) = layout.root.as_ref() else { return Vec::new() };
|
|
let frames = tree::frames(root, Rect::from_min_size(origin, AREA), &sizes(), &rules, false);
|
|
frames
|
|
.groups
|
|
.iter()
|
|
.filter_map(|group| match (group.anchor(), group.node) {
|
|
(Some(anchor), Node::Tabs { panels, .. }) => Some((anchor.clone(), panels.clone(), group.rect)),
|
|
_ => None,
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
type Bed = (Fixture, Rc<RefCell<Layout<String>>>, Rc<RefCell<Vec<DockDropped<String>>>>);
|
|
|
|
/// A dock of the layout with a host that applies the drops it takes. The groups show in the body of a `SplitArea`, as a
|
|
/// `DockArea` shows them, so each is a member of the area's arena.
|
|
fn bed(layout: Layout<String>) -> Bed {
|
|
let layout = Rc::new(RefCell::new(layout));
|
|
let taken = Rc::new(RefCell::new(Vec::new()));
|
|
let (shared_layout, shared_taken) = (Rc::clone(&layout), Rc::clone(&taken));
|
|
let fixture = Fixture::new(move |ui| {
|
|
let shown = shared_layout.borrow().clone();
|
|
let permissions = |_: &String| Permissions::default();
|
|
let ctx = ui.ctx().clone();
|
|
let target = DockDropTarget::new(&ctx, dock_id(), &shown, &permissions);
|
|
let origin = ui.max_rect().min;
|
|
Host { common: Common::new(), target: &target, seen: &shared_taken }.show(ui);
|
|
let limits = |_: &String| SizeLimits::default();
|
|
let placed = placed_groups(&shown, origin);
|
|
// The layout's saved form stands for it: the rules read nothing else.
|
|
let revision = MemoKey::of(&serde_json::to_value(&shown).expect("a layout saves"));
|
|
SplitArea::new("Panels", Bind::given(shown.clone()), FrameRules::new(&limits), revision).show_at(
|
|
ui,
|
|
Rect::from_min_size(origin, AREA),
|
|
|ui, _| {
|
|
for (anchor, panels, rect) in &placed {
|
|
let titles: Vec<PanelTitle> = panels.iter().map(|panel| PanelTitle::new(PanelKey::of(panel), panel.clone())).collect();
|
|
let active = titles.first().map_or(PanelKey::new(0), |title| title.key);
|
|
DockGroup::new(anchor.clone(), Titles::from_content(titles.clone()), active).dock(dock_id()).show_at(ui, *rect, |_, _| ());
|
|
}
|
|
},
|
|
);
|
|
for dropped in shared_taken.borrow_mut().drain(..) {
|
|
shared_layout.borrow_mut().apply_with_permissions(dropped.action, permissions).expect("the model takes what the dock accepted");
|
|
}
|
|
});
|
|
(fixture, layout, taken)
|
|
}
|
|
|
|
/// Presses the middle of `from` and moves it to `to` in steps, leaving the button down.
|
|
fn hold_and_move(fixture: &mut Fixture, from: Pos2, to: Pos2) {
|
|
fixture.frame(vec![moved(from)]);
|
|
fixture.frame(vec![press(from)]);
|
|
for step in 1..=4 {
|
|
fixture.frame(vec![moved(from + (to - from) * (step as f32 / 4.0))]);
|
|
}
|
|
fixture.idle(1);
|
|
}
|
|
|
|
/// The drag handle of the left group: the first button of its header, over the empty room right of its tabs.
|
|
fn left_grip(fixture: &Fixture) -> Pos2 {
|
|
let mut buttons = fixture.by_role(Role::BUTTON);
|
|
buttons.sort_by(|a, b| a.rect.min.x.total_cmp(&b.rect.min.x));
|
|
buttons[0].center()
|
|
}
|
|
|
|
#[trial]
|
|
fn a_header_drag_shows_the_drop_mark_over_a_group_side_and_docks_on_release() {
|
|
let (mut fixture, layout, taken) = bed(wide_left_group());
|
|
fixture.settle();
|
|
let grip = left_grip(&fixture);
|
|
hold_and_move(&mut fixture, grip, Pos2::new(395.0, 150.0));
|
|
assert!(drag::active::<GroupDrag<String>>(fixture.ctx()).is_some(), "the header drag started a group drag of the dock's panel type");
|
|
assert!(drag::mark(fixture.ctx()).is_some(), "the dock's target accepts the drag over a group side");
|
|
fixture.frame(vec![release(Pos2::new(395.0, 150.0))]);
|
|
fixture.idle(2);
|
|
assert!(taken.borrow().is_empty(), "the drop was applied in the pass it was taken");
|
|
assert_eq!(groups(&layout.borrow()).len(), 2, "the group moved beside the right group");
|
|
assert_eq!(groups(&layout.borrow())[1], panels(&["Layers", "Channels"]));
|
|
}
|
|
|
|
/// Panels with no body: the drag is all the trial needs.
|
|
#[derive(PlainData)]
|
|
struct Named;
|
|
|
|
impl PanelSpecs<String> for Named {
|
|
fn spec(&self, panel: &String) -> PanelSpec {
|
|
PanelSpec::new(panel.clone())
|
|
}
|
|
}
|
|
|
|
impl DockPanels<String> for Named {
|
|
fn body(&mut self, _: &String, _: &mut Ui, _: &mut BodyCx<'_>) {}
|
|
}
|
|
|
|
#[trial]
|
|
fn a_drag_started_under_another_workspace_revision_ends_when_the_workspace_is_replaced() {
|
|
let workspace = Rc::new(RefCell::new(Arc::new(Workspace::new(wide_left_group()))));
|
|
let (shared, specs) = (Rc::clone(&workspace), Tracked::new(Named));
|
|
let mut fixture = Fixture::new(move |ui| {
|
|
let mut held = shared.borrow_mut();
|
|
DockArea::new("Replaced dock", Bind::bound(&mut *held), &specs, &mut Named, DockConfig::default()).show(ui);
|
|
});
|
|
fixture.settle();
|
|
let grip = left_grip(&fixture);
|
|
hold_and_move(&mut fixture, grip, Pos2::new(395.0, 150.0));
|
|
assert!(drag::active::<GroupDrag<String>>(fixture.ctx()).is_some(), "the header drag started a group drag in the dock");
|
|
fixture.frame(Vec::new());
|
|
assert!(drag::active::<GroupDrag<String>>(fixture.ctx()).is_some(), "the dock's own passes keep the drag");
|
|
let replacement = Workspace::new(Layout::new(Some(Node::tabs(panels(&["Properties"]), 0)), Vec::new()));
|
|
Arc::make_mut(&mut workspace.borrow_mut()).replace(replacement);
|
|
fixture.frame(Vec::new());
|
|
fixture.idle(1);
|
|
assert!(drag::active::<GroupDrag<String>>(fixture.ctx()).is_none(), "replacing the workspace ended the drag");
|
|
assert_eq!(groups(workspace.borrow().layout()), vec![panels(&["Properties"])], "the new workspace's layout stays as it was set");
|
|
}
|