这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
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
2 changes: 1 addition & 1 deletion RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

- removed the public `Number` type; a more idiomatic `Option<f32>` is used instead
- the associated public `MinMax` and `OrElse` traits have also been removed; these should never have been public
- `Sprawl::remove` now returns a `Result<usize, Error>`, to indicate if the operation was sucessful, and if it was, which ID was invalidated.
- `Sprawl::remove` now returns a `Result<usize, InvalidNode>`, to indicate if the operation was sucessful, and if it was, which ID was invalidated.
- renamed `taffy::forest::Forest.new-node(..)` `taffy::forest::Forest.new_with_children(..)`
- renamed `taffy::node::Taffy.new-node(..)` -> `taffy::node::Taffy.new_with_children(..)`
- renamed `taffy::style::Style` -> `taffy::style::FlexboxLayout` to more precicely indicate its purpose
Expand Down
212 changes: 107 additions & 105 deletions src/flexbox.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::sys::{new_vec_with_capacity, ChildrenVec, ParentsVec, Vec};
pub(crate) struct NodeData {
/// The layout strategy used by this node
pub(crate) style: FlexboxLayout,
/// The mapping from the Size<Option<f32>> (in real units) to Size<f32> (in points) for this node
/// The mapping from the Size<f32> (in real units) to Size<f32> (in points) for this node
pub(crate) measure: Option<MeasureFunc>,
/// The results of the layout computation
pub(crate) layout: Layout,
Expand Down Expand Up @@ -235,7 +235,7 @@ impl Forest {
}

/// Computes the layout of the `node` and its children
pub(crate) fn compute_layout(&mut self, node: NodeId, size: Size<Option<f32>>) {
pub(crate) fn compute_layout(&mut self, node: NodeId, size: Size<f32>) {
// TODO: It's not clear why this method is distinct
self.compute(node, size)
}
Expand Down
72 changes: 40 additions & 32 deletions src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Geometric primitives useful for layout

use crate::style::{Dimension, FlexDirection};
use core::ops::Add;
use crate::{
math::MaybeMath,
style::{Dimension, FlexDirection},
};

/// An axis-aligned UI rectangle
#[derive(Debug, Copy, Clone, PartialEq)]
Expand All @@ -13,19 +15,19 @@ pub struct Rect<T> {
///
/// The starting edge is the left edge when working with LTR text,
/// and the right edge when working with RTL text.
pub start: T,
pub start: Option<T>,
/// This can represent either the x-coordinate of the ending edge,
/// or the amount of padding on the ending side.
///
/// The ending edge is the right edge when working with LTR text,
/// and the left edge when working with RTL text.
pub end: T,
pub end: Option<T>,
/// This can represent either the y-coordinate of the top edge,
/// or the amount of padding on the top side.
pub top: T,
pub top: Option<T>,
/// This can represent either the y-coordinate of the bottom edge,
/// or the amount of padding on the bottom side.
pub bottom: T,
pub bottom: Option<T>,
}

impl<T> Rect<T> {
Expand All @@ -34,7 +36,7 @@ impl<T> Rect<T> {
/// This is used to transform a `Rect<T>` into a `Rect<R>`.
pub(crate) fn map<R, F>(self, f: F) -> Rect<R>
where
F: Fn(T) -> R,
F: Fn(Option<T>) -> Option<R>,
{
Rect { start: f(self.start), end: f(self.end), top: f(self.top), bottom: f(self.bottom) }
}
Expand All @@ -46,7 +48,7 @@ impl<T> Rect<T> {
/// When applied to the top or bottom sides, the height is used instead.
pub(crate) fn zip_size<R, F, U>(self, size: Size<U>, f: F) -> Rect<R>
where
F: Fn(T, U) -> R,
F: Fn(Option<T>, Option<U>) -> Option<R>,
U: Copy,
{
Rect {
Expand All @@ -60,24 +62,26 @@ impl<T> Rect<T> {

impl<T> Rect<T>
where
T: Add<Output = T> + Copy + Clone,
T: MaybeMath<T, Option<T>> + Copy + Clone,
T: MaybeMath<Option<T>, Option<T>> + Copy + Clone,
// T: MaybeMath<Option<T>, T> + Copy + Clone,
{
/// The sum of [`Rect.start`](Rect) and [`Rect.end`](Rect)
///
/// This is typically used when computing total padding.
///
/// **NOTE:** this is *not* the width of the rectangle.
pub(crate) fn horizontal_axis_sum(&self) -> T {
self.start + self.end
pub(crate) fn horizontal_axis_sum(&self) -> Option<T> {
self.start.and_then(|start| start.maybe_add(self.end))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With MaybeMath I should be able to just use self.start.maybe_add(self.end) here (?) but not sure exactly how to get that to work.

Traits this involved are new to me, so bear with me while I try to figure this out 🙃

}

/// The sum of [`Rect.top`](Rect) and [`Rect.bottom`](Rect)
///
/// This is typically used when computing total padding.
///
/// **NOTE:** this is *not* the height of the rectangle.
pub(crate) fn vertical_axis_sum(&self) -> T {
self.top + self.bottom
pub(crate) fn vertical_axis_sum(&self) -> Option<T> {
self.top.and_then(|top| top.maybe_add(self.bottom))
}

/// The sum of the two fields of the [`Rect`] representing the main axis.
Expand All @@ -86,7 +90,7 @@ where
///
/// If the [`FlexDirection`] is [`FlexDirection::Row`] or [`FlexDirection::RowReverse`], this is [`Rect::horizontal`].
/// Otherwise, this is [`Rect::vertical`].
pub(crate) fn main_axis_sum(&self, direction: FlexDirection) -> T {
pub(crate) fn main_axis_sum(&self, direction: FlexDirection) -> Option<T> {
if direction.is_row() {
self.horizontal_axis_sum()
} else {
Expand All @@ -98,7 +102,7 @@ where
///
/// If the [`FlexDirection`] is [`FlexDirection::Row`] or [`FlexDirection::RowReverse`], this is [`Rect::vertical`].
/// Otherwise, this is [`Rect::horizontal`].
pub(crate) fn cross_axis_sum(&self, direction: FlexDirection) -> T {
pub(crate) fn cross_axis_sum(&self, direction: FlexDirection) -> Option<T> {
if direction.is_row() {
self.vertical_axis_sum()
} else {
Expand All @@ -112,7 +116,7 @@ where
T: Copy + Clone,
{
/// The `start` or `top` value of the [`Rect`], from the perspective of the main layout axis
pub(crate) fn main_start(&self, direction: FlexDirection) -> T {
pub(crate) fn main_start(&self, direction: FlexDirection) -> Option<T> {
if direction.is_row() {
self.start
} else {
Expand All @@ -121,7 +125,7 @@ where
}

/// The `end` or `bottom` value of the [`Rect`], from the perspective of the main layout axis
pub(crate) fn main_end(&self, direction: FlexDirection) -> T {
pub(crate) fn main_end(&self, direction: FlexDirection) -> Option<T> {
if direction.is_row() {
self.end
} else {
Expand All @@ -130,7 +134,7 @@ where
}

/// The `start` or `top` value of the [`Rect`], from the perspective of the cross layout axis
pub(crate) fn cross_start(&self, direction: FlexDirection) -> T {
pub(crate) fn cross_start(&self, direction: FlexDirection) -> Option<T> {
if direction.is_row() {
self.top
} else {
Expand All @@ -139,7 +143,7 @@ where
}

/// The `end` or `bottom` value of the [`Rect`], from the perspective of the main layout axis
pub(crate) fn cross_end(&self, direction: FlexDirection) -> T {
pub(crate) fn cross_end(&self, direction: FlexDirection) -> Option<T> {
if direction.is_row() {
self.bottom
} else {
Expand All @@ -148,21 +152,22 @@ where
}
}

// 2 dimensional
/// The width and height of a [`Rect`]
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct Size<T> {
/// The x extent of the rectangle
pub width: T,
pub width: Option<T>,
/// The y extent of the rectangle
pub height: T,
pub height: Option<T>,
}

impl Size<()> {
/// Generates a `Size<Option<f32>>` with undefined width and height
/// Generates a `Size<f32>` with undefined width and height
#[must_use]
pub fn undefined() -> Size<Option<f32>> {
pub fn undefined() -> Size<f32> {
Size { width: None, height: None }
}
}
Expand All @@ -173,15 +178,15 @@ impl<T> Size<T> {
/// This is used to transform a `Rect<T>` into a `Rect<R>`.
pub fn map<R, F>(self, f: F) -> Size<R>
where
F: Fn(T) -> R,
F: Fn(Option<T>) -> Option<R>,
{
Size { width: f(self.width), height: f(self.height) }
}

/// Sets the extent of the main layout axis
///
/// Whether this is the width or height depends on the `direction` provided
pub(crate) fn set_main(&mut self, direction: FlexDirection, value: T) {
pub(crate) fn set_main(&mut self, direction: FlexDirection, value: Option<T>) {
if direction.is_row() {
self.width = value
} else {
Expand All @@ -192,7 +197,7 @@ impl<T> Size<T> {
/// Sets the extent of the cross layout axis
///
/// Whether this is the width or height depends on the `direction` provided
pub(crate) fn set_cross(&mut self, direction: FlexDirection, value: T) {
pub(crate) fn set_cross(&mut self, direction: FlexDirection, value: Option<T>) {
if direction.is_row() {
self.height = value
} else {
Expand All @@ -203,7 +208,7 @@ impl<T> Size<T> {
/// Gets the extent of the main layout axis
///
/// Whether this is the width or height depends on the `direction` provided
pub(crate) fn main(self, direction: FlexDirection) -> T {
pub(crate) fn main(self, direction: FlexDirection) -> Option<T> {
if direction.is_row() {
self.width
} else {
Expand All @@ -214,7 +219,7 @@ impl<T> Size<T> {
/// Gets the extent of the cross layout axis
///
/// Whether this is the width or height depends on the `direction` provided
pub(crate) fn cross(self, direction: FlexDirection) -> T {
pub(crate) fn cross(self, direction: FlexDirection) -> Option<T> {
if direction.is_row() {
self.height
} else {
Expand All @@ -224,17 +229,20 @@ impl<T> Size<T> {
}

impl Size<f32> {
/// A [`Size`] with zero width and height
/// A [`Size<f32>`] with zero width and height
#[must_use]
pub fn zero() -> Self {
Self { width: 0.0, height: 0.0 }
Self { width: Some(0.0), height: Some(0.0) }
}
}

impl Size<Dimension> {
/// Converts any `parent`-relative values for size into an absolute size
pub(crate) fn resolve(&self, parent: Size<Option<f32>>) -> Size<Option<f32>> {
Size { width: self.width.resolve(parent.width), height: self.height.resolve(parent.height) }
pub(crate) fn resolve(&self, parent: Size<f32>) -> Size<f32> {
Size {
width: if let Some(width) = self.width { width.resolve(parent.width) } else { None },
height: if let Some(height) = self.height { height.resolve(parent.height) } else { None },
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ impl Layout {
#[derive(Debug, Clone)]
pub(crate) struct Cache {
/// The initial cached size of the node itself
pub(crate) node_size: Size<Option<f32>>,
pub(crate) node_size: Size<f32>,
/// The initial cached size of the parent's node
pub(crate) parent_size: Size<Option<f32>>,
pub(crate) parent_size: Size<f32>,
/// Whether or not layout should be recomputed
pub(crate) perform_layout: bool,

Expand Down
8 changes: 8 additions & 0 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl MaybeMath<Option<f32>, Option<f32>> for Option<f32> {
match (self, rhs) {
(Some(l), Some(r)) => Some(l.min(r)),
(Some(_l), None) => self,
(None, Some(_r)) => rhs,
(None, Some(_r)) => None,
(None, None) => None,
}
Expand Down Expand Up @@ -121,3 +122,10 @@ impl MaybeMath<Option<f32>, f32> for f32 {
}
}
}

pub(crate) trait MaybeAxisSum<In, Out> {
fn horizontal_axis_sum(self, rhs: In) -> Out;
fn vertical_axis_sum(self, rhs: In) -> Out;
fn main_axis_sum(self, rhs: In) -> Out;
fn cross_axis_sum(self, rhs: In) -> Out;
}
8 changes: 4 additions & 4 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ use crate::sys::Box;
use crate::sys::{new_map_with_capacity, ChildrenVec, Map, Vec};
use core::sync::atomic::{AtomicUsize, Ordering};

/// A function that can be applied to a `Size<Option<f32>>` to obtain a `Size<f32>`
/// A function that can be applied to a `Size<f32>` to obtain a `Size<f32>`
pub enum MeasureFunc {
/// Stores an unboxed function
Raw(fn(Size<Option<f32>>) -> Size<f32>),
Raw(fn(Size<f32>) -> Size<f32>),
/// Stores a boxed function
#[cfg(any(feature = "std", feature = "alloc"))]
Boxed(Box<dyn Fn(Size<Option<f32>>) -> Size<f32>>),
Boxed(Box<dyn Fn(Size<f32>) -> Size<f32>>),
}

/// Global taffy instance id allocator.
Expand Down Expand Up @@ -287,7 +287,7 @@ impl Taffy {
}

/// Updates the stored layout of the provided `node` and its children
pub fn compute_layout(&mut self, node: Node, size: Size<Option<f32>>) -> Result<(), error::InvalidNode> {
pub fn compute_layout(&mut self, node: Node, size: Size<f32>) -> Result<(), error::InvalidNode> {
let id = self.find_node(node)?;
self.forest.compute_layout(id, size);
Ok(())
Expand Down
Loading