From 75cfeae169a5099976f692a16c7dfd6158f7226c Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Thu, 5 Sep 2024 13:48:48 +0200 Subject: [PATCH] lib: Add imUI example --- examples/hello_im.rs | 78 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 examples/hello_im.rs diff --git a/examples/hello_im.rs b/examples/hello_im.rs new file mode 100644 index 00000000..af345369 --- /dev/null +++ b/examples/hello_im.rs @@ -0,0 +1,78 @@ +use anyhow::Result; + +use termion::event::Key; + +use ratatui::Frame; + +use radicle_tui as tui; + +use tui::store; +use tui::ui::im; +use tui::ui::im::widget::Window; +use tui::ui::im::{Borders, Context}; +use tui::{Channel, Exit}; + +const ALIEN: &str = r#" + /// /// ,---------------------------------. + /// /// | Hey there, press (q) to quit... | + // // '---------------------------------' + //,,,///,,,// .. + /////////////////// . + //////@@@@@//////@@@@@/// + //////@@###//////@@###/// +,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, + ,,, /// /// ,,, + ,,, /// /// ,,, + /// /// + ///// ///// +"#; + +#[derive(Clone, Debug)] +struct State { + alien: String, +} + +#[derive(Clone)] +enum Message { + Quit, +} + +impl store::State<()> for State { + type Message = Message; + + fn update(&mut self, message: Self::Message) -> Option> { + match message { + Message::Quit => Some(Exit { value: None }), + } + } +} + +#[derive(Default)] +struct App {} + +impl im::App for App { + type State = State; + type Message = Message; + + fn update(&self, ctx: &Context, frame: &mut Frame, state: &State) -> Result<()> { + Window::default().show(ctx, |ui| { + ui.text_view(frame, state.alien.clone(), &mut (0, 0), Some(Borders::None)); + + if ui.input_global(|key| key == Key::Char('q')) { + ui.send_message(Message::Quit); + } + }); + + Ok(()) + } +} + +#[tokio::main] +pub async fn main() -> Result<()> { + let state = State { + alien: ALIEN.to_string(), + }; + tui::im(Channel::default(), state, App::default()).await?; + + Ok(()) +}