Skip to main content

tui_realm_stdlib/components/
phantom.rs

1use tuirealm::command::{Cmd, CmdResult};
2use tuirealm::component::Component;
3use tuirealm::props::{AttrValue, Attribute, Props, QueryResult};
4use tuirealm::ratatui::Frame;
5use tuirealm::ratatui::layout::Rect;
6use tuirealm::state::State;
7
8/// [`Phantom`] is a component which is not rendered. It's only purpose is to become a global listener in a tui-realm application
9/// for some kind of events using subscriptions.
10///
11/// An example would be a listener for `<ESC>` key to terminate the application.
12/// The [`Phantom`] allows you not to write a listener for each component for the `ESC` key, but just to subscribe the phantom to it.
13#[derive(Default)]
14pub struct Phantom {
15    props: Props,
16}
17
18impl Component for Phantom {
19    fn view(&mut self, _render: &mut Frame, _area: Rect) {}
20
21    fn query<'a>(&'a self, attr: Attribute) -> Option<QueryResult<'a>> {
22        self.props.get_for_query(attr)
23    }
24
25    fn attr(&mut self, attr: Attribute, value: AttrValue) {
26        self.props.set(attr, value);
27    }
28
29    fn state(&self) -> State {
30        State::None
31    }
32
33    fn perform(&mut self, cmd: Cmd) -> CmdResult {
34        CmdResult::Invalid(cmd)
35    }
36}
37
38#[cfg(test)]
39mod tests {
40
41    use pretty_assertions::assert_eq;
42
43    use super::*;
44
45    #[test]
46    fn test_components_phantom() {
47        let component = Phantom::default();
48        // Get value
49        assert_eq!(component.state(), State::None);
50    }
51}