use crate::tui::app::mission_control::McPanel;
use crate::tui::render::responsive;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
pub const MIN_WIDTH: u16 = 40;
pub const MIN_PANEL_ROWS: u16 = 5;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct McLayout {
pub inbox: Rect,
pub analytics: Rect,
pub activity: Rect,
pub schedule: Rect,
pub help_bar: Rect,
pub panels: Rect,
pub too_small: bool,
}
impl McLayout {
#[cfg(test)]
pub fn rect(&self, panel: McPanel) -> Rect {
match panel {
McPanel::Inbox => self.inbox,
McPanel::Analytics => self.analytics,
McPanel::Activity => self.activity,
McPanel::Schedule => self.schedule,
}
}
#[cfg(test)]
pub fn panels_in_order(&self) -> [(McPanel, Rect); 4] {
[
(McPanel::Inbox, self.inbox),
(McPanel::Analytics, self.analytics),
(McPanel::Activity, self.activity),
(McPanel::Schedule, self.schedule),
]
}
fn all_empty(area: Rect, help_bar: Rect, panels: Rect) -> Self {
let empty = Rect::new(area.x, area.y, 0, 0);
Self {
inbox: empty,
analytics: empty,
activity: empty,
schedule: empty,
help_bar,
panels,
too_small: true,
}
}
}
pub fn compute_focused(area: Rect, focus: McPanel) -> McLayout {
let (panels_area, help_bar) = split_help_bar(area);
if area.width < MIN_WIDTH || panels_area.height < MIN_PANEL_ROWS {
return McLayout::all_empty(area, help_bar, panels_area);
}
if !responsive::mc_two_column(area.width) {
let empty = Rect::new(area.x, area.y, 0, 0);
return McLayout {
inbox: if focus == McPanel::Inbox {
panels_area
} else {
empty
},
analytics: if focus == McPanel::Analytics {
panels_area
} else {
empty
},
activity: if focus == McPanel::Activity {
panels_area
} else {
empty
},
schedule: if focus == McPanel::Schedule {
panels_area
} else {
empty
},
help_bar,
panels: panels_area,
too_small: false,
};
}
let left_percent = if area.width >= responsive::MC_WIDE_MIN {
40
} else {
45
};
let cols = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(left_percent),
Constraint::Percentage(100 - left_percent),
])
.split(panels_area);
let left_rows = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Percentage(22),
Constraint::Percentage(48),
Constraint::Percentage(30),
])
.split(cols[0]);
McLayout {
inbox: left_rows[0],
activity: left_rows[1],
schedule: left_rows[2],
analytics: cols[1],
help_bar,
panels: panels_area,
too_small: false,
}
}
const HELP_BAR_ROWS: u16 = 2;
fn split_help_bar(area: Rect) -> (Rect, Rect) {
if area.height <= HELP_BAR_ROWS {
let empty = Rect { height: 0, ..area };
return (area, empty);
}
let panels = Rect {
height: area.height - HELP_BAR_ROWS,
..area
};
let help_bar = Rect {
x: area.x,
y: area.y + area.height - HELP_BAR_ROWS,
width: area.width,
height: HELP_BAR_ROWS,
};
(panels, help_bar)
}