diff --git a/src/ui/layout/components/file_list.rs b/src/ui/layout/components/file_list.rs index eb93d4c..f53786c 100644 --- a/src/ui/layout/components/file_list.rs +++ b/src/ui/layout/components/file_list.rs @@ -43,6 +43,7 @@ use tui::{ struct OwnStates { list_index: usize, // Index of selected element in list list_len: usize, // Length of file list + focus: bool, // Has focus? } impl Default for OwnStates { @@ -50,6 +51,7 @@ impl Default for OwnStates { OwnStates { list_index: 0, list_len: 0, + focus: false, } } } @@ -141,7 +143,7 @@ impl Component for FileList { .map(|line: &String| ListItem::new(Span::from(line.to_string()))) .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), false => (self.props.foreground, Color::Reset), }; @@ -156,7 +158,7 @@ impl Component for FileList { .block( Block::default() .borders(Borders::ALL) - .border_style(match self.props.focus { + .border_style(match self.states.focus { true => Style::default().fg(self.props.foreground), false => Style::default(), }) @@ -265,6 +267,20 @@ impl Component for FileList { // Never true 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)] @@ -289,6 +305,12 @@ mod tests { // Verify states assert_eq!(component.states.list_index, 0); 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 component.states.list_index += 1; assert_eq!(component.render().unwrap().cursor, 1); diff --git a/src/ui/layout/components/input.rs b/src/ui/layout/components/input.rs index 68118ce..d478813 100644 --- a/src/ui/layout/components/input.rs +++ b/src/ui/layout/components/input.rs @@ -42,6 +42,7 @@ use tui::{ struct OwnStates { input: Vec, // Current input cursor: usize, // Input position + focus: bool, // Focus } impl Default for OwnStates { @@ -49,6 +50,7 @@ impl Default for OwnStates { OwnStates { input: Vec::new(), cursor: 0, + focus: false, } } } @@ -115,7 +117,7 @@ impl OwnStates { } /// ### render_value - /// + /// /// Get value as string to render pub fn render_value(&self, itype: InputType) -> String { match itype { @@ -172,7 +174,7 @@ impl Component for Input { None => String::new(), }; 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), false => Style::default(), }) @@ -284,6 +286,20 @@ impl Component for Input { fn should_umount(&self) -> bool { 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)] @@ -295,8 +311,5 @@ mod tests { use crossterm::event::KeyEvent; #[test] - fn test_ui_layout_components_input_text() { - - } - -} \ No newline at end of file + fn test_ui_layout_components_input_text() {} +} diff --git a/src/ui/layout/mod.rs b/src/ui/layout/mod.rs index f2add3b..a4a08b9 100644 --- a/src/ui/layout/mod.rs +++ b/src/ui/layout/mod.rs @@ -114,4 +114,14 @@ pub trait Component { /// 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; + + /// ### blur + /// + /// Blur component; basically remove focus + fn blur(&mut self); + + /// ### active + /// + /// Active component; basically give focus + fn active(&mut self); } diff --git a/src/ui/layout/props.rs b/src/ui/layout/props.rs index 7d9816d..d89e3a2 100644 --- a/src/ui/layout/props.rs +++ b/src/ui/layout/props.rs @@ -35,7 +35,6 @@ use tui::style::{Color, Modifier}; pub struct Props { // Values pub visible: bool, // Is the element visible ON CREATE? - pub focus: bool, // Is the element focused pub foreground: Color, // Foreground color pub background: Color, // Background color pub bold: bool, // Text bold @@ -52,7 +51,6 @@ impl Default for Props { Self { // Values visible: true, - focus: false, foreground: Color::Reset, background: Color::Reset, bold: false, @@ -133,26 +131,6 @@ impl PropsBuilder { 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 /// /// Set foreground color for component @@ -305,7 +283,6 @@ mod tests { assert_eq!(props.background, Color::Reset); assert_eq!(props.foreground, Color::Reset); assert_eq!(props.bold, false); - assert_eq!(props.focus, false); assert_eq!(props.italic, false); assert_eq!(props.underlined, false); assert!(props.texts.title.is_none()); @@ -330,7 +307,6 @@ mod tests { fn test_ui_layout_props_builder() { let props: Props = PropsBuilder::default() .hidden() - .has_focus() .with_background(Color::Blue) .with_foreground(Color::Green) .bold() @@ -346,7 +322,6 @@ mod tests { .build(); assert_eq!(props.background, Color::Blue); assert_eq!(props.bold, true); - assert_eq!(props.focus, true); assert_eq!(props.foreground, Color::Green); assert_eq!(props.italic, true); assert_eq!(props.texts.title.as_ref().unwrap().as_str(), "hello"); @@ -361,7 +336,6 @@ mod tests { assert_eq!(props.visible, false); let props: Props = PropsBuilder::default() .visible() - .hasnt_focus() .with_background(Color::Blue) .with_foreground(Color::Green) .bold() @@ -374,7 +348,6 @@ mod tests { .build(); assert_eq!(props.background, Color::Blue); assert_eq!(props.bold, true); - assert_eq!(props.focus, false); assert_eq!(props.foreground, Color::Green); assert_eq!(props.italic, true); assert_eq!(props.texts.title.as_ref().unwrap().as_str(), "hello");