textual 1.0.0-dev

A reactive TUI framework inspired by the Python Textual library
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
use rich_rs::{Console, ConsoleOptions, Renderable, Segment, Segments};

use crate::event::{Event, EventCtx};
use crate::message::MessageEvent;

use crate::widgets::{Spacer, Widget, WidgetStyles, helpers::fixed_height_from_constraints};

pub struct Panel {
    child: Box<dyn Widget>,
    title: Option<String>,
    padding: usize,
    border: bool,
    styles: WidgetStyles,
    child_extracted: bool,
}

impl Panel {
    pub fn new(child: impl Widget + 'static) -> Self {
        Self {
            child: Box::new(child),
            title: None,
            padding: 0,
            border: true,
            styles: WidgetStyles::default(),
            child_extracted: false,
        }
    }

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

    pub fn padding(mut self, padding: usize) -> Self {
        self.padding = padding;
        self
    }

    pub fn border(mut self, border: bool) -> Self {
        self.border = border;
        self
    }
}

impl Widget for Panel {
    fn border_title(&self) -> Option<&str> {
        self.title.as_deref()
    }

    fn take_composed_children(&mut self) -> Vec<Box<dyn Widget>> {
        if self.child_extracted {
            return Vec::new();
        }
        self.child_extracted = true;
        let child = std::mem::replace(&mut self.child, Box::new(Spacer::new(1)));
        vec![child]
    }

    fn render(&self, console: &Console, options: &ConsoleOptions) -> Segments {
        if self.child_extracted {
            // Tree-mode: render border + title chrome only, with blank content.
            let border_width: usize = if self.border { 1 } else { 0 };
            let total_padding = self.padding * 2;
            let width = options.size.0.max(1);
            let height = options.size.1.max(1);
            let inner_width = width
                .saturating_sub(border_width * 2 + total_padding)
                .max(1);
            let content_height = height.saturating_sub(border_width * 2).max(1);

            if !self.border {
                let mut out = Segments::new();
                for idx in 0..height {
                    out.push(Segment::new(" ".repeat(width)));
                    if idx + 1 < height {
                        out.push(Segment::line());
                    }
                }
                return out;
            }

            let box_chars = rich_rs::r#box::SQUARE;
            let mut out_lines: Vec<Vec<Segment>> = Vec::new();

            // Top border with optional title
            let mut top = String::new();
            top.push(box_chars.top_left);
            let mut title = self.title.clone().unwrap_or_default();
            if !title.is_empty() && inner_width >= 2 {
                title = format!(" {title} ");
            }
            let title_width = rich_rs::cell_len(&title);
            if title_width >= inner_width {
                top.push_str(&rich_rs::set_cell_size(&title, inner_width));
            } else {
                let remaining = inner_width.saturating_sub(title_width);
                let left = remaining / 2;
                let right = remaining - left;
                top.push_str(&box_chars.top.to_string().repeat(left));
                top.push_str(&title);
                top.push_str(&box_chars.top.to_string().repeat(right));
            }
            top.push(box_chars.top_right);
            out_lines.push(vec![Segment::new(top)]);

            // Blank content rows
            for _ in 0..content_height {
                let mut middle = Vec::new();
                middle.push(Segment::new(box_chars.mid_left.to_string()));
                middle.push(Segment::new(" ".repeat(inner_width)));
                middle.push(Segment::new(box_chars.mid_right.to_string()));
                out_lines.push(middle);
            }

            // Bottom border
            let mut bottom = String::new();
            bottom.push(box_chars.bottom_left);
            bottom.push_str(&box_chars.bottom.to_string().repeat(inner_width));
            bottom.push(box_chars.bottom_right);
            out_lines.push(vec![Segment::new(bottom)]);

            let out_lines = Segment::set_shape(&out_lines, width, Some(height), None, false);
            let line_count = out_lines.len();
            let mut out = Segments::new();
            for (idx, line) in out_lines.into_iter().enumerate() {
                out.extend(line);
                if idx + 1 < line_count {
                    out.push(Segment::line());
                }
            }
            return out;
        }

        let border_width: usize = if self.border { 1 } else { 0 };
        let total_padding = self.padding * 2;
        let width = options.size.0.max(1);
        let height = options.size.1.max(1);

        let inner_width = width
            .saturating_sub(border_width * 2 + total_padding)
            .max(1);
        let inner_height = height
            .saturating_sub(border_width * 2 + total_padding)
            .max(1);

        let mut child_options = options.clone();
        child_options.size = (inner_width, inner_height);
        child_options.max_width = inner_width;
        child_options.max_height = inner_height;

        let child_segments = self.child.render_styled(console, &child_options);
        let mut child_lines =
            Segment::split_and_crop_lines(child_segments, inner_width, None, true, false);
        if let Some(height) = self.child.layout_height() {
            let capped = height.min(inner_height);
            child_lines = Segment::set_shape(&child_lines, inner_width, Some(capped), None, false);
        }

        let padding_line = vec![Segment::new(" ".repeat(inner_width))];
        let mut content_lines: Vec<Vec<Segment>> = Vec::new();
        for _ in 0..self.padding {
            content_lines.push(padding_line.clone());
        }
        content_lines.extend(child_lines.into_iter());
        for _ in 0..self.padding {
            content_lines.push(padding_line.clone());
        }

        let content_height = content_lines.len().max(1);
        let content_height = content_height.min(height.saturating_sub(border_width * 2).max(1));
        let mut content_lines = Segment::set_shape(
            &content_lines,
            inner_width,
            Some(content_height),
            None,
            false,
        );

        if !self.border {
            let line_count = content_lines.len();
            let mut out = Segments::new();
            for (idx, line) in content_lines.into_iter().enumerate() {
                out.extend(line);
                if idx + 1 < line_count {
                    out.push(Segment::line());
                }
            }
            return out;
        }

        let box_chars = rich_rs::r#box::SQUARE;
        let mut out_lines: Vec<Vec<Segment>> = Vec::new();

        let mut top = String::new();
        top.push(box_chars.top_left);
        let mut title = self.title.clone().unwrap_or_default();
        if !title.is_empty() && inner_width >= 2 {
            title = format!(" {title} ");
        }
        let title_width = rich_rs::cell_len(&title);
        if title_width >= inner_width {
            top.push_str(&rich_rs::set_cell_size(&title, inner_width));
        } else {
            let remaining = inner_width.saturating_sub(title_width);
            let left = remaining / 2;
            let right = remaining - left;
            top.push_str(&box_chars.top.to_string().repeat(left));
            top.push_str(&title);
            top.push_str(&box_chars.top.to_string().repeat(right));
        }
        top.push(box_chars.top_right);
        out_lines.push(vec![Segment::new(top)]);

        for line in content_lines.drain(..) {
            let mut middle = Vec::new();
            middle.push(Segment::new(box_chars.mid_left.to_string()));
            middle.extend(line);
            middle.push(Segment::new(box_chars.mid_right.to_string()));
            out_lines.push(middle);
        }

        let mut bottom = String::new();
        bottom.push(box_chars.bottom_left);
        bottom.push_str(&box_chars.bottom.to_string().repeat(inner_width));
        bottom.push(box_chars.bottom_right);
        out_lines.push(vec![Segment::new(bottom)]);

        let out_lines = Segment::set_shape(&out_lines, width, Some(height), None, false);
        let line_count = out_lines.len();
        let mut out = Segments::new();
        for (idx, line) in out_lines.into_iter().enumerate() {
            out.extend(line);
            if idx + 1 < line_count {
                out.push(Segment::line());
            }
        }
        out
    }

    fn layout_height(&self) -> Option<usize> {
        if let Some(fixed) = fixed_height_from_constraints(self.layout_constraints()) {
            return Some(fixed);
        }
        self.child.layout_height().map(|child| {
            let border = if self.border { 2 } else { 0 };
            let meta = crate::css::selector_meta_generic(self);
            let resolved = crate::css::resolve_style(self, &meta);
            let padding = resolved.effective_padding();
            let (border_top, border_bottom, _border_left, _border_right) =
                crate::widgets::helpers::border_spacing_from_style(&resolved);
            let chrome_tb = usize::from(padding.top.saturating_add(padding.bottom))
                + border_top
                + border_bottom;
            child + self.padding * 2 + border + chrome_tb
        })
    }

    fn content_width(&self) -> Option<usize> {
        self.child.content_width().map(|child| {
            let border = if self.border { 2 } else { 0 };
            let meta = crate::css::selector_meta_generic(self);
            let resolved = crate::css::resolve_style(self, &meta);
            let padding = resolved.effective_padding();
            let (_border_top, _border_bottom, border_left, border_right) =
                crate::widgets::helpers::border_spacing_from_style(&resolved);
            let chrome_lr = usize::from(padding.left.saturating_add(padding.right))
                + border_left
                + border_right;
            child + self.padding * 2 + border + chrome_lr
        })
    }

    fn on_mount(&mut self) {
        if !self.child_extracted {
            self.child.on_mount();
        }
    }

    fn on_unmount(&mut self) {
        if !self.child_extracted {
            self.child.on_unmount();
        }
    }

    fn on_tick(&mut self, tick: u64) {
        if !self.child_extracted {
            self.child.on_tick(tick);
        }
    }

    fn on_resize(&mut self, width: u16, height: u16) {
        if !self.child_extracted {
            self.child.on_resize(width, height);
        }
    }

    fn on_layout(&mut self, width: u16, height: u16) {
        if self.child_extracted {
            return;
        }
        let border_width: usize = if self.border { 1 } else { 0 };
        let total_padding = self.padding.saturating_mul(2);
        let inner_width = usize::from(width)
            .saturating_sub(border_width.saturating_mul(2) + total_padding)
            .max(1);
        let inner_height = usize::from(height)
            .saturating_sub(border_width.saturating_mul(2) + total_padding)
            .max(1);
        self.child
            .on_layout(inner_width as u16, inner_height as u16);
    }

    fn on_event_capture(&mut self, event: &Event, ctx: &mut EventCtx) {
        if !self.child_extracted {
            self.child.on_event_capture(event, ctx);
        }
    }

    fn on_event(&mut self, event: &Event, ctx: &mut EventCtx) {
        if !self.child_extracted {
            self.child.on_event(event, ctx);
        }
    }

    fn on_message(&mut self, message: &MessageEvent, ctx: &mut EventCtx) {
        if !self.child_extracted {
            self.child.on_message(message, ctx);
        }
    }

    fn on_mouse_scroll(&mut self, delta_x: i32, delta_y: i32, ctx: &mut EventCtx) {
        if !self.child_extracted {
            self.child.on_mouse_scroll(delta_x, delta_y, ctx);
        }
    }

    fn focusable(&self) -> bool {
        if self.child_extracted {
            return false;
        }
        self.child.focusable()
    }

    fn set_focus(&mut self, focused: bool) {
        if !self.child_extracted {
            self.child.set_focus(focused);
        }
    }

    fn styles(&self) -> Option<&WidgetStyles> {
        Some(&self.styles)
    }

    fn styles_mut(&mut self) -> Option<&mut WidgetStyles> {
        Some(&mut self.styles)
    }
}

impl Renderable for Panel {
    fn render(&self, console: &Console, options: &ConsoleOptions) -> Segments {
        Widget::render(self, console, options)
    }
}

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

    #[test]
    fn panel_extraction_returns_child() {
        let mut panel = Panel::new(Spacer::new(1));
        let children = panel.take_composed_children();
        assert_eq!(children.len(), 1);
    }

    #[test]
    fn panel_extraction_idempotent() {
        let mut panel = Panel::new(Spacer::new(1));
        let _ = panel.take_composed_children();
        assert!(panel.take_composed_children().is_empty());
    }

    #[test]
    fn panel_render_after_extraction_with_border() {
        let mut panel = Panel::new(Spacer::new(1)).title("Test");
        let _ = panel.take_composed_children();
        let console = Console::new();
        let options = ConsoleOptions {
            size: (20, 5),
            max_width: 20,
            ..Default::default()
        };
        let segments = Widget::render(&panel, &console, &options);
        assert!(!segments.is_empty());
    }

    #[test]
    fn panel_render_after_extraction_no_border() {
        let mut panel = Panel::new(Spacer::new(1)).border(false);
        let _ = panel.take_composed_children();
        let console = Console::new();
        let options = ConsoleOptions {
            size: (20, 5),
            max_width: 20,
            ..Default::default()
        };
        let segments = Widget::render(&panel, &console, &options);
        assert!(!segments.is_empty());
    }

    #[test]
    fn panel_uses_tree_path_after_extraction() {
        let mut panel = Panel::new(Spacer::new(1));
        assert!(!panel.child_extracted);
        let _ = panel.take_composed_children();
        assert!(panel.child_extracted);
    }
}