From 29cfef0a6dfef5820bda339d327638e285aaf4d3 Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Tue, 8 Jun 2021 11:05:09 +0200 Subject: Add a grid layouter --- src/geom/gridu.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/geom/mod.rs | 2 ++ 2 files changed, 75 insertions(+) create mode 100644 src/geom/gridu.rs (limited to 'src/geom') diff --git a/src/geom/gridu.rs b/src/geom/gridu.rs new file mode 100644 index 00000000..70fc17e4 --- /dev/null +++ b/src/geom/gridu.rs @@ -0,0 +1,73 @@ +use super::*; + +/// An enum with the length that a grid cell may have. +#[derive(Copy, Clone, PartialEq, Hash)] +pub enum TrackSizing { + /// A length stated in absolute values and fractions of the parent's size. + Linear(Linear), + /// A length that is the fraction of the remaining free space in the parent. + Fractional(Fractional), + /// The cell will fit its contents. + Auto, +} + +impl TrackSizing { + pub fn is_zero(&self) -> bool { + match self { + Self::Linear(l) => l.is_zero(), + Self::Fractional(f) => f.is_zero(), + Self::Auto => false, + } + } + + pub fn preliminary_length(&self, resolve: Length) -> Length { + match self { + Self::Linear(l) => l.resolve(resolve), + _ => resolve, + } + } +} + +impl Display for TrackSizing { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + match self { + Self::Linear(x) => ::fmt(x, f), + Self::Fractional(x) => ::fmt(x, f), + Self::Auto => write!(f, "auto"), + } + } +} + +impl Debug for TrackSizing { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + match self { + Self::Linear(x) => ::fmt(x, f), + Self::Fractional(x) => ::fmt(x, f), + Self::Auto => write!(f, "auto"), + } + } +} + +impl From for TrackSizing { + fn from(abs: Length) -> Self { + Self::Linear(abs.into()) + } +} + +impl From for TrackSizing { + fn from(rel: Relative) -> Self { + Self::Linear(rel.into()) + } +} + +impl From for TrackSizing { + fn from(lin: Linear) -> Self { + Self::Linear(lin) + } +} + +impl From for TrackSizing { + fn from(fr: Fractional) -> Self { + Self::Fractional(fr) + } +} diff --git a/src/geom/mod.rs b/src/geom/mod.rs index ce8a7276..fdc3980e 100644 --- a/src/geom/mod.rs +++ b/src/geom/mod.rs @@ -7,6 +7,7 @@ mod angle; mod dir; mod fr; mod gen; +mod gridu; mod length; mod linear; mod path; @@ -21,6 +22,7 @@ pub use angle::*; pub use dir::*; pub use fr::*; pub use gen::*; +pub use gridu::*; pub use length::*; pub use linear::*; pub use path::*; -- cgit v1.2.3