Skip to main content

gm_lib/tui/app/widgets/
button.rs

1use crate::tui::theme::Theme;
2use crate::tui::traits::BorderedWidget;
3use ratatui::{layout::Rect, style::Style, text::Line, widgets::Block};
4
5pub struct Button {
6    pub focus: bool,
7    pub label: &'static str,
8}
9
10impl Button {
11    pub fn render(
12        &self,
13        area: ratatui::prelude::Rect,
14        buf: &mut ratatui::prelude::Buffer,
15        theme: &Theme,
16    ) where
17        Self: Sized,
18    {
19        let button_area = Rect {
20            width: (self.label.len() + 2) as u16,
21            height: 3,
22            x: area.x,
23            y: area.y,
24        };
25
26        Line::from(self.label).render_with_block(
27            button_area,
28            buf,
29            Block::bordered()
30                .border_type(theme.into())
31                .style(if self.focus {
32                    theme.button_focused()
33                } else {
34                    Style::default()
35                }),
36        );
37    }
38}