Files
craft-libs/crates/behavior/tests/dock_apply.rs
T

206 lines
9.9 KiB
Rust

//! Dock transactions and workspaces.
use craft_behavior::Axis;
use craft_behavior::dock::{
Action, Column, DockEdge, DockError, DockPlacement, Floating, Group, Layout, NamedWorkspace, Node, PanelPlace, Permissions, SplitSize, Workspace,
WorkspaceChrome, WorkspaceKeeps, WorkspaceSet, node_at,
};
use emath::{Rect, pos2, vec2};
fn rect(x: f32, y: f32, w: f32, h: f32) -> Rect {
Rect::from_min_size(pos2(x, y), vec2(w, h))
}
fn window() -> Rect {
rect(100.0, 100.0, 300.0, 400.0)
}
fn g(panels: &[u32], active: usize) -> Group<u32> {
Group::new(panels.to_vec(), active)
}
fn tabs(panels: &[u32], active: usize) -> Node<u32> {
Node::tabs(panels.to_vec(), active)
}
/// Canvas 1 above frame tabs [11, 12]; an end dock with column A ([2, 3], [4]) and column B ([5]); a floating
/// window with groups [6, 7] and [8].
fn docked() -> Layout<u32> {
let middle = Node::split(Axis::Y, SplitSize::Ratio(0.7), tabs(&[1], 0), tabs(&[11, 12], 1));
let dock = Node::dock(
DockEdge::End,
vec![Column::new(vec![g(&[2, 3], 1), g(&[4], 0)]).with_width(180.0), Column::new(vec![g(&[5], 0)]).with_width(120.0)],
);
let root = Node::split(Axis::X, SplitSize::FixedSecond(300.0), middle, dock);
Layout::new(Some(root), vec![Floating::stack(vec![g(&[6, 7], 1), g(&[8], 0)], window()).unwrap()])
}
const ALL: [u32; 10] = [1, 2, 3, 4, 5, 6, 7, 8, 11, 12];
fn allow(_: &u32) -> Permissions {
Permissions::ALL
}
fn sorted(layout: &Layout<u32>) -> Vec<u32> {
let mut panels: Vec<u32> = layout.panels().into_iter().copied().collect();
panels.sort_unstable();
panels
}
fn group_members(layout: &Layout<u32>, panel: u32) -> Vec<u32> {
match layout.place(&panel).unwrap() {
place @ (PanelPlace::Node(_) | PanelPlace::Floating { .. }) => match layout.node_of(&place).unwrap() {
Node::Tabs { panels, .. } => panels.clone(),
other => panic!("{other:?}"),
},
place => layout.group(&place).unwrap().panels.clone(),
}
}
fn end_dock(layout: &Layout<u32>) -> &Vec<Column<u32>> {
let path = layout.dock_path(DockEdge::End).unwrap();
let Some(Node::Dock { columns, .. }) = node_at(layout.root.as_ref().unwrap(), &path) else { panic!() };
columns
}
#[test]
fn apply_all_changes_everything_or_nothing_and_reports_the_closed_panels() {
let mut layout = docked();
let closed = layout.apply_all([Action::close_group(3), Action::activate(4)], allow).unwrap();
assert_eq!(closed, [2, 3]);
assert!(!layout.contains(&2));
let before = layout.clone();
let keep = |p: &u32| if *p == 4 { Permissions::ALL.closable(false) } else { Permissions::ALL };
assert_eq!(layout.apply_all([Action::close(8), Action::close(4)], keep), Err(DockError::NotAllowed));
assert_eq!(layout, before);
}
#[test]
fn reconcile_drops_unknown_panels_and_places_missing_registered_ones() {
let default = Workspace::new(docked());
let mut workspace = default.clone();
workspace.layout.apply(Action::close(5)).unwrap();
workspace.layout.apply(Action::close(12)).unwrap();
workspace.layout.apply(Action::open_at(99, 2, DockPlacement::tab(None))).unwrap();
let stray = workspace.layout.location(&99).unwrap();
workspace.hidden.push((98, stray));
let registered = |p: &u32| *p < 90;
workspace.reconcile(registered, allow, &default);
workspace.validate().unwrap();
// Unregistered panels wait in `hidden` and come back once registered.
assert_eq!(workspace.hidden.iter().map(|(p, _)| *p).collect::<Vec<_>>(), [98, 99]);
assert_eq!(sorted(&workspace.layout), ALL);
workspace.set_visible(99, true, None, false, allow).unwrap();
assert_eq!(group_members(&workspace.layout, 2), [2, 3, 99]);
assert_eq!(end_dock(&workspace.layout).len(), 2);
assert_eq!(group_members(&workspace.layout, 11), [11, 12]);
let mut broken = default.clone();
broken.layout.floating[0].rect = Rect::from_min_size(pos2(100.0, 100.0), vec2(-1.0, 400.0));
broken.reconcile(registered, allow, &default);
assert_eq!(broken.layout, default.layout);
}
#[test]
fn a_workspace_set_with_a_floating_group_round_trips_through_json() {
let layout = Layout::new(Some(Node::tabs(vec![1u32, 2], 0)), vec![Floating::single(vec![3u32], 0, rect(1.0, 2.0, 300.0, 200.0)).with_id(5)]);
let mut set = WorkspaceSet::new(vec![NamedWorkspace::new("essentials", "Essentials", Workspace::default())]);
set.save("Mine", Workspace::new(layout), WorkspaceChrome::default(), WorkspaceKeeps::new(false, true, true)).unwrap();
let saved = serde_json::to_string(&set).unwrap();
let mut loaded: WorkspaceSet<u32> = serde_json::from_str(&saved).unwrap();
assert!(loaded.builtin.is_empty(), "built-ins are code, not stored");
loaded.builtin = set.builtin.clone();
assert_eq!(loaded, set);
assert!(loaded.saved[0].keeps.toolbar && loaded.saved[0].keeps.menus, "the captures are stored with the workspace");
let older = saved.replace(",\"keeps_toolbar\":true", "");
assert_ne!(older, saved, "the file names keeps_toolbar");
let older: WorkspaceSet<u32> = serde_json::from_str(&older).unwrap();
assert_eq!(older.saved[0].keeps, WorkspaceKeeps::new(false, true, false), "a file from before the toolbar box loads with it off");
}
/// A workspace whose layout has a bottom frame (Motion's Timeline) shows it when selected and drops it when another
/// workspace is selected; the host sees the switch in the live layout.
#[test]
fn a_workspace_with_a_bottom_pane_opens_and_closes_it_on_switch() {
const TIMELINE: u32 = 9;
let photography = Workspace::new(Layout::new(Some(tabs(&[1, 2], 0)), Vec::new()));
let motion =
Workspace::new(Layout::new(Some(Node::split(Axis::Y, SplitSize::FixedSecond(200.0), tabs(&[1, 2], 0), tabs(&[TIMELINE], 0))), Vec::new()));
let mut set = WorkspaceSet::new(vec![
NamedWorkspace::new("photography", "Photography", photography.clone()),
NamedWorkspace::new("motion", "Motion", motion),
]);
let mut live = photography;
set.select("motion", &mut live).unwrap();
let bottom = live.layout.place(&TIMELINE).unwrap();
assert_eq!(bottom, PanelPlace::Node(vec![true]), "the Timeline is the second (bottom) frame of the split");
set.select("photography", &mut live).unwrap();
assert!(!live.layout.contains(&TIMELINE), "leaving Motion closes the Timeline");
set.select("motion", &mut live).unwrap();
assert!(live.layout.contains(&TIMELINE), "coming back opens it again");
}
/// Switching and resetting replace the live workspace and move it to a new revision; edits and selecting the shown
/// workspace keep it, and the revision is neither saved nor compared.
#[test]
fn only_a_replacement_changes_the_live_revision() {
let essentials = Workspace::new(Layout::new(Some(tabs(&[1, 2], 0)), Vec::new()));
let painting = Workspace::new(Layout::new(Some(tabs(&[3], 0)), Vec::new()));
let mut set = WorkspaceSet::new(vec![
NamedWorkspace::new("essentials", "Essentials", essentials.clone()),
NamedWorkspace::new("painting", "Painting", painting),
]);
let mut live = essentials.clone();
let start = live.revision();
live.apply(Action::activate(2), false, allow).unwrap();
live.set_visible(1, false, None, false, allow).unwrap();
set.select("essentials", &mut live).unwrap();
assert_eq!(live.revision(), start, "edits and selecting the shown workspace keep the revision");
set.select("painting", &mut live).unwrap();
let switched = live.revision();
assert!(switched != start && live.layout.contains(&3), "a switch replaces the live workspace");
set.reset(&mut live).unwrap();
assert!(live.revision() != switched, "a reset replaces it again");
set.select("essentials", &mut live).unwrap();
assert!(!live.layout.contains(&1), "the switch back shows the edited essentials");
let loaded: Workspace<u32> = serde_json::from_str(&serde_json::to_string(&live).unwrap()).unwrap();
assert_eq!(loaded, live, "the revision is not saved and not part of equality");
}
/// Open of a hidden panel follows its record: a floating panel returns to its window; a record whose anchor is gone
/// falls back to the anchor Open names.
#[test]
fn opening_a_closed_floating_panel_restores_its_window_place() {
let mut workspace = Workspace::new(docked());
workspace.apply(Action::close(8), false, allow).unwrap();
assert!(workspace.layout.place(&8).is_none());
workspace.apply(Action::open(8, Some(11)), false, allow).unwrap();
assert!(matches!(workspace.layout.place(&8), Some(PanelPlace::Floating { .. })), "back in its window");
assert!(workspace.hidden.is_empty());
let mut stray = workspace.layout.location(&5).unwrap();
stray.anchor = Some(99);
workspace.hidden.push((50, stray));
workspace.apply(Action::open(50, Some(11)), false, allow).unwrap();
assert_eq!(group_members(&workspace.layout, 11), [11, 12, 50]);
}
/// A panel that floats keeps its docked place until it docks or closes; floating again from the float keeps the first.
#[test]
fn a_floated_panel_keeps_where_it_was_docked_until_it_docks_or_closes() {
let mut workspace = Workspace::new(docked());
let docked_at = workspace.layout.location(&2).unwrap();
workspace.apply(Action::float(2, rect(10.0, 10.0, 200.0, 200.0)), false, allow).unwrap();
assert_eq!(workspace.floated, [(2, docked_at.clone())]);
workspace.apply(Action::float(2, rect(30.0, 30.0, 200.0, 200.0)), false, allow).unwrap();
assert_eq!(workspace.floated, [(2, docked_at)], "floating again from the float keeps the first record");
workspace.apply(Action::move_beside(2, 11, DockPlacement::tab(None)), false, allow).unwrap();
assert!(workspace.floated.is_empty(), "docking drops the record");
workspace.apply(Action::float(3, rect(10.0, 10.0, 200.0, 200.0)), false, allow).unwrap();
assert_eq!(workspace.floated.len(), 1);
workspace.apply(Action::close(3), false, allow).unwrap();
assert!(workspace.floated.is_empty(), "closing drops the record");
}