orchestrator
This commit is contained in:
75
examples/simple_usage.rs
Normal file
75
examples/simple_usage.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
extern crate alloc;
|
||||
|
||||
use tui_orchestrator::prelude::*;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
enum Focus {
|
||||
Username,
|
||||
Password,
|
||||
LoginButton,
|
||||
}
|
||||
|
||||
impl FocusId for Focus {}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
enum AppEvent {
|
||||
LoginAttempt {
|
||||
username: alloc::string::String,
|
||||
password: alloc::string::String,
|
||||
},
|
||||
Quit,
|
||||
}
|
||||
|
||||
struct LoginPage {
|
||||
username: alloc::string::String,
|
||||
password: alloc::string::String,
|
||||
}
|
||||
|
||||
impl Component for LoginPage {
|
||||
type Focus = Focus;
|
||||
type Action = ComponentAction;
|
||||
type Event = AppEvent;
|
||||
|
||||
fn targets(&self) -> &[Self::Focus] {
|
||||
&[Focus::Username, Focus::Password, Focus::LoginButton]
|
||||
}
|
||||
|
||||
fn handle(
|
||||
&mut self,
|
||||
focus: &Self::Focus,
|
||||
action: Self::Action,
|
||||
) -> Result<Option<Self::Event>, ComponentError> {
|
||||
match (focus, action) {
|
||||
(Focus::LoginButton, ComponentAction::Select) => Ok(Some(AppEvent::LoginAttempt {
|
||||
username: self.username.clone(),
|
||||
password: self.password.clone(),
|
||||
})),
|
||||
(Focus::Username, ComponentAction::TypeChar(c)) => {
|
||||
self.username.push(c);
|
||||
Ok(None)
|
||||
}
|
||||
(Focus::Password, ComponentAction::TypeChar(c)) => {
|
||||
self.password.push(c);
|
||||
Ok(None)
|
||||
}
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), ComponentError> {
|
||||
let mut orch = Orchestrator::new();
|
||||
|
||||
orch.bind(Key::enter(), ComponentAction::Select);
|
||||
orch.bind(Key::tab(), ComponentAction::Next);
|
||||
orch.bind(Key::shift_tab(), ComponentAction::Prev);
|
||||
|
||||
let login_page = LoginPage {
|
||||
username: alloc::string::String::new(),
|
||||
password: alloc::string::String::new(),
|
||||
};
|
||||
|
||||
orch.register_page(alloc::string::String::from("login"), login_page);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user