Skip to content

Commit

Permalink
Create style traits
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Oct 22, 2023
1 parent d4374b9 commit 9d39190
Show file tree
Hide file tree
Showing 3 changed files with 399 additions and 8 deletions.
72 changes: 72 additions & 0 deletions src/style/flex.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,76 @@
//! Style types for Flexbox layout
use super::{AlignContent, AlignItems, AlignSelf, CoreStyle, Dimension, LengthPercentage, Style};
use crate::geometry::Size;

/// The set of styles required for a Flexbox container
pub trait FlexboxContainerStyle: CoreStyle {
/// Which direction does the main axis flow in?
#[inline(always)]
fn flex_direction(&self) -> FlexDirection {
Style::DEFAULT.flex_direction
}
/// Should elements wrap, or stay in a single line?
#[inline(always)]
fn flex_wrap(&self) -> FlexWrap {
Style::DEFAULT.flex_wrap
}

/// How large should the gaps between items in a grid or flex container be?
#[inline(always)]
fn gap(&self) -> Size<LengthPercentage> {
Style::DEFAULT.gap
}

// Alignment properties

/// How should content contained within this item be aligned in the cross/block axis
#[inline(always)]
fn align_content(&self) -> Option<AlignContent> {
Style::DEFAULT.align_content
}
/// How this node's children aligned in the cross/block axis?
#[inline(always)]
fn align_items(&self) -> Option<AlignItems> {
Style::DEFAULT.align_items
}
/// How this node's children should be aligned in the inline axis
#[inline(always)]
fn justify_items(&self) -> Option<AlignItems> {
Style::DEFAULT.justify_items
}
}

/// The set of styles required for a Flexbox item (child of a Flexbox container)
pub trait FlexboxItemStyle: CoreStyle {
/// Sets the initial main axis size of the item
#[inline(always)]
fn flex_basis(&self) -> Dimension {
Style::DEFAULT.flex_basis
}
/// The relative rate at which this item grows when it is expanding to fill space
#[inline(always)]
fn flex_grow(&self) -> f32 {
Style::DEFAULT.flex_grow
}
/// The relative rate at which this item shrinks when it is contracting to fit into space
#[inline(always)]
fn flex_shrink(&self) -> f32 {
Style::DEFAULT.flex_shrink
}

/// How this node should be aligned in the cross/block axis
/// Falls back to the parents [`AlignItems`] if not set
#[inline(always)]
fn align_self(&self) -> Option<AlignSelf> {
Style::DEFAULT.align_self
}
/// How this node should be aligned in the inline axis
/// Falls back to the parents [`super::JustifyItems`] if not set
#[inline(always)]
fn justify_self(&self) -> Option<AlignSelf> {
Style::DEFAULT.justify_self
}
}

/// Controls whether flex items are forced onto one line or can wrap onto multiple lines.
///
Expand Down
90 changes: 87 additions & 3 deletions src/style/grid.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,97 @@
//! Style types for CSS Grid layout
use super::{AlignContent, LengthPercentage, Style};
use super::{AlignContent, AlignItems, AlignSelf, CoreStyle, JustifyContent, LengthPercentage, Style};
use crate::compute::grid::{GridCoordinate, GridLine, OriginZeroLine};
use crate::geometry::{AbsoluteAxis, AbstractAxis};
use crate::geometry::{Line, MinMax};
use crate::geometry::{AbsoluteAxis, AbstractAxis, Line, MinMax, Size};
use crate::style_helpers::*;
use crate::util::sys::GridTrackVec;
use core::cmp::{max, min};
use core::convert::Infallible;

/// The set of styles required for a CSS Grid container
pub trait GridContainerStyle: CoreStyle {
/// Defines the track sizing functions (heights) of the grid rows
#[inline(always)]
fn grid_template_rows(&self) -> &[TrackSizingFunction] {
&[]
}
/// Defines the track sizing functions (widths) of the grid columns
#[inline(always)]
fn grid_template_columns(&self) -> &[TrackSizingFunction] {
&[]
}
/// Defines the size of implicitly created rows
#[inline(always)]
fn grid_auto_rows(&self) -> &[NonRepeatedTrackSizingFunction] {
&[]
}
/// Defined the size of implicitly created columns
#[inline(always)]
fn grid_auto_columns(&self) -> &[NonRepeatedTrackSizingFunction] {
&[]
}
/// Controls how items get placed into the grid for auto-placed items
#[inline(always)]
fn grid_auto_flow(&self) -> GridAutoFlow {
Style::DEFAULT.grid_auto_flow
}

/// How large should the gaps between items in a grid or flex container be?
#[inline(always)]
fn gap(&self) -> Size<LengthPercentage> {
Style::DEFAULT.gap
}

// Alignment properties

/// How should content contained within this item be aligned in the cross/block axis
#[inline(always)]
fn align_content(&self) -> Option<AlignContent> {
Style::DEFAULT.align_content
}
/// How should contained within this item be aligned in the main/inline axis
#[inline(always)]
fn justify_content(&self) -> Option<JustifyContent> {
Style::DEFAULT.justify_content
}
/// How this node's children aligned in the cross/block axis?
#[inline(always)]
fn align_items(&self) -> Option<AlignItems> {
Style::DEFAULT.align_items
}
/// How this node's children should be aligned in the inline axis
#[inline(always)]
fn justify_items(&self) -> Option<AlignItems> {
Style::DEFAULT.justify_items
}
}

/// The set of styles required for a CSS Grid item (child of a CSS Grid container)
pub trait GridItemStyle: CoreStyle {
/// Defines which row in the grid the item should start and end at
#[inline(always)]
fn grid_row(&self) -> Line<GridPlacement> {
Style::DEFAULT.grid_row
}
/// Defines which column in the grid the item should start and end at
#[inline(always)]
fn grid_column(&self) -> Line<GridPlacement> {
Style::DEFAULT.grid_column
}

/// How this node should be aligned in the cross/block axis
/// Falls back to the parents [`AlignItems`] if not set
#[inline(always)]
fn align_self(&self) -> Option<AlignSelf> {
Style::DEFAULT.align_self
}
/// How this node should be aligned in the inline axis
/// Falls back to the parents [`super::JustifyItems`] if not set
#[inline(always)]
fn justify_self(&self) -> Option<AlignSelf> {
Style::DEFAULT.justify_self
}
}

/// Controls whether grid items are placed row-wise or column-wise. And whether the sparse or dense packing algorithm is used.
///
/// The "dense" packing algorithm attempts to fill in holes earlier in the grid, if smaller items come up later. This may cause items to appear out-of-order, when doing so would fill in holes left by larger items.
Expand Down
Loading

0 comments on commit 9d39190

Please sign in to comment.