use crate::modifier::Modifier;
use cranpose_ui_graphics::Dp;
use cranpose_ui_layout::{Alignment, Constraints, HorizontalAlignment, VerticalAlignment};
pub trait BoxScope {
fn align(&self, alignment: Alignment) -> Modifier;
}
pub trait ColumnScope {
fn align(&self, alignment: HorizontalAlignment) -> Modifier;
fn weight(&self, weight: f32, fill: bool) -> Modifier;
}
pub trait RowScope {
fn align(&self, alignment: VerticalAlignment) -> Modifier;
fn weight(&self, weight: f32, fill: bool) -> Modifier;
}
pub trait BoxWithConstraintsScope: BoxScope {
fn constraints(&self) -> Constraints;
fn min_width(&self) -> Dp;
fn max_width(&self) -> Dp;
fn min_height(&self) -> Dp;
fn max_height(&self) -> Dp;
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BoxWithConstraintsScopeImpl {
constraints: Constraints,
density: f32,
}
impl BoxWithConstraintsScopeImpl {
pub fn new(constraints: Constraints) -> Self {
Self {
constraints,
density: 1.0,
}
}
pub fn with_density(constraints: Constraints, density: f32) -> Self {
Self {
constraints,
density,
}
}
fn to_dp(self, raw: f32) -> Dp {
Dp::from_px(raw, self.density)
}
pub fn to_px(&self, dp: Dp) -> f32 {
dp.to_px(self.density)
}
pub fn density(&self) -> f32 {
self.density
}
}
impl BoxScope for BoxWithConstraintsScopeImpl {
fn align(&self, alignment: Alignment) -> Modifier {
BoxScopeImpl.align(alignment)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BoxScopeImpl;
impl BoxScope for BoxScopeImpl {
fn align(&self, alignment: Alignment) -> Modifier {
Modifier::empty().alignInBox(alignment)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ColumnScopeImpl;
impl ColumnScope for ColumnScopeImpl {
fn align(&self, alignment: HorizontalAlignment) -> Modifier {
Modifier::empty().alignInColumn(alignment)
}
fn weight(&self, weight: f32, fill: bool) -> Modifier {
Modifier::empty().columnWeight(weight, fill)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RowScopeImpl;
impl RowScope for RowScopeImpl {
fn align(&self, alignment: VerticalAlignment) -> Modifier {
Modifier::empty().alignInRow(alignment)
}
fn weight(&self, weight: f32, fill: bool) -> Modifier {
Modifier::empty().rowWeight(weight, fill)
}
}
impl BoxWithConstraintsScope for BoxWithConstraintsScopeImpl {
fn constraints(&self) -> Constraints {
self.constraints
}
fn min_width(&self) -> Dp {
self.to_dp(self.constraints.min_width)
}
fn max_width(&self) -> Dp {
self.to_dp(self.constraints.max_width)
}
fn min_height(&self) -> Dp {
self.to_dp(self.constraints.min_height)
}
fn max_height(&self) -> Dp {
self.to_dp(self.constraints.max_height)
}
}