Moved focus to states
This commit is contained in:
parent
135d947c39
commit
2692041329
|
@ -43,6 +43,7 @@ use tui::{
|
||||||
struct OwnStates {
|
struct OwnStates {
|
||||||
list_index: usize, // Index of selected element in list
|
list_index: usize, // Index of selected element in list
|
||||||
list_len: usize, // Length of file list
|
list_len: usize, // Length of file list
|
||||||
|
focus: bool, // Has focus?
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for OwnStates {
|
impl Default for OwnStates {
|
||||||
|
@ -50,6 +51,7 @@ impl Default for OwnStates {
|
||||||
OwnStates {
|
OwnStates {
|
||||||
list_index: 0,
|
list_index: 0,
|
||||||
list_len: 0,
|
list_len: 0,
|
||||||
|
focus: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -141,7 +143,7 @@ impl Component for FileList {
|
||||||
.map(|line: &String| ListItem::new(Span::from(line.to_string())))
|
.map(|line: &String| ListItem::new(Span::from(line.to_string())))
|
||||||
.collect(),
|
.collect(),
|
||||||
};
|
};
|
||||||
let (fg, bg): (Color, Color) = match self.props.focus {
|
let (fg, bg): (Color, Color) = match self.states.focus {
|
||||||
true => (Color::Reset, self.props.background),
|
true => (Color::Reset, self.props.background),
|
||||||
false => (self.props.foreground, Color::Reset),
|
false => (self.props.foreground, Color::Reset),
|
||||||
};
|
};
|
||||||
|
@ -156,7 +158,7 @@ impl Component for FileList {
|
||||||
.block(
|
.block(
|
||||||
Block::default()
|
Block::default()
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
.border_style(match self.props.focus {
|
.border_style(match self.states.focus {
|
||||||
true => Style::default().fg(self.props.foreground),
|
true => Style::default().fg(self.props.foreground),
|
||||||
false => Style::default(),
|
false => Style::default(),
|
||||||
})
|
})
|
||||||
|
@ -265,6 +267,20 @@ impl Component for FileList {
|
||||||
// Never true
|
// Never true
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// ### blur
|
||||||
|
///
|
||||||
|
/// Blur component; basically remove focus
|
||||||
|
fn blur(&mut self) {
|
||||||
|
self.states.focus = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ### active
|
||||||
|
///
|
||||||
|
/// Active component; basically give focus
|
||||||
|
fn active(&mut self) {
|
||||||
|
self.states.focus = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -289,6 +305,12 @@ mod tests {
|
||||||
// Verify states
|
// Verify states
|
||||||
assert_eq!(component.states.list_index, 0);
|
assert_eq!(component.states.list_index, 0);
|
||||||
assert_eq!(component.states.list_len, 2);
|
assert_eq!(component.states.list_len, 2);
|
||||||
|
assert_eq!(component.states.focus, false);
|
||||||
|
// Focus
|
||||||
|
component.active();
|
||||||
|
assert_eq!(component.states.focus, true);
|
||||||
|
component.blue();
|
||||||
|
assert_eq!(component.states.focus, false);
|
||||||
// Increment list index
|
// Increment list index
|
||||||
component.states.list_index += 1;
|
component.states.list_index += 1;
|
||||||
assert_eq!(component.render().unwrap().cursor, 1);
|
assert_eq!(component.render().unwrap().cursor, 1);
|
||||||
|
|
|
@ -42,6 +42,7 @@ use tui::{
|
||||||
struct OwnStates {
|
struct OwnStates {
|
||||||
input: Vec<char>, // Current input
|
input: Vec<char>, // Current input
|
||||||
cursor: usize, // Input position
|
cursor: usize, // Input position
|
||||||
|
focus: bool, // Focus
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for OwnStates {
|
impl Default for OwnStates {
|
||||||
|
@ -49,6 +50,7 @@ impl Default for OwnStates {
|
||||||
OwnStates {
|
OwnStates {
|
||||||
input: Vec::new(),
|
input: Vec::new(),
|
||||||
cursor: 0,
|
cursor: 0,
|
||||||
|
focus: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,7 +117,7 @@ impl OwnStates {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ### render_value
|
/// ### render_value
|
||||||
///
|
///
|
||||||
/// Get value as string to render
|
/// Get value as string to render
|
||||||
pub fn render_value(&self, itype: InputType) -> String {
|
pub fn render_value(&self, itype: InputType) -> String {
|
||||||
match itype {
|
match itype {
|
||||||
|
@ -172,7 +174,7 @@ impl Component for Input {
|
||||||
None => String::new(),
|
None => String::new(),
|
||||||
};
|
};
|
||||||
let p: Paragraph = Paragraph::new(self.states.get_value())
|
let p: Paragraph = Paragraph::new(self.states.get_value())
|
||||||
.style(match self.props.focus {
|
.style(match self.states.focus {
|
||||||
true => Style::default().fg(self.props.foreground),
|
true => Style::default().fg(self.props.foreground),
|
||||||
false => Style::default(),
|
false => Style::default(),
|
||||||
})
|
})
|
||||||
|
@ -284,6 +286,20 @@ impl Component for Input {
|
||||||
fn should_umount(&self) -> bool {
|
fn should_umount(&self) -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// ### blur
|
||||||
|
///
|
||||||
|
/// Blur component; basically remove focus
|
||||||
|
fn blur(&mut self) {
|
||||||
|
self.states.focus = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// ### active
|
||||||
|
///
|
||||||
|
/// Active component; basically give focus
|
||||||
|
fn active(&mut self) {
|
||||||
|
self.states.focus = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -295,8 +311,5 @@ mod tests {
|
||||||
use crossterm::event::KeyEvent;
|
use crossterm::event::KeyEvent;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ui_layout_components_input_text() {
|
fn test_ui_layout_components_input_text() {}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -114,4 +114,14 @@ pub trait Component {
|
||||||
/// The component must provide to the supervisor whether it should be umounted (destroyed)
|
/// 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.
|
/// This makes sense to be called after an `on` or after an `update`, where the states changes.
|
||||||
fn should_umount(&self) -> bool;
|
fn should_umount(&self) -> bool;
|
||||||
|
|
||||||
|
/// ### blur
|
||||||
|
///
|
||||||
|
/// Blur component; basically remove focus
|
||||||
|
fn blur(&mut self);
|
||||||
|
|
||||||
|
/// ### active
|
||||||
|
///
|
||||||
|
/// Active component; basically give focus
|
||||||
|
fn active(&mut self);
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,6 @@ use tui::style::{Color, Modifier};
|
||||||
pub struct Props {
|
pub struct Props {
|
||||||
// Values
|
// Values
|
||||||
pub visible: bool, // Is the element visible ON CREATE?
|
pub visible: bool, // Is the element visible ON CREATE?
|
||||||
pub focus: bool, // Is the element focused
|
|
||||||
pub foreground: Color, // Foreground color
|
pub foreground: Color, // Foreground color
|
||||||
pub background: Color, // Background color
|
pub background: Color, // Background color
|
||||||
pub bold: bool, // Text bold
|
pub bold: bool, // Text bold
|
||||||
|
@ -52,7 +51,6 @@ impl Default for Props {
|
||||||
Self {
|
Self {
|
||||||
// Values
|
// Values
|
||||||
visible: true,
|
visible: true,
|
||||||
focus: false,
|
|
||||||
foreground: Color::Reset,
|
foreground: Color::Reset,
|
||||||
background: Color::Reset,
|
background: Color::Reset,
|
||||||
bold: false,
|
bold: false,
|
||||||
|
@ -133,26 +131,6 @@ impl PropsBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ### has_focus
|
|
||||||
///
|
|
||||||
/// Initialize props with focus set to True
|
|
||||||
pub fn has_focus(&mut self) -> &mut Self {
|
|
||||||
if let Some(props) = self.props.as_mut() {
|
|
||||||
props.focus = true;
|
|
||||||
}
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// ### hasnt_focus
|
|
||||||
///
|
|
||||||
/// Initialize props with focus set to False
|
|
||||||
pub fn hasnt_focus(&mut self) -> &mut Self {
|
|
||||||
if let Some(props) = self.props.as_mut() {
|
|
||||||
props.focus = false;
|
|
||||||
}
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// ### with_foreground
|
/// ### with_foreground
|
||||||
///
|
///
|
||||||
/// Set foreground color for component
|
/// Set foreground color for component
|
||||||
|
@ -305,7 +283,6 @@ mod tests {
|
||||||
assert_eq!(props.background, Color::Reset);
|
assert_eq!(props.background, Color::Reset);
|
||||||
assert_eq!(props.foreground, Color::Reset);
|
assert_eq!(props.foreground, Color::Reset);
|
||||||
assert_eq!(props.bold, false);
|
assert_eq!(props.bold, false);
|
||||||
assert_eq!(props.focus, false);
|
|
||||||
assert_eq!(props.italic, false);
|
assert_eq!(props.italic, false);
|
||||||
assert_eq!(props.underlined, false);
|
assert_eq!(props.underlined, false);
|
||||||
assert!(props.texts.title.is_none());
|
assert!(props.texts.title.is_none());
|
||||||
|
@ -330,7 +307,6 @@ mod tests {
|
||||||
fn test_ui_layout_props_builder() {
|
fn test_ui_layout_props_builder() {
|
||||||
let props: Props = PropsBuilder::default()
|
let props: Props = PropsBuilder::default()
|
||||||
.hidden()
|
.hidden()
|
||||||
.has_focus()
|
|
||||||
.with_background(Color::Blue)
|
.with_background(Color::Blue)
|
||||||
.with_foreground(Color::Green)
|
.with_foreground(Color::Green)
|
||||||
.bold()
|
.bold()
|
||||||
|
@ -346,7 +322,6 @@ mod tests {
|
||||||
.build();
|
.build();
|
||||||
assert_eq!(props.background, Color::Blue);
|
assert_eq!(props.background, Color::Blue);
|
||||||
assert_eq!(props.bold, true);
|
assert_eq!(props.bold, true);
|
||||||
assert_eq!(props.focus, true);
|
|
||||||
assert_eq!(props.foreground, Color::Green);
|
assert_eq!(props.foreground, Color::Green);
|
||||||
assert_eq!(props.italic, true);
|
assert_eq!(props.italic, true);
|
||||||
assert_eq!(props.texts.title.as_ref().unwrap().as_str(), "hello");
|
assert_eq!(props.texts.title.as_ref().unwrap().as_str(), "hello");
|
||||||
|
@ -361,7 +336,6 @@ mod tests {
|
||||||
assert_eq!(props.visible, false);
|
assert_eq!(props.visible, false);
|
||||||
let props: Props = PropsBuilder::default()
|
let props: Props = PropsBuilder::default()
|
||||||
.visible()
|
.visible()
|
||||||
.hasnt_focus()
|
|
||||||
.with_background(Color::Blue)
|
.with_background(Color::Blue)
|
||||||
.with_foreground(Color::Green)
|
.with_foreground(Color::Green)
|
||||||
.bold()
|
.bold()
|
||||||
|
@ -374,7 +348,6 @@ mod tests {
|
||||||
.build();
|
.build();
|
||||||
assert_eq!(props.background, Color::Blue);
|
assert_eq!(props.background, Color::Blue);
|
||||||
assert_eq!(props.bold, true);
|
assert_eq!(props.bold, true);
|
||||||
assert_eq!(props.focus, false);
|
|
||||||
assert_eq!(props.foreground, Color::Green);
|
assert_eq!(props.foreground, Color::Green);
|
||||||
assert_eq!(props.italic, true);
|
assert_eq!(props.italic, true);
|
||||||
assert_eq!(props.texts.title.as_ref().unwrap().as_str(), "hello");
|
assert_eq!(props.texts.title.as_ref().unwrap().as_str(), "hello");
|
||||||
|
|
Loading…
Reference in a new issue