summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPiotr Wach <pwach@bloomberg.net>2023-12-10 00:39:35 +0000
committerPiotr Wach <pwach@bloomberg.net>2023-12-10 00:39:35 +0000
commit8740d4b332290b7fa661b157ed190df9f40ad349 (patch)
treeb78a8a9448f0a42e76c98cbf186140919fceda62
parenta9dd549dc85faf17ce211ff0ab5be4c9863440ed (diff)
Refactors MainWindow render to make it more readable
-rw-r--r--src/interactive/widgets/main.rs108
1 files 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),
+ }
}