Skip to content

Commit

Permalink
Add BoxGenerationMode style
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Oct 22, 2023
1 parent 412c075 commit dd0c5dd
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions src/style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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: <https://www.w3.org/TR/css-display-3/#box-generation>
#[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
}
Expand Down Expand Up @@ -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<Overflow> {
self.overflow
Expand Down

0 comments on commit dd0c5dd

Please sign in to comment.