From 974929a40dcb5963cddec3c4d562ec47401267b8 Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Sun, 22 Oct 2023 21:20:56 +0100 Subject: [PATCH] Add `BoxGenerationMode` style --- src/style/mod.rs | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/src/style/mod.rs b/src/style/mod.rs index 5a0de16d8..04608c62b 100644 --- a/src/style/mod.rs +++ b/src/style/mod.rs @@ -35,8 +35,11 @@ use crate::util::sys::GridTrackVec; /// but this is a just a convenience to save on boilerplate for styles that your implementation doesn't support. You will need /// to override the default implementation for each style property that your style type actually supports. pub trait CoreStyle { - /// What layout strategy should be used? - // fn display(&self); + /// Which box generation mode should be used + #[inline(always)] + fn box_generation_mode(&self) -> BoxGenerationMode { + BoxGenerationMode::DEFAULT + } // Overflow properties /// How children overflowing their container should affect layout @@ -118,28 +121,34 @@ pub enum Display { /// The children will follow the CSS Grid layout algorithm #[cfg(feature = "grid")] Grid, - /// The children will not be laid out, and will follow absolute positioning + /// The node is hidden, and it's children will also be hidden None, } impl Display { - /// The default of Display. + /// The default Display mode #[cfg(feature = "flexbox")] pub const DEFAULT: Display = Display::Flex; - /// The default of Display. + /// The default Display mode #[cfg(all(feature = "grid", not(feature = "flexbox")))] pub const DEFAULT: Display = Display::Grid; - /// The default of Display. + /// The default Display mode #[cfg(all(feature = "block_layout", not(feature = "flexbox"), not(feature = "grid")))] pub const DEFAULT: Display = Display::Block; - /// The default of Display. + /// The default Display mode #[cfg(all(not(feature = "flexbox"), not(feature = "grid"), not(feature = "block_layout")))] pub const DEFAULT: Display = Display::None; } +impl Default for Display { + fn default() -> Self { + Self::DEFAULT + } +} + impl core::fmt::Display for Display { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { @@ -154,7 +163,23 @@ impl core::fmt::Display for Display { } } -impl Default for Display { +/// An abstracted version of the CSS `display` property where any value other than "none" is represented by "normal" +/// See: +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub enum BoxGenerationMode { + /// The node generates a box in the regular way + Normal, + /// The node and it's descendants generate no boxes (they are hidden) + None, +} + +impl BoxGenerationMode { + /// The default of BoxGenerationMode + pub const DEFAULT: BoxGenerationMode = BoxGenerationMode::Normal; +} + +impl Default for BoxGenerationMode { fn default() -> Self { Self::DEFAULT } @@ -433,6 +458,13 @@ impl Default for Style { } impl CoreStyle for Style { + #[inline(always)] + fn box_generation_mode(&self) -> BoxGenerationMode { + match self.display { + Display::None => BoxGenerationMode::None, + _ => BoxGenerationMode::Normal, + } + } #[inline(always)] fn overflow(&self) -> Point { self.overflow