use ratatui::{
layout::{Constraint, Direction, Layout, Rect},
Frame,
};
use super::app::App;
use super::renderer::{render_compact, render_minimal, render_ui};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LayoutMode {
Full,
Standard,
Compact,
Minimal,
}
impl LayoutMode {
pub fn from_terminal_width(width: u16) -> Self {
match width {
0..=39 => Self::Minimal,
40..=79 => Self::Compact,
80..=119 => Self::Standard,
_ => Self::Full,
}
}
pub fn shows_subtasks(&self) -> bool {
matches!(self, Self::Full | Self::Standard)
}
pub fn shows_full_metrics(&self) -> bool {
matches!(self, Self::Full)
}
}
pub fn calculate_layout(area: Rect) -> Vec<Rect> {
Layout::default()
.direction(Direction::Vertical)
.margin(1)
.constraints([
Constraint::Length(5), Constraint::Min(10), Constraint::Length(1), ])
.split(area)
.to_vec()
}
pub fn render_adaptive(frame: &mut Frame, app: &App) {
let mode = LayoutMode::from_terminal_width(frame.area().width);
match mode {
LayoutMode::Full | LayoutMode::Standard => render_ui(frame, app),
LayoutMode::Compact => render_compact(frame, app),
LayoutMode::Minimal => render_minimal(frame, app),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_layout_mode_selection() {
assert_eq!(LayoutMode::from_terminal_width(30), LayoutMode::Minimal);
assert_eq!(LayoutMode::from_terminal_width(50), LayoutMode::Compact);
assert_eq!(LayoutMode::from_terminal_width(90), LayoutMode::Standard);
assert_eq!(LayoutMode::from_terminal_width(150), LayoutMode::Full);
}
#[test]
fn test_subtask_visibility() {
assert!(!LayoutMode::Minimal.shows_subtasks());
assert!(!LayoutMode::Compact.shows_subtasks());
assert!(LayoutMode::Standard.shows_subtasks());
assert!(LayoutMode::Full.shows_subtasks());
}
#[test]
fn test_metrics_visibility() {
assert!(!LayoutMode::Minimal.shows_full_metrics());
assert!(!LayoutMode::Compact.shows_full_metrics());
assert!(!LayoutMode::Standard.shows_full_metrics());
assert!(LayoutMode::Full.shows_full_metrics());
}
#[test]
fn test_layout_constraints() {
let area = Rect::new(0, 0, 100, 30);
let chunks = calculate_layout(area);
assert_eq!(chunks.len(), 3);
assert_eq!(chunks[0].height, 5);
assert_eq!(chunks[2].height, 1);
}
}