use crate::geometry::{Rect, Size};
use crate::style_helpers::{FromPercent, FromPoints, TaffyAuto, TaffyMaxContent, TaffyMinContent, TaffyZero};
use crate::sys::abs;
#[derive(Copy, Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum LengthPercentage {
Points(f32),
Percent(f32),
}
impl TaffyZero for LengthPercentage {
const ZERO: Self = Self::Points(0.0);
}
impl FromPoints for LengthPercentage {
fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
Self::Points(points.into())
}
}
impl FromPercent for LengthPercentage {
fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self {
Self::Percent(percent.into())
}
}
#[derive(Copy, Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum LengthPercentageAuto {
Points(f32),
Percent(f32),
Auto,
}
impl TaffyZero for LengthPercentageAuto {
const ZERO: Self = Self::Points(0.0);
}
impl TaffyAuto for LengthPercentageAuto {
const AUTO: Self = Self::Auto;
}
impl FromPoints for LengthPercentageAuto {
fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
Self::Points(points.into())
}
}
impl FromPercent for LengthPercentageAuto {
fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self {
Self::Percent(percent.into())
}
}
impl From<LengthPercentage> for LengthPercentageAuto {
fn from(input: LengthPercentage) -> Self {
match input {
LengthPercentage::Points(value) => Self::Points(value),
LengthPercentage::Percent(value) => Self::Percent(value),
}
}
}
impl LengthPercentageAuto {
pub fn resolve_to_option(self, context: f32) -> Option<f32> {
match self {
Self::Points(points) => Some(points),
Self::Percent(percent) => Some(context * percent),
Self::Auto => None,
}
}
}
#[derive(Copy, Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Dimension {
Points(f32),
Percent(f32),
Auto,
}
impl TaffyZero for Dimension {
const ZERO: Self = Self::Points(0.0);
}
impl TaffyAuto for Dimension {
const AUTO: Self = Self::Auto;
}
impl FromPoints for Dimension {
fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
Self::Points(points.into())
}
}
impl FromPercent for Dimension {
fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self {
Self::Percent(percent.into())
}
}
impl From<LengthPercentage> for Dimension {
fn from(input: LengthPercentage) -> Self {
match input {
LengthPercentage::Points(value) => Self::Points(value),
LengthPercentage::Percent(value) => Self::Percent(value),
}
}
}
impl From<LengthPercentageAuto> for Dimension {
fn from(input: LengthPercentageAuto) -> Self {
match input {
LengthPercentageAuto::Points(value) => Self::Points(value),
LengthPercentageAuto::Percent(value) => Self::Percent(value),
LengthPercentageAuto::Auto => Self::Auto,
}
}
}
impl Dimension {
#[cfg(feature = "grid")]
pub fn into_option(self) -> Option<f32> {
match self {
Dimension::Points(value) => Some(value),
_ => None,
}
}
}
impl Rect<Dimension> {
#[must_use]
pub const fn from_points(start: f32, end: f32, top: f32, bottom: f32) -> Self {
Rect {
left: Dimension::Points(start),
right: Dimension::Points(end),
top: Dimension::Points(top),
bottom: Dimension::Points(bottom),
}
}
#[must_use]
pub const fn from_percent(start: f32, end: f32, top: f32, bottom: f32) -> Self {
Rect {
left: Dimension::Percent(start),
right: Dimension::Percent(end),
top: Dimension::Percent(top),
bottom: Dimension::Percent(bottom),
}
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum AvailableSpace {
Definite(f32),
MinContent,
MaxContent,
}
impl TaffyZero for AvailableSpace {
const ZERO: Self = Self::Definite(0.0);
}
impl TaffyMaxContent for AvailableSpace {
const MAX_CONTENT: Self = Self::MaxContent;
}
impl TaffyMinContent for AvailableSpace {
const MIN_CONTENT: Self = Self::MinContent;
}
impl FromPoints for AvailableSpace {
fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
Self::Definite(points.into())
}
}
impl AvailableSpace {
pub fn is_definite(self) -> bool {
matches!(self, AvailableSpace::Definite(_))
}
pub fn into_option(self) -> Option<f32> {
match self {
AvailableSpace::Definite(value) => Some(value),
_ => None,
}
}
pub fn unwrap_or(self, default: f32) -> f32 {
self.into_option().unwrap_or(default)
}
#[track_caller]
pub fn unwrap(self) -> f32 {
self.into_option().unwrap()
}
pub fn or(self, default: AvailableSpace) -> AvailableSpace {
match self {
AvailableSpace::Definite(_) => self,
_ => default,
}
}
pub fn or_else(self, default_cb: impl FnOnce() -> AvailableSpace) -> AvailableSpace {
match self {
AvailableSpace::Definite(_) => self,
_ => default_cb(),
}
}
pub fn unwrap_or_else(self, default_cb: impl FnOnce() -> f32) -> f32 {
self.into_option().unwrap_or_else(default_cb)
}
pub fn maybe_set(self, value: Option<f32>) -> AvailableSpace {
match value {
Some(value) => AvailableSpace::Definite(value),
None => self,
}
}
pub fn map_definite_value(self, map_function: impl FnOnce(f32) -> f32) -> AvailableSpace {
match self {
AvailableSpace::Definite(value) => AvailableSpace::Definite(map_function(value)),
_ => self,
}
}
pub fn compute_free_space(&self, used_space: f32) -> f32 {
match self {
AvailableSpace::MaxContent => f32::INFINITY,
AvailableSpace::MinContent => 0.0,
AvailableSpace::Definite(available_space) => available_space - used_space,
}
}
pub fn is_roughly_equal(self, other: AvailableSpace) -> bool {
use AvailableSpace::*;
match (self, other) {
(Definite(a), Definite(b)) => abs(a - b) < f32::EPSILON,
(MinContent, MinContent) => true,
(MaxContent, MaxContent) => true,
_ => false,
}
}
}
impl From<f32> for AvailableSpace {
fn from(value: f32) -> Self {
Self::Definite(value)
}
}
impl From<Option<f32>> for AvailableSpace {
fn from(option: Option<f32>) -> Self {
match option {
Some(value) => Self::Definite(value),
None => Self::MaxContent,
}
}
}
impl Size<AvailableSpace> {
pub fn into_options(self) -> Size<Option<f32>> {
Size { width: self.width.into_option(), height: self.height.into_option() }
}
pub fn maybe_set(self, value: Size<Option<f32>>) -> Size<AvailableSpace> {
Size { width: self.width.maybe_set(value.width), height: self.height.maybe_set(value.height) }
}
}