diff --git a/src/components.rs b/src/components.rs index b268e57..be85507 100644 --- a/src/components.rs +++ b/src/components.rs @@ -112,7 +112,7 @@ pub fn button() -> Component { this.clicked = !this.clicked; (this.on_click)(this); } else { - return UpdateResponse::Tick(vec![1, 140]); + return UpdateResponse::Tick(vec![1, 100]); } } UpdateResponse::None diff --git a/src/lib.rs b/src/lib.rs index 455cf3a..02069f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,6 +51,25 @@ impl Component { pub fn get_active_child(&mut self) -> Option<&mut Component> { self.children.get_mut(self.active_child) } + + pub fn set_active_child(&mut self, idx: usize) { + let last_active = self.active_child; + if let Some(last_child) = self.children.get_mut(last_active) { + last_child.style.is_active = false; + } + if let Some(child) = self.children.get_mut(idx) { + self.active_child = idx; + if child.width == 0 { + child.width = self.width; + } + if child.height == 0 { + child.height = self.height; + } + child.style.is_active = self.style.is_active; + } else if let Some(last_child) = self.children.get_mut(last_active) { + last_child.style.is_active = true; + } + } } #[derive(Debug, Clone, PartialEq)] @@ -69,6 +88,7 @@ impl App { /// Sets a component pub fn set_component(&mut self, component: Component) { let (width, height) = crossterm::terminal::size().unwrap(); + self.component.style.is_active = true; self.component = component; if self.component.width == 0 { self.component.width = width as usize; @@ -76,6 +96,7 @@ impl App { if self.component.height == 0 { self.component.height = height as usize; } + self.component.style.is_active = true; } /// Render to the screen diff --git a/src/style.rs b/src/style.rs index 56f6763..63412b2 100644 --- a/src/style.rs +++ b/src/style.rs @@ -2,13 +2,15 @@ pub struct Style { pub bg: Color, pub fg: Color, + pub outline: Color, pub font: Font, pub hover_bg: Color, pub hover_fg: Color, + pub hover_outline: Color, pub hover_font: Font, - is_active: bool, + pub is_active: bool, } impl Style { @@ -16,31 +18,33 @@ impl Style { Style { bg: Color::None, fg: Color::None, + outline: Color::None, font: Font::None, hover_bg: Color::None, hover_fg: Color::None, + hover_outline: Color::None, hover_font: Font::None, is_active: false, } } - pub fn update(&mut self, active: bool) { - self.is_active = active - } - pub fn get(self) -> String { - String::from(self.fg.ansi() + &self.bg.ansi_bg() + &self.font.ansi()) + if self.is_active { + String::from( + self.fg.prioritize(self.hover_fg).ansi() + + &self.bg.prioritize(self.hover_bg).ansi_bg() + + &self.font.prioritize(self.hover_font).ansi(), + ) + } else { + String::from(self.fg.ansi() + &self.bg.ansi_bg() + &self.font.ansi()) + } } pub fn get_outline(self) -> String { String::from(self.fg.ansi() + &self.bg.ansi_bg() + &self.font.ansi()) } - - pub fn get_hover(self) -> String { - String::from(self.hover_fg.ansi() + &self.hover_bg.ansi_bg() + &self.hover_font.ansi()) - } } #[derive(Debug, Clone, PartialEq)] @@ -72,6 +76,14 @@ impl Font { } }) } + + pub fn prioritize(self, secondary: Font) -> Font { + if secondary == Font::None { + self + } else { + secondary + } + } } #[derive(Debug, Clone, PartialEq)] @@ -122,4 +134,12 @@ impl Color { Color::White => "\x1b[47m", }) } + + pub fn prioritize(self, secondary: Color) -> Color { + if secondary == Color::None { + self + } else { + secondary + } + } }