tuirealm/core/
component.rs

1//! # Component
2//!
3//! This module exposes the component traits
4
5use ratatui::Frame;
6
7use crate::command::{Cmd, CmdResult};
8use crate::ratatui::layout::Rect;
9use crate::{AttrValue, Attribute, Event, State};
10
11/// A Mock Component represents a component which defines all the properties and states it can handle and represent
12/// and the way it should be rendered. It must also define how to behave in case of a [`Cmd`] (command).
13/// Despite that, it won't define how to behave after an [`Event`] and it won't send any `Msg`.
14/// The MockComponent is intended to be used as a reusable component to implement your application component.
15///
16/// ### In practice
17///
18/// A real life example would be an Input field.
19/// The mock component is represented by the `Input`, which will define the properties (e.g. max input length, input type, ...)
20/// and by its behaviour (e.g. when the user types 'a', 'a' char is added to input state).
21///
22/// In your application though, you may use a `IpAddressInput` which is the [`Component`] using the `Input` mock component.
23/// If you want more example, just dive into the `examples/` folder in the project root.
24pub trait MockComponent {
25    /// Based on the current properties and states, renders the component in the provided area frame.
26    /// Render can also mutate the component state if this is required
27    fn view(&mut self, frame: &mut Frame, area: Rect);
28
29    /// Query attribute of component properties.
30    fn query(&self, attr: Attribute) -> Option<AttrValue>;
31
32    /// Set attribute to properties.
33    /// `query` describes the name, while `attr` the value it'll take
34    fn attr(&mut self, attr: Attribute, value: AttrValue);
35
36    /// Get current state from component
37    fn state(&self) -> State;
38
39    /// Perform a command on the component.
40    /// The command will may change the component state.
41    /// The method returns the result of the command applied (what changed if any)
42    fn perform(&mut self, cmd: Cmd) -> CmdResult;
43}
44
45/// The component describes the application level component, which is a wrapper around the [`MockComponent`],
46/// which, in addition to all the methods exposed by the mock, it will handle the event coming from the `View`.
47/// The Event are passed to the `on` method, which will eventually return a `Msg`,
48/// which is defined in your application as an enum. (Don't forget to derive [`PartialEq`] for your enum).
49/// In your application you should have a Component for each element on your UI, but the logic to implement
50/// is very tiny, since the most of the work should already be done into the [`MockComponent`]
51/// and many of them are available in the standard library at <https://github.com/veeso/tui-realm-stdlib>.
52///
53/// Don't forget you can find an example in the `examples/` directory and you can discover many more information
54/// about components in the repository documentation.
55pub trait Component<Msg, UserEvent>: MockComponent
56where
57    Msg: PartialEq,
58    UserEvent: Eq + PartialEq + Clone,
59{
60    /// Handle input event and update internal states.
61    /// Returns a Msg to the view.
62    /// If [`None`] is returned it means there's no message to return for the provided event.
63    fn on(&mut self, ev: Event<UserEvent>) -> Option<Msg>;
64}