Skip to main content

panes/preset/
monocle.rs

1use std::sync::Arc;
2
3use crate::builder::LayoutBuilder;
4use crate::error::PaneError;
5use crate::layout::Layout;
6use crate::preset::{add_active_hidden_panels, collect_kinds, validate_active, validate_kinds};
7
8/// Builder for the monocle preset layout.
9pub struct Monocle {
10    kinds: Arc<[Arc<str>]>,
11    active: usize,
12}
13
14impl Monocle {
15    pub(crate) fn new(kinds: impl IntoIterator<Item = impl Into<Arc<str>>>) -> Self {
16        Self {
17            kinds: collect_kinds(kinds),
18            active: 0,
19        }
20    }
21
22    /// Set which panel index is active (visible).
23    pub fn active(mut self, index: usize) -> Self {
24        self.active = index;
25        self
26    }
27
28    /// Consume the builder and produce a [`Layout`].
29    pub fn build(&self) -> Result<Layout, PaneError> {
30        validate_kinds(&self.kinds)?;
31        validate_active(self.active, self.kinds.len())?;
32
33        let mut b = LayoutBuilder::new();
34        let active = self.active;
35
36        b.col(|c| {
37            add_active_hidden_panels(c, &self.kinds, active);
38        })?;
39
40        b.build()
41    }
42}
43
44impl Monocle {
45    /// Consume the builder and produce a [`crate::runtime::LayoutRuntime`].
46    pub fn into_runtime(self) -> Result<crate::runtime::LayoutRuntime, PaneError> {
47        let strategy = crate::strategy::StrategyKind::ActivePanel {
48            variant: crate::strategy::ActivePanelVariant::Monocle,
49            bar_height: 0.0,
50        };
51        let kinds: Vec<Arc<str>> = self.kinds.to_vec();
52        crate::runtime::LayoutRuntime::from_strategy(strategy, &kinds)
53    }
54}
55
56super::impl_preset!(Monocle);