tui-lipan 0.2.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets on top of ratatui.
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
//! Modal widget.

use crate::callback::Callback;
use crate::core::element::{Element, ElementKind};
use crate::core::event::MouseEvent;
use crate::overlay::{
    DismissPolicy, OverlayLayer, OverlayPlacement, OverlayScope, PointerCapture, Portal,
};
use crate::style::{Align, BorderStyle, Color, Length, Padding, RichText, Size, Style, StyleSlot};
use crate::widgets::{BorderLabels, Center, Frame, FrameLabel, MouseRegion, Spacer, ZStack};

/// A modal dialog with optional title and child content.
#[derive(Clone)]
pub struct Modal {
    title: Option<RichText>,
    child: Element,
    on_close: Option<Callback<()>>,
    scope: OverlayScope,
    width: Length,
    height: Length,
    max_height: Option<Length>,
    reserve_height: Option<Length>,
    backdrop_style: Style,
    frame_style: Style,
    focus_style: StyleSlot,
    auto_focus: bool,
    border: bool,
    border_style: BorderStyle,
    padding: Padding,
    title_style: Style,
    title_alignment: Align,
}

impl Modal {
    /// Create a new modal.
    pub fn new() -> Self {
        Self {
            title: None,
            child: Spacer::new().into(),
            on_close: None,
            scope: OverlayScope::RootPortal,
            width: Length::Px(60),
            height: Length::Auto,
            max_height: None,
            reserve_height: None,
            backdrop_style: Style::default(),
            frame_style: Style::default(),
            focus_style: StyleSlot::Inherit,
            auto_focus: true,
            border: true,
            border_style: BorderStyle::Plain,
            padding: 1.into(),
            title_style: Style::default(),
            title_alignment: Align::Start,
        }
    }

    /// Set title.
    pub fn title(mut self, title: impl Into<RichText>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Set modal child content.
    pub fn child(mut self, child: impl Into<Element>) -> Self {
        self.child = child.into();
        self
    }

    /// Set width.
    pub fn width(mut self, width: Length) -> Self {
        self.width = width;
        self
    }

    /// Set height.
    pub fn height(mut self, height: Length) -> Self {
        self.height = height;
        self
    }

    /// Cap the modal height. Pair with `height(Length::Auto)` so the modal hugs its content
    /// but never exceeds this cap; the inner content scrolls when it overflows.
    pub fn max_height(mut self, max_height: Length) -> Self {
        self.max_height = Some(max_height);
        self
    }

    /// Center a `RootPortal` modal vertically as if it were this tall, then top-align the modal
    /// within that reserved band. Its top edge stays fixed at `(viewport - reserve_height) / 2`
    /// as its content grows and shrinks, instead of the whole modal drifting toward the vertical
    /// center. Content taller than the band keeps that same top edge and extends past the band's
    /// bottom, so pair this with [`max_height`](Self::max_height) to bound it. Has no effect in
    /// `OverlayScope::Local`.
    pub fn reserve_height(mut self, reserve_height: Length) -> Self {
        self.reserve_height = Some(reserve_height);
        self
    }

    /// Set on-close callback (fired when background is clicked).
    pub fn on_close(mut self, cb: Callback<()>) -> Self {
        self.on_close = Some(cb);
        self
    }

    /// Set overlay scope (portal vs local rendering).
    pub fn scope(mut self, scope: OverlayScope) -> Self {
        self.scope = scope;
        self
    }

    /// Control whether a root-portal modal focuses its first focusable descendant.
    ///
    /// Disabling this keeps keyboard and pointer capture active while focus is suspended.
    pub fn auto_focus(mut self, auto_focus: bool) -> Self {
        self.auto_focus = auto_focus;
        self
    }

    /// Set backdrop style.
    pub fn backdrop_style(mut self, style: Style) -> Self {
        self.backdrop_style = style;
        self
    }

    /// Set modal frame style.
    pub fn frame_style(mut self, style: Style) -> Self {
        self.frame_style = style;
        self
    }

    /// Set the frame style used while the modal (or a descendant input) holds focus. Root-portal
    /// modals capture focus as soon as they open, so without this the frame falls back to the theme
    /// focus role, which overrides a deliberate `frame_style` border color. Set both to keep an
    /// intentional accent (e.g. an error border) visible on a focused dialog.
    pub fn focus_style(mut self, style: Style) -> Self {
        self.focus_style = StyleSlot::Replace(style);
        self
    }

    /// Extend the themed focus style for the modal frame.
    pub fn extend_focus_style(mut self, style: Style) -> Self {
        self.focus_style = StyleSlot::Extend(style);
        self
    }

    /// Inherit the themed focus style for the modal frame.
    pub fn inherit_focus_style(mut self) -> Self {
        self.focus_style = StyleSlot::Inherit;
        self
    }

    /// Enable or disable border decoration.
    pub fn border(mut self, border: bool) -> Self {
        self.border = border;
        self
    }

    /// Set border style.
    pub fn border_style(mut self, border_style: BorderStyle) -> Self {
        self.border_style = border_style;
        self
    }

    /// Set padding.
    pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
        self.padding = padding.into();
        self
    }

    /// Set title style.
    pub fn title_style(mut self, style: Style) -> Self {
        self.title_style = style;
        self
    }

    /// Set title alignment.
    pub fn title_alignment(mut self, align: Align) -> Self {
        self.title_alignment = align;
        self
    }
}

impl Default for Modal {
    fn default() -> Self {
        Self::new()
    }
}

impl From<Modal> for Element {
    fn from(modal: Modal) -> Self {
        let frame_style = if modal.frame_style.bg.is_none() {
            modal.frame_style.bg(Color::Backdrop)
        } else {
            modal.frame_style
        };

        let mut base_frame = Frame::new()
            .border(modal.border)
            .border_style(modal.border_style)
            .padding(modal.padding)
            .child(modal.child)
            .style(frame_style)
            .focus_style_slot(modal.focus_style);
        if let Some(title) = modal.title {
            let label = FrameLabel::new(title).style(modal.title_style);
            let header = match modal.title_alignment {
                Align::Center => BorderLabels::new().center(label),
                Align::End => BorderLabels::new().right(label),
                Align::Start | Align::Stretch => BorderLabels::new().left(label),
            };
            base_frame = base_frame.header(header);
        }

        match modal.scope {
            OverlayScope::Local => {
                let mut backdrop = MouseRegion::new().capture_click(true);

                if !modal.backdrop_style.is_empty() {
                    backdrop = backdrop.child(
                        Center::new()
                            .width(Size::Percent(100))
                            .height(Size::Percent(100))
                            .style(modal.backdrop_style),
                    );
                }

                if let Some(on_close) = modal.on_close {
                    let cb = on_close.clone();
                    backdrop = backdrop.on_click(Callback::new(move |_: MouseEvent| cb.emit(())));
                } else {
                    backdrop = backdrop.enabled(false);
                }

                let local_width = match modal.width {
                    Length::Auto => Size::Auto,
                    Length::Px(px) => Size::Fixed(px),
                    Length::Percent(percent) => Size::Percent(percent),
                    Length::Flex(_) => Size::Percent(100),
                };
                let local_height = match modal.height {
                    Length::Auto => Size::Auto,
                    Length::Px(px) => Size::Fixed(px),
                    Length::Percent(percent) => Size::Percent(percent),
                    Length::Flex(_) => Size::Percent(100),
                };

                let local_frame_width = if matches!(modal.width, Length::Auto) {
                    Length::Auto
                } else {
                    Length::Flex(1)
                };
                let local_frame_height = if matches!(modal.height, Length::Auto) {
                    Length::Auto
                } else {
                    Length::Flex(1)
                };

                let frame = base_frame
                    .clone()
                    .width(local_frame_width)
                    .height(local_frame_height);

                let content = Center::new()
                    .width(local_width)
                    .height(local_height)
                    .child(frame);
                let content: Element = match modal.max_height {
                    Some(max_height) => Element::from(content).max_height(max_height),
                    None => content.into(),
                };
                ZStack::new().child(backdrop).child(content).into()
            }
            OverlayScope::RootPortal => {
                let frame = base_frame.width(modal.width).height(modal.height);
                let dismiss_policy = if modal.on_close.is_some() {
                    DismissPolicy::ClickOutsideOrEscape
                } else {
                    DismissPolicy::None
                };
                let portal = Portal {
                    layer: OverlayLayer::Modal,
                    content: Box::new(frame.into()),
                    placement: OverlayPlacement::Center {
                        reserve_height: modal.reserve_height,
                    },
                    dismiss_policy,
                    on_close: modal.on_close,
                    backdrop: Some(modal.backdrop_style),
                    captures_focus: true,
                    auto_focus: modal.auto_focus,
                    captures_pointer: PointerCapture::BackdropFullScreen,
                };
                let element = Element::new(ElementKind::Portal(portal));
                match modal.max_height {
                    Some(max_height) => element.max_height(max_height),
                    None => element,
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::style::Color;

    #[test]
    fn local_scope_percent_size_is_resolved_by_center_layer() {
        let element: Element = Modal::new()
            .title("Local Percent")
            .scope(OverlayScope::Local)
            .width(Length::Percent(70))
            .height(Length::Percent(50))
            .child(Spacer::new())
            .into();

        let ElementKind::ZStack(zstack) = element.kind else {
            panic!("modal local scope must render as zstack");
        };
        assert_eq!(zstack.children.len(), 2);

        let ElementKind::Center(center) = &zstack.children[1].kind else {
            panic!("modal content layer must be centered");
        };
        assert_eq!(center.width, Size::Percent(70));
        assert_eq!(center.height, Size::Percent(50));

        let frame = center
            .child
            .as_deref()
            .expect("center must contain modal frame");
        let ElementKind::Frame(frame) = &frame.kind else {
            panic!("center child must be frame");
        };
        assert_eq!(frame.props.width, Length::Flex(1));
        assert_eq!(frame.props.height, Length::Flex(1));
        assert_eq!(frame.props.style.bg, Some(Color::Backdrop.into()));
    }

    #[test]
    fn explicit_transparent_frame_style_bg_is_preserved() {
        let element: Element = Modal::new()
            .scope(OverlayScope::Local)
            .frame_style(Style::new().bg(Color::Transparent))
            .child(Spacer::new())
            .into();

        let ElementKind::ZStack(zstack) = element.kind else {
            panic!("modal local scope must render as zstack");
        };

        let ElementKind::Center(center) = &zstack.children[1].kind else {
            panic!("modal content layer must be centered");
        };
        let frame = center
            .child
            .as_deref()
            .expect("center must contain modal frame");
        let ElementKind::Frame(frame) = &frame.kind else {
            panic!("center child must be frame");
        };
        assert_eq!(frame.props.style.bg, Some(Color::Transparent.into()));
    }

    #[test]
    fn focus_style_is_forwarded_to_frame() {
        let focus_style = Style::new().fg(Color::Red);
        let element: Element = Modal::new()
            .scope(OverlayScope::Local)
            .focus_style(focus_style)
            .child(Spacer::new())
            .into();

        let ElementKind::ZStack(zstack) = element.kind else {
            panic!("modal local scope must render as zstack");
        };
        let ElementKind::Center(center) = &zstack.children[1].kind else {
            panic!("modal content layer must be centered");
        };
        let frame = center
            .child
            .as_deref()
            .expect("center must contain modal frame");
        let ElementKind::Frame(frame) = &frame.kind else {
            panic!("center child must be frame");
        };
        assert_eq!(frame.props.focus_style(), Some(focus_style));
    }

    #[test]
    fn local_scope_backdrop_uses_mouse_region_layer() {
        let element: Element = Modal::new()
            .title("Local Backdrop")
            .scope(OverlayScope::Local)
            .backdrop_style(Style::new().dim_by(0.5))
            .child(Spacer::new())
            .into();

        let ElementKind::ZStack(zstack) = element.kind else {
            panic!("modal local scope must render as zstack");
        };
        assert_eq!(zstack.children.len(), 2);

        let ElementKind::MouseRegion(backdrop) = &zstack.children[0].kind else {
            panic!("first local layer must be mouse region backdrop");
        };
        let backdrop_child = backdrop
            .child
            .as_deref()
            .expect("non-empty backdrop style must attach backdrop child");
        let ElementKind::Center(backdrop_layer) = &backdrop_child.kind else {
            panic!("backdrop child must be center layer");
        };
        assert_eq!(backdrop_layer.style.dim_amount, Some(0.5));
    }
}