2024-01-24 19:50:18 +00:00
|
|
|
//! # [Ratatui] Custom Widget example
|
|
|
|
//!
|
|
|
|
//! The latest version of this example is available in the [examples] folder in the repository.
|
|
|
|
//!
|
|
|
|
//! Please note that the examples are designed to be run against the `main` branch of the Github
|
|
|
|
//! repository. This means that you may not be able to compile with the latest release version on
|
|
|
|
//! crates.io, or the one that you have installed locally.
|
|
|
|
//!
|
|
|
|
//! See the [examples readme] for more information on finding examples that match the version of the
|
|
|
|
//! library you are using.
|
|
|
|
//!
|
|
|
|
//! [Ratatui]: https://github.com/ratatui-org/ratatui
|
|
|
|
//! [examples]: https://github.com/ratatui-org/ratatui/blob/main/examples
|
|
|
|
//! [examples readme]: https://github.com/ratatui-org/ratatui/blob/main/examples/README.md
|
|
|
|
|
2023-09-28 03:07:45 +00:00
|
|
|
use std::{error::Error, io, ops::ControlFlow, time::Duration};
|
2023-06-12 05:07:15 +00:00
|
|
|
|
2024-05-28 20:23:39 +00:00
|
|
|
use ratatui::{
|
2024-05-29 11:42:29 +00:00
|
|
|
backend::{Backend, CrosstermBackend},
|
|
|
|
buffer::Buffer,
|
2024-05-28 20:23:39 +00:00
|
|
|
crossterm::{
|
|
|
|
event::{
|
|
|
|
self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, MouseButton, MouseEvent,
|
|
|
|
MouseEventKind,
|
|
|
|
},
|
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
2023-09-28 03:07:45 +00:00
|
|
|
},
|
2024-05-29 11:42:29 +00:00
|
|
|
layout::{Constraint, Layout, Rect},
|
|
|
|
style::{Color, Style},
|
|
|
|
terminal::{Frame, Terminal},
|
|
|
|
text::Line,
|
|
|
|
widgets::{Paragraph, Widget},
|
2021-11-01 21:27:46 +00:00
|
|
|
};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2023-09-28 03:07:45 +00:00
|
|
|
/// A custom widget that renders a button with a label, theme and state.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct Button<'a> {
|
|
|
|
label: Line<'a>,
|
|
|
|
theme: Theme,
|
|
|
|
state: State,
|
2016-11-02 18:17:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 03:07:45 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
enum State {
|
|
|
|
Normal,
|
|
|
|
Selected,
|
|
|
|
Active,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
struct Theme {
|
|
|
|
text: Color,
|
|
|
|
background: Color,
|
|
|
|
highlight: Color,
|
|
|
|
shadow: Color,
|
|
|
|
}
|
|
|
|
|
|
|
|
const BLUE: Theme = Theme {
|
|
|
|
text: Color::Rgb(16, 24, 48),
|
|
|
|
background: Color::Rgb(48, 72, 144),
|
|
|
|
highlight: Color::Rgb(64, 96, 192),
|
|
|
|
shadow: Color::Rgb(32, 48, 96),
|
|
|
|
};
|
|
|
|
|
|
|
|
const RED: Theme = Theme {
|
|
|
|
text: Color::Rgb(48, 16, 16),
|
|
|
|
background: Color::Rgb(144, 48, 48),
|
|
|
|
highlight: Color::Rgb(192, 64, 64),
|
|
|
|
shadow: Color::Rgb(96, 32, 32),
|
|
|
|
};
|
|
|
|
|
|
|
|
const GREEN: Theme = Theme {
|
|
|
|
text: Color::Rgb(16, 48, 16),
|
|
|
|
background: Color::Rgb(48, 144, 48),
|
|
|
|
highlight: Color::Rgb(64, 192, 64),
|
|
|
|
shadow: Color::Rgb(32, 96, 32),
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A button with a label that can be themed.
|
|
|
|
impl<'a> Button<'a> {
|
2024-03-02 09:06:53 +00:00
|
|
|
pub fn new<T: Into<Line<'a>>>(label: T) -> Self {
|
2023-09-28 03:07:45 +00:00
|
|
|
Button {
|
|
|
|
label: label.into(),
|
|
|
|
theme: BLUE,
|
|
|
|
state: State::Normal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
pub const fn theme(mut self, theme: Theme) -> Self {
|
2023-09-28 03:07:45 +00:00
|
|
|
self.theme = theme;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
pub const fn state(mut self, state: State) -> Self {
|
2023-09-28 03:07:45 +00:00
|
|
|
self.state = state;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Widget for Button<'a> {
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::cast_possible_truncation)]
|
2019-12-15 20:38:18 +00:00
|
|
|
fn render(self, area: Rect, buf: &mut Buffer) {
|
2023-09-28 03:07:45 +00:00
|
|
|
let (background, text, shadow, highlight) = self.colors();
|
|
|
|
buf.set_style(area, Style::new().bg(background).fg(text));
|
|
|
|
|
|
|
|
// render top line if there's enough space
|
|
|
|
if area.height > 2 {
|
|
|
|
buf.set_string(
|
|
|
|
area.x,
|
|
|
|
area.y,
|
|
|
|
"▔".repeat(area.width as usize),
|
|
|
|
Style::new().fg(highlight).bg(background),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// render bottom line if there's enough space
|
|
|
|
if area.height > 1 {
|
|
|
|
buf.set_string(
|
|
|
|
area.x,
|
|
|
|
area.y + area.height - 1,
|
|
|
|
"▁".repeat(area.width as usize),
|
|
|
|
Style::new().fg(shadow).bg(background),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// render label centered
|
|
|
|
buf.set_line(
|
|
|
|
area.x + (area.width.saturating_sub(self.label.width() as u16)) / 2,
|
|
|
|
area.y + (area.height.saturating_sub(1)) / 2,
|
|
|
|
&self.label,
|
|
|
|
area.width,
|
|
|
|
);
|
2016-11-02 18:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 03:07:45 +00:00
|
|
|
impl Button<'_> {
|
2024-03-02 09:06:53 +00:00
|
|
|
const fn colors(&self) -> (Color, Color, Color, Color) {
|
2023-09-28 03:07:45 +00:00
|
|
|
let theme = self.theme;
|
|
|
|
match self.state {
|
|
|
|
State::Normal => (theme.background, theme.text, theme.shadow, theme.highlight),
|
|
|
|
State::Selected => (theme.highlight, theme.text, theme.shadow, theme.highlight),
|
|
|
|
State::Active => (theme.background, theme.text, theme.highlight, theme.shadow),
|
|
|
|
}
|
2016-11-02 18:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2021-11-01 21:27:46 +00:00
|
|
|
// setup terminal
|
|
|
|
enable_raw_mode()?;
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
2018-09-23 18:59:51 +00:00
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// create app and run it
|
|
|
|
let res = run_app(&mut terminal);
|
|
|
|
|
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
if let Err(err) = res {
|
2023-05-22 03:46:02 +00:00
|
|
|
println!("{err:?}");
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()> {
|
2023-09-28 03:07:45 +00:00
|
|
|
let mut selected_button: usize = 0;
|
2024-03-02 09:06:53 +00:00
|
|
|
let mut button_states = [State::Selected, State::Normal, State::Normal];
|
2018-09-23 18:59:51 +00:00
|
|
|
loop {
|
2023-09-28 03:07:45 +00:00
|
|
|
terminal.draw(|frame| ui(frame, button_states))?;
|
|
|
|
if !event::poll(Duration::from_millis(100))? {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
match event::read()? {
|
|
|
|
Event::Key(key) => {
|
|
|
|
if key.kind != event::KeyEventKind::Press {
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-02 09:06:53 +00:00
|
|
|
if handle_key_event(key, &mut button_states, &mut selected_button).is_break() {
|
2023-09-28 03:07:45 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-03-02 09:06:53 +00:00
|
|
|
Event::Mouse(mouse) => {
|
|
|
|
handle_mouse_event(mouse, &mut button_states, &mut selected_button);
|
|
|
|
}
|
2023-09-28 03:07:45 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
fn ui(frame: &mut Frame, states: [State; 3]) {
|
2024-01-05 15:45:14 +00:00
|
|
|
let vertical = Layout::vertical([
|
|
|
|
Constraint::Length(1),
|
|
|
|
Constraint::Max(3),
|
|
|
|
Constraint::Length(1),
|
|
|
|
Constraint::Min(0), // ignore remaining space
|
|
|
|
]);
|
2024-02-02 04:26:35 +00:00
|
|
|
let [title, buttons, help, _] = vertical.areas(frame.size());
|
2024-01-05 15:45:14 +00:00
|
|
|
|
2023-09-28 03:07:45 +00:00
|
|
|
frame.render_widget(
|
|
|
|
Paragraph::new("Custom Widget Example (mouse enabled)"),
|
2024-01-05 15:45:14 +00:00
|
|
|
title,
|
2023-09-28 03:07:45 +00:00
|
|
|
);
|
2024-01-05 15:45:14 +00:00
|
|
|
render_buttons(frame, buttons, states);
|
|
|
|
frame.render_widget(Paragraph::new("←/→: select, Space: toggle, q: quit"), help);
|
2023-09-28 03:07:45 +00:00
|
|
|
}
|
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
fn render_buttons(frame: &mut Frame<'_>, area: Rect, states: [State; 3]) {
|
2024-01-05 15:45:14 +00:00
|
|
|
let horizontal = Layout::horizontal([
|
|
|
|
Constraint::Length(15),
|
|
|
|
Constraint::Length(15),
|
|
|
|
Constraint::Length(15),
|
|
|
|
Constraint::Min(0), // ignore remaining space
|
|
|
|
]);
|
2024-02-02 04:26:35 +00:00
|
|
|
let [red, green, blue, _] = horizontal.areas(area);
|
2024-01-05 15:45:14 +00:00
|
|
|
|
|
|
|
frame.render_widget(Button::new("Red").theme(RED).state(states[0]), red);
|
|
|
|
frame.render_widget(Button::new("Green").theme(GREEN).state(states[1]), green);
|
|
|
|
frame.render_widget(Button::new("Blue").theme(BLUE).state(states[2]), blue);
|
2023-09-28 03:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_key_event(
|
|
|
|
key: event::KeyEvent,
|
|
|
|
button_states: &mut [State; 3],
|
|
|
|
selected_button: &mut usize,
|
|
|
|
) -> ControlFlow<()> {
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Char('q') => return ControlFlow::Break(()),
|
2023-12-15 03:11:48 +00:00
|
|
|
KeyCode::Left | KeyCode::Char('h') => {
|
2023-09-28 03:07:45 +00:00
|
|
|
button_states[*selected_button] = State::Normal;
|
|
|
|
*selected_button = selected_button.saturating_sub(1);
|
|
|
|
button_states[*selected_button] = State::Selected;
|
|
|
|
}
|
2023-12-15 03:11:48 +00:00
|
|
|
KeyCode::Right | KeyCode::Char('l') => {
|
2023-09-28 03:07:45 +00:00
|
|
|
button_states[*selected_button] = State::Normal;
|
|
|
|
*selected_button = selected_button.saturating_add(1).min(2);
|
|
|
|
button_states[*selected_button] = State::Selected;
|
|
|
|
}
|
|
|
|
KeyCode::Char(' ') => {
|
|
|
|
if button_states[*selected_button] == State::Active {
|
|
|
|
button_states[*selected_button] = State::Normal;
|
|
|
|
} else {
|
|
|
|
button_states[*selected_button] = State::Active;
|
2018-12-07 15:17:33 +00:00
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
}
|
2023-09-28 03:07:45 +00:00
|
|
|
_ => (),
|
2018-09-23 18:59:51 +00:00
|
|
|
}
|
2023-09-28 03:07:45 +00:00
|
|
|
ControlFlow::Continue(())
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2023-09-28 03:07:45 +00:00
|
|
|
fn handle_mouse_event(
|
|
|
|
mouse: MouseEvent,
|
|
|
|
button_states: &mut [State; 3],
|
|
|
|
selected_button: &mut usize,
|
|
|
|
) {
|
|
|
|
match mouse.kind {
|
|
|
|
MouseEventKind::Moved => {
|
|
|
|
let old_selected_button = *selected_button;
|
|
|
|
*selected_button = match mouse.column {
|
|
|
|
x if x < 15 => 0,
|
|
|
|
x if x < 30 => 1,
|
|
|
|
_ => 2,
|
|
|
|
};
|
|
|
|
if old_selected_button != *selected_button {
|
2023-10-06 09:17:14 +00:00
|
|
|
if button_states[old_selected_button] != State::Active {
|
|
|
|
button_states[old_selected_button] = State::Normal;
|
|
|
|
}
|
|
|
|
if button_states[*selected_button] != State::Active {
|
|
|
|
button_states[*selected_button] = State::Selected;
|
|
|
|
}
|
2023-09-28 03:07:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
MouseEventKind::Down(MouseButton::Left) => {
|
|
|
|
if button_states[*selected_button] == State::Active {
|
|
|
|
button_states[*selected_button] = State::Normal;
|
|
|
|
} else {
|
|
|
|
button_states[*selected_button] = State::Active;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
2016-11-02 18:17:18 +00:00
|
|
|
}
|