From bbe754f3401220d8765f8885f1ab3f291eaab1cb Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Thu, 5 Sep 2024 20:03:19 -0700 Subject: [PATCH] move draw() above handle_events() (#79) When writing the hello-world tutorial, this is covered first, so putting it early makes more sense --- simple-generated/src/app.rs | 49 ++++++++++++++++++++----------------- simple/template/src/app.rs | 49 ++++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 44 deletions(-) diff --git a/simple-generated/src/app.rs b/simple-generated/src/app.rs index 0f599cd..46d10f6 100644 --- a/simple-generated/src/app.rs +++ b/simple-generated/src/app.rs @@ -19,6 +19,7 @@ impl App { Self::default() } + /// Run the application's main loop. pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> { self.running = true; while self.running { @@ -28,8 +29,34 @@ impl App { Ok(()) } + /// Renders the user interface. + /// + /// This is where you add new widgets. See the following resources for more information: + /// - + /// - + fn draw(&mut self, frame: &mut Frame) { + let title = Line::from("Ratatui Simple Template") + .bold() + .blue() + .centered(); + let text = "Hello, Ratatui!\n\n\ + Created using https://github.com/ratatui/templates\n\ + Press `Esc`, `Ctrl-C` or `q` to stop running."; + frame.render_widget( + Paragraph::new(text) + .block(Block::bordered().title(title)) + .centered(), + frame.area(), + ) + } + + /// Reads the crossterm events and updates the state of [`App`]. + /// + /// If your application needs to perform work in between handling events, you can use the + /// [`event::poll`] function to check if there are any events available with a timeout. fn handle_crossterm_events(&mut self) -> Result<()> { match event::read()? { + // it's important to check KeyEventKind::Press to avoid handling key release events Event::Key(key) if key.kind == KeyEventKind::Press => self.on_key_event(key), Event::Mouse(_) => {} Event::Resize(_, _) => {} @@ -52,26 +79,4 @@ impl App { fn quit(&mut self) { self.running = false; } - - /// Renders the user interface widgets. - fn draw(&mut self, frame: &mut Frame) { - // This is where you add new widgets. - // See the following resources: - // - https://docs.rs/ratatui/latest/ratatui/widgets/index.html - // - https://github.com/ratatui/ratatui/tree/master/examples - - let title = Line::from("Ratatui Simple Template") - .bold() - .blue() - .centered(); - let text = "Hello, Ratatui!\n\n\ - Created using https://github.com/ratatui/templates\n\ - Press `Esc`, `Ctrl-C` or `q` to stop running."; - frame.render_widget( - Paragraph::new(text) - .block(Block::bordered().title(title)) - .centered(), - frame.area(), - ) - } } diff --git a/simple/template/src/app.rs b/simple/template/src/app.rs index 0f599cd..46d10f6 100644 --- a/simple/template/src/app.rs +++ b/simple/template/src/app.rs @@ -19,6 +19,7 @@ impl App { Self::default() } + /// Run the application's main loop. pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> { self.running = true; while self.running { @@ -28,8 +29,34 @@ impl App { Ok(()) } + /// Renders the user interface. + /// + /// This is where you add new widgets. See the following resources for more information: + /// - + /// - + fn draw(&mut self, frame: &mut Frame) { + let title = Line::from("Ratatui Simple Template") + .bold() + .blue() + .centered(); + let text = "Hello, Ratatui!\n\n\ + Created using https://github.com/ratatui/templates\n\ + Press `Esc`, `Ctrl-C` or `q` to stop running."; + frame.render_widget( + Paragraph::new(text) + .block(Block::bordered().title(title)) + .centered(), + frame.area(), + ) + } + + /// Reads the crossterm events and updates the state of [`App`]. + /// + /// If your application needs to perform work in between handling events, you can use the + /// [`event::poll`] function to check if there are any events available with a timeout. fn handle_crossterm_events(&mut self) -> Result<()> { match event::read()? { + // it's important to check KeyEventKind::Press to avoid handling key release events Event::Key(key) if key.kind == KeyEventKind::Press => self.on_key_event(key), Event::Mouse(_) => {} Event::Resize(_, _) => {} @@ -52,26 +79,4 @@ impl App { fn quit(&mut self) { self.running = false; } - - /// Renders the user interface widgets. - fn draw(&mut self, frame: &mut Frame) { - // This is where you add new widgets. - // See the following resources: - // - https://docs.rs/ratatui/latest/ratatui/widgets/index.html - // - https://github.com/ratatui/ratatui/tree/master/examples - - let title = Line::from("Ratatui Simple Template") - .bold() - .blue() - .centered(); - let text = "Hello, Ratatui!\n\n\ - Created using https://github.com/ratatui/templates\n\ - Press `Esc`, `Ctrl-C` or `q` to stop running."; - frame.render_widget( - Paragraph::new(text) - .block(Block::bordered().title(title)) - .centered(), - frame.area(), - ) - } }