mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
Dock workspace: tabs closed together reopen together (photocraft#15545)
When one apply_all batch closes several tabs of a group (Close Tab Group, or a preset that closes panels one at a time), each closed panel's Location records the set in closed_with. Reopening any of them restores the set in group order, each by its own record, with the requested one in front. A tab closed in a batch of its own stays closed: the set only counts members whose records name each other.
This commit is contained in:
@@ -93,6 +93,11 @@ pub struct Location<P> {
|
||||
/// restoring creates a new group, column, dock or window for the panel.
|
||||
#[serde(default)]
|
||||
pub group: Option<GroupLocation>,
|
||||
/// The panels of its group that closed in the same batch of actions, itself included, in group order; empty
|
||||
/// when it closed alone. Reopening any of them brings back the others that are still hidden and were closed with
|
||||
/// it.
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub closed_with: Vec<P>,
|
||||
}
|
||||
|
||||
/// Groups, columns and floating windows are named by a panel they contain.
|
||||
@@ -340,7 +345,7 @@ pub enum Action<P> {
|
||||
impl<P> Location<P> {
|
||||
/// A location relative to `anchor` by `placement`, with no recorded sizes or siblings.
|
||||
pub fn new(anchor: Option<P>, placement: DockPlacement<P>) -> Self {
|
||||
Self { floating: None, anchor, placement, size: None, stack: None, sibling: Vec::new(), group: None }
|
||||
Self { floating: None, anchor, placement, size: None, stack: None, sibling: Vec::new(), group: None, closed_with: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -554,7 +554,7 @@ pub(super) fn group_location<P: Clone + Eq>(panels: &[P], panel: &P) -> Option<L
|
||||
let index = panels.iter().position(|p| p == panel)?;
|
||||
let anchor = panels.iter().find(|p| *p != panel).cloned();
|
||||
let before = panels.get(index + 1).cloned();
|
||||
Some(Location { floating: None, anchor, placement: DockPlacement::Tab { before }, size: None, stack: None, sibling: Vec::new(), group: None })
|
||||
Some(Location::new(anchor, DockPlacement::Tab { before }))
|
||||
}
|
||||
|
||||
pub(super) fn first_panel<P>(node: &Node<P>) -> Option<&P> {
|
||||
|
||||
@@ -12,7 +12,7 @@ use super::{
|
||||
DockSide,
|
||||
action::{Action, DockPlacement, Location},
|
||||
edit::tree_panels,
|
||||
groups::{Column, DockEdge, PanelPlace, Target},
|
||||
groups::{Column, DockEdge, PanelPlace, Target, Unit},
|
||||
layout::Layout,
|
||||
limits::MAX_PANELS,
|
||||
node::Node,
|
||||
@@ -163,7 +163,7 @@ impl<P> Workspace<P> {
|
||||
|
||||
impl<P: Clone + Eq + Hash> Workspace<P> {
|
||||
/// Check the version, the layout, that every hidden panel is hidden once and absent
|
||||
/// from the layout, and that the floated records are bounded and one per panel.
|
||||
/// from the layout and its record is bounded, and that the floated records are bounded and one per panel.
|
||||
pub fn validate(&self) -> Result<(), DockError> {
|
||||
if self.version > Self::VERSION {
|
||||
return Err(DockError::UnsupportedVersion);
|
||||
@@ -174,10 +174,13 @@ impl<P: Clone + Eq + Hash> Workspace<P> {
|
||||
}
|
||||
let visible: HashSet<&P> = self.layout.panels().into_iter().collect();
|
||||
let mut seen = HashSet::new();
|
||||
for (panel, _) in &self.hidden {
|
||||
for (panel, location) in &self.hidden {
|
||||
if visible.contains(panel) || !seen.insert(panel) {
|
||||
return Err(DockError::DuplicatePanel);
|
||||
}
|
||||
if location.closed_with.len() > MAX_PANELS {
|
||||
return Err(DockError::LimitExceeded);
|
||||
}
|
||||
}
|
||||
if self.floated.len() > MAX_PANELS {
|
||||
return Err(DockError::LimitExceeded);
|
||||
@@ -211,8 +214,11 @@ impl<P: Clone + Eq + Hash> Workspace<P> {
|
||||
let policy = |panel: &P| if locked { permissions(panel).locked() } else { permissions(panel) };
|
||||
let mut proposed = self.clone();
|
||||
let mut all_closed = Vec::new();
|
||||
// The group of each panel as it closes, in closing order.
|
||||
let mut closing_groups = Vec::new();
|
||||
for action in actions {
|
||||
let closed = proposed.layout.closed_by(&action)?;
|
||||
closing_groups.extend(closed.iter().map(|panel| proposed.layout.members(panel, Unit::Group).unwrap_or_else(|_| vec![panel.clone()])));
|
||||
let locations = close_locations(&proposed.layout, &closed);
|
||||
let before = proposed.layout.clone();
|
||||
// Opening a hidden panel puts it back where it was.
|
||||
@@ -240,6 +246,7 @@ impl<P: Clone + Eq + Hash> Workspace<P> {
|
||||
proposed.hidden.retain(|(p, _)| !visible.contains(p));
|
||||
let mut seen = HashSet::new();
|
||||
all_closed.retain(|p| !visible.contains(p) && seen.insert(p.clone()));
|
||||
proposed.record_closed_together(&all_closed, &closing_groups);
|
||||
proposed.validate()?;
|
||||
let Workspace { layout, hidden, floated, .. } = proposed;
|
||||
(self.layout, self.hidden, self.floated) = (layout, hidden, floated);
|
||||
@@ -248,10 +255,11 @@ impl<P: Clone + Eq + Hash> Workspace<P> {
|
||||
}
|
||||
|
||||
/// Show or hide one panel. Showing restores its recorded location, following the chain of
|
||||
/// hidden anchors when its own anchor is hidden too; otherwise it opens as
|
||||
/// [`Action::Open`] with `fallback` as the anchor, following `permissions`. The hidden
|
||||
/// panels on that chain reopen beside it later, so a group or window closed one panel at a
|
||||
/// time comes back as one.
|
||||
/// hidden anchors when its own anchor is hidden too, and brings back the hidden panels of its
|
||||
/// group that closed with it in one batch (Close Tab Group, a preset) as tabs of its group;
|
||||
/// otherwise it opens as [`Action::Open`] with `fallback` as the anchor, following
|
||||
/// `permissions`. The other hidden panels on that chain reopen beside it later, so a group or
|
||||
/// window closed one panel at a time comes back as one.
|
||||
/// A shown panel becomes the active tab of its group. Hiding follows `permissions` and
|
||||
/// `locked`, as [`Self::apply`] does, so a protected canvas stays shown.
|
||||
pub fn set_visible(
|
||||
@@ -276,29 +284,91 @@ impl<P: Clone + Eq + Hash> Workspace<P> {
|
||||
self.apply_all([open, Action::Activate { panel }], locked, permissions).map(drop)
|
||||
}
|
||||
|
||||
/// Restore a hidden `panel` to its recorded place and make it the active tab. `Ok(false)` when the record
|
||||
/// cannot be restored, or the place it restores to refuses the panel under `permissions`; the workspace stays
|
||||
/// as it was and the caller opens the panel by the ordinary, checked path.
|
||||
/// Restore a hidden `panel` to its recorded place, with the hidden panels closed with it, and make it the
|
||||
/// active tab. The set is restored in group order, each panel by its own record, so the group comes back in
|
||||
/// the order it had; a panel closed with it whose place refuses it stays hidden. `Ok(false)` when `panel`'s
|
||||
/// record cannot be restored, or the place it restores to refuses the panel under `permissions`; the workspace
|
||||
/// stays as it was and the caller opens the panel by the ordinary, checked path.
|
||||
fn reopen(&mut self, panel: &P, permissions: &impl Fn(&P) -> Permissions) -> Result<bool, DockError> {
|
||||
// The restore also rewrites the records of the hidden panels it passed, so it runs on a copy.
|
||||
let mut restored = self.clone();
|
||||
for member in self.closed_together(panel) {
|
||||
match restored.restored(&member, permissions) {
|
||||
Some(next) => restored = next,
|
||||
None if &member == panel => return Ok(false),
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
restored.layout.apply(Action::Activate { panel: panel.clone() })?;
|
||||
self.layout = restored.layout;
|
||||
self.hidden = restored.hidden;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// The workspace with the hidden `panel` restored to its recorded place, `None` when the record cannot be
|
||||
/// restored or the place refuses the panel under `permissions`. The restore also rewrites the records of the
|
||||
/// hidden panels it passed, so it runs on a copy.
|
||||
fn restored(&self, panel: &P, permissions: &impl Fn(&P) -> Permissions) -> Option<Self> {
|
||||
let mut trial = self.clone();
|
||||
let mut layout = self.layout.clone();
|
||||
if trial.restore_from_hidden(&mut layout, panel).is_err() {
|
||||
return Ok(false);
|
||||
}
|
||||
trial.restore_from_hidden(&mut layout, panel).ok()?;
|
||||
let allowed = layout
|
||||
.location(panel)
|
||||
.ok()
|
||||
.and_then(|location| location.anchor.map(|anchor| location.placement.allowed_by(permissions(&anchor))))
|
||||
.unwrap_or(true);
|
||||
if !allowed || layout.breaks_columns_beyond(&self.layout, permissions) {
|
||||
return Ok(false);
|
||||
return None;
|
||||
}
|
||||
trial.layout = layout;
|
||||
trial.hidden.retain(|(p, _)| p != panel);
|
||||
Some(trial)
|
||||
}
|
||||
|
||||
/// The hidden `panel` and the hidden panels closed with it, in group order: those its record's
|
||||
/// [`closed_with`](Location::closed_with) names whose own records name it back. A panel that closed again on
|
||||
/// its own since names it no more. `[panel]` when it closed alone.
|
||||
fn closed_together(&self, panel: &P) -> Vec<P> {
|
||||
let record = |p: &P| self.hidden.iter().find(|(q, _)| q == p).map(|(_, location)| location);
|
||||
let mut seen = HashSet::new();
|
||||
let together: Vec<P> = record(panel)
|
||||
.map(|location| &location.closed_with[..])
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.filter(|&p| p == panel || record(p).is_some_and(|location| location.closed_with.contains(panel)))
|
||||
.filter(|&p| seen.insert(p))
|
||||
.cloned()
|
||||
.collect();
|
||||
if together.len() > 1 && together.contains(panel) { together } else { vec![panel.clone()] }
|
||||
}
|
||||
|
||||
/// Records on the hidden panels `closed`, which this batch closed, the panels of their group that closed with
|
||||
/// them. `groups` holds each closing panel's group as it closed, in closing order. The closed panels of one
|
||||
/// group, or of groups that share one, form one set in the order the groups name them.
|
||||
fn record_closed_together(&mut self, closed: &[P], groups: &[Vec<P>]) {
|
||||
let closed: HashSet<&P> = closed.iter().collect();
|
||||
let mut sets: Vec<Vec<P>> = Vec::new();
|
||||
for group in groups {
|
||||
let members: Vec<&P> = group.iter().filter(|p| closed.contains(p)).collect();
|
||||
let (joined, rest): (Vec<Vec<P>>, Vec<Vec<P>>) =
|
||||
std::mem::take(&mut sets).into_iter().partition(|set| set.iter().any(|p| members.contains(&p)));
|
||||
let mut set: Vec<P> = joined.into_iter().flatten().collect();
|
||||
for panel in members {
|
||||
if !set.contains(panel) {
|
||||
set.push(panel.clone());
|
||||
}
|
||||
}
|
||||
sets = rest;
|
||||
if !set.is_empty() {
|
||||
sets.push(set);
|
||||
}
|
||||
}
|
||||
for set in sets.into_iter().filter(|set| set.len() > 1) {
|
||||
for (panel, location) in &mut self.hidden {
|
||||
if set.contains(panel) {
|
||||
location.closed_with.clone_from(&set);
|
||||
}
|
||||
}
|
||||
}
|
||||
layout.apply(Action::Activate { panel: panel.clone() })?;
|
||||
self.layout = layout;
|
||||
self.hidden = trial.hidden;
|
||||
self.hidden.retain(|(p, _)| p != panel);
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// Update the floated records after an action that turned `before` into the current layout: drop the records of
|
||||
|
||||
@@ -188,6 +188,42 @@ fn opening_a_closed_floating_panel_restores_its_window_place() {
|
||||
assert_eq!(group_members(workspace.layout(), 11), [11, 12, 50]);
|
||||
}
|
||||
|
||||
/// The active tab of the dock or floating group holding `panel`.
|
||||
fn front(layout: &Layout<u32>, panel: u32) -> u32 {
|
||||
*layout.group(&layout.place(&panel).unwrap()).unwrap().active_panel().unwrap()
|
||||
}
|
||||
|
||||
/// Tabs closed in one batch (one at a time, as a preset does, or by Close Tab Group) come back together with whichever
|
||||
/// of them reopens, in their order and with that one in front, after a save and a load; a tab closed in a batch of its
|
||||
/// own stays closed.
|
||||
#[test]
|
||||
fn tabs_closed_together_reopen_together_and_a_tab_closed_alone_stays_closed() {
|
||||
let batches = [vec![Action::close(2), Action::close(3)], vec![Action::close(3), Action::close(2)], vec![Action::close_group(2)]];
|
||||
for batch in batches {
|
||||
for open in [2, 3] {
|
||||
let mut workspace = Workspace::new(docked());
|
||||
workspace.apply_all(batch.clone(), false, allow).unwrap();
|
||||
let saved = serde_json::to_string(&workspace).unwrap();
|
||||
let mut workspace: Workspace<u32> = serde_json::from_str(&saved).unwrap();
|
||||
workspace.apply(Action::open(open, None), false, allow).unwrap();
|
||||
let layout = workspace.layout();
|
||||
assert_eq!(group_members(layout, open), [2, 3], "{batch:?}, then {open} opens");
|
||||
assert_eq!(front(layout, open), open, "{batch:?}: the reopened tab is in front");
|
||||
assert_eq!(end_dock(layout)[0].groups[0].panels, [2, 3], "{batch:?}: back above 4 in its column");
|
||||
assert!(workspace.hidden().is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
let mut workspace = Workspace::new(docked());
|
||||
workspace.apply(Action::close(3), false, allow).unwrap();
|
||||
workspace.apply(Action::close(2), false, allow).unwrap();
|
||||
workspace.apply(Action::open(2, None), false, allow).unwrap();
|
||||
assert_eq!(group_members(workspace.layout(), 2), [2]);
|
||||
assert!(workspace.layout().place(&3).is_none(), "3 closed on its own stays closed");
|
||||
workspace.apply(Action::open(3, None), false, allow).unwrap();
|
||||
assert_eq!(group_members(workspace.layout(), 2), [2, 3], "it reopens beside 2");
|
||||
}
|
||||
|
||||
/// 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() {
|
||||
|
||||
Reference in New Issue
Block a user