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
//!
//! Button widget.
//!
use crate::_private::NonExhaustive;
use rat_focus::{FocusFlag, HasFocusFlag};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::prelude::{Line, Span, StatefulWidget, Style, Text};
use ratatui::widgets::{Block, StatefulWidgetRef};

use crate::event::{FocusKeys, MouseOnly};
pub use rat_input::button::{ButtonOutcome, ButtonStyle};
use rat_input::event::HandleEvent;

/// Button widget.
#[derive(Debug, Default, Clone)]
pub struct RButton<'a> {
    widget: rat_input::button::Button<'a>,
}

/// Button state.
#[derive(Debug, Clone)]
pub struct RButtonState {
    /// Button state
    pub widget: rat_input::button::ButtonState,
    pub non_exhaustive: NonExhaustive,
}

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

    /// Set all styles.
    #[inline]
    pub fn styles(mut self, styles: ButtonStyle) -> Self {
        self.widget = self.widget.styles(styles);
        self
    }

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

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

    /// Style when the button is clicked but not yet released.
    #[inline]
    pub fn armed_style(mut self, style: impl Into<Style>) -> Self {
        self.widget = self.widget.armed_style(style);
        self
    }

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

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

impl<'a> From<&'a str> for RButton<'a> {
    fn from(value: &'a str) -> Self {
        Self {
            widget: value.into(),
        }
    }
}

impl<'a> From<String> for RButton<'a> {
    fn from(value: String) -> Self {
        Self {
            widget: value.into(),
        }
    }
}

impl<'a> From<Span<'a>> for RButton<'a> {
    fn from(value: Span<'a>) -> Self {
        Self {
            widget: value.into(),
        }
    }
}

impl<'a, const N: usize> From<[Span<'a>; N]> for RButton<'a> {
    fn from(value: [Span<'a>; N]) -> Self {
        Self {
            widget: value.into(),
        }
    }
}

impl<'a> From<Vec<Span<'a>>> for RButton<'a> {
    fn from(value: Vec<Span<'a>>) -> Self {
        Self {
            widget: value.into(),
        }
    }
}

impl<'a> From<Line<'a>> for RButton<'a> {
    fn from(value: Line<'a>) -> Self {
        Self {
            widget: value.into(),
        }
    }
}

impl<'a> StatefulWidgetRef for RButton<'a> {
    type State = RButtonState;

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

impl<'a> StatefulWidget for RButton<'a> {
    type State = RButtonState;

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

impl Default for RButtonState {
    fn default() -> Self {
        Self {
            widget: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }
}

impl HasFocusFlag for RButtonState {
    #[inline]
    fn focus(&self) -> &FocusFlag {
        &self.widget.focus
    }

    #[inline]
    fn area(&self) -> Rect {
        self.widget.area
    }
}

impl HandleEvent<crossterm::event::Event, FocusKeys, ButtonOutcome> for RButtonState {
    fn handle(&mut self, event: &crossterm::event::Event, _keymap: FocusKeys) -> ButtonOutcome {
        if self.is_focused() {
            self.widget.handle(event, FocusKeys)
        } else {
            self.widget.handle(event, MouseOnly)
        }
    }
}

impl HandleEvent<crossterm::event::Event, MouseOnly, ButtonOutcome> for RButtonState {
    fn handle(&mut self, event: &crossterm::event::Event, _keymap: MouseOnly) -> ButtonOutcome {
        self.widget.handle(event, MouseOnly)
    }
}