All components must have focus

This commit is contained in:
veeso
2021-03-07 17:40:45 +01:00
parent 5a2d0b7b0b
commit 43c177e04d
3 changed files with 105 additions and 27 deletions

View File

@@ -31,12 +31,23 @@ use tui::{
widgets::{Block, Borders, Gauge},
};
// NOTE: this component doesn't handle any state
// -- state
struct OwnStates {
focus: bool,
}
impl Default for OwnStates {
fn default() -> Self {
OwnStates { focus: false }
}
}
// -- component
pub struct ProgressBar {
props: Props,
states: OwnStates,
}
impl ProgressBar {
@@ -44,7 +55,10 @@ impl ProgressBar {
///
/// Instantiate a new Progress Bar
pub fn new(props: Props) -> Self {
ProgressBar { props }
ProgressBar {
props,
states: OwnStates::default(),
}
}
}
@@ -121,8 +135,13 @@ impl Component for ProgressBar {
/// Handle input event and update internal states.
/// Returns a Msg to the view.
/// Returns always None, since cannot have any focus
fn on(&mut self, _ev: InputEvent) -> Msg {
Msg::None
fn on(&mut self, ev: InputEvent) -> Msg {
// Return key
if let InputEvent::Key(key) = ev {
Msg::OnKey(key)
} else {
Msg::None
}
}
/// ### get_value
@@ -146,13 +165,17 @@ impl Component for ProgressBar {
/// ### blur
///
/// Blur component; does nothing on this component
fn blur(&mut self) {}
/// Blur component
fn blur(&mut self) {
self.states.focus = false;
}
/// ### active
///
/// Active component; does nothing on this component
fn active(&mut self) {}
/// Active component
fn active(&mut self) {
self.states.focus = true;
}
}
#[cfg(test)]
@@ -174,8 +197,11 @@ mod tests {
.build(),
);
// Focus
assert_eq!(component.states.focus, false);
component.active();
assert_eq!(component.states.focus, true);
component.blur();
assert_eq!(component.states.focus, false);
// Should umount
assert_eq!(component.should_umount(), false);
// Get value
@@ -185,7 +211,7 @@ mod tests {
// Event
assert_eq!(
component.on(InputEvent::Key(KeyEvent::from(KeyCode::Delete))),
Msg::None
Msg::OnKey(KeyEvent::from(KeyCode::Delete))
);
}
}