mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
Defined Component and State
This commit is contained in:
@@ -24,4 +24,64 @@
|
||||
*/
|
||||
|
||||
// Modules
|
||||
pub(crate) mod props;
|
||||
pub mod props;
|
||||
|
||||
// locals
|
||||
use props::{Props, PropsBuilder};
|
||||
// ext
|
||||
use crossterm::event::Event as InputEvent;
|
||||
use tui::widgets::Widget;
|
||||
|
||||
// -- States
|
||||
|
||||
/// ## States
|
||||
///
|
||||
/// States is a trait which defines the behaviours for the states model for the different component.
|
||||
/// A state contains internal values for each component.
|
||||
pub(crate) trait States {
|
||||
/// ### update
|
||||
///
|
||||
/// Create a new state from current one and new
|
||||
fn update(&self, new: dyn States) -> dyn States;
|
||||
}
|
||||
|
||||
// -- Component
|
||||
|
||||
/// ## Component
|
||||
///
|
||||
/// Component is a trait which defines the behaviours for a Layout component.
|
||||
/// All layout components must implement a method to render and one to update
|
||||
pub trait Component {
|
||||
/// ### render
|
||||
///
|
||||
/// Based on the current properties and states, return a Widget instance for the Component
|
||||
/// Returns None if the component is hidden
|
||||
fn render(&self) -> Option<Box<dyn Widget>>;
|
||||
|
||||
/// ### update
|
||||
///
|
||||
/// Update component properties
|
||||
/// Properties should first be retrieved through `get_props` which creates a builder from
|
||||
/// existing properties and then edited before calling update
|
||||
fn update(&mut self, props: Option<Props>);
|
||||
|
||||
/// ### get_props
|
||||
///
|
||||
/// Returns a props builder starting from component properties.
|
||||
/// This returns a prop builder in order to make easier to create
|
||||
/// new properties for the element.
|
||||
fn get_props(&self) -> PropsBuilder;
|
||||
|
||||
/// ### on
|
||||
///
|
||||
/// Handle input event and update internal states
|
||||
fn on(&mut self, ev: InputEvent);
|
||||
|
||||
// -- events
|
||||
|
||||
/// ### should_umount
|
||||
///
|
||||
/// The component must provide to the supervisor whether it should be umounted (destroyed)
|
||||
/// This makes sense to be called after an `on` or after an `update`, where the states changes.
|
||||
fn should_umount(&self) -> bool;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user