mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
All components must have focus
This commit is contained in:
@@ -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))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user