1use crate::_private::NonExhaustive;
24use crate::button::event::ButtonOutcome;
25use crate::text::HasScreenCursor;
26use crate::util::{block_size, revert_style};
27use rat_event::util::{MouseFlags, have_keyboard_enhancement};
28use rat_event::{ConsumedEvent, HandleEvent, MouseOnly, Regular, ct_event};
29use rat_focus::{FocusBuilder, FocusFlag, HasFocus};
30use rat_reloc::{RelocatableState, relocate_area};
31use ratatui::buffer::Buffer;
32use ratatui::layout::Rect;
33use ratatui::prelude::BlockExt;
34use ratatui::style::Style;
35use ratatui::text::Text;
36use ratatui::widgets::{Block, StatefulWidget, Widget};
37use std::thread;
38use std::time::Duration;
39
40#[derive(Debug, Default, Clone)]
42pub struct Button<'a> {
43 text: Text<'a>,
44 style: Style,
45 block: Option<Block<'a>>,
46 focus_style: Option<Style>,
47 hover_style: Option<Style>,
48 armed_style: Option<Style>,
49 armed_delay: Option<Duration>,
50}
51
52#[derive(Debug, Clone)]
54pub struct ButtonStyle {
55 pub style: Style,
57 pub block: Option<Block<'static>>,
59 pub border_style: Option<Style>,
60 pub title_style: Option<Style>,
61 pub focus: Option<Style>,
63 pub armed: Option<Style>,
65 pub hover: Option<Style>,
67 pub armed_delay: Option<Duration>,
71
72 pub non_exhaustive: NonExhaustive,
73}
74
75#[derive(Debug)]
77pub struct ButtonState {
78 pub area: Rect,
81 pub inner: Rect,
84 pub armed: bool,
87 pub armed_delay: Option<Duration>,
94
95 pub focus: FocusFlag,
98
99 pub mouse: MouseFlags,
102
103 pub non_exhaustive: NonExhaustive,
104}
105
106impl Default for ButtonStyle {
107 fn default() -> Self {
108 Self {
109 style: Default::default(),
110 block: Default::default(),
111 border_style: Default::default(),
112 title_style: Default::default(),
113 focus: Default::default(),
114 armed: Default::default(),
115 hover: Default::default(),
116 armed_delay: Default::default(),
117 non_exhaustive: NonExhaustive,
118 }
119 }
120}
121
122impl<'a> Button<'a> {
123 #[inline]
124 pub fn new(text: impl Into<Text<'a>>) -> Self {
125 Self::default().text(text)
126 }
127
128 #[inline]
130 pub fn styles_opt(self, styles: Option<ButtonStyle>) -> Self {
131 if let Some(styles) = styles {
132 self.styles(styles)
133 } else {
134 self
135 }
136 }
137
138 #[inline]
140 pub fn styles(mut self, styles: ButtonStyle) -> Self {
141 self.style = styles.style;
142 if styles.block.is_some() {
143 self.block = styles.block;
144 }
145 if let Some(border_style) = styles.border_style {
146 self.block = self.block.map(|v| v.border_style(border_style));
147 }
148 if let Some(title_style) = styles.title_style {
149 self.block = self.block.map(|v| v.title_style(title_style));
150 }
151 self.block = self.block.map(|v| v.style(self.style));
152 if styles.focus.is_some() {
153 self.focus_style = styles.focus;
154 }
155 if styles.armed.is_some() {
156 self.armed_style = styles.armed;
157 }
158 if styles.armed_delay.is_some() {
159 self.armed_delay = styles.armed_delay;
160 }
161 if styles.hover.is_some() {
162 self.hover_style = styles.hover;
163 }
164 self
165 }
166
167 #[inline]
169 pub fn style(mut self, style: impl Into<Style>) -> Self {
170 let style = style.into();
171 self.style = style.clone();
172 self.block = self.block.map(|v| v.style(style));
173 self
174 }
175
176 #[inline]
178 pub fn focus_style(mut self, style: impl Into<Style>) -> Self {
179 self.focus_style = Some(style.into());
180 self
181 }
182
183 #[inline]
185 pub fn armed_style(mut self, style: impl Into<Style>) -> Self {
186 self.armed_style = Some(style.into());
187 self
188 }
189
190 #[inline]
194 pub fn armed_delay(mut self, delay: Duration) -> Self {
195 self.armed_delay = Some(delay);
196 self
197 }
198
199 #[inline]
201 pub fn hover_style(mut self, style: impl Into<Style>) -> Self {
202 self.hover_style = Some(style.into());
203 self
204 }
205
206 #[inline]
208 pub fn text(mut self, text: impl Into<Text<'a>>) -> Self {
209 self.text = text.into().centered();
210 self
211 }
212
213 #[inline]
215 pub fn left_aligned(mut self) -> Self {
216 self.text = self.text.left_aligned();
217 self
218 }
219
220 #[inline]
222 pub fn right_aligned(mut self) -> Self {
223 self.text = self.text.right_aligned();
224 self
225 }
226
227 #[inline]
229 pub fn block(mut self, block: Block<'a>) -> Self {
230 self.block = Some(block.style(self.style));
231 self
232 }
233
234 #[inline]
236 pub fn width(&self) -> u16 {
237 self.text.width() as u16 + block_size(&self.block).width
238 }
239
240 #[inline]
242 pub fn height(&self) -> u16 {
243 self.text.height() as u16 + block_size(&self.block).height
244 }
245}
246
247impl<'a> StatefulWidget for &Button<'a> {
248 type State = ButtonState;
249
250 fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
251 render_ref(self, area, buf, state);
252 }
253}
254
255impl StatefulWidget for Button<'_> {
256 type State = ButtonState;
257
258 fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
259 render_ref(&self, area, buf, state);
260 }
261}
262
263fn render_ref(widget: &Button<'_>, area: Rect, buf: &mut Buffer, state: &mut ButtonState) {
264 state.area = area;
265 state.inner = widget.block.inner_if_some(area);
266 state.armed_delay = widget.armed_delay;
267
268 let style = widget.style;
269 let focus_style = if let Some(focus_style) = widget.focus_style {
270 focus_style
271 } else {
272 revert_style(style)
273 };
274 let armed_style = if let Some(armed_style) = widget.armed_style {
275 armed_style
276 } else {
277 if state.is_focused() {
278 revert_style(focus_style)
279 } else {
280 revert_style(style)
281 }
282 };
283
284 if let Some(block) = &widget.block {
285 block.render(area, buf);
286 } else {
287 buf.set_style(area, style);
288 }
289
290 if state.mouse.hover.get() && widget.hover_style.is_some() {
291 buf.set_style(state.inner, widget.hover_style.expect("style"))
292 } else if state.is_focused() {
293 buf.set_style(state.inner, focus_style);
294 }
295
296 if state.armed {
297 let armed_area = Rect::new(
298 state.inner.x + 1,
299 state.inner.y,
300 state.inner.width.saturating_sub(2),
301 state.inner.height,
302 );
303 buf.set_style(armed_area, style.patch(armed_style));
304 }
305
306 let h = widget.text.height() as u16;
307 let r = state.inner.height.saturating_sub(h) / 2;
308 let area = Rect::new(state.inner.x, state.inner.y + r, state.inner.width, h);
309 (&widget.text).render(area, buf);
310}
311
312impl Clone for ButtonState {
313 fn clone(&self) -> Self {
314 Self {
315 area: self.area,
316 inner: self.inner,
317 armed: self.armed,
318 armed_delay: self.armed_delay,
319 focus: self.focus.new_instance(),
320 mouse: Default::default(),
321 non_exhaustive: NonExhaustive,
322 }
323 }
324}
325
326impl Default for ButtonState {
327 fn default() -> Self {
328 Self {
329 area: Default::default(),
330 inner: Default::default(),
331 armed: Default::default(),
332 armed_delay: Default::default(),
333 focus: Default::default(),
334 mouse: Default::default(),
335 non_exhaustive: NonExhaustive,
336 }
337 }
338}
339
340impl ButtonState {
341 pub fn new() -> Self {
342 Self::default()
343 }
344
345 pub fn named(name: &str) -> Self {
346 let mut z = Self::default();
347 z.focus = z.focus.with_name(name);
348 z
349 }
350
351 #[deprecated(since = "2.1.0", note = "use relocate_hidden() to clear the areas.")]
352 pub fn clear_areas(&mut self) {
353 self.area = Rect::default();
354 self.inner = Rect::default();
355 }
356}
357
358impl ButtonState {
359 pub fn pressed(&mut self, is_pressed: bool) -> ButtonOutcome {
361 if have_keyboard_enhancement() {
363 if is_pressed {
364 self.armed = true;
365 ButtonOutcome::Changed
366 } else if !is_pressed {
367 if self.armed {
368 if let Some(delay) = self.armed_delay {
369 thread::sleep(delay);
370 }
371 self.armed = false;
372 ButtonOutcome::Pressed
373 } else {
374 ButtonOutcome::Unchanged
376 }
377 } else {
378 ButtonOutcome::Continue
379 }
380 } else {
381 if is_pressed {
382 ButtonOutcome::Pressed
383 } else {
384 ButtonOutcome::Continue
385 }
386 }
387 }
388}
389
390impl HasFocus for ButtonState {
391 fn build(&self, builder: &mut FocusBuilder) {
392 builder.leaf_widget(self);
393 }
394
395 #[inline]
396 fn focus(&self) -> FocusFlag {
397 self.focus.clone()
398 }
399
400 #[inline]
401 fn area(&self) -> Rect {
402 self.area
403 }
404}
405
406impl HasScreenCursor for ButtonState {
407 fn screen_cursor(&self) -> Option<(u16, u16)> {
408 None
409 }
410}
411
412impl RelocatableState for ButtonState {
413 fn relocate(&mut self, shift: (i16, i16), clip: Rect) {
414 self.area = relocate_area(self.area, shift, clip);
415 self.inner = relocate_area(self.inner, shift, clip);
416 }
417}
418
419pub(crate) mod event {
420 use rat_event::{ConsumedEvent, Outcome};
421
422 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
426 pub enum ButtonOutcome {
427 Continue,
429 Unchanged,
431 Changed,
433 Pressed,
435 }
436
437 impl ConsumedEvent for ButtonOutcome {
438 fn is_consumed(&self) -> bool {
439 *self != ButtonOutcome::Continue
440 }
441 }
442
443 impl From<ButtonOutcome> for Outcome {
444 fn from(value: ButtonOutcome) -> Self {
445 match value {
446 ButtonOutcome::Continue => Outcome::Continue,
447 ButtonOutcome::Unchanged => Outcome::Unchanged,
448 ButtonOutcome::Changed => Outcome::Changed,
449 ButtonOutcome::Pressed => Outcome::Changed,
450 }
451 }
452 }
453}
454
455impl HandleEvent<crossterm::event::Event, Regular, ButtonOutcome> for ButtonState {
456 fn handle(&mut self, event: &crossterm::event::Event, _keymap: Regular) -> ButtonOutcome {
457 let r = if self.is_focused() {
458 if have_keyboard_enhancement() {
460 match event {
461 ct_event!(keycode press Enter) | ct_event!(key press ' ') => {
462 self.armed = true;
463 ButtonOutcome::Changed
464 }
465 ct_event!(keycode release Enter) | ct_event!(key release ' ') => {
466 if self.armed {
467 if let Some(delay) = self.armed_delay {
468 thread::sleep(delay);
469 }
470 self.armed = false;
471 ButtonOutcome::Pressed
472 } else {
473 ButtonOutcome::Unchanged
475 }
476 }
477 _ => ButtonOutcome::Continue,
478 }
479 } else {
480 match event {
481 ct_event!(keycode press Enter) | ct_event!(key press ' ') => {
482 ButtonOutcome::Pressed
483 }
484 _ => ButtonOutcome::Continue,
485 }
486 }
487 } else {
488 ButtonOutcome::Continue
489 };
490
491 if r == ButtonOutcome::Continue {
492 HandleEvent::handle(self, event, MouseOnly)
493 } else {
494 r
495 }
496 }
497}
498
499impl HandleEvent<crossterm::event::Event, MouseOnly, ButtonOutcome> for ButtonState {
500 fn handle(&mut self, event: &crossterm::event::Event, _keymap: MouseOnly) -> ButtonOutcome {
501 match event {
502 ct_event!(mouse down Left for column, row) => {
503 if self.area.contains((*column, *row).into()) {
504 self.armed = true;
505 ButtonOutcome::Changed
506 } else {
507 ButtonOutcome::Continue
508 }
509 }
510 ct_event!(mouse up Left for column, row) => {
511 if self.area.contains((*column, *row).into()) {
512 if self.armed {
513 self.armed = false;
514 ButtonOutcome::Pressed
515 } else {
516 ButtonOutcome::Continue
517 }
518 } else {
519 if self.armed {
520 self.armed = false;
521 ButtonOutcome::Changed
522 } else {
523 ButtonOutcome::Continue
524 }
525 }
526 }
527 ct_event!(mouse any for m) if self.mouse.hover(self.area, m) => ButtonOutcome::Changed,
528 _ => ButtonOutcome::Continue,
529 }
530 }
531}
532
533impl HandleEvent<crossterm::event::Event, crossterm::event::KeyEvent, ButtonOutcome>
535 for ButtonState
536{
537 fn handle(
538 &mut self,
539 event: &crossterm::event::Event,
540 hotkey: crossterm::event::KeyEvent,
541 ) -> ButtonOutcome {
542 use crossterm::event::Event;
543
544 let r = match event {
545 Event::Key(key) => {
546 if hotkey.code == key.code && hotkey.modifiers == key.modifiers {
547 self.pressed(key.kind == crossterm::event::KeyEventKind::Press)
548 } else {
549 ButtonOutcome::Continue
550 }
551 }
552 _ => ButtonOutcome::Continue,
553 };
554
555 r.or_else(|| self.handle(event, Regular))
556 }
557}
558
559pub fn handle_events(
563 state: &mut ButtonState,
564 focus: bool,
565 event: &crossterm::event::Event,
566) -> ButtonOutcome {
567 state.focus.set(focus);
568 HandleEvent::handle(state, event, Regular)
569}
570
571pub fn handle_mouse_events(
573 state: &mut ButtonState,
574 event: &crossterm::event::Event,
575) -> ButtonOutcome {
576 HandleEvent::handle(state, event, MouseOnly)
577}