use super::PanelRect;
pub const PANEL_GAP: f32 = 0.0;
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub enum SplitterPolicy {
#[default]
Cascade,
RejectSnapBack,
Clamp {
min_frac: f64,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SplitKind {
SplitRight,
SplitBottom,
Grid2x2,
OneLeftTwoRight,
TwoLeftOneRight,
OneTopTwoBottom,
TwoTopOneBottom,
ThreeColumns,
ThreeRows,
OneBig3Small,
}
impl SplitKind {
#[deprecated(note = "use SplitKind::SplitRight — clearer about new panel placement")]
#[allow(non_upper_case_globals)]
pub const Horizontal: SplitKind = SplitKind::SplitRight;
#[deprecated(note = "use SplitKind::SplitBottom — clearer about new panel placement")]
#[allow(non_upper_case_globals)]
pub const Vertical: SplitKind = SplitKind::SplitBottom;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub enum WindowLayout {
#[default]
Single,
SplitHorizontal,
SplitVertical,
Grid2x2,
TwoLeftOneRight,
OneLeftTwoRight,
TwoTopOneBottom,
OneTopTwoBottom,
ThreeColumns,
ThreeRows,
Custom,
}
impl WindowLayout {
pub fn panel_count(&self) -> usize {
match self {
WindowLayout::Single => 1,
WindowLayout::SplitHorizontal | WindowLayout::SplitVertical => 2,
WindowLayout::Grid2x2 => 4,
WindowLayout::TwoLeftOneRight | WindowLayout::OneLeftTwoRight |
WindowLayout::TwoTopOneBottom | WindowLayout::OneTopTwoBottom |
WindowLayout::ThreeColumns | WindowLayout::ThreeRows => 3,
WindowLayout::Custom => usize::MAX, }
}
pub fn calculate_rects(&self, total_width: f32, total_height: f32, panel_count: usize) -> Vec<PanelRect> {
self.calculate_rects_with_gap(total_width, total_height, panel_count, PANEL_GAP)
}
pub fn calculate_rects_with_gap(&self, total_width: f32, total_height: f32, panel_count: usize, gap: f32) -> Vec<PanelRect> {
let mut rects = Vec::new();
match self {
WindowLayout::Single => {
rects.push(PanelRect::new(0.0, 0.0, total_width, total_height));
}
WindowLayout::SplitHorizontal => {
let half = (total_width - gap) / 2.0;
rects.push(PanelRect::new(0.0, 0.0, half, total_height));
if panel_count > 1 {
rects.push(PanelRect::new(half + gap, 0.0, half, total_height));
}
}
WindowLayout::SplitVertical => {
let half = (total_height - gap) / 2.0;
rects.push(PanelRect::new(0.0, 0.0, total_width, half));
if panel_count > 1 {
rects.push(PanelRect::new(0.0, half + gap, total_width, half));
}
}
WindowLayout::Grid2x2 => {
let half_w = (total_width - gap) / 2.0;
let half_h = (total_height - gap) / 2.0;
rects.push(PanelRect::new(0.0, 0.0, half_w, half_h));
if panel_count > 1 { rects.push(PanelRect::new(half_w + gap, 0.0, half_w, half_h)); }
if panel_count > 2 { rects.push(PanelRect::new(0.0, half_h + gap, half_w, half_h)); }
if panel_count > 3 { rects.push(PanelRect::new(half_w + gap, half_h + gap, half_w, half_h)); }
}
WindowLayout::TwoLeftOneRight => {
let left_w = (total_width - gap) * 0.4;
let right_w = (total_width - gap) * 0.6;
let half_h = (total_height - gap) / 2.0;
rects.push(PanelRect::new(0.0, 0.0, left_w, half_h));
if panel_count > 1 { rects.push(PanelRect::new(0.0, half_h + gap, left_w, half_h)); }
if panel_count > 2 { rects.push(PanelRect::new(left_w + gap, 0.0, right_w, total_height)); }
}
WindowLayout::OneLeftTwoRight => {
let left_w = (total_width - gap) * 0.6;
let right_w = (total_width - gap) * 0.4;
let half_h = (total_height - gap) / 2.0;
rects.push(PanelRect::new(0.0, 0.0, left_w, total_height));
if panel_count > 1 { rects.push(PanelRect::new(left_w + gap, 0.0, right_w, half_h)); }
if panel_count > 2 { rects.push(PanelRect::new(left_w + gap, half_h + gap, right_w, half_h)); }
}
WindowLayout::TwoTopOneBottom => {
let top_h = (total_height - gap) * 0.4;
let bottom_h = (total_height - gap) * 0.6;
let half_w = (total_width - gap) / 2.0;
rects.push(PanelRect::new(0.0, 0.0, half_w, top_h));
if panel_count > 1 { rects.push(PanelRect::new(half_w + gap, 0.0, half_w, top_h)); }
if panel_count > 2 { rects.push(PanelRect::new(0.0, top_h + gap, total_width, bottom_h)); }
}
WindowLayout::OneTopTwoBottom => {
let top_h = (total_height - gap) * 0.6;
let bottom_h = (total_height - gap) * 0.4;
let half_w = (total_width - gap) / 2.0;
rects.push(PanelRect::new(0.0, 0.0, total_width, top_h));
if panel_count > 1 { rects.push(PanelRect::new(0.0, top_h + gap, half_w, bottom_h)); }
if panel_count > 2 { rects.push(PanelRect::new(half_w + gap, top_h + gap, half_w, bottom_h)); }
}
WindowLayout::ThreeColumns => {
let col_w = (total_width - gap * 2.0) / 3.0;
rects.push(PanelRect::new(0.0, 0.0, col_w, total_height));
if panel_count > 1 { rects.push(PanelRect::new(col_w + gap, 0.0, col_w, total_height)); }
if panel_count > 2 { rects.push(PanelRect::new(col_w * 2.0 + gap * 2.0, 0.0, col_w, total_height)); }
}
WindowLayout::ThreeRows => {
let row_h = (total_height - gap * 2.0) / 3.0;
rects.push(PanelRect::new(0.0, 0.0, total_width, row_h));
if panel_count > 1 { rects.push(PanelRect::new(0.0, row_h + gap, total_width, row_h)); }
if panel_count > 2 { rects.push(PanelRect::new(0.0, row_h * 2.0 + gap * 2.0, total_width, row_h)); }
}
WindowLayout::Custom => {
if panel_count == 0 {
return rects;
}
let cols = (panel_count as f32).sqrt().ceil() as usize;
let rows = panel_count.div_ceil(cols); let cell_w = (total_width - gap * (cols as f32 - 1.0).max(0.0)) / cols as f32;
let cell_h = (total_height - gap * (rows as f32 - 1.0).max(0.0)) / rows as f32;
if cell_w <= 0.0 || cell_h <= 0.0 {
rects.push(PanelRect::new(0.0, 0.0, total_width, total_height));
return rects;
}
for i in 0..panel_count {
let col = i % cols;
let row = i / cols;
let x = col as f32 * (cell_w + gap);
let y = row as f32 * (cell_h + gap);
rects.push(PanelRect::new(x, y, cell_w, cell_h));
}
}
}
rects
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct GridSpec {
pub rows: usize,
pub cols: usize,
pub row_ratios: Vec<f64>,
pub col_ratios: Vec<f64>,
}
impl GridSpec {
pub fn new(rows: usize, cols: usize) -> Option<Self> {
if rows == 0 || cols == 0 {
return None;
}
Some(Self {
rows,
cols,
row_ratios: vec![1.0 / rows as f64; rows],
col_ratios: vec![1.0 / cols as f64; cols],
})
}
pub fn with_ratios(row_ratios: Vec<f64>, col_ratios: Vec<f64>) -> Option<Self> {
if !Self::valid_block(&row_ratios) || !Self::valid_block(&col_ratios) {
return None;
}
Some(Self {
rows: row_ratios.len(),
cols: col_ratios.len(),
row_ratios,
col_ratios,
})
}
pub(crate) fn valid_block(ratios: &[f64]) -> bool {
!ratios.is_empty() && ratios.iter().all(|r| r.is_finite() && *r > 0.0)
}
pub fn cell_count(&self) -> usize {
self.rows * self.cols
}
pub fn cell_index(&self, row: usize, col: usize) -> usize {
row * self.cols + col
}
pub fn normalized_rows(&self) -> Vec<f64> {
Self::normalize(&self.row_ratios, self.rows)
}
pub fn normalized_cols(&self) -> Vec<f64> {
Self::normalize(&self.col_ratios, self.cols)
}
fn normalize(ratios: &[f64], n: usize) -> Vec<f64> {
if n == 0 {
return Vec::new();
}
if ratios.len() != n || !Self::valid_block(ratios) {
return vec![1.0 / n as f64; n];
}
let sum: f64 = ratios.iter().sum();
ratios.iter().map(|r| r / sum).collect()
}
pub fn cell_rects(&self, area: PanelRect, gap: f32) -> Vec<PanelRect> {
let (xs, ws) = Self::tracks(&self.normalized_cols(), area.x, area.width, gap);
let (ys, hs) = Self::tracks(&self.normalized_rows(), area.y, area.height, gap);
let mut out = Vec::with_capacity(self.cell_count());
for r in 0..self.rows {
for c in 0..self.cols {
out.push(PanelRect::new(xs[c], ys[r], ws[c], hs[r]));
}
}
out
}
pub(crate) fn tracks(fracs: &[f64], start: f32, extent: f32, gap: f32) -> (Vec<f32>, Vec<f32>) {
let n = fracs.len();
let available = (extent - gap * n.saturating_sub(1) as f32).max(0.0) as f64;
let mut starts = Vec::with_capacity(n);
let mut sizes = Vec::with_capacity(n);
let mut acc = 0.0_f64;
for (i, f) in fracs.iter().enumerate() {
let a = start + (acc * available) as f32 + gap * i as f32;
acc += f;
let b = if i + 1 == n {
start + extent
} else {
start + (acc * available) as f32 + gap * i as f32
};
starts.push(a);
sizes.push((b - a).max(0.0));
}
(starts, sizes)
}
}