Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uses the sum of border and padding if it's greater than the container definite size #651

Closed
wants to merge 1 commit into from
Closed
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
44 changes: 42 additions & 2 deletions src/compute/flexbox.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Computes the [flexbox](https://css-tricks.com/snippets/css/a-guide-to-flexbox/) layout algorithm on [`TaffyTree`](crate::TaffyTree) according to the [spec](https://www.w3.org/TR/css-flexbox-1/)
use core::ops::Add;

use crate::compute::common::alignment::compute_alignment_offset;
use crate::geometry::{Line, Point, Rect, Size};
use crate::style::{
Expand Down Expand Up @@ -171,7 +173,17 @@ pub fn compute_flexbox_layout(tree: &mut impl LayoutPartialTree, node: NodeId, i
(Some(min), Some(max)) if max <= min => Some(min),
_ => None,
});
let styled_based_known_dimensions = known_dimensions.or(min_max_definite_size).or(clamped_style_size);

// if the sum of the padding and border is greater than the size of the container, the
// the result overwrites the size of the container
let padding_border_sum = style
.padding
.resolve_or_zero(parent_size.width)
.sum_axes()
.add(style.border.resolve_or_zero(parent_size.width).sum_axes());

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

// 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 @@ -2167,9 +2179,10 @@ mod tests {

use crate::{
geometry::Size,
prelude::{length, TaffyMaxContent},
style::{FlexWrap, Style},
util::{MaybeMath, ResolveOrZero},
TaffyTree,
Rect, TaffyTree,
};

// Make sure we get correct constants
Expand Down Expand Up @@ -2208,4 +2221,31 @@ mod tests {
assert_eq!(constants.container_size, Size::zero());
assert_eq!(constants.inner_container_size, Size::zero());
}

#[test]
pub fn test_padding_and_border_larger_than_definite_size() {
let mut tree: TaffyTree<()> = TaffyTree::with_capacity(16);

let child = tree.new_leaf(Style::default()).unwrap();

let root = tree
.new_with_children(
Style {
size: Size { width: length(10.0), height: length(10.0) },
padding: Rect { left: length(10.0), right: length(10.0), top: length(10.0), bottom: length(10.0) },

border: Rect { left: length(10.0), right: length(10.0), top: length(10.0), bottom: length(10.0) },
..Default::default()
},
&[child],
)
.unwrap();

tree.compute_layout(root, Size::MAX_CONTENT).unwrap();

let layout = tree.layout(root).unwrap();

assert_eq!(layout.size.width, 40.0);
assert_eq!(layout.size.height, 40.0);
}
}