use crate::constraints::Constraints;
use cranpose_core::NodeId;
use cranpose_ui_graphics::Size;
use std::rc::Rc;
#[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) -> 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 struct Placeable {
width: f32,
height: f32,
node_id: NodeId,
content_offset_x: f32,
content_offset_y: f32,
place_fn: Option<Rc<dyn Fn(f32, f32)>>,
}
impl Placeable {
pub fn value(width: f32, height: f32, node_id: NodeId) -> Self {
Self {
width,
height,
node_id,
content_offset_x: 0.0,
content_offset_y: 0.0,
place_fn: None,
}
}
pub fn value_with_offset(
width: f32,
height: f32,
node_id: NodeId,
content_offset: (f32, f32),
) -> Self {
Self {
width,
height,
node_id,
content_offset_x: content_offset.0,
content_offset_y: content_offset.1,
place_fn: None,
}
}
pub fn with_place_fn(
width: f32,
height: f32,
node_id: NodeId,
place_fn: Rc<dyn Fn(f32, f32)>,
) -> Self {
Self {
width,
height,
node_id,
content_offset_x: 0.0,
content_offset_y: 0.0,
place_fn: Some(place_fn),
}
}
pub fn place(&self, x: f32, y: f32) {
if let Some(f) = &self.place_fn {
f(x, y);
}
}
pub fn width(&self) -> f32 {
self.width
}
pub fn height(&self) -> f32 {
self.height
}
pub fn node_id(&self) -> NodeId {
self.node_id
}
pub fn content_offset(&self) -> (f32, f32) {
(self.content_offset_x, self.content_offset_y)
}
}
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,
}
}
}