Skip to content

Commit

Permalink
Merge main into calc
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoISnoTarget committed Jul 11, 2024
1 parent 659c4fb commit 53a6378
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 59 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ env:
CARGO_REGISTRIES_CRATES_IO_PROTOCOL: "sparse"

jobs:

# MSRV check.
# Taffy only guarantees "latest stable". However we have this check here to ensure that we advertise
# our MSRV. We also make an effort not to increase MSRV in patch versions of Taffy.
Expand Down Expand Up @@ -161,16 +160,21 @@ jobs:
components: clippy
- run: cargo +nightly clippy --workspace -- -D warnings

# Run rustdoc with the `docsrs` cfg to ensure that the documentation is compatible with docs.rs.
# This enables the doc_auto_cfg feature, which requires the nightly toolchain.
# Also checks that the examples can be scraped.
doc:
name: Documentation
runs-on: ubuntu-latest
env:
RUSTDOCFLAGS: "--cfg docsrs"
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
toolchain: nightly
components: clippy
- run: cargo doc
- run: cargo +nightly doc -Zunstable-options -Zrustdoc-scrape-examples --all-features --no-deps

markdownlint:
name: Markdown Lint
Expand All @@ -180,7 +184,7 @@ jobs:
- name: Run Markdown Lint
uses: DavidAnson/markdownlint-cli2-action@v16
with:
globs: '**/*.md'
globs: "**/*.md"

fixture-format:
name: Test Fixture Formatting
Expand All @@ -207,7 +211,7 @@ jobs:
- run: cargo xbench --no-run
name: Build benchmarks
env:
RUSTFLAGS: "-C opt-level=0"
RUSTFLAGS: "-C opt-level=0"

benchmarks-with-yoga:
name: Build Benchmarks (w/ yoga)
Expand All @@ -218,4 +222,4 @@ jobs:
- run: cargo xbench --no-run --features yoga
name: Build benchmarks (w/yoga)
env:
RUSTFLAGS: "-C opt-level=0"
RUSTFLAGS: "-C opt-level=0"
71 changes: 50 additions & 21 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "taffy"
version = "0.5.1"
version = "0.5.2"
authors = [
"Alice Cecile <[email protected]>",
"Johnathan Kelley <[email protected]>",
"Nico Burns <[email protected]>",
]
edition = "2021"
rust-version = "1.65"
include = ["src/**/*", "Cargo.toml", "README.md"]
include = ["src/**/*", "examples/**/*", "Cargo.toml", "README.md"]
description = "A flexible UI layout library "
repository = "https://github.com/DioxusLabs/taffy"
keywords = ["cross-platform", "layout", "flexbox", "css-grid", "grid"]
Expand All @@ -17,50 +17,69 @@ license = "MIT"

[dependencies]
arrayvec = { version = "0.7", default-features = false }
document-features = { version = "0.2.7", optional = true }
num-traits = { version = "0.2", default-features = false }
serde = { version = "1.0", default-features = false, optional = true, features = ["rc", "serde_derive"] }
serde = { version = "1.0", default-features = false, optional = true, features = [
"serde_derive", "rc"
] }
slotmap = { version = "1.0.6", default-features = false, optional = true }
grid = { version = "0.14.0", default-features = false, optional = true }
sptr = { version = "0.3.2", optional = true }

### FEATURES #################################################################
[package.metadata.docs.rs]
# To test all the documentation related features, run:
# RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc -Zunstable-options -Zrustdoc-scrape-examples --all-features --no-deps --open

[features]
default = ["std", "taffy_tree", "flexbox", "grid", "block_layout", "content_size", "calc"]
all-features = true
# see https://doc.rust-lang.org/nightly/rustdoc/scraped-examples.html
cargo-args = ["-Zunstable-options", "-Zrustdoc-scrape-examples"]
rustdoc-args = ["--cfg", "docsrs"]

### Algorithms
[features]
default = [
"std",
"taffy_tree",
"flexbox",
"grid",
"block_layout",
"content_size",
"calc",
]
#! ## Feature Flags
#!
#! ### Algorithms

# Enables the Block layout algorithm
## Enables the Block layout algorithm. See [`compute_block_layout`](crate::compute_block_layout).
block_layout = []
# Enables the Flexbox layout algorithm
## Enables the Flexbox layout algorithm. See [`compute_flexbox_layout`](crate::compute_flexbox_layout).
flexbox = []
# Enables the CSS Grid layout algorithm
## Enables the CSS Grid layout algorithm. See [`compute_grid_layout`](crate::compute_grid_layout).
grid = ["alloc", "dep:grid"]
# Causes all algorithms to compute and output a content size for each node
## Causes all algorithms to compute and output a content size for each node
content_size = []

### Taffy Tree
#! ### Taffy Tree

# Enable the built-in Taffy node tree
## Enable the built-in Taffy node tree. See [`TaffyTree`](crate::TaffyTree).
taffy_tree = ["dep:slotmap"]

### Other
#! ### Other

# Add css calc to sizes
calc = ["alloc", "dep:sptr"]
# Add serde derives to Style structs
## Add [`serde`] derives to Style structs
serde = ["dep:serde"]
# Allow Taffy to depend on the standard library
## Allow Taffy to depend on the [`Rust Standard Library`](std)
std = ["num-traits/std", "grid?/std", "serde?/std", "slotmap?/std"]
# Allow Taffy to depend on the alloc library
## Allow Taffy to depend on the alloc library
alloc = ["serde?/alloc"]
# Internal feature for debugging
## Internal feature for debugging
debug = ["std"]
# Internal feature for profiling
## Internal feature for profiling
profile = ["std"]

[dev-dependencies]
cosmic-text = "0.11.2"
cosmic-text = "0.12"
serde_json = "1.0.93"

# Enable default features for tests and examples
Expand All @@ -75,5 +94,15 @@ name = "dummy_benchmark"
path = "benches/dummy_benchmark.rs"
harness = false

[[example]]
name = "basic"
# This causes all the examples to be scraped for documentation, not just the basic example
doc-scrape-examples = true

[workspace]
members = ["scripts/gentest", "scripts/format-fixtures", "scripts/import-yoga-tests", "benches"]
members = [
"scripts/gentest",
"scripts/format-fixtures",
"scripts/import-yoga-tests",
"benches",
]
8 changes: 8 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Release Notes

## 0.5.2

- Fix block stretch sizing (don't always apply stretch sizing to block containers) (#674)
- Fix computation of intrinsic main size when it depends on a child's known cross size (#673)
- Fix panic when GridLine 0 is specified (#671)
- Docs: Document feature flags and scrape examples (#672)
- Docs: Update cosmic-text example to cosmic-text 0.12 (#670)

## 0.5.1

- Fix: Clamp block item stretch widths by their min and max width (#664)
Expand Down
12 changes: 6 additions & 6 deletions examples/cosmic_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct CosmicTextContext {
impl CosmicTextContext {
fn new(metrics: Metrics, text: &str, attrs: Attrs, font_system: &mut FontSystem) -> Self {
let mut buffer = Buffer::new_empty(metrics);
buffer.set_size(font_system, f32::INFINITY, f32::INFINITY);
buffer.set_size(font_system, None, None);
buffer.set_text(font_system, text, attrs, Shaping::Advanced);
Self { buffer }
}
Expand All @@ -26,12 +26,12 @@ impl CosmicTextContext {
font_system: &mut FontSystem,
) -> taffy::Size<f32> {
// Set width constraint
let width_constraint = known_dimensions.width.unwrap_or_else(|| match available_space.width {
AvailableSpace::MinContent => 0.0,
AvailableSpace::MaxContent => f32::INFINITY,
AvailableSpace::Definite(width) => width,
let width_constraint = known_dimensions.width.or_else(|| match available_space.width {
AvailableSpace::MinContent => Some(0.0),
AvailableSpace::MaxContent => None,
AvailableSpace::Definite(width) => Some(width),
});
self.buffer.set_size(font_system, width_constraint, f32::INFINITY);
self.buffer.set_size(font_system, width_constraint, None);

// Compute layout
self.buffer.shape_until_scroll(font_system, false);
Expand Down
18 changes: 7 additions & 11 deletions src/compute/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ struct BlockItem {

/// Computes the layout of [`LayoutPartialTree`] according to the block layout algorithm
pub fn compute_block_layout(tree: &mut impl LayoutPartialTree, node_id: NodeId, inputs: LayoutInput) -> LayoutOutput {
let LayoutInput { known_dimensions, parent_size, available_space, run_mode, .. } = inputs;
let LayoutInput { known_dimensions, parent_size, run_mode, .. } = inputs;
let style = tree.get_style(node_id);

// Pull these out earlier to avoid borrowing issues
let aspect_ratio = style.aspect_ratio;
let margin = style.margin.resolve_or_zero(parent_size.width);
let min_size = style.min_size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio);
let max_size = style.max_size.maybe_resolve(parent_size).maybe_apply_aspect_ratio(aspect_ratio);
let padding = style.padding.resolve_or_zero(parent_size.width);
Expand All @@ -81,15 +80,8 @@ pub fn compute_block_layout(tree: &mut impl LayoutPartialTree, node_id: NodeId,
_ => None,
});

// Block nodes automatically stretch fit their width to fit available space if available space is definite
let available_space_based_size =
Size { width: available_space.width.into_option().maybe_sub(margin.horizontal_axis_sum()), height: None };

let styled_based_known_dimensions = known_dimensions
.or(min_max_definite_size)
.or(clamped_style_size)
.or(available_space_based_size)
.maybe_max(padding_border_size);
let styled_based_known_dimensions =
known_dimensions.or(min_max_definite_size).or(clamped_style_size).maybe_max(padding_border_size);

// Short-circuit layout if the container's size is fully determined by the container's size and the run mode
// is ComputeSize (and thus the container's size is all that we're interested in)
Expand Down Expand Up @@ -361,6 +353,8 @@ fn perform_final_layout_on_in_flow_children(
let known_dimensions = item
.size
.map_width(|width| {
// TODO: Allow stretch-sizing to be conditional, as there are exceptions.
// e.g. Table children of blocks do not stretch fit
Some(
width
.unwrap_or(container_inner_width - item_non_auto_x_margin_sum)
Expand Down Expand Up @@ -444,6 +438,7 @@ fn perform_final_layout_on_in_flow_children(
location,
padding: item.padding,
border: item.border,
margin: resolved_margin,
},
);

Expand Down Expand Up @@ -669,6 +664,7 @@ fn perform_absolute_layout_on_absolute_children(
location,
padding,
border,
margin: resolved_margin,
},
);

Expand Down
23 changes: 21 additions & 2 deletions src/compute/flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ pub fn compute_flexbox_layout(tree: &mut impl LayoutPartialTree, node: NodeId, i
}
}

debug_log!("FLEX: single-pass");
debug_log!("FLEX:", dbg:style.flex_direction);
compute_preliminary(tree, node, LayoutInput { known_dimensions: styled_based_known_dimensions, ..inputs })
}

Expand Down Expand Up @@ -657,6 +657,7 @@ fn determine_flex_base_size(
)
.with_cross(dir, cross_axis_available_space);

debug_log!("COMPUTE CHILD BASE SIZE:");
break 'flex_basis tree.measure_child_size(
child.node,
child_known_dimensions,
Expand Down Expand Up @@ -707,6 +708,7 @@ fn determine_flex_base_size(
let min_content_main_size = {
let child_available_space = Size::MIN_CONTENT.with_cross(dir, cross_axis_available_space);

debug_log!("COMPUTE CHILD MIN SIZE:");
tree.measure_child_size(
child.node,
child_known_dimensions,
Expand Down Expand Up @@ -917,11 +919,26 @@ fn determine_container_main_size(

let child_available_space = available_space.with_cross(dir, cross_axis_available_space);

// Known dimensions for child sizing
let child_known_dimensions = {
let mut ckd = item.size.with_main(dir, None);
if item.align_self == AlignSelf::Stretch && ckd.cross(dir).is_none() {
ckd.set_cross(
dir,
cross_axis_available_space
.into_option()
.maybe_sub(item.margin.cross_axis_sum(dir)),
);
}
ckd
};

// Either the min- or max- content size depending on which constraint we are sizing under.
// TODO: Optimise by using already computed values where available
debug_log!("COMPUTE CHILD BASE SIZE (for intrinsic main size):");
let content_main_size = tree.measure_child_size(
item.node,
Size::NONE,
child_known_dimensions,
constants.node_inner_size,
child_available_space,
SizingMode::InherentSize,
Expand Down Expand Up @@ -1800,6 +1817,7 @@ fn calculate_flex_item(
location,
padding: item.padding,
border: item.border,
margin: item.margin,
},
);

Expand Down Expand Up @@ -2131,6 +2149,7 @@ fn perform_absolute_layout_on_absolute_children(
location,
padding,
border,
margin: resolved_margin,
},
);

Expand Down
11 changes: 7 additions & 4 deletions src/compute/grid/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ pub(super) fn align_and_position_item(
// Resolve final size
let Size { width, height } = Size { width, height }.unwrap_or(layout_output.size).maybe_clamp(min_size, max_size);

let x = align_item_within_area(
let (x, x_margin) = align_item_within_area(
Line { start: grid_area.left, end: grid_area.right },
justify_self.unwrap_or(alignment_styles.horizontal),
width,
Expand All @@ -198,7 +198,7 @@ pub(super) fn align_and_position_item(
margin.horizontal_components(),
0.0,
);
let y = align_item_within_area(
let (y, y_margin) = align_item_within_area(
Line { start: grid_area.top, end: grid_area.bottom },
align_self.unwrap_or(alignment_styles.vertical),
height,
Expand All @@ -213,6 +213,8 @@ pub(super) fn align_and_position_item(
height: if overflow.x == Overflow::Scroll { scrollbar_width } else { 0.0 },
};

let resolved_margin = Rect { left: x_margin.start, right: x_margin.end, top: y_margin.start, bottom: y_margin.end };

tree.set_unrounded_layout(
node,
&Layout {
Expand All @@ -224,6 +226,7 @@ pub(super) fn align_and_position_item(
scrollbar_size,
padding,
border,
margin: resolved_margin,
},
);

Expand All @@ -245,7 +248,7 @@ pub(super) fn align_item_within_area(
inset: Line<Option<f32>>,
margin: Line<Option<f32>>,
baseline_shim: f32,
) -> f32 {
) -> (f32, Line<f32>) {
// Calculate grid area dimension in the axis
let non_auto_margin = Line { start: margin.start.unwrap_or(0.0) + baseline_shim, end: margin.end.unwrap_or(0.0) };
let grid_area_size = f32_max(grid_area.end - grid_area.start, 0.0);
Expand Down Expand Up @@ -286,5 +289,5 @@ pub(super) fn align_item_within_area(
start += inset.start.or(inset.end.map(|pos| -pos)).unwrap_or(0.0);
}

start
(start, resolved_margin)
}
Loading

0 comments on commit 53a6378

Please sign in to comment.