From 0597c540dca4d32a23090f33a50b356f0a50342c Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Mon, 3 Jun 2024 16:45:15 +0200 Subject: [PATCH] lib: Improve example --- examples/hello.rs | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/examples/hello.rs b/examples/hello.rs index 0c50101a..18f8b851 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -4,7 +4,9 @@ use radicle_tui as tui; use termion::event::Key; use tui::store; +use tui::ui::widget::container::Container; use tui::ui::widget::text::{Paragraph, ParagraphProps}; +use tui::ui::widget::window::{Page, Shortcuts, ShortcutsProps, Window, WindowProps}; use tui::ui::widget::ToWidget; use tui::{BoxedAny, Channel, Exit}; @@ -37,25 +39,47 @@ impl store::State<()> for State { #[tokio::main] pub async fn main() -> Result<()> { let channel = Channel::default(); + let sender = channel.tx.clone(); let state = State { welcome: "Hello TUI".to_string(), }; - let welcome = Paragraph::default() - .to_widget(channel.tx.clone()) - .on_update(|state: &State| { - ParagraphProps::default() - .text(&state.welcome.clone().into()) - .to_boxed_any() - .into() - }) + let welcome = Page::default() + .content( + Container::default() + .content(Paragraph::default().to_widget(sender.clone()).on_update( + |state: &State| { + ParagraphProps::default() + .text(&state.welcome.clone().into()) + .to_boxed_any() + .into() + }, + )) + .to_widget(sender.clone()), + ) + .shortcuts( + Shortcuts::default() + .to_widget(sender.clone()) + .on_update(|_| { + ShortcutsProps::default() + .shortcuts(&[("q", "quit"), ("r", "reverse")]) + .to_boxed_any() + .into() + }), + ) + .to_widget(sender.clone()); + + let window = Window::default() + .page(0, welcome) + .to_widget(sender.clone()) .on_event(|key, _, _| match key { Key::Char('r') => Some(Message::ReverseWelcome), Key::Char('q') => Some(Message::Quit), _ => None, - }); + }) + .on_update(|_| WindowProps::default().current_page(0).to_boxed_any().into()); - tui::run(channel, state, welcome).await?; + tui::run(channel, state, window).await?; Ok(()) }