use super::Layout;
use crate::core::{ObjectId, Rect};
pub struct GridLayout {
rows: u32,
cols: u32,
spacing: u32,
margin: u32,
column_stretch: u32,
row_stretch: u32,
cells: Vec<Option<ObjectId>>,
}
impl GridLayout {
pub fn new(rows: u32, cols: u32, spacing: u32, margin: u32) -> Self {
let safe_rows = rows.max(1);
let safe_cols = cols.max(1);
Self {
rows: safe_rows,
cols: safe_cols,
spacing,
margin,
column_stretch: 1,
row_stretch: 1,
cells: vec![None; (safe_rows * safe_cols) as usize],
}
}
pub fn set_widget(&mut self, row: u32, col: u32, widget_id: ObjectId) {
if row < self.rows && col < self.cols {
self.cells[(row * self.cols + col) as usize] = Some(widget_id);
}
}
pub fn cell_count(&self) -> usize {
self.cells.iter().filter(|cell| cell.is_some()).count()
}
pub fn total_cells(&self) -> usize {
self.cells.len()
}
pub fn rows(&self) -> u32 {
self.rows
}
pub fn cols(&self) -> u32 {
self.cols
}
pub fn spacing(&self) -> u32 {
self.spacing
}
pub fn margin(&self) -> u32 {
self.margin
}
pub fn column_stretch(&self) -> u32 {
self.column_stretch
}
pub fn set_column_stretch(&mut self, stretch: u32) {
self.column_stretch = stretch.max(1);
}
pub fn row_stretch(&self) -> u32 {
self.row_stretch
}
pub fn set_row_stretch(&mut self, stretch: u32) {
self.row_stretch = stretch.max(1);
}
}
impl Layout for GridLayout {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn add_widget(&mut self, widget_id: ObjectId, _stretch: u32) {
if let Some(slot) = self.cells.iter_mut().find(|cell| cell.is_none()) {
*slot = Some(widget_id);
}
}
fn remove_widget(&mut self, widget_id: ObjectId) {
for cell in &mut self.cells {
if *cell == Some(widget_id) {
*cell = None;
}
}
}
fn child_ids(&self) -> Vec<ObjectId> {
self.cells.iter().filter_map(|cell| *cell).collect()
}
fn has_child(&self, id: ObjectId) -> bool {
self.cells.contains(&Some(id))
}
fn clear(&mut self) {
for cell in &mut self.cells {
*cell = None;
}
}
fn update(&self, rect: Rect, widgets: &mut dyn FnMut(ObjectId, Rect)) {
let cell_width = rect
.width
.saturating_sub(self.margin * 2)
.saturating_sub((self.cols - 1) * self.spacing)
/ self.cols;
let cell_height = rect
.height
.saturating_sub(self.margin * 2)
.saturating_sub((self.rows - 1) * self.spacing)
/ self.rows;
for row in 0..self.rows {
for col in 0..self.cols {
if let Some(widget_id) = self.cells[(row * self.cols + col) as usize] {
let x =
rect.x + self.margin as i32 + (col * (cell_width + self.spacing)) as i32;
let y =
rect.y + self.margin as i32 + (row * (cell_height + self.spacing)) as i32;
widgets(widget_id, Rect::new(x, y, cell_width, cell_height));
}
}
}
}
}