Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move draw() above handle_events() #79

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions simple-generated/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
/// - <https://docs.rs/ratatui/latest/ratatui/widgets/index.html>
/// - <https://github.com/ratatui/ratatui/tree/master/examples>
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(_, _) => {}
Expand All @@ -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(),
)
}
}
49 changes: 27 additions & 22 deletions simple/template/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
/// - <https://docs.rs/ratatui/latest/ratatui/widgets/index.html>
/// - <https://github.com/ratatui/ratatui/tree/master/examples>
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(_, _) => {}
Expand All @@ -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(),
)
}
}