use crate::state::gameday::GamedayPanels;
use crate::ui::input_popup;
use tui::layout::{Constraint, Layout, Rect, Size};
pub struct LayoutAreas {
pub top_bar: [Rect; 2],
pub main: Rect,
}
const TOP_BAR_HEIGHT: u16 = 3; const MAIN_HEIGHT: u16 = 100;
impl LayoutAreas {
pub fn new(size: Size) -> Self {
let rect = Rect::new(0, 0, size.width, size.height);
let [top, main] = Layout::vertical([
Constraint::Length(TOP_BAR_HEIGHT),
Constraint::Percentage(MAIN_HEIGHT),
])
.margin(1)
.areas(rect);
LayoutAreas {
top_bar: LayoutAreas::create_top_bar(top),
main,
}
}
pub(crate) fn update(&mut self, size: Rect, full_screen: bool) {
let constraints = match full_screen {
true => vec![Constraint::Percentage(0), Constraint::Percentage(100)],
false => vec![
Constraint::Length(TOP_BAR_HEIGHT),
Constraint::Percentage(MAIN_HEIGHT),
],
};
let [top, main] = Layout::vertical(constraints).areas(size);
self.top_bar = LayoutAreas::create_top_bar(top);
self.main = main;
}
pub fn create_top_bar(area: Rect) -> [Rect; 2] {
Layout::horizontal([Constraint::Percentage(90), Constraint::Percentage(10)]).areas(area)
}
pub fn for_boxscore(rect: Rect) -> [Rect; 2] {
Layout::vertical([
Constraint::Length(4), Constraint::Fill(1), ])
.horizontal_margin(2)
.vertical_margin(1)
.areas(rect)
}
pub fn for_at_bat(rect: Rect) -> [Rect; 2] {
Layout::vertical([
Constraint::Length(7), Constraint::Fill(1), ])
.areas(rect)
}
pub fn for_info(rect: Rect, show_win_probability: bool) -> Vec<Rect> {
let constraints = match show_win_probability {
true => vec![Constraint::Fill(1), Constraint::Length(6)],
false => vec![Constraint::Fill(1)],
};
Layout::vertical(constraints)
.horizontal_margin(2)
.vertical_margin(1)
.split(rect)
.to_vec()
}
pub fn generate_gameday_panels(active: &GamedayPanels, area: Rect) -> Vec<Rect> {
let constraints = match active.count() {
0 | 1 => vec![Constraint::Percentage(100)],
2 => vec![Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)],
3 => vec![
Constraint::Ratio(1, 3),
Constraint::Ratio(1, 3),
Constraint::Ratio(1, 3),
],
_ => vec![],
};
Layout::horizontal(constraints.as_slice())
.split(area)
.to_vec()
}
pub fn create_date_picker(area: Rect) -> Rect {
input_popup::create_popup(area, 4, 42)
}
}