Components.rs
components/mod.rs
では、Component
という trait
を実装します。
pub trait Component { #[allow(unused_variables)] fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> { Ok(()) } #[allow(unused_variables)] fn register_config_handler(&mut self, config: Config) -> Result<()> { Ok(()) } fn init(&mut self) -> Result<()> { Ok(()) } fn handle_events(&mut self, event: Option<Event>) -> Result<Option<Action>> { let r = match event { Some(Event::Key(key_event)) => self.handle_key_events(key_event)?, Some(Event::Mouse(mouse_event)) => self.handle_mouse_events(mouse_event)?, _ => None, }; Ok(r) } #[allow(unused_variables)] fn handle_key_events(&mut self, key: KeyEvent) -> Result<Option<Action>> { Ok(None) } #[allow(unused_variables)] fn handle_mouse_events(&mut self, mouse: MouseEvent) -> Result<Option<Action>> { Ok(None) } #[allow(unused_variables)] fn update(&mut self, action: Action) -> Result<Option<Action>> { Ok(None) } fn draw(&mut self, f: &mut Frame, rect: Rect) -> Result<()>;}
私は個人的に、handle_events
(つまり、イベント -> アクション マッピング)、dispatch
(つまり、アクション -> 状態更新マッピング)、および render
(つまり、状態 -> 描画マッピング) の関数をすべて、アプリケーションの各コンポーネントの 1 つのファイルに保持することを好みます。
init
関数もあり、 Component
をロードしたときにセットアップするために使用できます。
Home
struct (すなわち、他の Component
を保持する可能性のあるルート構造) は、 Component
trait を実装します。 Home
次をご覧ください。