2023-05-04 13:00:16 +02:00
|
|
|
use crate::Pixels;
|
|
|
|
|
|
2019-09-24 15:15:34 +02:00
|
|
|
/// The strategy used to fill space in a specific dimension.
|
2023-02-04 12:24:13 +01:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
2019-09-20 19:15:31 +02:00
|
|
|
pub enum Length {
|
2019-11-18 07:16:19 +01:00
|
|
|
/// Fill all the remaining space
|
2019-09-20 19:15:31 +02:00
|
|
|
Fill,
|
2019-11-18 07:16:19 +01:00
|
|
|
|
2019-12-30 18:37:13 +01:00
|
|
|
/// 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.
|
|
|
|
|
///
|
|
|
|
|
/// `Length::Fill` is equivalent to `Length::FillPortion(1)`.
|
|
|
|
|
FillPortion(u16),
|
|
|
|
|
|
2019-11-18 07:16:19 +01:00
|
|
|
/// Fill the least amount of space
|
2019-09-20 19:15:31 +02:00
|
|
|
Shrink,
|
2019-11-18 07:16:19 +01:00
|
|
|
|
|
|
|
|
/// Fill a fixed amount of space
|
2023-02-04 12:24:13 +01:00
|
|
|
Fixed(f32),
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
2019-11-10 06:05:20 +01:00
|
|
|
|
|
|
|
|
impl Length {
|
2019-11-18 07:16:19 +01:00
|
|
|
/// 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.
|
2019-11-10 06:05:20 +01:00
|
|
|
pub fn fill_factor(&self) -> u16 {
|
|
|
|
|
match self {
|
|
|
|
|
Length::Fill => 1,
|
2019-12-30 18:37:13 +01:00
|
|
|
Length::FillPortion(factor) => *factor,
|
2019-11-10 06:05:20 +01:00
|
|
|
Length::Shrink => 0,
|
2023-02-04 12:24:13 +01:00
|
|
|
Length::Fixed(_) => 0,
|
2019-11-10 06:05:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-01-05 17:24:43 +01:00
|
|
|
|
2025-04-17 16:30:44 +02:00
|
|
|
/// Returns `true` if the [`Length`] is either [`Length::Fill`] or
|
|
|
|
|
/// [`Length::FillPortion`].
|
2024-01-05 17:24:43 +01:00
|
|
|
pub fn is_fill(&self) -> bool {
|
|
|
|
|
self.fill_factor() != 0
|
|
|
|
|
}
|
2024-01-11 06:12:19 +01:00
|
|
|
|
|
|
|
|
/// Returns the "fluid" variant of the [`Length`].
|
|
|
|
|
///
|
|
|
|
|
/// Specifically:
|
|
|
|
|
/// - [`Length::Shrink`] if [`Length::Shrink`] or [`Length::Fixed`].
|
|
|
|
|
/// - [`Length::Fill`] otherwise.
|
2024-02-27 01:21:02 +01:00
|
|
|
pub fn fluid(&self) -> Self {
|
2024-01-11 06:12:19 +01:00
|
|
|
match self {
|
|
|
|
|
Length::Fill | Length::FillPortion(_) => Length::Fill,
|
|
|
|
|
Length::Shrink | Length::Fixed(_) => Length::Shrink,
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-27 01:21:02 +01:00
|
|
|
|
|
|
|
|
/// Adapts the [`Length`] so it can contain the other [`Length`] and
|
|
|
|
|
/// match its fluidity.
|
2025-07-18 03:53:59 +02:00
|
|
|
#[inline]
|
2024-02-27 01:21:02 +01:00
|
|
|
pub fn enclose(self, other: Length) -> Self {
|
|
|
|
|
match (self, other) {
|
|
|
|
|
(Length::Shrink, Length::Fill | Length::FillPortion(_)) => other,
|
|
|
|
|
_ => self,
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-10 06:05:20 +01:00
|
|
|
}
|
2019-11-29 20:52:26 -05:00
|
|
|
|
2023-05-04 13:00:16 +02:00
|
|
|
impl From<Pixels> for Length {
|
|
|
|
|
fn from(amount: Pixels) -> Self {
|
|
|
|
|
Length::Fixed(f32::from(amount))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-04 12:24:13 +01:00
|
|
|
impl From<f32> for Length {
|
|
|
|
|
fn from(amount: f32) -> Self {
|
|
|
|
|
Length::Fixed(amount)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-03 16:45:44 -05:00
|
|
|
impl From<u16> for Length {
|
|
|
|
|
fn from(amount: u16) -> Self {
|
|
|
|
|
Length::Fixed(amount as f32)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-09 06:38:48 +01:00
|
|
|
impl From<u32> for Length {
|
|
|
|
|
fn from(units: u32) -> Self {
|
|
|
|
|
Length::Fixed(units as f32)
|
2019-11-29 20:52:26 -05:00
|
|
|
}
|
|
|
|
|
}
|