mod definition;
mod layout;
mod render_grid;
pub use definition::*;
pub use layout::CellPlacement;
pub use render_grid::*;
use crate::{BackgroundSizing, Brush, ControlBorder};
use waft_foundation::App;
use waft_rendering::{AnyRenderObject, RenderBox, RenderHandle};
use waft_widgets::*;
#[derive(Debug, Default)]
pub struct Grid {
pub key: Option<KeyRef>,
pub row_definitions: Vec<RowDefinition>,
pub column_definitions: Vec<ColumnDefinition>,
pub row_spacing: f64,
pub column_spacing: f64,
pub background: Option<Brush>,
pub border_brush: Option<Brush>,
pub border_thickness: [f64; 4],
pub corner_radius: [f64; 4],
pub background_sizing: BackgroundSizing,
pub padding: [f64; 4],
pub children: Vec<WidgetRef>,
}
impl Grid {
pub fn new() -> Grid {
Grid::default()
}
pub fn key(mut self, key: KeyRef) -> Grid {
self.key = Some(key);
self
}
pub fn row_definitions(mut self, rows: impl IntoIterator<Item = RowDefinition>) -> Grid {
self.row_definitions = rows.into_iter().collect();
self
}
pub fn column_definitions(
mut self,
columns: impl IntoIterator<Item = ColumnDefinition>,
) -> Grid {
self.column_definitions = columns.into_iter().collect();
self
}
pub fn row_spacing(mut self, spacing: f64) -> Grid {
self.row_spacing = spacing;
self
}
pub fn column_spacing(mut self, spacing: f64) -> Grid {
self.column_spacing = spacing;
self
}
pub fn background(mut self, background: Brush) -> Grid {
self.background = Some(background);
self
}
pub fn border_brush(mut self, border_brush: Brush) -> Grid {
self.border_brush = Some(border_brush);
self
}
pub fn border_thickness(mut self, thickness: f64) -> Grid {
self.border_thickness = [thickness; 4];
self
}
pub fn border_thickness_ltrb(mut self, thickness: [f64; 4]) -> Grid {
self.border_thickness = thickness;
self
}
pub fn corner_radius(mut self, radius: f64) -> Grid {
self.corner_radius = [radius; 4];
self
}
pub fn corner_radius_corners(mut self, radii: [f64; 4]) -> Grid {
self.corner_radius = radii;
self
}
pub fn background_sizing(mut self, sizing: BackgroundSizing) -> Grid {
self.background_sizing = sizing;
self
}
pub fn padding(mut self, padding: [f64; 4]) -> Grid {
self.padding = padding;
self
}
pub fn children(mut self, children: impl IntoIterator<Item = WidgetRef>) -> Grid {
self.children = children.into_iter().collect();
self
}
}
impl StatelessWidget for Grid {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
let panel = GridPanel {
key: self.key.clone(),
row_definitions: self.row_definitions.clone(),
column_definitions: self.column_definitions.clone(),
row_spacing: self.row_spacing,
column_spacing: self.column_spacing,
children: self.children.clone(),
};
ControlBorder::new_optional(self.background, self.border_brush)
.border_thickness_ltrb(self.border_thickness)
.corner_radius_corners(self.corner_radius)
.background_sizing(self.background_sizing)
.padding(self.padding)
.child(panel)
.into_widget()
}
}
#[derive(Debug)]
struct GridPanel {
key: Option<KeyRef>,
row_definitions: Vec<RowDefinition>,
column_definitions: Vec<ColumnDefinition>,
row_spacing: f64,
column_spacing: f64,
children: Vec<WidgetRef>,
}
impl RenderObjectWidget for GridPanel {
type RenderObject = RenderGrid;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
let render_object = RenderGrid::new(app);
self.update(app, render_object);
render_object.as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderGrid>,
) {
self.update(app, render_object);
}
}
impl GridPanel {
fn update(&self, app: &mut App, render_object: RenderHandle<RenderGrid>) {
render_object.set_definitions(
app,
self.row_definitions.clone(),
self.column_definitions.clone(),
);
render_object.set_row_spacing(app, self.row_spacing);
render_object.set_column_spacing(app, self.column_spacing);
}
}
impl MultiChildRenderObjectWidget for GridPanel {
fn children(&self) -> &[WidgetRef] {
&self.children
}
}
#[derive(Debug)]
pub struct GridCell {
pub key: Option<KeyRef>,
pub placement: CellPlacement,
pub child: WidgetRef,
}
impl GridCell {
#[cfg_attr(debug_assertions, track_caller)]
pub fn new<K>(child: impl IntoWidget<K>) -> GridCell {
GridCell {
key: None,
placement: CellPlacement::default(),
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> GridCell {
self.key = Some(key);
self
}
pub fn row(mut self, row: usize) -> GridCell {
self.placement.row = row;
self
}
pub fn column(mut self, column: usize) -> GridCell {
self.placement.column = column;
self
}
pub fn row_span(mut self, span: usize) -> GridCell {
self.placement.row_span = span;
self
}
pub fn column_span(mut self, span: usize) -> GridCell {
self.placement.column_span = span;
self
}
}
impl ParentDataWidget for GridCell {
type ParentData = GridParentData;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn apply_parent_data(&self, app: &mut App, render_object: AnyRenderObject) {
let parent_data = render_object.parent_data_of_mut::<GridParentData>(app);
if parent_data.placement == self.placement {
return;
}
parent_data.placement = self.placement;
if let Some(parent) = render_object.parent(app) {
parent.mark_needs_layout(app);
}
}
}