From 8740d4b332290b7fa661b157ed190df9f40ad349 Mon Sep 17 00:00:00 2001 From: Piotr Wach Date: Sun, 10 Dec 2023 00:39:35 +0000 Subject: Refactors MainWindow render to make it more readable --- src/interactive/widgets/main.rs | 108 +++++++++++++++++++++++----------------- 1 file changed, 63 insertions(+), 45 deletions(-) diff --git a/src/interactive/widgets/main.rs b/src/interactive/widgets/main.rs index 2b5d2c3..68b1ed9 100644 --- a/src/interactive/widgets/main.rs +++ b/src/interactive/widgets/main.rs @@ -50,58 +50,22 @@ impl MainWindow { state, } = props.borrow(); - let (entries_style, help_style, mark_style) = { - let grey = Style { - fg: Color::DarkGray.into(), - bg: Color::Reset.into(), - add_modifier: Modifier::empty(), - ..Style::default() - }; - let bold = Style::default().add_modifier(Modifier::BOLD); - match state.focussed { - Main => (bold, grey, grey), - Help => (grey, bold, grey), - Mark => (grey, grey, bold), - } - }; + let (entries_style, help_style, mark_style) = pane_border_style(state.focussed); + let (header_area, content_area, footer_area) = main_window_layout(area); + + let header_bg_color = header_background_color(self.is_anything_marked(), state.focussed); + Header.render(header_bg_color, header_area, buf); - let (header_area, entries_area, footer_area) = { - let regions = Layout::default() - .direction(Direction::Vertical) - .constraints([Length(1), Max(256), Length(1)].as_ref()) - .split(area); - (regions[0], regions[1], regions[2]) - }; - { - let marked = self.mark_pane.as_ref().map(|p| p.marked()); - let bg_color = match (marked.map_or(true, |m| m.is_empty()), state.focussed) { - (false, FocussedPane::Mark) => Color::LightRed, - (false, _) => COLOR_MARKED, - (_, _) => Color::White, - }; - Header.render(bg_color, header_area, buf); - } let (entries_area, help_pane, mark_pane) = { - let regions = Layout::default() - .direction(Direction::Horizontal) - .constraints([Percentage(50), Percentage(50)].as_ref()) - .split(entries_area); - let (left_pane, right_pane) = (regions[0], regions[1]); + let (left_pane, right_pane) = content_layout(content_area); match (&mut self.help_pane, &mut self.mark_pane) { (Some(ref mut pane), None) => (left_pane, Some((right_pane, pane)), None), (None, Some(ref mut pane)) => (left_pane, None, Some((right_pane, pane))), (Some(ref mut help), Some(ref mut mark)) => { - let regions = Layout::default() - .direction(Direction::Vertical) - .constraints([Percentage(50), Percentage(50)].as_ref()) - .split(right_pane); - ( - left_pane, - Some((regions[0], help)), - Some((regions[1], mark)), - ) + let (top_area, bottom_area) = right_pane_layout(right_pane); + (left_pane, Some((top_area, help)), Some((bottom_area, mark))) } - (None, None) => (entries_area, None, None), + (None, None) => (content_area, None, None), } }; @@ -149,4 +113,58 @@ impl MainWindow { buf, ); } + + fn is_anything_marked(&self) -> bool { + self.mark_pane + .as_ref() + .map(|p| p.marked()) + .map_or(true, |m| m.is_empty()) + } +} + +fn right_pane_layout(right_pane: Rect) -> (Rect, Rect) { + let regions = Layout::default() + .direction(Direction::Vertical) + .constraints([Percentage(50), Percentage(50)].as_ref()) + .split(right_pane); + (regions[0], regions[1]) +} + +fn content_layout(content_area: Rect) -> (Rect, Rect) { + let regions = Layout::default() + .direction(Direction::Horizontal) + .constraints([Percentage(50), Percentage(50)].as_ref()) + .split(content_area); + (regions[0], regions[1]) +} + +fn header_background_color(is_marked: bool, focused_pane: FocussedPane) -> Color { + match (is_marked, focused_pane) { + (false, FocussedPane::Mark) => Color::LightRed, + (false, _) => COLOR_MARKED, + (_, _) => Color::White, + } +} + +fn main_window_layout(area: Rect) -> (Rect, Rect, Rect) { + let regions = Layout::default() + .direction(Direction::Vertical) + .constraints([Length(1), Max(256), Length(1)].as_ref()) + .split(area); + (regions[0], regions[1], regions[2]) +} + +fn pane_border_style(focused_pane: FocussedPane) -> (Style, Style, Style) { + let grey = Style { + fg: Color::DarkGray.into(), + bg: Color::Reset.into(), + add_modifier: Modifier::empty(), + ..Style::default() + }; + let bold = Style::default().add_modifier(Modifier::BOLD); + match focused_pane { + Main => (bold, grey, grey), + Help => (grey, bold, grey), + Mark => (grey, grey, bold), + } } -- cgit v1.2.3 From 49772d17dca72006e602f8707121b3378f948981 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 10 Dec 2023 09:21:15 +0100 Subject: refactor --- src/interactive/widgets/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interactive/widgets/main.rs b/src/interactive/widgets/main.rs index 68b1ed9..d4b15bc 100644 --- a/src/interactive/widgets/main.rs +++ b/src/interactive/widgets/main.rs @@ -140,7 +140,7 @@ fn content_layout(content_area: Rect) -> (Rect, Rect) { fn header_background_color(is_marked: bool, focused_pane: FocussedPane) -> Color { match (is_marked, focused_pane) { - (false, FocussedPane::Mark) => Color::LightRed, + (false, Mark) => Color::LightRed, (false, _) => COLOR_MARKED, (_, _) => Color::White, } -- cgit v1.2.3