iced_rizzen 0.14.0

Extra widgets for official releases of iced GUI library.
Documentation
//! ------- differences from `iced/core/src/length.rs`
//!
//! * Added new variant `FillRange(Range)`.
//!
//! * Added new variant `FillPortionRange(u16, Range)`.
//!
//! * Added new variant `ShrinkRange(Range)`.
//!
//! * Added new struct `Range` located in `range.rs`.
//!
//! -------

use super::range::Range;
use crate::core::Pixels;

/// The strategy used to fill space in a specific dimension.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Length {
    /// Fill a fixed amount of space.
    Fixed(f32),

    /// Fill all the remaining space.
    ///
    /// `Length::Fill` is equivalent to `Length::FillPortion(1)`.
    Fill,

    /// Is a Fill variant, that also has space bounds limitation.
    ///
    /// When converted to `iced::core::Length`, the bounds are simply removed.
    FillRange(Range),

    /// Fill a portion of the remaining space relative to other elements.
    ///
    /// Let's say we have two elements: one with `FillPortion(2)` and one with
    /// `FillPortion(3)`. The first will get 2 portions of the available space,
    /// while the second one would get 3.
    FillPortion(u16),

    /// Is a Fill variant, that also has space bounds limitation.
    ///
    /// When converted to `iced::core::Length`, the bounds are simply removed.
    FillPortionRange(u16, Range),

    /// Fill the least amount of space.
    Shrink,

    /// Is a Shrink variant, that also has space bounds limitation.
    ///
    /// When converted to `iced::core::Length`, the bounds are simply removed.
    ShrinkRange(Range),
}

impl Length {
    /// Returns the _fill factor_ of the [`Length`].
    ///
    /// The _fill factor_ is a relative unit describing how much of the
    /// remaining space should be filled when compared to other elements. It
    /// is only meant to be used by layout engines.
    pub fn fill_factor(&self) -> u16 {
        match self {
            Length::Fill | Length::FillRange(_) => 1,
            Length::FillPortion(factor) | Length::FillPortionRange(factor, _) => *factor,
            Length::Shrink | Length::ShrinkRange(_) | Length::Fixed(_) => 0,
        }
    }

    /// Returns `true` if the [`Length`] is either [`Length::Fill`],
    /// [`Length::FillRange`], [`Length::FillPortion`]
    /// or [`Length::FillPortionRange`].
    pub fn is_fill(&self) -> bool {
        self.fill_factor() != 0
    }

    /// Returns the "fluid" variant of the [`Length`].
    ///
    /// Specifically:
    /// - [`Length::Shrink`] if [`Length::Shrink`], [`Length::ShrinkRange`] or
    ///   [`Length::Fixed`].
    /// - [`Length::Fill`] otherwise.
    pub fn fluid(&self) -> Self {
        match self {
            Length::Fill
            | Length::FillRange(_)
            | Length::FillPortion(_)
            | Length::FillPortionRange(_, _) => Length::Fill,
            Length::Shrink | Length::ShrinkRange(_) | Length::Fixed(_) => Length::Shrink,
        }
    }

    /// Adapts the [`Length`] so it can contain the other [`Length`] and
    /// match its fluidity.
    pub fn enclose(self, other: Length) -> Self {
        match (self, other) {
            (
                Length::Shrink | Length::ShrinkRange(_),
                Length::Fill
                | Length::FillRange(_)
                | Length::FillPortion(_)
                | Length::FillPortionRange(_, _),
            ) => other,
            _ => self,
        }
    }
}

impl From<Pixels> for Length {
    fn from(amount: Pixels) -> Self {
        Length::Fixed(f32::from(amount))
    }
}

impl From<f32> for Length {
    fn from(amount: f32) -> Self {
        Length::Fixed(amount)
    }
}

impl From<u16> for Length {
    fn from(units: u16) -> Self {
        Length::Fixed(f32::from(units))
    }
}

impl From<crate::core::Length> for Length {
    fn from(length: crate::core::Length) -> Self {
        match length {
            crate::core::Length::Shrink => Length::Shrink,
            crate::core::Length::Fixed(value) => Length::Fixed(value),
            crate::core::Length::Fill => Length::Fill,
            crate::core::Length::FillPortion(value) => Length::FillPortion(value),
        }
    }
}

/// Simply remove the limitations from [`Length::FillRange`],
/// [`Length::FillPortionRange`] and [`Length::ShrinkRange`] for functions that
/// do not support limitations on length.
impl From<Length> for crate::core::Length {
    fn from(length: Length) -> Self {
        match length {
            Length::Shrink | Length::ShrinkRange(_) => crate::core::Length::Shrink,
            Length::Fixed(value) => crate::core::Length::Fixed(value),
            Length::Fill | Length::FillRange(_) => crate::core::Length::Fill,
            Length::FillPortion(value) | Length::FillPortionRange(value, _) => {
                crate::core::Length::FillPortion(value)
            }
        }
    }
}