Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/custom_tree_owned_partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use common::image::{image_measure_function, ImageContext};
use common::text::{text_measure_function, FontMetrics, TextContext, WritingMode, LOREM_IPSUM};
use taffy::{
compute_cached_layout, compute_flexbox_layout, compute_grid_layout, compute_leaf_layout, compute_root_layout,
prelude::*, Cache, CacheTree, Layout, Style,
prelude::*, Cache, CacheTree, DefaultUnits, Layout, Style,
};

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -138,7 +138,7 @@ impl taffy::LayoutPartialTree for Node {
where
Self: 'a;

type CustomIdent = String;
type Units = DefaultUnits;

fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
&self.node_from_id(node_id).style
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_tree_owned_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use taffy::tree::Cache;
use taffy::util::print_tree;
use taffy::{
compute_cached_layout, compute_flexbox_layout, compute_grid_layout, compute_leaf_layout, compute_root_layout,
prelude::*, round_layout, CacheTree,
prelude::*, round_layout, CacheTree, DefaultUnits,
};

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -136,7 +136,7 @@ impl LayoutPartialTree for StatelessLayoutTree {
where
Self: 'a;

type CustomIdent = String;
type Units = DefaultUnits;

fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
unsafe { &node_from_id(node_id).style }
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_tree_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use common::text::{text_measure_function, FontMetrics, TextContext, WritingMode,
use taffy::util::print_tree;
use taffy::{
compute_cached_layout, compute_flexbox_layout, compute_grid_layout, compute_leaf_layout, compute_root_layout,
prelude::*, round_layout, Cache, CacheTree,
prelude::*, round_layout, Cache, CacheTree, DefaultUnits,
};

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -139,7 +139,7 @@ impl taffy::TraversePartialTree for Tree {
impl taffy::TraverseTree for Tree {}

impl taffy::LayoutPartialTree for Tree {
type CustomIdent = String;
type Units = DefaultUnits;

type CoreContainerStyle<'a>
= &'a Style
Expand Down
22 changes: 11 additions & 11 deletions src/compute/grid/explicit_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ mod test {
use crate::compute::grid::util::*;
use crate::geometry::AbsoluteAxis;
use crate::prelude::*;
use crate::sys::DefaultCheapStr;
use crate::DefaultUnits;

#[test]
fn explicit_grid_sizing_no_repeats() {
Expand Down Expand Up @@ -352,7 +352,7 @@ mod test {
#[test]
fn explicit_grid_sizing_auto_fill_exact_fit() {
use RepetitionCount::AutoFill;
let grid_style: Style<DefaultCheapStr> = Style {
let grid_style: Style<DefaultUnits> = Style {
display: Display::Grid,
size: Size { width: length(120.0), height: length(80.0) },
grid_template_columns: vec![repeat(AutoFill, vec![length(40.0)])],
Expand Down Expand Up @@ -383,7 +383,7 @@ mod test {
#[test]
fn explicit_grid_sizing_auto_fill_non_exact_fit() {
use RepetitionCount::AutoFill;
let grid_style: Style<DefaultCheapStr> = Style {
let grid_style: Style<DefaultUnits> = Style {
display: Display::Grid,
size: Size { width: length(140.0), height: length(90.0) },
grid_template_columns: vec![repeat(AutoFill, vec![length(40.0)])],
Expand Down Expand Up @@ -414,7 +414,7 @@ mod test {
#[test]
fn explicit_grid_sizing_auto_fill_min_size_exact_fit() {
use RepetitionCount::AutoFill;
let grid_style: Style<DefaultCheapStr> = Style {
let grid_style: Style<DefaultUnits> = Style {
display: Display::Grid,
min_size: Size { width: length(120.0), height: length(80.0) },
grid_template_columns: vec![repeat(AutoFill, vec![length(40.0)])],
Expand Down Expand Up @@ -445,7 +445,7 @@ mod test {
#[test]
fn explicit_grid_sizing_auto_fill_min_size_non_exact_fit() {
use RepetitionCount::AutoFill;
let grid_style: Style<DefaultCheapStr> = Style {
let grid_style: Style<DefaultUnits> = Style {
display: Display::Grid,
min_size: Size { width: length(140.0), height: length(90.0) },
grid_template_columns: vec![repeat(AutoFill, vec![length(40.0)])],
Expand Down Expand Up @@ -476,7 +476,7 @@ mod test {
#[test]
fn explicit_grid_sizing_auto_fill_multiple_repeated_tracks() {
use RepetitionCount::AutoFill;
let grid_style: Style<DefaultCheapStr> = Style {
let grid_style: Style<DefaultUnits> = Style {
display: Display::Grid,
size: Size { width: length(140.0), height: length(100.0) },
grid_template_columns: vec![repeat(AutoFill, vec![length(40.0), length(20.0)])],
Expand Down Expand Up @@ -507,7 +507,7 @@ mod test {
#[test]
fn explicit_grid_sizing_auto_fill_gap() {
use RepetitionCount::AutoFill;
let grid_style: Style<DefaultCheapStr> = Style {
let grid_style: Style<DefaultUnits> = Style {
display: Display::Grid,
size: Size { width: length(140.0), height: length(100.0) },
grid_template_columns: vec![repeat(AutoFill, vec![length(40.0)])],
Expand Down Expand Up @@ -539,7 +539,7 @@ mod test {
#[test]
fn explicit_grid_sizing_no_defined_size() {
use RepetitionCount::AutoFill;
let grid_style: Style<DefaultCheapStr> = Style {
let grid_style: Style<DefaultUnits> = Style {
display: Display::Grid,
grid_template_columns: vec![repeat(AutoFill, vec![length(40.0), percent(0.5), length(20.0)])],
grid_template_rows: vec![repeat(AutoFill, vec![length(20.0)])],
Expand Down Expand Up @@ -570,7 +570,7 @@ mod test {
#[test]
fn explicit_grid_sizing_mix_repeated_and_non_repeated() {
use RepetitionCount::AutoFill;
let grid_style: Style<DefaultCheapStr> = Style {
let grid_style: Style<DefaultUnits> = Style {
display: Display::Grid,
size: Size { width: length(140.0), height: length(100.0) },
grid_template_columns: vec![length(20.0), repeat(AutoFill, vec![length(40.0)])],
Expand Down Expand Up @@ -602,7 +602,7 @@ mod test {
#[test]
fn explicit_grid_sizing_mix_with_padding() {
use RepetitionCount::AutoFill;
let grid_style: Style<DefaultCheapStr> = Style {
let grid_style: Style<DefaultUnits> = Style {
display: Display::Grid,
size: Size { width: length(120.0), height: length(120.0) },
padding: Rect { left: length(10.0), right: length(10.0), top: length(20.0), bottom: length(20.0) },
Expand Down Expand Up @@ -642,7 +642,7 @@ mod test {
let maxpx100 = MaxTrackSizingFunction::from_length(100.0);

// Setup test
let grid_style: Style<DefaultCheapStr> = Style {
let grid_style: Style<DefaultUnits> = Style {
display: Display::Grid,
gap: length(20.0),
grid_template_columns: vec![length(100.0), minmax(length(100.0), fr(2.0)), fr(1.0)],
Expand Down
6 changes: 3 additions & 3 deletions src/compute/grid/implicit_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! to reduce the number of allocations required when creating a grid.
use crate::geometry::Line;
use crate::style::{GenericGridPlacement, GridPlacement};
use crate::{CheapCloneStr, GridItemStyle};
use crate::{CheapCloneStr, GridItemStyle, Units};
use core::cmp::{max, min};

use super::types::TrackCounts;
Expand Down Expand Up @@ -72,9 +72,9 @@ fn get_known_child_positions<'a, S: GridItemStyle + 'a>(
// Note: that the children reference the lines in between (and around) the tracks not tracks themselves,
// and thus we must subtract 1 to get an accurate estimate of the number of tracks
let (child_col_min, child_col_max, child_col_span) =
child_min_line_max_line_span::<S::CustomIdent>(child_style.grid_column(), explicit_col_count);
child_min_line_max_line_span::<<S::Units as Units>::Str>(child_style.grid_column(), explicit_col_count);
let (child_row_min, child_row_max, child_row_span) =
child_min_line_max_line_span::<S::CustomIdent>(child_style.grid_row(), explicit_row_count);
child_min_line_max_line_span::<<S::Units as Units>::Str>(child_style.grid_row(), explicit_row_count);
col_min = min(col_min, child_col_min);
col_max = max(col_max, child_col_max);
col_max_span = max(col_max_span, child_col_span);
Expand Down
4 changes: 2 additions & 2 deletions src/compute/grid/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::geometry::{AbsoluteAxis, InBothAbsAxis};
use crate::style::{AlignItems, GridAutoFlow, OriginZeroGridPlacement};
use crate::tree::NodeId;
use crate::util::sys::Vec;
use crate::{CoreStyle, GridItemStyle};
use crate::GridItemStyle;

/// 8.5. Grid Item Placement Algorithm
/// Place items into the grid, generating new rows/column into the implicit grid as required
Expand All @@ -20,7 +20,7 @@ pub(super) fn place_grid_items<'a, S, ChildIter>(
grid_auto_flow: GridAutoFlow,
align_items: AlignItems,
justify_items: AlignItems,
named_line_resolver: &NamedLineResolver<<S as CoreStyle>::CustomIdent>,
named_line_resolver: &NamedLineResolver<S::Units>,
) where
S: GridItemStyle + 'a,
ChildIter: Iterator<Item = (usize, NodeId, S)>,
Expand Down
38 changes: 19 additions & 19 deletions src/compute/grid/types/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
CheapCloneStr, GenericGridTemplateComponent, GenericRepetition as _, GridAreaAxis, GridAreaEnd, GridContainerStyle,
GridPlacement, GridTemplateArea, Line, NonNamedGridPlacement, RepetitionCount,
GridPlacement, GridTemplateArea, Line, NonNamedGridPlacement, RepetitionCount, Units,
};
use core::{borrow::Borrow, cmp::Ordering, fmt::Debug};

Expand Down Expand Up @@ -44,15 +44,15 @@ impl<T: CheapCloneStr> Borrow<str> for StrHasher<T> {

/// Resolver that takes grid lines names and area names as input and can then be used to
/// resolve line names of grid placement properties into line numbers.
pub(crate) struct NamedLineResolver<S: CheapCloneStr> {
pub(crate) struct NamedLineResolver<U: Units> {
/// Map of row line names to line numbers. Each line name may correspond to multiple lines
/// so we store a `Vec`
row_lines: Map<StrHasher<S>, Vec<u16>>,
row_lines: Map<StrHasher<U::Str>, Vec<u16>>,
/// Map of column line names to line numbers. Each line name may correspond to multiple lines
/// so we store a `Vec`
column_lines: Map<StrHasher<S>, Vec<u16>>,
column_lines: Map<StrHasher<U::Str>, Vec<u16>>,
/// Map of area names to area definitions (start and end lines numbers in each axis)
areas: Map<StrHasher<S>, GridTemplateArea<S>>,
areas: Map<StrHasher<U::Str>, GridTemplateArea<U::Str>>,
/// Number of columns implied by grid area definitions
area_column_count: u16,
/// Number of rows implied by grid area definitions
Expand All @@ -70,16 +70,16 @@ fn upsert_line_name_map<S: CheapCloneStr>(map: &mut Map<StrHasher<S>, Vec<u16>>,
map.entry(StrHasher(key)).and_modify(|lines| lines.push(value)).or_insert_with(|| single_value_vec(value));
}

impl<S: CheapCloneStr> NamedLineResolver<S> {
impl<U: Units> NamedLineResolver<U> {
/// Create and initialise a new `NamedLineResolver`
pub(crate) fn new(
style: &impl GridContainerStyle<CustomIdent = S>,
style: &impl GridContainerStyle<Units = U>,
column_auto_repetitions: u16,
row_auto_repetitions: u16,
) -> Self {
let mut areas: Map<StrHasher<S>, GridTemplateArea<_>> = Map::new();
let mut column_lines: Map<StrHasher<S>, Vec<u16>> = Map::new();
let mut row_lines: Map<StrHasher<S>, Vec<u16>> = Map::new();
let mut areas: Map<StrHasher<U::Str>, GridTemplateArea<_>> = Map::new();
let mut column_lines: Map<StrHasher<U::Str>, Vec<u16>> = Map::new();
let mut row_lines: Map<StrHasher<U::Str>, Vec<u16>> = Map::new();

let mut area_column_count = 0;
let mut area_row_count = 0;
Expand All @@ -91,13 +91,13 @@ impl<S: CheapCloneStr> NamedLineResolver<S> {
area_column_count = area_column_count.max(area.column_end.max(1) - 1);
area_row_count = area_row_count.max(area.row_end.max(1) - 1);

let col_start_name = S::from(format!("{}-start", area.name.as_ref()));
let col_start_name = U::Str::from(format!("{}-start", area.name.as_ref()));
upsert_line_name_map(&mut column_lines, col_start_name, area.column_start);
let col_end_name = S::from(format!("{}-end", area.name.as_ref()));
let col_end_name = U::Str::from(format!("{}-end", area.name.as_ref()));
upsert_line_name_map(&mut column_lines, col_end_name, area.column_end);
let row_start_name = S::from(format!("{}-start", area.name.as_ref()));
let row_start_name = U::Str::from(format!("{}-start", area.name.as_ref()));
upsert_line_name_map(&mut row_lines, row_start_name, area.row_start);
let row_end_name = S::from(format!("{}-end", area.name.as_ref()));
let row_end_name = U::Str::from(format!("{}-end", area.name.as_ref()));
upsert_line_name_map(&mut row_lines, row_end_name, area.row_end);
}
}
Expand Down Expand Up @@ -197,21 +197,21 @@ impl<S: CheapCloneStr> NamedLineResolver<S> {

/// Resolve named lines for both the `start` and `end` of a row-axis grid placement
#[inline(always)]
pub(crate) fn resolve_row_names(&self, line: &Line<GridPlacement<S>>) -> Line<NonNamedGridPlacement> {
pub(crate) fn resolve_row_names(&self, line: &Line<GridPlacement<U::Str>>) -> Line<NonNamedGridPlacement> {
self.resolve_line_names(line, GridAreaAxis::Row)
}

/// Resolve named lines for both the `start` and `end` of a column-axis grid placement
#[inline(always)]
pub(crate) fn resolve_column_names(&self, line: &Line<GridPlacement<S>>) -> Line<NonNamedGridPlacement> {
pub(crate) fn resolve_column_names(&self, line: &Line<GridPlacement<U::Str>>) -> Line<NonNamedGridPlacement> {
self.resolve_line_names(line, GridAreaAxis::Column)
}

/// Resolve named lines for both the `start` and `end` of a grid placement
#[inline(always)]
pub(crate) fn resolve_line_names(
&self,
line: &Line<GridPlacement<S>>,
line: &Line<GridPlacement<U::Str>>,
axis: GridAreaAxis,
) -> Line<NonNamedGridPlacement> {
let start_holder;
Expand Down Expand Up @@ -293,7 +293,7 @@ impl<S: CheapCloneStr> NamedLineResolver<S> {
/// Resolve the grid line for a named grid line or span
fn find_line_index(
&self,
name: &S,
name: &U::Str,
idx: i16,
axis: GridAreaAxis,
end: GridAreaEnd,
Expand Down Expand Up @@ -390,7 +390,7 @@ impl<S: CheapCloneStr> NamedLineResolver<S> {
}
}

impl<S: CheapCloneStr> Debug for NamedLineResolver<S> {
impl<U: Units> Debug for NamedLineResolver<U> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
writeln!(f, "Grid Areas:")?;
for area in self.areas.values() {
Expand Down
4 changes: 2 additions & 2 deletions src/compute/grid/util/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Helpers for use in unit tests within the grid module
use super::super::OriginZeroLine;
use crate::prelude::*;
use crate::style::{Dimension, GridPlacement, Style};
use crate::{prelude::*, DefaultUnits};

pub(crate) trait CreateParentTestNode {
fn into_grid(self) -> Style;
Expand All @@ -23,7 +23,7 @@ pub(crate) trait CreateChildTestNode {
impl CreateChildTestNode
for (GridPlacement<String>, GridPlacement<String>, GridPlacement<String>, GridPlacement<String>)
{
fn into_grid_child(self) -> Style<String> {
fn into_grid_child(self) -> Style<DefaultUnits> {
Style {
display: Display::Grid,
grid_column: Line { start: self.0, end: self.1 },
Expand Down
2 changes: 1 addition & 1 deletion src/style/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub trait BlockContainerStyle: CoreStyle {
/// Defines which row in the grid the item should start and end at
#[inline(always)]
fn text_align(&self) -> TextAlign {
Style::<Self::CustomIdent>::DEFAULT.text_align
Style::<Self::Units>::DEFAULT.text_align
}
}

Expand Down
Loading
Loading