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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/// Generates a newtype wrapper around `u32` with `from_raw`, `raw`, and `Display`.
macro_rules! id_newtype {
    ($(#[$meta:meta])* $vis:vis $Name:ident) => {
        $(#[$meta])*
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
        $vis struct $Name(u32);

        impl $Name {
            /// Construct from a raw integer.
            pub fn from_raw(raw: u32) -> Self {
                Self(raw)
            }

            /// Return the underlying integer.
            pub fn raw(self) -> u32 {
                self.0
            }
        }

        impl std::fmt::Display for $Name {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "{}", self.0)
            }
        }
    };
}

pub(crate) use id_newtype;

/// Generates simple builder setter methods: `self.field = param; self`.
///
/// Each item is separated by `;`:
/// ```ignore
/// builder_setters!(
///     /// doc
///     sidebar_width(width: f32);
///     /// doc
///     gap(gap: f32)
/// );
/// ```
macro_rules! builder_setters {
    ($( $(#[$doc:meta])* $field:ident ($param:ident : $ty:ty) );+ $(;)?) => {
        $(
            $(#[$doc])*
            pub fn $field(mut self, $param: $ty) -> Self {
                self.$field = $param;
                self
            }
        )+
    };
}

/// Generates mapped builder setter methods: `self.field = expr; self`.
///
/// Method name may differ from field name. Each item is separated by `;`:
/// ```ignore
/// builder_mapped_setters!(
///     /// doc
///     columns(columns: usize) -> columns = GridColumnMode::Fixed(columns);
///     /// doc
///     auto_fill(min_width: f32) -> columns = GridColumnMode::AutoFill { min_width }
/// );
/// ```
macro_rules! builder_mapped_setters {
    ($( $(#[$doc:meta])* $method:ident ($param:ident : $ty:ty) -> $field:ident = $val:expr );+ $(;)?) => {
        $(
            $(#[$doc])*
            pub fn $method(mut self, $param: $ty) -> Self {
                self.$field = $val;
                self
            }
        )+
    };
}

/// Generates no-arg builder setter methods: `self.field = expr; self`.
///
/// Each item is separated by `;`:
/// ```ignore
/// builder_flag_setters!(
///     /// doc
///     auto_rows -> auto_rows = true
/// );
/// ```
macro_rules! builder_flag_setters {
    ($( $(#[$doc:meta])* $method:ident -> $field:ident = $val:expr );+ $(;)?) => {
        $(
            $(#[$doc])*
            pub fn $method(mut self) -> Self {
                self.$field = $val;
                self
            }
        )+
    };
}

pub(crate) use builder_flag_setters;
pub(crate) use builder_mapped_setters;
pub(crate) use builder_setters;

/// Declarative macro for building layouts from a concise DSL.
///
/// Returns `Result<Layout, PaneError>`.
///
/// # Syntax
///
/// ```text
/// layout! {
///     row(gap: 8.0) {
///         panel("editor", grow: 2.0)
///         col {
///             panel("chat")
///             panel("status", fixed: 3.0)
///         }
///     }
/// }
/// ```
///
/// - Root must be a single `row` or `col`.
/// - Containers accept an optional `(gap: N)` parameter.
/// - Bare `panel("kind")` defaults to `grow(1.0)`.
/// - `panel("kind", grow: N)` and `panel("kind", fixed: N)` set constraints.
/// - Panels accept optional modifiers after the primary constraint:
///   `min:`, `max:`, `min_width:`, `max_width:`, `min_height:`, `max_height:`, `align:`.
/// - Align values: `start`, `center`, `end`, `stretch`.
#[macro_export]
macro_rules! layout {
    // -- Root: row/col with or without gap --

    (row(gap: $gap:expr) { $($children:tt)* }) => {
        $crate::layout!(@root_gap row_gap [$gap] $($children)*)
    };
    (row { $($children:tt)* }) => {
        $crate::layout!(@root row $($children)*)
    };
    (col(gap: $gap:expr) { $($children:tt)* }) => {
        $crate::layout!(@root_gap col_gap [$gap] $($children)*)
    };
    (col { $($children:tt)* }) => {
        $crate::layout!(@root col $($children)*)
    };

    // -- Root: grid --
    (grid($($config:tt)*) { $($children:tt)* }) => {
        $crate::Layout::build_grid(
            $crate::layout!(@grid_config $($config)*),
            |__gctx| { $crate::layout!(@grid_children __gctx $($children)*); },
        )
    };

    // Internal: root without gap
    (@root $dir:ident $($children:tt)*) => {
        (|| -> ::core::result::Result<$crate::Layout, $crate::PaneError> {
            let mut __builder = $crate::LayoutBuilder::new();
            __builder.$dir(|__ctx| {
                $crate::layout!(@children __ctx $($children)*);
            })?;
            __builder.build()
        })()
    };

    // Internal: root with gap
    (@root_gap $dir:ident [$gap:expr] $($children:tt)*) => {
        (|| -> ::core::result::Result<$crate::Layout, $crate::PaneError> {
            let mut __builder = $crate::LayoutBuilder::new();
            __builder.$dir($gap, |__ctx| {
                $crate::layout!(@children __ctx $($children)*);
            })?;
            __builder.build()
        })()
    };

    // -- Children dispatch: peel off one child at a time --

    // Base case: no more children
    (@children $ctx:ident) => {};

    // -- Panel with grow + modifiers --
    (@children $ctx:ident panel($kind:expr, grow: $val:expr, $($mods:tt)+) $($rest:tt)*) => {
        $ctx.panel_with($kind, $crate::layout!(@apply $crate::grow($val), $($mods)+));
        $crate::layout!(@children $ctx $($rest)*);
    };

    // -- Panel with grow only --
    (@children $ctx:ident panel($kind:expr, grow: $val:expr) $($rest:tt)*) => {
        $ctx.panel_with($kind, $crate::grow($val));
        $crate::layout!(@children $ctx $($rest)*);
    };

    // -- Panel with fixed + modifiers --
    (@children $ctx:ident panel($kind:expr, fixed: $val:expr, $($mods:tt)+) $($rest:tt)*) => {
        $ctx.panel_with($kind, $crate::layout!(@apply $crate::fixed($val), $($mods)+));
        $crate::layout!(@children $ctx $($rest)*);
    };

    // -- Panel with fixed only --
    (@children $ctx:ident panel($kind:expr, fixed: $val:expr) $($rest:tt)*) => {
        $ctx.panel_with($kind, $crate::fixed($val));
        $crate::layout!(@children $ctx $($rest)*);
    };

    // -- Panel bare — defaults to grow(1.0) --
    (@children $ctx:ident panel($kind:expr) $($rest:tt)*) => {
        $ctx.panel($kind);
        $crate::layout!(@children $ctx $($rest)*);
    };

    // -- Nested grid inside row/col --
    (@children $ctx:ident grid($($config:tt)*) { $($inner:tt)* } $($rest:tt)*) => {
        $ctx.grid($crate::layout!(@grid_config $($config)*), |__gctx| {
            $crate::layout!(@grid_children __gctx $($inner)*);
        });
        $crate::layout!(@children $ctx $($rest)*);
    };

    // -- Nested row/col with or without gap --
    (@children $ctx:ident row(gap: $gap:expr) { $($inner:tt)* } $($rest:tt)*) => {
        $crate::layout!(@nested_gap $ctx row_gap [$gap] { $($inner)* } $($rest)*);
    };
    (@children $ctx:ident row { $($inner:tt)* } $($rest:tt)*) => {
        $crate::layout!(@nested $ctx row { $($inner)* } $($rest)*);
    };
    (@children $ctx:ident col(gap: $gap:expr) { $($inner:tt)* } $($rest:tt)*) => {
        $crate::layout!(@nested_gap $ctx col_gap [$gap] { $($inner)* } $($rest)*);
    };
    (@children $ctx:ident col { $($inner:tt)* } $($rest:tt)*) => {
        $crate::layout!(@nested $ctx col { $($inner)* } $($rest)*);
    };

    // Internal: nested container without gap
    (@nested $ctx:ident $dir:ident { $($inner:tt)* } $($rest:tt)*) => {
        $ctx.$dir(|__ctx| {
            $crate::layout!(@children __ctx $($inner)*);
        });
        $crate::layout!(@children $ctx $($rest)*);
    };

    // Internal: nested container with gap
    (@nested_gap $ctx:ident $dir:ident [$gap:expr] { $($inner:tt)* } $($rest:tt)*) => {
        $ctx.$dir($gap, |__ctx| {
            $crate::layout!(@children __ctx $($inner)*);
        });
        $crate::layout!(@children $ctx $($rest)*);
    };

    // -- @apply: recursive modifier chaining --
    // Each modifier peels off one key: value pair and chains the builder method.

    (@apply $c:expr, min: $v:expr, $($rest:tt)+) => {
        $crate::layout!(@apply $c.min($v), $($rest)+)
    };
    (@apply $c:expr, min: $v:expr) => { $c.min($v) };

    (@apply $c:expr, max: $v:expr, $($rest:tt)+) => {
        $crate::layout!(@apply $c.max($v), $($rest)+)
    };
    (@apply $c:expr, max: $v:expr) => { $c.max($v) };

    (@apply $c:expr, min_width: $v:expr, $($rest:tt)+) => {
        $crate::layout!(@apply $c.min_width($v), $($rest)+)
    };
    (@apply $c:expr, min_width: $v:expr) => { $c.min_width($v) };

    (@apply $c:expr, max_width: $v:expr, $($rest:tt)+) => {
        $crate::layout!(@apply $c.max_width($v), $($rest)+)
    };
    (@apply $c:expr, max_width: $v:expr) => { $c.max_width($v) };

    (@apply $c:expr, min_height: $v:expr, $($rest:tt)+) => {
        $crate::layout!(@apply $c.min_height($v), $($rest)+)
    };
    (@apply $c:expr, min_height: $v:expr) => { $c.min_height($v) };

    (@apply $c:expr, max_height: $v:expr, $($rest:tt)+) => {
        $crate::layout!(@apply $c.max_height($v), $($rest)+)
    };
    (@apply $c:expr, max_height: $v:expr) => { $c.max_height($v) };

    (@apply $c:expr, align: $v:ident, $($rest:tt)+) => {
        $crate::layout!(@apply $c.align($crate::layout!(@align $v)), $($rest)+)
    };
    (@apply $c:expr, align: $v:ident) => { $c.align($crate::layout!(@align $v)) };

    // size_mode: simple variants (min_content, max_content)
    (@apply $c:expr, size_mode: $v:ident, $($rest:tt)+) => {
        $crate::layout!(@apply $c.size_mode($crate::layout!(@size_mode $v)), $($rest)+)
    };
    (@apply $c:expr, size_mode: $v:ident) => { $c.size_mode($crate::layout!(@size_mode $v)) };

    // size_mode: fit_content(N)
    (@apply $c:expr, size_mode: fit_content($v:expr), $($rest:tt)+) => {
        $crate::layout!(@apply $c.size_mode($crate::SizeMode::FitContent($v)), $($rest)+)
    };
    (@apply $c:expr, size_mode: fit_content($v:expr)) => { $c.size_mode($crate::SizeMode::FitContent($v)) };

    // -- @grid_config: parse the initial grid constructor keyword --

    (@grid_config columns: $n:expr $(, $($rest:tt)*)?) => {
        $crate::layout!(@grid_apply $crate::Grid::columns($n) $(, $($rest)*)?)
    };
    (@grid_config auto_fit: $w:expr $(, $($rest:tt)*)?) => {
        $crate::layout!(@grid_apply $crate::Grid::auto_fit($w) $(, $($rest)*)?)
    };
    (@grid_config auto_fill: $w:expr $(, $($rest:tt)*)?) => {
        $crate::layout!(@grid_apply $crate::Grid::auto_fill($w) $(, $($rest)*)?)
    };

    // -- @grid_apply: chain optional grid config methods --

    (@grid_apply $g:expr) => { $g };
    (@grid_apply $g:expr, gap: $v:expr $(, $($rest:tt)*)?) => {
        $crate::layout!(@grid_apply $g.gap($v) $(, $($rest)*)?)
    };
    (@grid_apply $g:expr, auto_rows: true $(, $($rest:tt)*)?) => {
        $crate::layout!(@grid_apply $g.auto_rows() $(, $($rest)*)?)
    };

    // -- @grid_children: dispatch for GridCtx panels --

    // Base case
    (@grid_children $gctx:ident) => {};

    // Panel with span
    (@grid_children $gctx:ident panel($kind:expr, span: $n:expr) $($rest:tt)*) => {
        $gctx.panel_span($kind, $crate::CardSpan::Columns($n));
        $crate::layout!(@grid_children $gctx $($rest)*);
    };

    // Panel with full_width
    (@grid_children $gctx:ident panel($kind:expr, full_width: true) $($rest:tt)*) => {
        $gctx.panel_span($kind, $crate::CardSpan::FullWidth);
        $crate::layout!(@grid_children $gctx $($rest)*);
    };

    // Panel with grow
    (@grid_children $gctx:ident panel($kind:expr, grow: $val:expr) $($rest:tt)*) => {
        $gctx.panel_with($kind, $crate::grow($val));
        $crate::layout!(@grid_children $gctx $($rest)*);
    };

    // Panel with fixed
    (@grid_children $gctx:ident panel($kind:expr, fixed: $val:expr) $($rest:tt)*) => {
        $gctx.panel_with($kind, $crate::fixed($val));
        $crate::layout!(@grid_children $gctx $($rest)*);
    };

    // Panel bare — defaults to grow(1.0)
    (@grid_children $gctx:ident panel($kind:expr) $($rest:tt)*) => {
        $gctx.panel($kind);
        $crate::layout!(@grid_children $gctx $($rest)*);
    };

    // Nested row inside grid
    (@grid_children $gctx:ident row(gap: $gap:expr) { $($inner:tt)* } $($rest:tt)*) => {
        $gctx.row_gap($gap, |__ctx| {
            $crate::layout!(@children __ctx $($inner)*);
        });
        $crate::layout!(@grid_children $gctx $($rest)*);
    };
    (@grid_children $gctx:ident row { $($inner:tt)* } $($rest:tt)*) => {
        $gctx.row(|__ctx| {
            $crate::layout!(@children __ctx $($inner)*);
        });
        $crate::layout!(@grid_children $gctx $($rest)*);
    };

    // Nested col inside grid
    (@grid_children $gctx:ident col(gap: $gap:expr) { $($inner:tt)* } $($rest:tt)*) => {
        $gctx.col_gap($gap, |__ctx| {
            $crate::layout!(@children __ctx $($inner)*);
        });
        $crate::layout!(@grid_children $gctx $($rest)*);
    };
    (@grid_children $gctx:ident col { $($inner:tt)* } $($rest:tt)*) => {
        $gctx.col(|__ctx| {
            $crate::layout!(@children __ctx $($inner)*);
        });
        $crate::layout!(@grid_children $gctx $($rest)*);
    };

    // Nested grid inside grid
    (@grid_children $gctx:ident grid($($config:tt)*) { $($inner:tt)* } $($rest:tt)*) => {
        $gctx.grid($crate::layout!(@grid_config $($config)*), |__gctx| {
            $crate::layout!(@grid_children __gctx $($inner)*);
        });
        $crate::layout!(@grid_children $gctx $($rest)*);
    };

    // -- @align: map bare identifiers to Align variants --
    (@align start) => { $crate::Align::Start };
    (@align center) => { $crate::Align::Center };
    (@align end) => { $crate::Align::End };
    (@align stretch) => { $crate::Align::Stretch };

    // -- @size_mode: map bare identifiers to SizeMode variants --
    (@size_mode min_content) => { $crate::SizeMode::MinContent };
    (@size_mode max_content) => { $crate::SizeMode::MaxContent };
}

/// Generates the 5 standard adapter functions for a renderer backend.
///
/// Each adapter converts `panes::Rect` into a renderer-specific rect type.
/// The macro eliminates the duplicated `convert`, `panels`, `panels_at`,
/// `overlays`, `overlays_at` pattern across adapter crates.
///
/// # Parameters
///
/// - `rect`: target rect type (e.g. `ratatui::layout::Rect`, `egui::Rect`)
/// - `origin`: origin type for `_at` offset variants (must be `Copy`)
/// - `convert_fn`: expression `&panes::Rect -> TargetRect`
/// - `convert_at_fn`: expression `(&panes::Rect, Origin) -> TargetRect`
#[macro_export]
macro_rules! impl_adapter {
    (
        rect: $target_rect:ty,
        origin: $origin_ty:ty,
        convert_fn: $convert:expr,
        convert_at_fn: $convert_at:expr $(,)?
    ) => {
        pub fn convert(
            resolved: &$crate::ResolvedLayout,
        ) -> $crate::__FxHashMap<$crate::PanelId, $target_rect> {
            resolved
                .iter()
                .map(|(pid, r)| (pid, ($convert)(r)))
                .collect()
        }

        pub fn panels(
            resolved: &$crate::ResolvedLayout,
        ) -> impl Iterator<Item = $crate::PanelEntry<'_, $target_rect>> {
            resolved.panels().map(|e| e.map_rect($convert))
        }

        pub fn panels_at(
            resolved: &$crate::ResolvedLayout,
            origin: $origin_ty,
        ) -> impl Iterator<Item = $crate::PanelEntry<'_, $target_rect>> {
            resolved
                .panels()
                .map(move |e| e.map_rect(|r| ($convert_at)(r, origin)))
        }

        pub fn overlays(
            resolved: &$crate::ResolvedLayout,
        ) -> impl Iterator<Item = $crate::OverlayEntry<'_, $target_rect>> {
            resolved.overlays().map(|e| e.map_rect($convert))
        }

        pub fn overlays_at(
            resolved: &$crate::ResolvedLayout,
            origin: $origin_ty,
        ) -> impl Iterator<Item = $crate::OverlayEntry<'_, $target_rect>> {
            resolved
                .overlays()
                .map(move |e| e.map_rect(|r| ($convert_at)(r, origin)))
        }
    };
}