mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-22 04:33:13 +00:00
c12bcfefa2
Fixes many not yet enabled lints (mostly pedantic) on everything that is not the lib (examples, benchs, tests). Therefore, this is not containing anything that can be a breaking change. Lints are not enabled as that should be the job of #974. I created this as a separate PR as its mostly independent and would only clutter up the diff of #974 even more. Also see https://github.com/ratatui-org/ratatui/pull/974#discussion_r1506458743 --------- Co-authored-by: Josh McKinney <joshka@users.noreply.github.com>
130 lines
3.9 KiB
Rust
130 lines
3.9 KiB
Rust
//! # [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
|
|
|
|
use std::{error::Error, io};
|
|
|
|
use crossterm::{
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
|
|
execute,
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
};
|
|
use ratatui::{
|
|
prelude::*,
|
|
widgets::{Block, Borders, Clear, Paragraph, Wrap},
|
|
};
|
|
|
|
struct App {
|
|
show_popup: bool,
|
|
}
|
|
|
|
impl App {
|
|
const fn new() -> Self {
|
|
Self { 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 {
|
|
println!("{err:?}");
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<()> {
|
|
loop {
|
|
terminal.draw(|f| ui(f, &app))?;
|
|
|
|
if let Event::Key(key) = event::read()? {
|
|
if key.kind == KeyEventKind::Press {
|
|
match key.code {
|
|
KeyCode::Char('q') => return Ok(()),
|
|
KeyCode::Char('p') => app.show_popup = !app.show_popup,
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn ui(f: &mut Frame, app: &App) {
|
|
let area = f.size();
|
|
|
|
let vertical = Layout::vertical([Constraint::Percentage(20), Constraint::Percentage(80)]);
|
|
let [instructions, content] = vertical.areas(area);
|
|
|
|
let text = if app.show_popup {
|
|
"Press p to close the popup"
|
|
} else {
|
|
"Press p to show the popup"
|
|
};
|
|
let paragraph = Paragraph::new(text.slow_blink())
|
|
.centered()
|
|
.wrap(Wrap { trim: true });
|
|
f.render_widget(paragraph, instructions);
|
|
|
|
let block = Block::default()
|
|
.title("Content")
|
|
.borders(Borders::ALL)
|
|
.on_blue();
|
|
f.render_widget(block, content);
|
|
|
|
if app.show_popup {
|
|
let block = Block::default().title("Popup").borders(Borders::ALL);
|
|
let area = centered_rect(60, 20, area);
|
|
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`
|
|
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
|
|
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]
|
|
}
|