rat-widget 3.0.2

ratatui widgets extended edition
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//!
//! A message dialog.
//!
//! ```
//! use ratatui_core::buffer::Buffer;
//! use ratatui_core::layout::Rect;
//! use ratatui_core::widgets::{StatefulWidget};
//! use ratatui_widgets::block::{Block};
//! use rat_event::{Dialog, HandleEvent, Outcome};
//! use rat_widget::msgdialog::{MsgDialog, MsgDialogState};
//!
//! let mut state = MsgDialogState::new_active(
//!     "Warning",
//!     "This is some warning etc etc");
//!
//! # let area = Rect::new(5,5,60,15);
//! # let mut buf = Buffer::empty(area);
//! # let buf = &mut buf;
//!
//! MsgDialog::new()
//!     .block(Block::bordered())
//!     .render(area, buf, &mut state);
//!
//!
//! // ...
//!
//! # use ratatui_crossterm::crossterm::event::Event;
//! # let event = Event::FocusGained;//dummy
//! # let event = &event;
//! match state.handle(event, Dialog) {
//!     Outcome::Continue => {}
//!     Outcome::Unchanged | Outcome::Changed => { return; }
//! };
//!
//! ```
//!
//! The trick to make this work like a dialog is to render it
//! as the last thing during your rendering and to let it
//! handle events before any other widgets.
//!
//! Then it will be rendered on top of everything else and will
//! react to events first if it is `active`.
//!

use crate::_private::NonExhaustive;
use crate::button::{Button, ButtonState, ButtonStyle};
use crate::event::ButtonOutcome;
use crate::layout::{DialogItem, LayoutOuter, layout_dialog};
use crate::paragraph::{Paragraph, ParagraphState};
use crate::text::HasScreenCursor;
use crate::util::{block_padding2, reset_buf_area};
use rat_event::{ConsumedEvent, Dialog, HandleEvent, Outcome, Regular, ct_event};
use rat_focus::{Focus, FocusBuilder, FocusFlag, HasFocus};
use rat_reloc::RelocatableState;
use rat_scrolled::{Scroll, ScrollStyle};
use ratatui_core::buffer::Buffer;
use ratatui_core::layout::{Alignment, Constraint, Flex, Position, Rect, Size};
use ratatui_core::style::Style;
use ratatui_core::text::{Line, Text};
use ratatui_core::widgets::{StatefulWidget, Widget};
use ratatui_crossterm::crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use ratatui_widgets::block::{Block, Padding};
use std::cell::{Cell, RefCell};
use std::cmp::max;
use std::fmt::Debug;

/// Basic status dialog for longer messages.
#[derive(Debug, Default, Clone)]
pub struct MsgDialog<'a> {
    style: Style,
    block: Option<Block<'a>>,
    scroll_style: Option<ScrollStyle>,
    button_style: Option<ButtonStyle>,
    markdown: bool,
    markdown_header_1: Option<Style>,
    markdown_header_n: Option<Style>,
    hide_paragraph_focus: bool,

    layout: LayoutOuter,
}

/// Combined style.
#[derive(Debug, Clone)]
pub struct MsgDialogStyle {
    pub style: Style,
    pub block: Option<Block<'static>>,
    pub border_style: Option<Style>,
    pub title_style: Option<Style>,
    pub markdown: Option<bool>,
    pub markdown_header_1: Option<Style>,
    pub markdown_header_n: Option<Style>,
    pub hide_paragraph_focus: Option<bool>,
    pub scroll: Option<ScrollStyle>,

    pub button: Option<ButtonStyle>,

    pub non_exhaustive: NonExhaustive,
}

/// State & event handling.
#[derive(Debug, Clone)]
pub struct MsgDialogState {
    /// Full area.
    /// __readonly__. renewed for each render.
    pub area: Rect,
    /// Area inside the borders.
    /// __readonly__. renewed for each render.
    pub inner: Rect,

    /// Dialog is active.
    /// __read+write__
    pub active: Cell<bool>,
    /// Dialog title
    /// __read+write__
    pub message_title: RefCell<String>,
    /// Dialog text.
    /// __read+write__
    pub message: RefCell<String>,

    /// Ok button
    button: RefCell<ButtonState>,
    /// message-text
    paragraph: RefCell<ParagraphState>,
}

impl<'a> MsgDialog<'a> {
    /// New widget
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable some markdown formatting.
    pub fn markdown(mut self, md: bool) -> Self {
        self.markdown = md;
        self
    }

    /// Header 1 style
    pub fn markdown_header_1(mut self, style: impl Into<Style>) -> Self {
        self.markdown_header_1 = Some(style.into());
        self
    }

    /// Other headers style
    pub fn markdown_header_n(mut self, style: impl Into<Style>) -> Self {
        self.markdown_header_n = Some(style.into());
        self
    }

    /// Show the focus markers for the paragraph.
    pub fn hide_paragraph_focus(mut self, hide: bool) -> Self {
        self.hide_paragraph_focus = hide;
        self
    }

    /// Block
    pub fn block(mut self, block: Block<'a>) -> Self {
        self.block = Some(block);
        self.block = self.block.map(|v| v.style(self.style));
        self
    }

    /// Margin constraint for the left side.
    pub fn left(mut self, left: Constraint) -> Self {
        self.layout = self.layout.left(left);
        self
    }

    /// Margin constraint for the top side.
    pub fn top(mut self, top: Constraint) -> Self {
        self.layout = self.layout.top(top);
        self
    }

    /// Margin constraint for the right side.
    pub fn right(mut self, right: Constraint) -> Self {
        self.layout = self.layout.right(right);
        self
    }

    /// Margin constraint for the bottom side.
    pub fn bottom(mut self, bottom: Constraint) -> Self {
        self.layout = self.layout.bottom(bottom);
        self
    }

    /// Put at a fixed position.
    pub fn position(mut self, pos: Position) -> Self {
        self.layout = self.layout.position(pos);
        self
    }

    /// Constraint for the width.
    pub fn width(mut self, width: Constraint) -> Self {
        self.layout = self.layout.width(width);
        self
    }

    /// Constraint for the height.
    pub fn height(mut self, height: Constraint) -> Self {
        self.layout = self.layout.height(height);
        self
    }

    /// Set at a fixed size.
    pub fn size(mut self, size: Size) -> Self {
        self.layout = self.layout.size(size);
        self
    }

    /// Combined style
    pub fn styles(mut self, styles: MsgDialogStyle) -> Self {
        self.style = styles.style;
        if let Some(markdown) = styles.markdown {
            self.markdown = markdown;
        }
        if let Some(markdown_header_1) = styles.markdown_header_1 {
            self.markdown_header_1 = Some(markdown_header_1);
        }
        if let Some(markdown_header_n) = styles.markdown_header_n {
            self.markdown_header_n = Some(markdown_header_n);
        }
        if let Some(hide_paragraph_focus) = styles.hide_paragraph_focus {
            self.hide_paragraph_focus = hide_paragraph_focus;
        }
        if styles.block.is_some() {
            self.block = styles.block;
        }
        if let Some(border_style) = styles.border_style {
            self.block = self.block.map(|v| v.border_style(border_style));
        }
        if let Some(title_style) = styles.title_style {
            self.block = self.block.map(|v| v.title_style(title_style));
        }
        self.block = self.block.map(|v| v.style(self.style));

        if styles.scroll.is_some() {
            self.scroll_style = styles.scroll;
        }

        if styles.button.is_some() {
            self.button_style = styles.button;
        }

        self
    }

    /// Base style
    pub fn style(mut self, style: impl Into<Style>) -> Self {
        self.style = style.into();
        self.block = self.block.map(|v| v.style(self.style));
        self
    }

    /// Scroll style.
    pub fn scroll_style(mut self, style: ScrollStyle) -> Self {
        self.scroll_style = Some(style);
        self
    }

    /// Button style.
    pub fn button_style(mut self, style: ButtonStyle) -> Self {
        self.button_style = Some(style);
        self
    }
}

impl Default for MsgDialogStyle {
    fn default() -> Self {
        Self {
            style: Default::default(),
            block: Default::default(),
            border_style: Default::default(),
            title_style: Default::default(),
            markdown: Default::default(),
            markdown_header_1: Default::default(),
            markdown_header_n: Default::default(),
            hide_paragraph_focus: Default::default(),
            scroll: Default::default(),
            button: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }
}

impl HasFocus for MsgDialogState {
    fn build(&self, _builder: &mut FocusBuilder) {
        // don't expose inner workings.
    }

    fn focus(&self) -> FocusFlag {
        unimplemented!("not available")
    }

    fn area(&self) -> Rect {
        unimplemented!("not available")
    }
}

impl RelocatableState for MsgDialogState {
    fn relocate(&mut self, shift: (i16, i16), clip: Rect) {
        self.area.relocate(shift, clip);
        self.inner.relocate(shift, clip);
        self.button.borrow_mut().relocate(shift, clip);
        self.paragraph.borrow_mut().relocate(shift, clip);
    }
}

impl HasScreenCursor for MsgDialogState {
    fn screen_cursor(&self) -> Option<(u16, u16)> {
        None
    }
}

impl MsgDialogState {
    pub fn new() -> Self {
        Self::default()
    }

    /// New dialog with active-flag set.
    pub fn new_active(title: impl Into<String>, msg: impl AsRef<str>) -> Self {
        let zelf = Self::default();
        zelf.set_active(true);
        zelf.title(title);
        zelf.append(msg.as_ref());
        zelf
    }

    /// Show the dialog.
    pub fn set_active(&self, active: bool) {
        self.active.set(active);
        self.build_focus().focus(&*self.paragraph.borrow());
        self.paragraph.borrow_mut().set_line_offset(0);
        self.paragraph.borrow_mut().set_col_offset(0);
    }

    /// Dialog is active.
    pub fn active(&self) -> bool {
        self.active.get()
    }

    /// Clear message text, set active to false.
    pub fn clear(&self) {
        self.active.set(false);
        *self.message.borrow_mut() = Default::default();
    }

    /// Set the title for the message.
    pub fn title(&self, title: impl Into<String>) {
        *self.message_title.borrow_mut() = title.into();
    }

    /// *Append* to the message.
    pub fn append(&self, msg: &str) {
        self.set_active(true);
        let mut message = self.message.borrow_mut();
        if !message.is_empty() {
            message.push('\n');
        }
        message.push_str(msg);
    }
}

impl Default for MsgDialogState {
    fn default() -> Self {
        let s = Self {
            active: Default::default(),
            area: Default::default(),
            inner: Default::default(),
            message: Default::default(),
            button: Default::default(),
            paragraph: Default::default(),
            message_title: Default::default(),
        };
        s.paragraph.borrow().focus.set(true);
        s
    }
}

impl MsgDialogState {
    fn build_focus(&self) -> Focus {
        let mut fb = FocusBuilder::default();
        fb.widget(&*self.paragraph.borrow())
            .widget(&*self.button.borrow());
        fb.build()
    }
}

impl<'a> StatefulWidget for &MsgDialog<'a> {
    type State = MsgDialogState;

    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        render_ref(self, area, buf, state);
    }
}

impl StatefulWidget for MsgDialog<'_> {
    type State = MsgDialogState;

    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        render_ref(&self, area, buf, state);
    }
}

fn render_ref(widget: &MsgDialog<'_>, area: Rect, buf: &mut Buffer, state: &mut MsgDialogState) {
    state.area = area;

    if !state.active.get() {
        return;
    }

    let mut block;
    let title = state.message_title.borrow();
    let block = if let Some(b) = &widget.block {
        if !title.is_empty() {
            block = b.clone().title(title.as_str());
            &block
        } else {
            b
        }
    } else {
        block = Block::bordered()
            .style(widget.style)
            .padding(Padding::new(1, 1, 1, 1));
        if !title.is_empty() {
            block = block.title(title.as_str());
        }
        &block
    };

    let l_dlg = layout_dialog(
        area, //
        block_padding2(block),
        [Constraint::Length(10)],
        0,
        Flex::End,
    );
    state.area = l_dlg.area();
    state.inner = l_dlg.widget_for(DialogItem::Inner);

    let header_1 = widget.markdown_header_1.unwrap_or_default();
    let header_n = widget.markdown_header_n.unwrap_or_default();

    reset_buf_area(state.area, buf);
    block.render(state.area, buf);

    {
        let scroll = if let Some(style) = &widget.scroll_style {
            Scroll::new().styles(style.clone())
        } else {
            Scroll::new().style(widget.style)
        };

        let message = state.message.borrow();
        let mut lines = Vec::new();
        if widget.markdown {
            for t in message.lines() {
                if t.starts_with("##") {
                    lines.push(Line::from(t).style(header_n));
                } else if t.starts_with("#") {
                    lines.push(Line::from(t).style(header_1));
                } else {
                    lines.push(Line::from(t));
                }
            }
        } else {
            for t in message.lines() {
                lines.push(Line::from(t));
            }
        }

        let text = Text::from(lines).alignment(Alignment::Center);

        Paragraph::new(text)
            .scroll(scroll)
            .hide_focus(widget.hide_paragraph_focus)
            .render(
                l_dlg.widget_for(DialogItem::Content),
                buf,
                &mut state.paragraph.borrow_mut(),
            );
    }

    Button::new("Ok")
        .styles_opt(widget.button_style.clone())
        .render(
            l_dlg.widget_for(DialogItem::Button(0)),
            buf,
            &mut state.button.borrow_mut(),
        );
}

impl HandleEvent<Event, Dialog, Outcome> for MsgDialogState {
    fn handle(&mut self, event: &Event, _: Dialog) -> Outcome {
        if self.active.get() {
            let mut focus = self.build_focus();
            let f = focus.handle(event, Regular);

            let mut r = match self
                .button
                .borrow_mut()
                .handle(event, KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE))
            {
                ButtonOutcome::Pressed => {
                    self.clear();
                    self.active.set(false);
                    Outcome::Changed
                }
                v => v.into(),
            };
            r = r.or_else(|| self.paragraph.borrow_mut().handle(event, Regular));
            r = r.or_else(|| match event {
                ct_event!(keycode press Esc) => {
                    self.clear();
                    self.active.set(false);
                    Outcome::Changed
                }
                _ => Outcome::Continue,
            });
            // mandatory consume everything else.
            max(max(Outcome::Unchanged, f), r)
        } else {
            Outcome::Continue
        }
    }
}

/// Handle events for the MsgDialog.
pub fn handle_dialog_events(state: &mut MsgDialogState, event: &Event) -> Outcome {
    state.handle(event, Dialog)
}