tui_realm_stdlib/components/
phantom.rs

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