2024-01-24 19:50:18 +00:00
|
|
|
//! # [Ratatui] Popup 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
|
|
|
|
|
|
|
|
/// See also https://github.com/joshka/tui-popup and
|
|
|
|
/// https://github.com/sephiroth74/tui-confirm-dialog
|
2023-06-12 05:07:15 +00:00
|
|
|
use std::{error::Error, io};
|
|
|
|
|
|
|
|
use crossterm::{
|
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
|
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
};
|
2023-07-16 09:11:59 +00:00
|
|
|
use ratatui::{prelude::*, widgets::*};
|
2020-03-22 14:50:34 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
struct App {
|
|
|
|
show_popup: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl App {
|
|
|
|
fn new() -> App {
|
|
|
|
App { show_popup: false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
// setup terminal
|
|
|
|
enable_raw_mode()?;
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
|
|
|
|
// create app and run it
|
|
|
|
let app = App::new();
|
|
|
|
let res = run_app(&mut terminal, app);
|
|
|
|
|
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
|
|
|
|
|
|
|
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>, mut app: App) -> io::Result<()> {
|
|
|
|
loop {
|
2021-12-23 17:46:25 +00:00
|
|
|
terminal.draw(|f| ui(f, &app))?;
|
2021-11-01 21:27:46 +00:00
|
|
|
|
|
|
|
if let Event::Key(key) = event::read()? {
|
2023-04-13 20:21:35 +00:00
|
|
|
if key.kind == KeyEventKind::Press {
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Char('q') => return Ok(()),
|
|
|
|
KeyCode::Char('p') => app.show_popup = !app.show_popup,
|
|
|
|
_ => {}
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-26 05:30:36 +00:00
|
|
|
fn ui(f: &mut Frame, app: &App) {
|
2024-01-05 15:45:14 +00:00
|
|
|
let area = f.size();
|
2021-11-01 21:27:46 +00:00
|
|
|
|
2024-01-05 15:45:14 +00:00
|
|
|
let vertical = Layout::vertical([Constraint::Percentage(20), Constraint::Percentage(80)]);
|
|
|
|
let [instructions, content] = area.split(&vertical);
|
2021-11-01 21:27:46 +00:00
|
|
|
|
|
|
|
let text = if app.show_popup {
|
|
|
|
"Press p to close the popup"
|
|
|
|
} else {
|
|
|
|
"Press p to show the popup"
|
|
|
|
};
|
2023-07-01 09:14:16 +00:00
|
|
|
let paragraph = Paragraph::new(text.slow_blink())
|
2024-01-24 11:31:52 +00:00
|
|
|
.centered()
|
2023-07-01 09:14:16 +00:00
|
|
|
.wrap(Wrap { trim: true });
|
2024-01-05 15:45:14 +00:00
|
|
|
f.render_widget(paragraph, instructions);
|
2021-11-01 21:27:46 +00:00
|
|
|
|
|
|
|
let block = Block::default()
|
|
|
|
.title("Content")
|
|
|
|
.borders(Borders::ALL)
|
2023-07-01 09:14:16 +00:00
|
|
|
.on_blue();
|
2024-01-05 15:45:14 +00:00
|
|
|
f.render_widget(block, content);
|
2021-11-01 21:27:46 +00:00
|
|
|
|
|
|
|
if app.show_popup {
|
|
|
|
let block = Block::default().title("Popup").borders(Borders::ALL);
|
2024-01-05 15:45:14 +00:00
|
|
|
let area = centered_rect(60, 20, area);
|
2021-11-01 21:27:46 +00:00
|
|
|
f.render_widget(Clear, area); //this clears out the background
|
|
|
|
f.render_widget(block, area);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// helper function to create a centered rect using up certain percentage of the available rect `r`
|
2020-03-22 14:50:34 +00:00
|
|
|
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
|
2024-01-05 15:45:14 +00:00
|
|
|
let popup_layout = Layout::vertical([
|
|
|
|
Constraint::Percentage((100 - percent_y) / 2),
|
|
|
|
Constraint::Percentage(percent_y),
|
|
|
|
Constraint::Percentage((100 - percent_y) / 2),
|
|
|
|
])
|
|
|
|
.split(r);
|
|
|
|
|
|
|
|
Layout::horizontal([
|
|
|
|
Constraint::Percentage((100 - percent_x) / 2),
|
|
|
|
Constraint::Percentage(percent_x),
|
|
|
|
Constraint::Percentage((100 - percent_x) / 2),
|
|
|
|
])
|
|
|
|
.split(popup_layout[1])[1]
|
2020-03-22 14:50:34 +00:00
|
|
|
}
|