diff --git a/crates/ui-panels/src/docking/commands/dock.rs b/crates/ui-panels/src/docking/commands/dock.rs index 1ee24161e..81fef5765 100644 --- a/crates/ui-panels/src/docking/commands/dock.rs +++ b/crates/ui-panels/src/docking/commands/dock.rs @@ -4,7 +4,7 @@ use crate::docking::drop::{beside, members, moving_panels, to_target}; use crate::docking::{DockEvent, DragUnit, FloatSpace}; use crate::runtime::commands::CommandState; use craft_behavior::DockSide; -use craft_behavior::dock::{Action, DockEdge, DockError, DockPlacement, Layout, Location, Node, PanelPlace, Permissions, Target, node_at}; +use craft_behavior::dock::{Action, Column, DockEdge, DockError, DockPlacement, Layout, Location, Node, PanelPlace, Permissions, Target, node_at}; use egui::{Rect, Vec2}; use std::hash::Hash; @@ -200,11 +200,14 @@ pub fn actions(scene: &DockScene<'_, P>, command: DockComm DockCommands::StepGroupDown => one(Action::step_group(panel.clone(), false)), DockCommands::ToggleMinimized => { shown()?; - one(Action::set_minimized(panel.clone(), !crate::docking::rail::minimized(layout, panel))) + one(Action::set_minimized(panel.clone(), !minimized(layout, panel))) } DockCommands::CollapseToIcons => { let place = layout.place(panel).ok_or(DockError::MissingPanel)?; let column = layout.column(&place).ok_or(DockError::InvalidPlacement)?; + if !column.iconic && !collapses(scene, column) { + return Err(DockError::NotAllowed); + } one(Action::set_iconic(panel.clone(), !column.iconic)) } DockCommands::MovePanelTo | DockCommands::MoveGroupTo | DockCommands::ToggleMaximize | DockCommands::AutoCollapse => Ok(Vec::new()), @@ -265,16 +268,37 @@ pub fn move_actions(scene: &DockScene<'_, P>, unit: &DragU } } -/// The state of each panel command for `panel`: enabled as its permissions and place allow, Collapse to Icons -/// checked while its column is iconic. Maximize Frame applies to a frame of the docked layout, never to a floating -/// one. `DockArea` publishes them in the panel's group scope. +/// Whether the group or accordion section of `panel` is minimized to its tab strip. An accordion entry is minimized +/// while it is closed. A panel the layout does not hold is not. +fn minimized(layout: &Layout

, panel: &P) -> bool { + let Some(place) = layout.place(panel) else { return false }; + match layout.group(&place) { + Some(group) => group.minimized, + None => match layout.node_of(&place) { + Some(Node::Stack { entries, .. }) => entries.iter().any(|entry| &entry.panel == panel && !entry.open), + _ => false, + }, + } +} + +/// Whether `column` may collapse to icons: every panel in it collapses ([`Permissions::collapses`]). +fn collapses(scene: &DockScene<'_, P>, column: &Column

) -> bool { + column.panels().all(|panel| scene.policy(panel).collapses) +} + +/// The state of each panel command for `panel`: enabled as its permissions and place allow. Collapse to Icons applies +/// to every column, docked or floating, checked while it is iconic, and is off for a column that holds a panel that +/// does not collapse. Maximize Frame applies to a frame of the docked layout, never to a floating one. `DockArea` +/// publishes them in the panel's group scope. pub fn dock_states(scene: &DockScene<'_, P>, panel: &P) -> Vec<(DockCommands, CommandState)> { let layout = scene.layout; let policy = |p: &P| scene.policy(p); let allowed = scene.policy(panel); let place = layout.place(panel); let floatable = place.is_some() && allowed.movable && allowed.floatable; - let iconic = place.as_ref().and_then(|place| layout.column(place)).map(|column| column.iconic); + let column = place.as_ref().and_then(|place| layout.column(place)); + let iconic = column.map(|column| column.iconic); + let collapsible = column.is_some_and(|column| column.iconic || collapses(scene, column)); let enabled = |on: bool| CommandState::new().enabled(on); let shown = place.is_some(); let floating = matches!(place, Some(PanelPlace::Floating { .. })); @@ -296,19 +320,20 @@ pub fn dock_states(scene: &DockScene<'_, P>, panel: &P) -> (DockCommands::StepGroupUp, enabled(layout.can_step_group(panel, true, policy))), (DockCommands::StepGroupDown, enabled(layout.can_step_group(panel, false, policy))), (DockCommands::ToggleMinimized, enabled(shown)), - (DockCommands::CollapseToIcons, CommandState::new().enabled(iconic.is_some()).checked(iconic.unwrap_or(false))), + (DockCommands::CollapseToIcons, CommandState::new().enabled(collapsible).checked(iconic.unwrap_or(false))), (DockCommands::ToggleMaximize, enabled(shown && !floating)), (DockCommands::AutoCollapse, enabled(true)), ] } -/// The actions that float `unit` at the rect a drag would float it at, or keep its window where it is. +/// The actions that float `unit` at the rect a drag would float it at, or keep its window where it is: a window's +/// only panel, and a window's only group, already float alone. fn float_actions(scene: &DockScene<'_, P>, unit: &DragUnit

) -> Vec> { let layout = scene.layout; let panel = unit.lead(); let window = layout.floating.iter().find(|window| window.contains(panel)); let lone = window.is_some_and(|window| window.panels().count() == 1); - let whole = window.is_some_and(|window| matches!(window.root, Node::Tabs { .. } | Node::Stack { .. })); + let whole = window.is_some_and(|window| window.groups().len() == 1); let kept = window.map(|window| window.rect); let action = match (unit, kept) { (DragUnit::Panel(_), Some(rect)) if lone => Action::move_floating(panel.clone(), rect), diff --git a/crates/ui-panels/src/docking/drop.rs b/crates/ui-panels/src/docking/drop.rs index 627bded35..6ca845a26 100644 --- a/crates/ui-panels/src/docking/drop.rs +++ b/crates/ui-panels/src/docking/drop.rs @@ -11,7 +11,7 @@ use crate::contract::{Component, Cx, DropMark, DropOperation, DropTarget, Pointe use crate::runtime::drag::{self, DragPreview}; use crate::runtime::env::{AppValues, EnvValue, EnvView, Label}; use crate::runtime::pointer; -use craft_behavior::dock::{Action, DockEdge, DockPlacement, Layout, Node, PanelPlace, Permissions, Target}; +use craft_behavior::dock::{Action, DockEdge, DockPlacement, Layout, Node, Permissions, Target}; use craft_behavior::{CancelReason, DockSide}; use egui::{Context, Id, Pos2, Rect, Vec2}; use std::hash::Hash; @@ -165,10 +165,7 @@ fn spot_action( return None; } let moved = moving_panels(layout, unit); - let place = |anchor: P, placement: DockPlacement

| { - let floating = matches!(layout.place(&anchor), Some(PanelPlace::Floating { .. })); - placement.allowed_by(permissions(&anchor), floating).then(|| beside(unit, anchor, placement)) - }; + let place = |anchor: P, placement: DockPlacement

| placement.allowed_by(permissions(&anchor)).then(|| beside(unit, anchor, placement)); match spot { DockSpot::Tab { anchor, index } => { let destination = outside(layout, anchor, &moved)?; diff --git a/crates/ui-panels/src/docking/frame.rs b/crates/ui-panels/src/docking/frame.rs index 6b92d1e33..d7db4a713 100644 --- a/crates/ui-panels/src/docking/frame.rs +++ b/crates/ui-panels/src/docking/frame.rs @@ -23,15 +23,12 @@ pub struct FrameView<'a, P: Clone> { pub hidden: Vec

, /// The layout path of each split of the view, by the split's view path; `None` when the view is the layout. pub paths: Option, Vec>>, - /// The layout index of each floating window of the view, in order. - pub windows: Vec, - /// For each floating window of the view, the path in the window's layout tree of each split of its pruned tree. - window_paths: Vec, Vec>>, } impl FrameView<'_, P> { /// Writes the split sizes of `edited`, an edit of the view's layout, into `layout`, the layout the view was made - /// from. Panels the view left out keep their place; only sizes change. + /// from. Panels the view left out keep their place; only the docked tree's split sizes change: floating windows + /// have no splits. pub fn write_back(&self, edited: &Layout

, layout: &mut Layout

) { let Some(paths) = &self.paths else { layout.clone_from(edited); @@ -40,11 +37,6 @@ impl FrameView<'_, P> { if let (Some(shown), Some(root)) = (edited.root.as_ref(), layout.root.as_mut()) { tree::apply_sizes(root, &split_sizes(shown, paths)); } - for ((shown, at), paths) in edited.floating.iter().zip(&self.windows).zip(&self.window_paths) { - if let Some(window) = layout.floating.get_mut(*at) { - tree::apply_sizes(&mut window.root, &split_sizes(&shown.root, paths)); - } - } } } @@ -93,42 +85,36 @@ pub fn maximize<'a, P: Clone + Eq + Hash>( }); let unchanged = frame.is_none() && matches!(hide, HidePanels::None); if unchanged { - return FrameView { - layout: Cow::Borrowed(layout), - maximized: None, - hidden: Vec::new(), - paths: None, - windows: (0..layout.floating.len()).collect(), - window_paths: Vec::new(), - }; + return FrameView { layout: Cow::Borrowed(layout), maximized: None, hidden: Vec::new(), paths: None }; } let left_out = layout.panels().into_iter().filter(|panel| hidden(panel)).cloned().collect(); let mut paths = HashMap::new(); - let mut windows = Vec::new(); - let mut window_paths = Vec::new(); let (shown, kept) = match frame { Some((panel, path, node)) => (Layout::new(prune(&node, path, Vec::new(), &hidden, &mut paths), Vec::new()), Some(panel)), None => { let root = layout.root.as_ref().and_then(|root| prune(root, Vec::new(), Vec::new(), &hidden, &mut paths)); - let mut floating = Vec::new(); - for (at, window) in layout.floating.iter().enumerate() { - let mut window_map = HashMap::new(); - let Some(root) = prune(&window.root, Vec::new(), Vec::new(), &hidden, &mut window_map) else { continue }; - window_paths.push(window_map); - let mut shown: Floating

= window.clone(); - shown.root = root; - floating.push(shown); - windows.push(at); - } + // A window whose panels are all hidden is left out. + let floating = layout + .floating + .iter() + .filter_map(|window| { + let column = prune_column(&window.column, &hidden)?; + let mut shown: Floating

= window.clone(); + shown.column = column; + Some(shown) + }) + .collect(); (Layout::new(root, floating), None) } }; - FrameView { layout: Cow::Owned(shown), maximized: kept, hidden: left_out, paths: Some(paths), windows, window_paths } + FrameView { layout: Cow::Owned(shown), maximized: kept, hidden: left_out, paths: Some(paths) } } +/// The path of the docked node that holds a place of the docked tree: the `Tabs` or `Stack` node, or the `Dock` node +/// of a column's group. fn path_of(place: &PanelPlace) -> Vec { match place { - PanelPlace::Node(path) | PanelPlace::Dock { path, .. } | PanelPlace::Floating { path, .. } => path.clone(), + PanelPlace::Node(path) | PanelPlace::Dock { path, .. } => path.clone(), _ => Vec::new(), } } @@ -174,23 +160,23 @@ fn prune( (!entries.is_empty()).then(|| Node::stack(entries)) } Node::Dock { side, columns, .. } => { - let columns: Vec> = columns - .iter() - .filter_map(|column| { - let groups: Vec<_> = column.groups.iter().filter_map(|group| prune_group(group, hidden)).collect(); - (!groups.is_empty()).then(|| { - let mut kept = column.clone(); - kept.groups = groups; - kept - }) - }) - .collect(); + let columns: Vec> = columns.iter().filter_map(|column| prune_column(column, hidden)).collect(); (!columns.is_empty()).then(|| Node::dock(*side, columns)) } _ => Some(node.clone()), } } +/// A column without hidden panels, keeping its state; `None` when none of its panels shows. +fn prune_column(column: &Column

, hidden: &dyn Fn(&P) -> bool) -> Option> { + let groups: Vec<_> = column.groups.iter().filter_map(|group| prune_group(group, hidden)).collect(); + (!groups.is_empty()).then(|| { + let mut kept = column.clone(); + kept.groups = groups; + kept + }) +} + /// A group without hidden panels; the selected panel stays selected when it is shown. fn prune_group(group: &Group

, hidden: &dyn Fn(&P) -> bool) -> Option> { let selected = group.active_panel(); diff --git a/crates/ui-panels/src/docking/mod.rs b/crates/ui-panels/src/docking/mod.rs index 06b607412..306939775 100644 --- a/crates/ui-panels/src/docking/mod.rs +++ b/crates/ui-panels/src/docking/mod.rs @@ -13,7 +13,6 @@ mod group; mod floating; mod panels; -mod rail; mod root; mod spec; mod splitter; @@ -61,7 +60,6 @@ pub use dock_column::{ }; pub use flyout::{Flyout, FlyoutOutput, FlyoutShown, FlyoutStyle, GroupAt}; pub use iconic::{FlyoutResize, IconicColumn, IconicColumnOutput, IconicColumnStyle, IconicSizes}; -pub use rail::{rail_actions, rail_lit}; pub use window_menu::{WindowMenuRows, WindowMenuRowsOutput}; use crate::contract::{ diff --git a/crates/ui-panels/src/docking/rail.rs b/crates/ui-panels/src/docking/rail.rs deleted file mode 100644 index 952427876..000000000 --- a/crates/ui-panels/src/docking/rail.rs +++ /dev/null @@ -1,34 +0,0 @@ -//! What a rail icon does for a panel and whether it shows lit, from the dock layout. - -use craft_behavior::dock::{Action, Layout, Node, PanelPlace, PanelState}; -use std::hash::Hash; - -/// Whether the group, window or accordion section of `panel` is minimized. An accordion entry is minimized while it -/// is closed. A panel the layout does not hold is not. -pub(crate) fn minimized(layout: &Layout

, panel: &P) -> bool { - let Some(place) = layout.place(panel) else { return false }; - match &place { - PanelPlace::Node(_) => match layout.node_of(&place) { - Some(Node::Stack { entries, .. }) => entries.iter().any(|entry| &entry.panel == panel && !entry.open), - _ => false, - }, - PanelPlace::Floating { window, .. } => layout.floating.get(*window).is_some_and(|window| window.minimized), - _ => layout.group(&place).is_some_and(|group| group.minimized), - } -} - -/// The actions a click on the rail icon of `panel` takes: minimize a docked and shown panel, restore and activate a -/// minimized one, close a floating one's window, open a hidden one. -pub fn rail_actions(layout: &Layout

, panel: &P) -> Vec> { - match layout.place(panel) { - None => vec![Action::open(panel.clone(), None)], - Some(PanelPlace::Floating { .. }) => vec![Action::close_window(panel.clone())], - Some(_) if minimized(layout, panel) => vec![Action::set_minimized(panel.clone(), false), Action::activate(panel.clone())], - Some(_) => vec![Action::set_minimized(panel.clone(), true)], - } -} - -/// Whether the rail icon of `panel` shows lit: its group is in the layout and not minimized. -pub fn rail_lit(layout: &Layout

, panel: &P) -> bool { - layout.panel_state(panel) != PanelState::Hidden && !minimized(layout, panel) -}