panes 0.19.0

Renderer-agnostic layout engine with declarative ergonomics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use std::sync::Arc;

use crate::error::PaneError;
use crate::layout::Layout;
use crate::runtime::LayoutRuntime;
use crate::tree::LayoutTree;

use super::build::build_tree_for_strategy;
use super::dashboard::DashboardStrategy;
use super::holy_grail::HolyGrailStrategy;
use super::sidebar::SidebarStrategy;
use crate::panel::Axis;

use super::{ActivePanelVariant, GridColumnMode, StrategyKind};

/// Generate a `build() -> Strategy` method from 1:1 field-to-variant mapping.
macro_rules! impl_build_strategy {
    ($Builder:ty => $Variant:ident { $($field:ident),* }) => {
        impl $Builder {
            /// Convert to a generic [`Strategy`].
            pub fn build(self) -> Strategy {
                Strategy {
                    kind: StrategyKind::$Variant { $($field: self.$field),* },
                }
            }
        }
    };
}

impl_build_strategy!(MasterStackStrategy => MasterStack { master_ratio, gap });
impl_build_strategy!(CenteredMasterStrategy => CenteredMaster { master_ratio, gap });
impl_build_strategy!(DeckStrategy => Deck { master_ratio, gap });
impl_build_strategy!(ActivePanelStrategy => ActivePanel { variant, bar_height });
impl_build_strategy!(WindowStrategy => Window { panel_count, gap });
impl_build_strategy!(BinarySplitStrategy => BinarySplit { spiral, ratio, gap });

/// Generate a `with_panels` shorthand that delegates to `self.build().with_panels(panels)`.
macro_rules! impl_with_panels {
    ($($ty:ty),+ $(,)?) => { $(
        impl $ty {
            /// Bind panels directly.
            pub fn with_panels(
                self,
                panels: impl IntoIterator<Item = impl Into<Arc<str>>>,
            ) -> BoundStrategy {
                self.build().with_panels(panels)
            }
        }
    )+ };
}

impl_with_panels!(
    MasterStackStrategy,
    CenteredMasterStrategy,
    DeckStrategy,
    ActivePanelStrategy,
    WindowStrategy,
    BinarySplitStrategy,
);

/// Generate `From<Builder> for Strategy` impls via `.build()`.
macro_rules! impl_into_strategy {
    ($($ty:ty),+ $(,)?) => { $(
        impl From<$ty> for Strategy {
            fn from(builder: $ty) -> Self {
                builder.build()
            }
        }
    )+ };
}

impl_into_strategy!(
    MasterStackStrategy,
    CenteredMasterStrategy,
    DeckStrategy,
    ActivePanelStrategy,
    WindowStrategy,
    BinarySplitStrategy,
    SplitStrategy,
);

// ---------------------------------------------------------------------------
// Strategy — a configured layout shape, decoupled from panel content.
// ---------------------------------------------------------------------------

/// A configured layout strategy, decoupled from panel content.
/// Clone and reuse across different panel sets.
#[derive(Debug, Clone)]
pub struct Strategy {
    pub(crate) kind: StrategyKind,
}

impl Strategy {
    /// Wrap an existing [`StrategyKind`].
    pub fn from_kind(kind: StrategyKind) -> Self {
        Self { kind }
    }

    /// Access the inner strategy kind.
    pub fn kind(&self) -> &StrategyKind {
        &self.kind
    }

    /// Bind panels to this strategy. Works for all non-dashboard strategies.
    /// Dashboard strategies with spans must use [`DashboardStrategy::with_cards`].
    pub fn with_panels(
        self,
        panels: impl IntoIterator<Item = impl Into<Arc<str>>>,
    ) -> BoundStrategy {
        let panels: Box<[Arc<str>]> = panels.into_iter().map(Into::into).collect();
        BoundStrategy {
            kind: self.kind,
            panels,
            tree_override: None,
        }
    }

    // -- Factory methods --

    /// Master-stack strategy: one master panel with a vertical stack.
    pub fn master_stack() -> MasterStackStrategy {
        MasterStackStrategy {
            master_ratio: 0.5,
            gap: 0.0,
        }
    }

    /// Centered-master strategy: master panel centered between two side stacks.
    pub fn centered_master() -> CenteredMasterStrategy {
        CenteredMasterStrategy {
            master_ratio: 0.5,
            gap: 0.0,
        }
    }

    /// Deck strategy: master panel with one-at-a-time stack.
    pub fn deck() -> DeckStrategy {
        DeckStrategy {
            master_ratio: 0.5,
            gap: 0.0,
        }
    }

    /// Monocle strategy: full-screen single panel.
    pub fn monocle() -> ActivePanelStrategy {
        ActivePanelStrategy {
            variant: ActivePanelVariant::Monocle,
            bar_height: 0.0,
        }
    }

    /// Tabbed strategy: tab bar above content panels.
    pub fn tabbed() -> ActivePanelStrategy {
        ActivePanelStrategy {
            variant: ActivePanelVariant::Tabbed,
            bar_height: 1.0,
        }
    }

    /// Stacked strategy: title bars stacked vertically above content.
    pub fn stacked() -> ActivePanelStrategy {
        ActivePanelStrategy {
            variant: ActivePanelVariant::Stacked,
            bar_height: 1.0,
        }
    }

    /// Scrollable strategy: window showing N adjacent panels.
    pub fn scrollable() -> WindowStrategy {
        WindowStrategy {
            panel_count: 2,
            gap: 0.0,
        }
    }

    /// Dwindle strategy: recursive binary split without spiral.
    pub fn dwindle() -> BinarySplitStrategy {
        BinarySplitStrategy {
            spiral: false,
            ratio: 0.5,
            gap: 0.0,
        }
    }

    /// Spiral strategy: recursive binary split with spiral.
    pub fn spiral() -> BinarySplitStrategy {
        BinarySplitStrategy {
            spiral: true,
            ratio: 0.5,
            gap: 0.0,
        }
    }

    /// Split strategy: two panels with configurable ratio.
    pub fn split() -> SplitStrategy {
        SplitStrategy {
            ratio: 0.5,
            gap: 0.0,
            is_vertical: false,
        }
    }

    /// Dashboard strategy: CSS-grid layout with per-card column spans.
    pub fn dashboard() -> DashboardStrategy {
        DashboardStrategy {
            columns: GridColumnMode::Fixed(4),
            gap: 0.0,
            auto_rows: false,
        }
    }

    /// Sidebar strategy: fixed-width sidebar with grow content.
    pub fn sidebar() -> SidebarStrategy {
        SidebarStrategy::new(0.0, 20.0)
    }

    /// Holy-grail strategy: header, footer, left sidebar, main, right sidebar.
    pub fn holy_grail() -> HolyGrailStrategy {
        HolyGrailStrategy::new(0.0, 20.0, 1.0, 1.0)
    }
}

// ---------------------------------------------------------------------------
// BoundStrategy — strategy with panels attached, ready to build.
// ---------------------------------------------------------------------------

/// A strategy with panels bound, ready to produce a layout or runtime.
pub struct BoundStrategy {
    kind: StrategyKind,
    panels: Box<[Arc<str>]>,
    tree_override: Option<Layout>,
}

impl BoundStrategy {
    /// Create a new bound strategy.
    pub(crate) fn new(
        kind: StrategyKind,
        panels: Box<[Arc<str>]>,
        tree_override: Option<Layout>,
    ) -> Self {
        Self {
            kind,
            panels,
            tree_override,
        }
    }

    /// Produce a static [`Layout`] from this bound strategy.
    pub fn build(self) -> Result<Layout, PaneError> {
        match self.tree_override {
            Some(layout) => Ok(layout),
            None => {
                let tree = build_tree_for_strategy(&self.kind, &self.panels)?;
                Ok(Layout::from_tree(tree))
            }
        }
    }

    /// Produce a [`LayoutRuntime`] from this bound strategy.
    pub fn into_runtime(self) -> Result<LayoutRuntime, PaneError> {
        match self.tree_override {
            Some(layout) => {
                let tree = LayoutTree::from(layout);
                Ok(LayoutRuntime::from_tree_and_strategy(
                    tree,
                    self.kind,
                    &self.panels,
                ))
            }
            None => LayoutRuntime::from_strategy(self.kind, &self.panels),
        }
    }
}

// ---------------------------------------------------------------------------
// Builder structs — one per strategy family.
// ---------------------------------------------------------------------------

macro_rules! impl_master_ratio_gap {
    ($($Builder:ident),+) => { $(
        impl $Builder {
            /// Set the master panel's share of the viewport (0.0–1.0).
            pub fn master_ratio(mut self, ratio: f32) -> Self {
                self.master_ratio = ratio;
                self
            }

            /// Set the gap between panels.
            pub fn gap(mut self, gap: f32) -> Self {
                self.gap = gap;
                self
            }
        }
    )+ };
}

/// Builder for [`StrategyKind::MasterStack`].
#[derive(Debug, Clone)]
pub struct MasterStackStrategy {
    master_ratio: f32,
    gap: f32,
}

/// Builder for [`StrategyKind::CenteredMaster`].
#[derive(Debug, Clone)]
pub struct CenteredMasterStrategy {
    master_ratio: f32,
    gap: f32,
}

/// Builder for [`StrategyKind::Deck`].
#[derive(Debug, Clone)]
pub struct DeckStrategy {
    master_ratio: f32,
    gap: f32,
}

impl_master_ratio_gap!(MasterStackStrategy, CenteredMasterStrategy, DeckStrategy);

/// Builder for [`StrategyKind::ActivePanel`] (monocle, tabbed, stacked).
#[derive(Debug, Clone)]
pub struct ActivePanelStrategy {
    variant: ActivePanelVariant,
    bar_height: f32,
}

impl ActivePanelStrategy {
    crate::macros::builder_setters!(
        /// Set the bar height (tab bar or title bar height).
        bar_height(height: f32)
    );
}

/// Builder for [`StrategyKind::Window`] (scrollable).
#[derive(Debug, Clone)]
pub struct WindowStrategy {
    panel_count: usize,
    gap: f32,
}

impl WindowStrategy {
    crate::macros::builder_setters!(
        /// Set how many panels are visible at once in the active window.
        panel_count(panel_count: usize);
        /// Set the gap between visible panels.
        gap(gap: f32)
    );
}

/// Builder for [`StrategyKind::BinarySplit`] (dwindle, spiral).
#[derive(Debug, Clone)]
pub struct BinarySplitStrategy {
    spiral: bool,
    ratio: f32,
    gap: f32,
}

impl BinarySplitStrategy {
    crate::macros::builder_setters!(
        /// Set the split ratio at each level.
        ratio(ratio: f32);
        /// Set the gap between panels.
        gap(gap: f32)
    );
}

/// Builder for split (two panels with configurable ratio and direction).
#[derive(Debug, Clone)]
pub struct SplitStrategy {
    ratio: f32,
    gap: f32,
    is_vertical: bool,
}

impl SplitStrategy {
    crate::macros::builder_setters!(
        /// Set the split ratio.
        ratio(ratio: f32);
        /// Set the gap between panels.
        gap(gap: f32)
    );

    crate::macros::builder_flag_setters!(
        /// Use vertical split direction.
        vertical -> is_vertical = true
    );

    /// Convert to a generic [`Strategy`].
    pub fn build(self) -> Strategy {
        Strategy {
            kind: StrategyKind::Sequence {
                axis: match self.is_vertical {
                    true => Axis::Col,
                    false => Axis::Row,
                },
                gap: self.gap,
                ratio: Some(self.ratio),
            },
        }
    }

    /// Bind two named panels directly.
    pub fn with_panels(
        self,
        first: impl Into<Arc<str>>,
        second: impl Into<Arc<str>>,
    ) -> BoundStrategy {
        let panels: Box<[Arc<str>]> = Box::from([first.into(), second.into()]);
        let kind = self.build().kind;
        BoundStrategy {
            kind,
            panels,
            tree_override: None,
        }
    }
}