impl Trait(can run)

This commit is contained in:
RainBus
2024-10-12 11:54:27 +08:00
commit ef6f088fde
11 changed files with 824 additions and 0 deletions

46
src/app.rs Normal file
View File

@@ -0,0 +1,46 @@
use std::io;
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind};
use ratatui::{DefaultTerminal, Frame};
use crate::view::home::{self, Home};
pub struct App {
running: bool,
view: Home,
}
impl Default for App {
fn default() -> Self {
App {
running: true,
view: Home::default(),
}
}
}
impl App {
pub fn run(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
while self.running {
terminal.draw(|frame| self.draw(frame))?;
self.handle_event()?;
}
Ok(())
}
fn draw(&self, frame: &mut Frame) {
self.view.draw(frame);
}
fn handle_event(&mut self) -> io::Result<()> {
match event::read()? {
Event::Key(KeyEvent {
kind: KeyEventKind::Press,
code: KeyCode::Esc,
..
}) => self.running = false,
event => self.view.handle_event(event)?,
}
Ok(())
}
}