rat_widget/
checkbox.rs

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
//!
//! Checkbox widget.
//!
//! Can use a third optional/defaulted state too.
//!
//! ```rust ignore
//! use rat_widget::checkbox::{Checkbox, CheckboxState};
//! use ratatui::widgets::StatefulWidget;
//!
//! Checkbox::new()
//!     .text("Carrots 🥕")
//!     .default_settable()
//!     .styles(THEME.checkbox_style())
//!     .render(layout[1][1], frame.buffer_mut(), &mut state.c1);
//!
//! Checkbox::new()
//!     .text("Potatoes 🥔\nTomatoes 🍅")
//!     .default_settable()
//!     .styles(THEME.checkbox_style())
//!     .render(layout[1][2], frame.buffer_mut(), &mut state.c2);
//! ```
//!
use crate::_private::NonExhaustive;
use crate::checkbox::event::CheckOutcome;
use crate::util::{block_size, revert_style};
use rat_event::util::MouseFlags;
use rat_event::{ct_event, HandleEvent, MouseOnly, Regular};
use rat_focus::{FocusBuilder, FocusFlag, HasFocus};
use rat_reloc::{relocate_area, RelocatableState};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::prelude::{BlockExt, StatefulWidget, Text, Widget};
use ratatui::style::Style;
use ratatui::text::Span;
use ratatui::widgets::Block;
#[cfg(feature = "unstable-widget-ref")]
use ratatui::widgets::StatefulWidgetRef;
use std::cmp::max;
use unicode_segmentation::UnicodeSegmentation;

/// Checkbox widget.
#[derive(Debug, Clone)]
pub struct Checkbox<'a> {
    text: Text<'a>,

    // Check state override.
    checked: Option<bool>,
    default: Option<bool>,

    true_str: Span<'a>,
    false_str: Span<'a>,

    style: Style,
    focus_style: Option<Style>,
    block: Option<Block<'a>>,
}

/// Composite style.
#[derive(Debug, Clone)]
pub struct CheckboxStyle {
    /// Base style.
    pub style: Style,
    /// Focused style
    pub focus: Option<Style>,
    /// Border
    pub block: Option<Block<'static>>,

    /// Display text for 'true'
    pub true_str: Option<Span<'static>>,
    /// Display text for 'false'
    pub false_str: Option<Span<'static>>,

    pub non_exhaustive: NonExhaustive,
}

/// State.
#[derive(Debug)]
pub struct CheckboxState {
    /// Complete area
    /// __read only__. renewed for each render.
    pub area: Rect,
    /// Area inside the block.
    /// __read only__. renewed for each render.
    pub inner: Rect,
    /// Area of the check mark.
    /// __read only__. renewed for each render.
    pub check_area: Rect,
    /// Area for the text.
    /// __read only__. renewed for each render.
    pub text_area: Rect,

    /// Checked state.
    /// __read+write__
    pub checked: bool,

    /// Default state.
    /// __read+write__ Maybe overriden by a default set for the widget.
    pub default: bool,

    /// Current focus state.
    /// __read+write__
    pub focus: FocusFlag,

    /// Mouse helper
    /// __read+write__
    pub mouse: MouseFlags,

    pub non_exhaustive: NonExhaustive,
}

pub(crate) mod event {
    use rat_event::{ConsumedEvent, Outcome};

    /// Result value for event-handling.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum CheckOutcome {
        /// The given event was not handled at all.
        Continue,
        /// The event was handled, no repaint necessary.
        Unchanged,
        /// The event was handled, repaint necessary.
        Changed,
        /// Checkbox has been checked or unchecked.
        Value,
    }

    impl ConsumedEvent for CheckOutcome {
        fn is_consumed(&self) -> bool {
            *self != CheckOutcome::Continue
        }
    }

    impl From<CheckOutcome> for Outcome {
        fn from(value: CheckOutcome) -> Self {
            match value {
                CheckOutcome::Continue => Outcome::Continue,
                CheckOutcome::Unchanged => Outcome::Unchanged,
                CheckOutcome::Changed => Outcome::Changed,
                CheckOutcome::Value => Outcome::Changed,
            }
        }
    }
}

impl Default for CheckboxStyle {
    fn default() -> Self {
        Self {
            style: Default::default(),
            focus: None,
            block: Default::default(),
            true_str: None,
            false_str: None,
            non_exhaustive: NonExhaustive,
        }
    }
}

impl Default for Checkbox<'_> {
    fn default() -> Self {
        Self {
            text: Default::default(),
            checked: None,
            default: None,
            true_str: Span::from("[\u{2713}]"),
            false_str: Span::from("[ ]"),
            style: Default::default(),
            focus_style: None,
            block: None,
        }
    }
}

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

    /// Set all styles.
    pub fn styles(mut self, styles: CheckboxStyle) -> Self {
        self.style = styles.style;
        if styles.focus.is_some() {
            self.focus_style = styles.focus;
        }
        if let Some(block) = styles.block {
            self.block = Some(block);
        }
        if let Some(true_str) = styles.true_str {
            self.true_str = true_str;
        }
        if let Some(false_str) = styles.false_str {
            self.false_str = false_str;
        }
        self.block = self.block.map(|v| v.style(self.style));
        self
    }

    /// Set the base-style.
    #[inline]
    pub fn style(mut self, style: impl Into<Style>) -> Self {
        self.style = style.into();
        self
    }

    /// Style when focused.
    #[inline]
    pub fn focus_style(mut self, style: impl Into<Style>) -> Self {
        self.focus_style = Some(style.into());
        self
    }

    /// Button text.
    #[inline]
    pub fn text(mut self, text: impl Into<Text<'a>>) -> Self {
        self.text = text.into();
        self
    }

    /// Checked state. If set overrides the value from the state.
    pub fn checked(mut self, checked: bool) -> Self {
        self.checked = Some(checked);
        self
    }

    /// Default state. If set overrides the value from the state.
    pub fn default_(mut self, default: bool) -> Self {
        self.default = Some(default);
        self
    }

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

    /// Text for true
    pub fn true_str(mut self, str: Span<'a>) -> Self {
        self.true_str = str;
        self
    }

    /// Text for false
    pub fn false_str(mut self, str: Span<'a>) -> Self {
        self.false_str = str;
        self
    }

    /// Length of the check
    fn check_len(&self) -> u16 {
        max(
            self.true_str.content.graphemes(true).count(),
            self.false_str.content.graphemes(true).count(),
        ) as u16
    }

    /// Inherent width.
    pub fn width(&self) -> u16 {
        let chk_len = self.check_len();
        let txt_len = self.text.width() as u16;

        chk_len + 1 + txt_len + block_size(&self.block).width
    }

    /// Inherent height.
    pub fn height(&self) -> u16 {
        self.text.height() as u16 + block_size(&self.block).height
    }
}

#[cfg(feature = "unstable-widget-ref")]
impl<'a> StatefulWidgetRef for Checkbox<'a> {
    type State = CheckboxState;

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

impl StatefulWidget for Checkbox<'_> {
    type State = CheckboxState;

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

fn render_ref(widget: &Checkbox<'_>, area: Rect, buf: &mut Buffer, state: &mut CheckboxState) {
    state.area = area;
    state.inner = widget.block.inner_if_some(area);

    let chk_len = widget.check_len();
    state.check_area = Rect::new(state.inner.x, state.inner.y, chk_len, 1);
    state.text_area = Rect::new(
        state.inner.x + chk_len + 1,
        state.inner.y,
        state.inner.width.saturating_sub(chk_len + 1),
        state.inner.height,
    );

    if let Some(checked) = widget.checked {
        state.checked = checked;
    }
    if let Some(default) = widget.default {
        state.default = default;
    }

    let style = widget.style;
    let focus_style = if let Some(focus_style) = widget.focus_style {
        style.patch(focus_style)
    } else {
        revert_style(style)
    };

    if widget.block.is_some() {
        widget.block.render(area, buf);
        if state.focus.get() {
            buf.set_style(state.inner, focus_style);
        }
    } else {
        if state.focus.get() {
            buf.set_style(state.inner, focus_style);
        } else {
            buf.set_style(state.inner, widget.style);
        }
    }

    let cc = if state.checked {
        &widget.true_str
    } else {
        &widget.false_str
    };
    cc.render(state.check_area, buf);
    (&widget.text).render(state.text_area, buf);
}

impl Clone for CheckboxState {
    fn clone(&self) -> Self {
        Self {
            area: self.area,
            inner: self.inner,
            check_area: self.check_area,
            text_area: self.text_area,
            checked: self.checked,
            default: self.default,
            focus: FocusFlag::named(self.focus.name()),
            mouse: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }
}

impl Default for CheckboxState {
    fn default() -> Self {
        Self {
            area: Default::default(),
            inner: Default::default(),
            check_area: Default::default(),
            text_area: Default::default(),
            checked: false,
            default: false,
            focus: Default::default(),
            mouse: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }
}

impl HasFocus for CheckboxState {
    fn build(&self, builder: &mut FocusBuilder) {
        builder.leaf_widget(self);
    }

    fn focus(&self) -> FocusFlag {
        self.focus.clone()
    }

    fn area(&self) -> Rect {
        self.area
    }
}

impl RelocatableState for CheckboxState {
    fn relocate(&mut self, shift: (i16, i16), clip: Rect) {
        self.area = relocate_area(self.area, shift, clip);
        self.inner = relocate_area(self.inner, shift, clip);
    }
}

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

    pub fn named(name: &str) -> Self {
        Self {
            focus: FocusFlag::named(name),
            ..Default::default()
        }
    }

    /// Get the value.
    pub fn checked(&self) -> bool {
        self.checked
    }

    /// Set the value.
    pub fn set_checked(&mut self, checked: bool) -> bool {
        let old_value = self.checked;
        self.checked = checked;
        old_value != self.checked
    }

    /// Get the default value.
    pub fn default_(&self) -> bool {
        self.default
    }

    /// Set the default value.
    pub fn set_default(&mut self, default: bool) -> bool {
        let old_value = self.default;
        self.default = default;
        old_value != self.default
    }

    /// Get the checked value, disregarding of the default state.
    pub fn value(&self) -> bool {
        self.checked
    }

    /// Set checked value. Always sets default to false.
    pub fn set_value(&mut self, checked: bool) -> bool {
        let old_value = self.checked;
        self.checked = checked;
        old_value != self.checked
    }

    /// Flip the checkbox.
    /// If it was in default state it just switches off
    /// the default flag. Otherwise, it flips true/false.
    pub fn flip_checked(&mut self) {
        self.checked = !self.checked;
    }
}

impl HandleEvent<crossterm::event::Event, Regular, CheckOutcome> for CheckboxState {
    fn handle(&mut self, event: &crossterm::event::Event, _qualifier: Regular) -> CheckOutcome {
        let r = if self.is_focused() {
            match event {
                ct_event!(keycode press Enter) | ct_event!(key press ' ') => {
                    self.flip_checked();
                    CheckOutcome::Value
                }
                ct_event!(keycode press Backspace) | ct_event!(keycode press Delete) => {
                    self.set_value(self.default);
                    CheckOutcome::Value
                }
                _ => CheckOutcome::Continue,
            }
        } else {
            CheckOutcome::Continue
        };

        if r == CheckOutcome::Continue {
            HandleEvent::handle(self, event, MouseOnly)
        } else {
            r
        }
    }
}

impl HandleEvent<crossterm::event::Event, MouseOnly, CheckOutcome> for CheckboxState {
    fn handle(&mut self, event: &crossterm::event::Event, _keymap: MouseOnly) -> CheckOutcome {
        match event {
            ct_event!(mouse any for m) if self.mouse.doubleclick(self.area, m) => {
                self.flip_checked();
                CheckOutcome::Value
            }
            _ => CheckOutcome::Continue,
        }
    }
}

/// Handle all events.
/// Text events are only processed if focus is true.
/// Mouse events are processed if they are in range.
pub fn handle_events(
    state: &mut CheckboxState,
    focus: bool,
    event: &crossterm::event::Event,
) -> CheckOutcome {
    state.focus.set(focus);
    HandleEvent::handle(state, event, Regular)
}

/// Handle only mouse-events.
pub fn handle_mouse_events(
    state: &mut CheckboxState,
    event: &crossterm::event::Event,
) -> CheckOutcome {
    HandleEvent::handle(state, event, MouseOnly)
}