Skip to main content

hac_client/
pages.rs

1pub mod collection_dashboard;
2pub mod collection_viewer;
3pub mod confirm_popup;
4pub mod error_popup;
5pub mod input;
6pub mod overlay;
7mod spinner;
8pub mod terminal_too_small;
9mod under_construction;
10
11use crate::event_pool::Event;
12use crossterm::event::KeyEvent;
13use hac_core::command::Command;
14use ratatui::{layout::Rect, Frame};
15use tokio::sync::mpsc::UnboundedSender;
16
17/// A `Page` is anything that is a top level page and can be drawn to the screen
18pub trait Renderable {
19    fn draw(&mut self, frame: &mut Frame, size: Rect) -> anyhow::Result<()>;
20
21    /// pages need to adapt to change of sizes on the application, this function is called
22    /// by the top level event loop whenever a resize event is produced
23    #[allow(unused_variables)]
24    fn resize(&mut self, new_size: Rect) {}
25
26    /// register a page to be a command handler, which means this page will now receive
27    /// commands from the channel to handle whatever the commands it is interested into
28    #[allow(unused_variables)]
29    fn register_command_handler(&mut self, sender: UnboundedSender<Command>) -> anyhow::Result<()> {
30        Ok(())
31    }
32
33    /// tick is a bigger interval than the one used by the render cycle, it is mainly used
34    /// for actions that rely on time, such as syncing changes to disk
35    fn handle_tick(&mut self) -> anyhow::Result<()> {
36        Ok(())
37    }
38}
39
40/// An `Eventful` page is a page that can handle key events, and mouse events
41/// when support for them gets added.
42pub trait Eventful {
43    type Result;
44
45    /// the top level event loop doesnt differentiate between kinds of events, so this is what
46    /// delegate each kind of events to the responsible function
47    fn handle_event(&mut self, event: Option<Event>) -> anyhow::Result<Option<Self::Result>> {
48        match event {
49            Some(Event::Key(key_event)) => self.handle_key_event(key_event),
50            _ => Ok(None),
51        }
52    }
53
54    /// when we get a key_event, this will be called for the eventful component to handle it
55    #[allow(unused_variables)]
56    fn handle_key_event(&mut self, key_event: KeyEvent) -> anyhow::Result<Option<Self::Result>> {
57        Ok(None)
58    }
59}