//! ## Model //! //! app model use tuirealm::ratatui::layout::{Constraint, Direction, Layout}; use tuirealm::terminal::{TerminalAdapter, TerminalBridge}; use tuirealm::{Application, Update}; use super::{Id, Msg, UserEvent}; pub struct Model where T: TerminalAdapter, { /// Application pub app: Application, /// Indicates that the application must quit pub quit: bool, /// Tells whether to redraw interface pub redraw: bool, /// Used to draw to terminal pub terminal: TerminalBridge, } impl Model where T: TerminalAdapter, { pub fn new(app: Application, adapter: T) -> Self { Self { app, quit: false, redraw: true, terminal: TerminalBridge::init(adapter).expect("Cannot initialize terminal"), } } pub fn view(&mut self) { assert!(self .terminal .draw(|f| { let chunks = Layout::default() .direction(Direction::Vertical) .margin(1) .constraints( [ Constraint::Length(3), // Label Constraint::Length(3), // Other ] .as_ref(), ) .split(f.area()); self.app.view(&Id::Label, f, chunks[0]); self.app.view(&Id::Other, f, chunks[1]); }) .is_ok()); } } // Let's implement Update for model impl Update for Model where T: TerminalAdapter, { fn update(&mut self, msg: Option) -> Option { if let Some(msg) = msg { // Set redraw self.redraw = true; // Match message match msg { Msg::AppClose => { self.quit = true; // Terminate None } Msg::None => None, } } else { None } } }