Skip to main content

gpui_material_components/navigation/
multi_pane.rs

1use std::time::Duration;
2
3use gpui::{ElementId, Entity, IntoElement, Pixels, Size};
4
5mod geometry;
6mod render;
7mod state;
8mod types;
9
10pub use geometry::{PaneExpansionAnchor, PaneLayoutDirective, PaneOverlayAlignment};
11pub use state::{
12    AdaptivePaneState, PaneAdaptStrategy, PaneBackBehavior, PaneDestination, PaneLayoutValue,
13    PanePlacement, PaneRole,
14};
15pub use types::{AdaptivePane, PaneSeparator};
16
17use types::pair_index;
18
19/// A three-role scaffold with caller-supplied panes, an explicit available viewport,
20/// and retained layout, expansion and navigation state.
21#[derive(IntoElement)]
22pub struct AdaptivePaneLayout {
23    pub(super) id: ElementId,
24    pub(super) viewport: Size<Pixels>,
25    pub(super) state: Entity<AdaptivePaneState>,
26    pub(super) directive: Option<PaneLayoutDirective>,
27    pub(super) panes: [Option<AdaptivePane>; 3],
28    pub(super) order: [PaneRole; 3],
29    pub(super) separators: [Option<PaneSeparator>; 9],
30    pub(super) duration: Duration,
31}
32
33impl AdaptivePaneLayout {
34    /// Creates an adaptive multi-pane layout for the given viewport size and retained [`AdaptivePaneState`].
35    pub fn new(
36        id: impl Into<ElementId>,
37        viewport: Size<Pixels>,
38        state: &Entity<AdaptivePaneState>,
39    ) -> Self {
40        Self {
41            id: id.into(),
42            viewport,
43            state: state.clone(),
44            directive: None,
45            panes: Default::default(),
46            order: PaneRole::ALL,
47            separators: Default::default(),
48            duration: Duration::from_millis(240),
49        }
50    }
51
52    /// Assigns an [`AdaptivePane`] to the given logical role slot.
53    pub fn pane(mut self, role: PaneRole, pane: AdaptivePane) -> Self {
54        self.panes[role.index()] = Some(pane);
55        self
56    }
57
58    /// Overrides the [`PaneLayoutDirective`] derived from the viewport size.
59    pub fn directive(mut self, directive: PaneLayoutDirective) -> Self {
60        self.directive = Some(directive);
61        self
62    }
63
64    /// Logical order of the three roles. Duplicate roles leave the previous order intact.
65    pub fn order(mut self, order: [PaneRole; 3]) -> Self {
66        if PaneRole::ALL.into_iter().all(|role| order.contains(&role)) {
67            self.order = order;
68        }
69        self
70    }
71
72    /// Registers a resize separator between two adjacent logical pane roles.
73    pub fn separator(mut self, start: PaneRole, end: PaneRole, separator: PaneSeparator) -> Self {
74        self.separators[pair_index(start, end)] = Some(separator);
75        self
76    }
77
78    /// Sets the animation duration used for pane layout transitions (default 240 ms).
79    pub fn transition_duration(mut self, duration: Duration) -> Self {
80        self.duration = duration;
81        self
82    }
83}