use crate::constraints::Constraints;
use cranpose_core::NodeId;
use cranpose_ui_graphics::Size;
#[derive(Clone, Copy, Debug, Default)]
pub struct FlexParentData {
pub weight: f32,
pub fill: bool,
}
impl FlexParentData {
pub fn new(weight: f32, fill: bool) -> Self {
Self { weight, fill }
}
pub fn has_weight(&self) -> bool {
self.weight > 0.0
}
}
pub trait Measurable {
fn measure(&self, constraints: Constraints) -> Box<dyn Placeable>;
fn min_intrinsic_width(&self, height: f32) -> f32;
fn max_intrinsic_width(&self, height: f32) -> f32;
fn min_intrinsic_height(&self, width: f32) -> f32;
fn max_intrinsic_height(&self, width: f32) -> f32;
fn flex_parent_data(&self) -> Option<FlexParentData> {
None
}
}
pub trait Placeable {
fn place(&self, x: f32, y: f32);
fn width(&self) -> f32;
fn height(&self) -> f32;
fn node_id(&self) -> NodeId;
fn content_offset(&self) -> (f32, f32) {
(0.0, 0.0)
}
}
pub trait MeasureScope {
fn density(&self) -> f32 {
1.0
}
fn font_scale(&self) -> f32 {
1.0
}
}
pub trait MeasurePolicy {
fn measure(
&self,
measurables: &[Box<dyn Measurable>],
constraints: Constraints,
) -> MeasureResult;
fn min_intrinsic_width(&self, measurables: &[Box<dyn Measurable>], height: f32) -> f32;
fn max_intrinsic_width(&self, measurables: &[Box<dyn Measurable>], height: f32) -> f32;
fn min_intrinsic_height(&self, measurables: &[Box<dyn Measurable>], width: f32) -> f32;
fn max_intrinsic_height(&self, measurables: &[Box<dyn Measurable>], width: f32) -> f32;
}
#[derive(Clone, Debug)]
pub struct MeasureResult {
pub size: Size,
pub placements: Vec<Placement>,
}
impl MeasureResult {
pub fn new(size: Size, placements: Vec<Placement>) -> Self {
Self { size, placements }
}
}
#[derive(Clone, Copy, Debug)]
pub struct Placement {
pub node_id: NodeId,
pub x: f32,
pub y: f32,
pub z_index: i32,
}
impl Placement {
pub fn new(node_id: NodeId, x: f32, y: f32, z_index: i32) -> Self {
Self {
node_id,
x,
y,
z_index,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct LayoutModifierMeasureResult {
pub size: Size,
pub placement_offset_x: f32,
pub placement_offset_y: f32,
}
impl LayoutModifierMeasureResult {
pub fn new(size: Size, placement_offset_x: f32, placement_offset_y: f32) -> Self {
Self {
size,
placement_offset_x,
placement_offset_y,
}
}
pub fn with_size(size: Size) -> Self {
Self {
size,
placement_offset_x: 0.0,
placement_offset_y: 0.0,
}
}
}