2 unstable releases
0.2.0 | Feb 10, 2025 |
---|---|
0.1.0 | Feb 10, 2025 |
#872 in Command-line interface
245 downloads per month
Used in mdedit
780KB
18K
SLoC
unstable
This crate is a part of rat-salsa.
Rat Dialog
This crates provides a DialogStack that can be used with rat-salsa apps. It can stack and render any number of dialog-windows on top of the main application.
It uses the rat-salsa traits for AppWidget/AppState for rendering and event handling.
# use std::path::PathBuf;
use anyhow::Error;
use rat_dialog::DialogStackState;
use rat_dialog::widgets::{FileDialog, FileDialogState};
# use rat_salsa::{AppState, Control};
# use rat_theme2::DarkTheme;
# use rat_widget::event::FileOutcome;
# struct GlobalState { dialogs: DialogStackState<GlobalState, MyEvent, Error>, theme: DarkTheme }
# enum MyEvent { Event(crossterm::event::Event), Status(u16, String) }
# struct MyAppState {}
impl AppState<GlobalState, MyEvent, Error> for MyAppState {
fn event(
&mut self,
event: &MyEvent,
ctx: &mut rat_salsa::AppContext<'_, GlobalState, MyEvent, Error>,
) -> Result<Control<MyEvent>, Error> {
if matches!(event, MyEvent::Event(event)) {
ctx.g.dialogs.push_dialog(
|_, ctx| {
Box::new(FileDialog::new().styles(ctx.g.theme.file_dialog_style()))
},
FileDialogState::new()
.save_dialog_ext(PathBuf::from("."), "", "pas")?
.map_outcome(|r| match r {
FileOutcome::Ok(f) => {
Control::Event(MyEvent::Status(0, format!("New file {:?}", f)))
}
r => r.into(),
}),
);
Ok(Control::Changed)
} else {
Ok(Control::Continue)
}
}
}
During rendering of the application:
# use anyhow::Error;
# use ratatui::buffer::Buffer;
# use ratatui::layout::Rect;
use rat_dialog::{DialogStack, DialogStackState};
# use rat_salsa::{AppWidget, RenderContext};
# use rat_theme2::DarkTheme;
# struct MainApp;
# struct GlobalState { dialogs: DialogStackState<GlobalState, MyEvent, Error>, theme: DarkTheme }
# enum MyEvent { Event(crossterm::event::Event), Status(u16, String) }
# struct MainAppState {}
impl AppWidget<GlobalState, MyEvent, Error> for MainApp {
type State = MainAppState;
fn render(
&self,
area: Rect,
buf: &mut Buffer,
state: &mut Self::State,
ctx: &mut RenderContext<'_, GlobalState>,
) -> Result<(), Error> {
// ... do all the rendering ...
DialogStack.render(area, buf, &mut ctx.g.dialogs.clone(), ctx)?;
Ok(())
}
}
Dependencies
~12–22MB
~298K SLoC