tui_realm_stdlib/components/
phantom.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//! ## Phantom
//!
//! `Phantom` is a component which is not rendered. It only purpose is to become a global listener in a tui-realm application
//! for some kind of events using subscriptions.
//!
//! An example would be a listener for `<ESC>` key to terminate the application.
//! The Phantom allows you not to write a listener for each component for the `ESC` key, but just to subscribe the phantom to it.

use tuirealm::command::{Cmd, CmdResult};
use tuirealm::props::{AttrValue, Attribute, Props};
use tuirealm::ratatui::layout::Rect;
use tuirealm::{Frame, MockComponent, State};

// -- Component

/// ## Spinner
///
/// a component which is not rendered. It only purpose is to become a global listener in a tui-realm application
/// for some kind of events using subscriptions.
#[derive(Default)]
pub struct Phantom {
    props: Props,
}

impl MockComponent for Phantom {
    fn view(&mut self, _render: &mut Frame, _area: Rect) {}

    fn query(&self, attr: Attribute) -> Option<AttrValue> {
        self.props.get(attr)
    }

    fn attr(&mut self, attr: Attribute, value: AttrValue) {
        self.props.set(attr, value)
    }

    fn state(&self) -> State {
        State::None
    }

    fn perform(&mut self, _cmd: Cmd) -> CmdResult {
        CmdResult::None
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    use pretty_assertions::assert_eq;

    #[test]
    fn test_components_phantom() {
        let component = Phantom::default();
        // Get value
        assert_eq!(component.state(), State::None);
    }
}