mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 19:13:28 +00:00
Merge pull request #2099 from davidmann4/fix/photocraft-12517-column-only-panels
dock: panels that dock only in dock columns (photocraft#12517)
This commit is contained in:
@@ -22,8 +22,8 @@ impl<P: Clone + Eq + Hash> Layout<P> {
|
||||
/// action closed (see [`Self::closed_by`]) so the application can release their state.
|
||||
/// The callback must be stable throughout one call. Registry validation remains with the
|
||||
/// application, particularly Open. The column rights ([`Permissions::collapses`],
|
||||
/// [`Permissions::shares_column`]) are checked on the result: an action that leaves a panel breaking them,
|
||||
/// which did not before, is refused, so a drag's dry run refuses it alike.
|
||||
/// [`Permissions::shares_column`], [`Permissions::docks_outside_columns`]) are checked on the result: an action
|
||||
/// that leaves a panel breaking them, which did not before, is refused, so a drag's dry run refuses it alike.
|
||||
pub fn apply_with_permissions(&mut self, action: Action<P>, permissions: impl Fn(&P) -> Permissions) -> Result<Vec<P>, DockError> {
|
||||
self.validate()?;
|
||||
let action = match action {
|
||||
@@ -125,10 +125,12 @@ impl<P: Clone + Eq + Hash> Layout<P> {
|
||||
}
|
||||
|
||||
/// The panels that break their column rights: a panel that does not collapse ([`Permissions::collapses`]) in
|
||||
/// a collapsed column, and every panel of a column (or node of the docked tree) that holds a panel that does
|
||||
/// not share it ([`Permissions::shares_column`]) beside another panel.
|
||||
/// a collapsed column, every panel of a column (or node of the docked tree) that holds a panel that does
|
||||
/// not share it ([`Permissions::shares_column`]) beside another panel, and a panel that docks only in dock
|
||||
/// columns ([`Permissions::docks_outside_columns`]) in a frame of the docked tree.
|
||||
pub(super) fn column_breaches(&self, permissions: &impl Fn(&P) -> Permissions) -> HashSet<P> {
|
||||
let mut breaches = HashSet::new();
|
||||
let mut framed = Vec::new();
|
||||
let mut check = |panels: Vec<&P>, iconic: bool| {
|
||||
if iconic {
|
||||
breaches.extend(panels.iter().filter(|p| !permissions(p).collapses).map(|p| (*p).clone()));
|
||||
@@ -147,9 +149,14 @@ impl<P: Clone + Eq + Hash> Layout<P> {
|
||||
check(column.panels().collect(), column.iconic);
|
||||
}
|
||||
}
|
||||
leaf => check(tree_panels(leaf), false),
|
||||
leaf => {
|
||||
let panels = tree_panels(leaf);
|
||||
framed.extend(panels.iter().filter(|p| !permissions(p).docks_outside_columns).map(|p| (*p).clone()));
|
||||
check(panels, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
breaches.extend(framed);
|
||||
breaches
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,10 @@ pub struct Permissions {
|
||||
/// panels: refused are tab, center, group and seam placements across that boundary in either direction, while a
|
||||
/// new column, a new dock and a float stay allowed. A toolbar takes its own column.
|
||||
pub shares_column: bool,
|
||||
/// The panel may dock outside a dock column: in a frame of the docked tree, as a split beside another frame or at
|
||||
/// an edge of the docked area, where it can lie across the window. Without it the panel docks only in dock columns
|
||||
/// or floats: refused is any action that leaves it in a frame, so a toolbar is always a vertical strip.
|
||||
pub docks_outside_columns: bool,
|
||||
}
|
||||
|
||||
impl Default for Permissions {
|
||||
@@ -35,9 +39,18 @@ impl Default for Permissions {
|
||||
}
|
||||
|
||||
impl Permissions {
|
||||
/// Every right: the panel moves, floats, closes, takes tabs and splits, collapses and shares its column.
|
||||
pub const ALL: Self =
|
||||
Self { movable: true, floatable: true, closable: true, accepts_tabs: true, accepts_splits: true, collapses: true, shares_column: true };
|
||||
/// Every right: the panel moves, floats, closes, takes tabs and splits, collapses, shares its column and docks
|
||||
/// outside dock columns.
|
||||
pub const ALL: Self = Self {
|
||||
movable: true,
|
||||
floatable: true,
|
||||
closable: true,
|
||||
accepts_tabs: true,
|
||||
accepts_splits: true,
|
||||
collapses: true,
|
||||
shares_column: true,
|
||||
docks_outside_columns: true,
|
||||
};
|
||||
|
||||
/// A document canvas: it stays docked and open, and takes neither tabs nor side splits.
|
||||
/// A group dropped on it floats there. An application that opens panels beside its canvas
|
||||
@@ -86,6 +99,12 @@ impl Permissions {
|
||||
self
|
||||
}
|
||||
|
||||
/// These permissions with [`Self::docks_outside_columns`] set to `docks_outside_columns`.
|
||||
pub const fn docks_outside_columns(mut self, docks_outside_columns: bool) -> Self {
|
||||
self.docks_outside_columns = docks_outside_columns;
|
||||
self
|
||||
}
|
||||
|
||||
/// A canvas: it can neither move, close nor take tabs. `accepts_splits` and `floatable`
|
||||
/// do not matter, so a canvas that opens panels beside it still counts.
|
||||
pub fn is_protected(self) -> bool {
|
||||
|
||||
@@ -10,7 +10,7 @@ use super::{
|
||||
DockSide,
|
||||
action::{Action, DockPlacement, Location},
|
||||
edit::tree_panels,
|
||||
groups::{Column, PanelPlace, Target},
|
||||
groups::{Column, DockEdge, PanelPlace, Target},
|
||||
layout::Layout,
|
||||
limits::MAX_PANELS,
|
||||
node::Node,
|
||||
@@ -285,8 +285,9 @@ impl<P: Clone + Eq + Hash> Workspace<P> {
|
||||
/// A registered panel that `permissions` says cannot close, such as a protected canvas, is
|
||||
/// always shown: at its recorded or default place, else at the outer edge of the docked
|
||||
/// area. A layout that breaks the column rights is repaired: a collapsed column holding a panel that does not
|
||||
/// collapse ([`Permissions::collapses`]) expands, and a panel that shares no column
|
||||
/// ([`Permissions::shares_column`]) moves to a column of its own. Pass the panels' own permissions, not
|
||||
/// collapse ([`Permissions::collapses`]) expands, a panel that shares no column
|
||||
/// ([`Permissions::shares_column`]) moves to a column of its own, and a panel that docks only in dock columns
|
||||
/// ([`Permissions::docks_outside_columns`]) moves from a frame to a dock column. Pass the panels' own permissions, not
|
||||
/// [`Permissions::locked`] ones.
|
||||
/// `hidden` keeps at most [`MAX_PANELS`] entries, dropping the oldest unregistered ones first.
|
||||
pub fn reconcile(&mut self, registered: impl Fn(&P) -> bool, permissions: impl Fn(&P) -> Permissions, default: &Workspace<P>) {
|
||||
@@ -347,10 +348,41 @@ impl<P: Clone + Eq + Hash> Workspace<P> {
|
||||
}
|
||||
|
||||
impl<P: Clone + Eq + Hash> Workspace<P> {
|
||||
/// Moves each panel that docks only in dock columns ([`Permissions::docks_outside_columns`]) out of a frame of the
|
||||
/// docked tree: to a new dock at the start edge, else a new dock at the end edge, else a column of its own at the
|
||||
/// outer side of the first docked column. A move the layout refuses leaves the panel where it was.
|
||||
fn repair_framed(&mut self, permissions: &impl Fn(&P) -> Permissions) {
|
||||
let framed: Vec<P> = self
|
||||
.layout
|
||||
.panels()
|
||||
.into_iter()
|
||||
.filter(|p| !permissions(p).docks_outside_columns && matches!(self.layout.place(p), Some(PanelPlace::Node(_))))
|
||||
.cloned()
|
||||
.collect();
|
||||
for panel in framed {
|
||||
let column_anchor =
|
||||
self.layout.panels().into_iter().find(|p| *p != &panel && matches!(self.layout.place(p), Some(PanelPlace::Dock { .. }))).cloned();
|
||||
let targets = [Some(Target::NewDock { side: DockEdge::Start }), Some(Target::NewDock { side: DockEdge::End })]
|
||||
.into_iter()
|
||||
.chain([column_anchor.map(|anchor| Target::At { anchor, placement: DockPlacement::Column { after: false } })]);
|
||||
for to in targets.flatten() {
|
||||
let mut layout = self.layout.clone();
|
||||
let moved = layout.apply_inner(Action::MovePanel { panel: panel.clone(), to }, &|p| permissions(p).is_protected()).is_ok();
|
||||
if moved && layout.validate().is_ok() {
|
||||
self.layout = layout;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves each panel that shares no column but has company to a column of its own: a new dock column beside its
|
||||
/// column, a new window over its window, or a frame beside its node of the docked tree. Then expands each
|
||||
/// collapsed column that holds a panel that does not collapse. A repair the layout refuses leaves it as it was.
|
||||
/// collapsed column that holds a panel that does not collapse, and moves each panel that docks only in dock
|
||||
/// columns out of a frame of the docked tree into a dock column ([`Self::repair_framed`]). A repair the layout
|
||||
/// refuses leaves it as it was.
|
||||
fn repair_columns(&mut self, permissions: &impl Fn(&P) -> Permissions) {
|
||||
self.repair_framed(permissions);
|
||||
let lonely: Vec<P> = self.layout.panels().into_iter().filter(|p| !permissions(p).shares_column).cloned().collect();
|
||||
for panel in lonely {
|
||||
let Some(place) = self.layout.place(&panel) else { continue };
|
||||
|
||||
@@ -355,6 +355,51 @@ fn a_panel_that_shares_no_column_moves_only_to_a_column_of_its_own() {
|
||||
assert_eq!(sorted(&saved.layout), [1, 2, 3, 4, 6]);
|
||||
}
|
||||
|
||||
/// A panel that docks only in dock columns (a toolbar, photocraft#12517) refuses every placement that leaves it in a
|
||||
/// frame of the docked tree: a split beside the canvas on any side and an edge of the docked area, so it never lies
|
||||
/// across the window. A new dock, a column beside another and a float still take it, other panels still move while
|
||||
/// one sits in a frame (only a new breach is refused), and reconcile moves a saved one into a dock column.
|
||||
#[test]
|
||||
fn a_panel_that_docks_only_in_columns_never_lands_in_a_frame() {
|
||||
let strip = |p: &u32| match p {
|
||||
2 => Permissions::ALL.shares_column(false).docks_outside_columns(false),
|
||||
1 => Permissions::PROTECTED.accepts_splits(true),
|
||||
_ => Permissions::ALL,
|
||||
};
|
||||
let layout = columns();
|
||||
let sides = [DockSide::Top, DockSide::Bottom, DockSide::Start, DockSide::End];
|
||||
let framed = sides.iter().map(|side| DockPlacement::Split(*side)).chain(sides.iter().map(|side| DockPlacement::Edge(*side)));
|
||||
for placement in framed {
|
||||
let mut moved = layout.clone();
|
||||
let action = Action::move_column(2, Target::at(1, placement.clone()));
|
||||
assert_eq!(moved.apply_with_permissions(action, strip), Err(DockError::NotAllowed), "{placement:?}");
|
||||
assert_eq!(moved, layout, "{placement:?}");
|
||||
}
|
||||
for to in [Target::new_dock(DockEdge::Start), Target::at(3, DockPlacement::column(true)), Target::float(window())] {
|
||||
let mut moved = layout.clone();
|
||||
moved.apply_with_permissions(Action::move_column(2, to.clone()), strip).unwrap_or_else(|e| panic!("{to:?}: {e}"));
|
||||
assert!(!matches!(moved.place(&2), Some(PanelPlace::Node(_))), "{to:?}");
|
||||
}
|
||||
// Without the right the same split takes it, so the refusal above is this right's.
|
||||
let mut split = layout.clone();
|
||||
split
|
||||
.apply_with_permissions(Action::move_column(2, Target::at(1, DockPlacement::Split(DockSide::Bottom))), |p: &u32| match p {
|
||||
2 => Permissions::ALL.shares_column(false),
|
||||
_ => Permissions::ALL,
|
||||
})
|
||||
.unwrap();
|
||||
assert!(matches!(split.place(&2), Some(PanelPlace::Node(_))));
|
||||
|
||||
// A layout saved with the panel in a frame: other panels still move, and reconcile docks it in a column.
|
||||
let mut other = split.clone();
|
||||
other.apply_with_permissions(Action::move_group(6, Target::at(3, DockPlacement::column(false))), strip).unwrap();
|
||||
let mut saved = Workspace::new(split);
|
||||
saved.reconcile(|_| true, strip, &Workspace::new(columns()));
|
||||
assert!(matches!(saved.layout.place(&2), Some(PanelPlace::Dock { .. })), "reconcile docks it in a column: {:?}", saved.layout);
|
||||
assert_eq!(column_of(&saved.layout, 2).panels().collect::<Vec<_>>(), [&2]);
|
||||
assert_eq!(sorted(&saved.layout), [1, 2, 3, 4, 6]);
|
||||
}
|
||||
|
||||
/// A stepped column (a toolbar's) keeps its step count docked, floating and through JSON, and its width moves only
|
||||
/// by steps.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user