use crate::{
AbsoluteLength, App, Bounds, DefiniteLength, Edges, GridTemplate, Length, PathHash, Pixels,
Point, Size, Style, Window, size,
util::{
ceil_to_device_pixel, round_half_toward_zero, round_stroke_to_device_pixel,
round_to_device_pixel,
},
};
use collections::{FxHashMap, FxHashSet};
use slotmap::{DefaultKey, KeyData, SecondaryMap};
use smallvec::SmallVec;
use std::{any::Any, fmt::Debug, ops::Range};
use taffy::{
TaffyTree, TraversePartialTree as _,
geometry::{Point as TaffyPoint, Rect as TaffyRect, Size as TaffySize},
prelude::{max_content, min_content},
style::AvailableSpace as TaffyAvailableSpace,
tree::NodeId,
};
#[cfg(feature = "stacker")]
type StackSafe<T> = stacksafe::StackSafe<T>;
#[cfg(not(feature = "stacker"))]
type StackSafe<T> = T;
type NodeContext = StackSafe<Box<dyn Measure>>;
trait Measure: Any {
fn measure(
&mut self,
known_dimensions: Size<Option<Pixels>>,
available_space: Size<AvailableSpace>,
final_layout: bool,
window: &mut Window,
cx: &mut App,
) -> Size<Pixels>;
}
#[cfg_attr(feature = "stacker", stacksafe::stacksafe)]
fn replace_measure_in_place<K, F>(
previous: &mut NodeContext,
mut key: K,
reuse: fn(&mut K, &mut K) -> bool,
measure: F,
) -> Result<bool, (K, F)>
where
K: 'static,
F: FnMut(
&K,
Size<Option<Pixels>>,
Size<AvailableSpace>,
bool,
&mut Window,
&mut App,
) -> Size<Pixels>
+ 'static,
{
let previous: &mut Box<dyn Measure> = previous;
let Some(previous) = (&mut **previous as &mut dyn Any).downcast_mut::<KeyedMeasure<K, F>>()
else {
return Err((key, measure));
};
let reused = reuse(&mut key, &mut previous.key);
previous.key = key;
previous.measure = measure;
Ok(reused)
}
fn box_measure<K, F>(key: K, measure: F) -> NodeContext
where
K: 'static,
F: FnMut(
&K,
Size<Option<Pixels>>,
Size<AvailableSpace>,
bool,
&mut Window,
&mut App,
) -> Size<Pixels>
+ 'static,
{
let measure: Box<dyn Measure> = Box::new(KeyedMeasure { key, measure });
#[cfg(feature = "stacker")]
let measure = StackSafe::new(measure);
measure
}
struct KeyedMeasure<K, F> {
key: K,
measure: F,
}
impl<K, F> Measure for KeyedMeasure<K, F>
where
K: 'static,
F: FnMut(
&K,
Size<Option<Pixels>>,
Size<AvailableSpace>,
bool,
&mut Window,
&mut App,
) -> Size<Pixels>
+ 'static,
{
fn measure(
&mut self,
known_dimensions: Size<Option<Pixels>>,
available_space: Size<AvailableSpace>,
final_layout: bool,
window: &mut Window,
cx: &mut App,
) -> Size<Pixels> {
(self.measure)(
&self.key,
known_dimensions,
available_space,
final_layout,
window,
cx,
)
}
}
pub struct TaffyLayoutEngine {
taffy: TaffyTree,
contexts: SecondaryMap<DefaultKey, NodeContext>,
cache: LayoutCache,
absolute_layout_bounds: FxHashMap<LayoutId, Bounds<Pixels>>,
absolute_outer_origins: FxHashMap<LayoutId, Point<f32>>,
computed_layouts: FxHashSet<LayoutId>,
layout_bounds_scratch_space: Vec<LayoutId>,
}
#[derive(Default)]
struct LayoutScope {
requested: usize,
nodes: SmallVec<[NodeId; 4]>,
}
struct CachedNode {
style: LayoutStyle,
frame: u64,
stretched: bool,
}
#[derive(Default)]
struct LayoutCache {
scopes: FxHashMap<PathHash, LayoutScope>,
nodes: SecondaryMap<DefaultKey, CachedNode>,
free_nodes: Vec<NodeId>,
frame: u64,
scale_factor: Option<f32>,
}
fn node_key(id: NodeId) -> DefaultKey {
KeyData::from_ffi(id.into()).into()
}
const EXPECT_MESSAGE: &str = "we should avoid taffy layout errors by construction if possible";
impl TaffyLayoutEngine {
pub fn new() -> Self {
let mut taffy = TaffyTree::new();
taffy.disable_rounding();
TaffyLayoutEngine {
taffy,
contexts: SecondaryMap::new(),
cache: LayoutCache::default(),
absolute_layout_bounds: FxHashMap::default(),
absolute_outer_origins: FxHashMap::default(),
computed_layouts: FxHashSet::default(),
layout_bounds_scratch_space: Vec::new(),
}
}
pub(crate) fn clear_layout_cache(&mut self) {
self.taffy.clear();
self.contexts.clear();
self.cache = LayoutCache::default();
self.absolute_layout_bounds.clear();
self.absolute_outer_origins.clear();
self.computed_layouts.clear();
}
pub(crate) fn clear_if_scale_factor_changed(&mut self, scale_factor: f32) {
if self
.cache
.scale_factor
.is_some_and(|previous| previous != scale_factor)
{
self.clear_layout_cache();
}
self.cache.scale_factor = Some(scale_factor);
}
#[cfg(test)]
pub(crate) fn node_count(&self) -> usize {
self.taffy.total_node_count()
}
pub fn clear(&mut self) {
self.cache
.evict_unrequested(&mut self.taffy, &mut self.contexts);
self.absolute_layout_bounds.clear();
self.absolute_outer_origins.clear();
self.computed_layouts.clear();
}
pub fn request_layout(
&mut self,
scope: PathHash,
style: &Style,
rem_size: Pixels,
scale_factor: f32,
children: &[LayoutId],
) -> LayoutId {
let id = self.acquire_node(scope, style, rem_size, scale_factor, children);
if self.contexts.remove(node_key(id)).is_some() {
self.taffy.mark_dirty(id).expect(EXPECT_MESSAGE);
}
id.into()
}
pub(crate) fn request_keyed_measure<K, F>(
&mut self,
scope: PathHash,
style: &Style,
rem_size: Pixels,
scale_factor: f32,
key: K,
reuse: fn(&mut K, &mut K) -> bool,
measure: F,
) -> LayoutId
where
K: 'static,
F: FnMut(
&K,
Size<Option<Pixels>>,
Size<AvailableSpace>,
bool,
&mut Window,
&mut App,
) -> Size<Pixels>
+ 'static,
{
let id = self.acquire_node(scope, style, rem_size, scale_factor, &[]);
let key_id = node_key(id);
let reused = if let Some(previous) = self.contexts.get_mut(key_id) {
match replace_measure_in_place(previous, key, reuse, measure) {
Ok(reused) => reused,
Err((key, measure)) => {
*previous = box_measure(key, measure);
false
}
}
} else {
self.contexts.insert(key_id, box_measure(key, measure));
false
};
if !reused {
self.taffy.mark_dirty(id).expect(EXPECT_MESSAGE);
}
id.into()
}
fn acquire_node(
&mut self,
scope: PathHash,
style: &Style,
rem_size: Pixels,
scale_factor: f32,
children: &[LayoutId],
) -> NodeId {
let children = LayoutId::to_taffy_slice(children);
let taffy = &mut self.taffy;
let cache = &mut self.cache;
let frame = cache.frame;
let scope = cache.scopes.entry(scope).or_default();
let ordinal = scope.requested;
scope.requested += 1;
let id = if let Some(&id) = scope.nodes.get(ordinal) {
let node = &mut cache.nodes[node_key(id)];
debug_assert_ne!(
node.frame, frame,
"layout node requested twice in one frame"
);
if node.stretched || !node.style.matches(style, rem_size, scale_factor) {
node.style = LayoutStyle::new(style, rem_size, scale_factor);
node.stretched = false;
let taffy_style = node.style.to_taffy();
if taffy.style(id).expect(EXPECT_MESSAGE) != &taffy_style {
taffy.set_style(id, taffy_style).expect(EXPECT_MESSAGE);
}
}
if !taffy.child_ids(id).eq(children.iter().copied()) {
taffy.set_children(id, children).expect(EXPECT_MESSAGE);
}
id
} else {
let style = LayoutStyle::new(style, rem_size, scale_factor);
let taffy_style = style.to_taffy();
let id = if let Some(id) = cache.free_nodes.pop() {
taffy.set_style(id, taffy_style).expect(EXPECT_MESSAGE);
let node = &mut cache.nodes[node_key(id)];
node.style = style;
node.stretched = false;
id
} else {
let id = taffy.new_leaf(taffy_style).expect(EXPECT_MESSAGE);
let node = CachedNode {
style,
frame,
stretched: false,
};
cache.nodes.insert(node_key(id), node);
id
};
if !children.is_empty() {
taffy.set_children(id, children).expect(EXPECT_MESSAGE);
}
scope.nodes.push(id);
id
};
cache.nodes[node_key(id)].frame = frame;
id
}
pub fn stretch_auto_size_to_fill(
&mut self,
id: LayoutId,
size: Size<Pixels>,
scale_factor: f32,
) {
let style = self.taffy.style(id.0).expect(EXPECT_MESSAGE);
let stretch_width = style.size.width.is_auto();
let stretch_height = style.size.height.is_auto();
if !stretch_width && !stretch_height {
return;
}
if let Some(node) = self.cache.nodes.get_mut(node_key(id.0)) {
node.stretched = true;
}
let mut style = style.clone();
if stretch_width {
style.size.width =
taffy::style::Dimension::length(round_to_device_pixel(size.width.0, scale_factor));
}
if stretch_height {
style.size.height =
taffy::style::Dimension::length(round_to_device_pixel(size.height.0, scale_factor));
}
self.taffy.set_style(id.0, style).expect(EXPECT_MESSAGE);
}
#[allow(dead_code)]
fn count_all_children(&self, parent: LayoutId) -> anyhow::Result<u32> {
let mut count = 0;
for child in self.taffy.children(parent.0)? {
count += 1;
count += self.count_all_children(LayoutId(child))?
}
Ok(count)
}
#[allow(dead_code)]
fn max_depth(&self, depth: u32, parent: LayoutId) -> anyhow::Result<u32> {
println!(
"{parent:?} at depth {depth} has {} children",
self.taffy.child_count(parent.0)
);
let mut max_child_depth = 0;
for child in self.taffy.children(parent.0)? {
max_child_depth = std::cmp::max(max_child_depth, self.max_depth(0, LayoutId(child))?);
}
Ok(depth + 1 + max_child_depth)
}
#[allow(dead_code)]
fn get_edges(&self, parent: LayoutId) -> anyhow::Result<Vec<(LayoutId, LayoutId)>> {
let mut edges = Vec::new();
for child in self.taffy.children(parent.0)? {
edges.push((parent, LayoutId(child)));
edges.extend(self.get_edges(LayoutId(child))?);
}
Ok(edges)
}
#[cfg_attr(feature = "stacker", stacksafe::stacksafe)]
pub fn compute_layout(
&mut self,
id: LayoutId,
available_space: Size<AvailableSpace>,
window: &mut Window,
cx: &mut App,
) {
let cache = &self.cache;
if let Some(parent) = self.taffy.parent(id.0)
&& cache
.nodes
.get(node_key(parent))
.is_none_or(|node| node.frame != cache.frame)
{
self.taffy.remove_child(parent, id.0).expect(EXPECT_MESSAGE);
}
if !self.computed_layouts.insert(id) {
let stack = &mut self.layout_bounds_scratch_space;
stack.push(id);
while let Some(id) = stack.pop() {
self.absolute_layout_bounds.remove(&id);
self.absolute_outer_origins.remove(&id);
stack.extend(
self.taffy
.children(id.into())
.expect(EXPECT_MESSAGE)
.into_iter()
.map(LayoutId::from),
);
}
}
let scale_factor = window.scale_factor();
let transform = |v: AvailableSpace| match v {
AvailableSpace::Definite(pixels) => {
AvailableSpace::Definite(Pixels(pixels.0 * scale_factor))
}
AvailableSpace::MinContent => AvailableSpace::MinContent,
AvailableSpace::MaxContent => AvailableSpace::MaxContent,
};
let available_space = size(
transform(available_space.width),
transform(available_space.height),
);
let contexts = &mut self.contexts;
self.taffy
.compute_layout_with_measure(
id.into(),
available_space.into(),
|input, id, _, style| {
let node_context = contexts.get_mut(node_key(id));
taffy::compute_leaf_layout(
input,
style,
|_, _| 0.0,
|known_dimensions, available_space| {
let Some(node_context) = node_context else {
return taffy::geometry::Size::default();
};
let known_dimensions = Size {
width: known_dimensions.width.map(|e| Pixels(e / scale_factor)),
height: known_dimensions.height.map(|e| Pixels(e / scale_factor)),
};
let available_space: Size<AvailableSpace> = available_space.into();
let untransform = |ev: AvailableSpace| match ev {
AvailableSpace::Definite(pixels) => {
AvailableSpace::Definite(Pixels(pixels.0 / scale_factor))
}
AvailableSpace::MinContent => AvailableSpace::MinContent,
AvailableSpace::MaxContent => AvailableSpace::MaxContent,
};
let available_space = size(
untransform(available_space.width),
untransform(available_space.height),
);
let final_layout = input.run_mode == taffy::RunMode::PerformLayout;
let measured_size = node_context.measure(
known_dimensions,
available_space,
final_layout,
window,
cx,
);
snap_measured_size_to_device_pixels(measured_size, scale_factor).into()
},
)
},
)
.expect(EXPECT_MESSAGE);
}
pub fn layout_bounds(&mut self, id: LayoutId, scale_factor: f32) -> Bounds<Pixels> {
if let Some(layout) = self.absolute_layout_bounds.get(&id).cloned() {
return layout;
}
let layout = self.taffy.layout(id.into()).expect(EXPECT_MESSAGE);
let layout_location = layout.location;
let layout_size = layout.size;
let parent = self.taffy.parent(id.0);
let absolute_outer_origin = match parent {
Some(parent_id) => {
let parent_id = LayoutId::from(parent_id);
self.layout_bounds(parent_id, scale_factor);
let parent_origin = *self
.absolute_outer_origins
.get(&parent_id)
.expect("parent absolute outer origin should be cached");
parent_origin + Point::from(layout_location)
}
None => Point::from(layout_location),
};
self.absolute_outer_origins
.insert(id, absolute_outer_origin);
let absolute_far = absolute_outer_origin + Point::from(Size::from(layout_size));
let snapped_bounds = Bounds::from_corners(
absolute_outer_origin.map(round_half_toward_zero),
absolute_far.map(round_half_toward_zero),
);
let bounds = (snapped_bounds / scale_factor).map(Pixels);
self.absolute_layout_bounds.insert(id, bounds);
bounds
}
}
impl LayoutCache {
fn evict_unrequested(
&mut self,
taffy: &mut TaffyTree,
contexts: &mut SecondaryMap<DefaultKey, NodeContext>,
) {
for id in self.free_nodes.drain(..) {
self.nodes.remove(node_key(id));
taffy.remove(id).expect(EXPECT_MESSAGE);
}
let free_nodes = &mut self.free_nodes;
self.scopes.retain(|_, scope| {
let requested = std::mem::take(&mut scope.requested);
free_nodes.extend(scope.nodes.drain(requested..));
requested > 0
});
for &id in &self.free_nodes {
contexts.remove(node_key(id));
if taffy.child_count(id) > 0 {
taffy.set_children(id, &[]).expect(EXPECT_MESSAGE);
}
}
debug_assert!(self.free_nodes.iter().all(|&id| taffy.parent(id).is_none()));
self.frame += 1;
}
}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[repr(transparent)]
pub struct LayoutId(NodeId);
impl LayoutId {
fn to_taffy_slice(node_ids: &[Self]) -> &[taffy::NodeId] {
unsafe { std::mem::transmute::<&[LayoutId], &[taffy::NodeId]>(node_ids) }
}
}
impl std::hash::Hash for LayoutId {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
u64::from(self.0).hash(state);
}
}
impl From<NodeId> for LayoutId {
fn from(node_id: NodeId) -> Self {
Self(node_id)
}
}
impl From<LayoutId> for NodeId {
fn from(layout_id: LayoutId) -> NodeId {
layout_id.0
}
}
fn snap_measured_size_to_device_pixels(size: Size<Pixels>, scale_factor: f32) -> Size<f32> {
size.map(|d| ceil_to_device_pixel(d.0.max(0.0), scale_factor))
}
fn border_widths_to_taffy(
widths: &Edges<AbsoluteLength>,
rem_size: Pixels,
scale_factor: f32,
) -> TaffyRect<taffy::style::LengthPercentage> {
let snap = |w: &AbsoluteLength| {
taffy::style::LengthPercentage::length(round_stroke_to_device_pixel(
w.to_pixels(rem_size).0,
scale_factor,
))
};
TaffyRect {
top: snap(&widths.top),
right: snap(&widths.right),
bottom: snap(&widths.bottom),
left: snap(&widths.left),
}
}
trait ToTaffy<Output> {
fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> Output;
}
macro_rules! layout_style {
($($field:ident: $field_type:ty),* $(,)?) => {
#[derive(Clone, Debug)]
struct LayoutStyle {
rem_size: Pixels,
scale_factor: f32,
$($field: $field_type,)*
}
impl LayoutStyle {
fn new(style: &Style, rem_size: Pixels, scale_factor: f32) -> Self {
Self {
rem_size,
scale_factor,
$($field: Clone::clone(&style.$field),)*
}
}
fn matches(&self, style: &Style, rem_size: Pixels, scale_factor: f32) -> bool {
self.rem_size == rem_size
&& self.scale_factor == scale_factor
$(&& self.$field == style.$field)*
}
}
};
}
layout_style! {
display: crate::Display,
overflow: Point<crate::Overflow>,
scrollbar_width: AbsoluteLength,
position: crate::Position,
inset: Edges<Length>,
size: Size<Length>,
min_size: Size<Length>,
max_size: Size<Length>,
aspect_ratio: Option<f32>,
margin: Edges<Length>,
padding: Edges<DefiniteLength>,
border_widths: Edges<AbsoluteLength>,
align_items: Option<crate::AlignItems>,
align_self: Option<crate::AlignSelf>,
align_content: Option<crate::AlignContent>,
justify_content: Option<crate::JustifyContent>,
gap: Size<DefiniteLength>,
flex_direction: crate::FlexDirection,
flex_wrap: crate::FlexWrap,
flex_basis: Length,
flex_grow: f32,
flex_shrink: f32,
grid_rows: Option<GridTemplate>,
grid_cols: Option<GridTemplate>,
grid_location: Option<crate::GridLocation>,
}
impl LayoutStyle {
fn to_taffy(&self) -> taffy::style::Style {
use taffy::style_helpers::{fr, length, minmax, repeat};
let rem_size = self.rem_size;
let scale_factor = self.scale_factor;
fn to_grid_line(
placement: &Range<crate::GridPlacement>,
) -> taffy::Line<taffy::GridPlacement> {
taffy::Line {
start: placement.start.into(),
end: placement.end.into(),
}
}
fn to_grid_repeat<T: taffy::style::CheapCloneStr>(
unit: &Option<GridTemplate>,
) -> Vec<taffy::GridTemplateComponent<T>> {
unit.map(|template| {
match template.min_size {
crate::GridTemplateMinSize::Zero => {
vec![repeat(
template.repeat,
vec![minmax(length(0.0_f32), fr(1.0_f32))],
)]
}
crate::GridTemplateMinSize::MinContent => {
vec![repeat(
template.repeat,
vec![minmax(min_content(), fr(1.0_f32))],
)]
}
crate::GridTemplateMinSize::MaxContent => {
vec![repeat(
template.repeat,
vec![minmax(length(0.0_f32), max_content())],
)]
}
}
})
.unwrap_or_default()
}
taffy::style::Style {
display: self.display.into(),
overflow: self.overflow.into(),
scrollbar_width: self.scrollbar_width.to_taffy(rem_size, scale_factor),
position: self.position.into(),
inset: self.inset.to_taffy(rem_size, scale_factor),
size: self.size.to_taffy(rem_size, scale_factor),
min_size: self.min_size.to_taffy(rem_size, scale_factor),
max_size: self.max_size.to_taffy(rem_size, scale_factor),
aspect_ratio: self.aspect_ratio,
margin: self.margin.to_taffy(rem_size, scale_factor),
padding: self.padding.to_taffy(rem_size, scale_factor),
border: border_widths_to_taffy(&self.border_widths, rem_size, scale_factor),
align_items: self.align_items.map(Into::into).unwrap_or_default(),
align_self: self.align_self.map(|x| x.into()),
align_content: self.align_content.map(Into::into).unwrap_or_default(),
justify_content: self.justify_content.map(Into::into).unwrap_or_default(),
gap: self.gap.to_taffy(rem_size, scale_factor),
flex_direction: self.flex_direction.into(),
flex_wrap: self.flex_wrap.into(),
flex_basis: self.flex_basis.to_taffy(rem_size, scale_factor),
flex_grow: self.flex_grow,
flex_shrink: self.flex_shrink,
grid_template_rows: to_grid_repeat(&self.grid_rows),
grid_template_columns: to_grid_repeat(&self.grid_cols),
grid_row: self
.grid_location
.as_ref()
.map(|location| to_grid_line(&location.row))
.unwrap_or_default(),
grid_column: self
.grid_location
.as_ref()
.map(|location| to_grid_line(&location.column))
.unwrap_or_default(),
..Default::default()
}
}
}
impl ToTaffy<f32> for AbsoluteLength {
fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> f32 {
round_to_device_pixel(self.to_pixels(rem_size).0, scale_factor)
}
}
impl ToTaffy<taffy::style::LengthPercentageAuto> for Length {
fn to_taffy(
&self,
rem_size: Pixels,
scale_factor: f32,
) -> taffy::prelude::LengthPercentageAuto {
match self {
Length::Definite(length) => length.to_taffy(rem_size, scale_factor),
Length::Auto => taffy::prelude::LengthPercentageAuto::auto(),
}
}
}
impl ToTaffy<taffy::style::Dimension> for Length {
fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::prelude::Dimension {
match self {
Length::Definite(length) => length.to_taffy(rem_size, scale_factor),
Length::Auto => taffy::prelude::Dimension::auto(),
}
}
}
impl ToTaffy<taffy::style::LengthPercentage> for DefiniteLength {
fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::LengthPercentage {
match self {
DefiniteLength::Absolute(length) => length.to_taffy(rem_size, scale_factor),
DefiniteLength::Fraction(fraction) => {
taffy::style::LengthPercentage::percent(*fraction)
}
}
}
}
impl ToTaffy<taffy::style::LengthPercentageAuto> for DefiniteLength {
fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::LengthPercentageAuto {
match self {
DefiniteLength::Absolute(length) => length.to_taffy(rem_size, scale_factor),
DefiniteLength::Fraction(fraction) => {
taffy::style::LengthPercentageAuto::percent(*fraction)
}
}
}
}
impl ToTaffy<taffy::style::Dimension> for DefiniteLength {
fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::Dimension {
match self {
DefiniteLength::Absolute(length) => length.to_taffy(rem_size, scale_factor),
DefiniteLength::Fraction(fraction) => taffy::style::Dimension::percent(*fraction),
}
}
}
impl ToTaffy<taffy::style::LengthPercentage> for AbsoluteLength {
fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::LengthPercentage {
taffy::style::LengthPercentage::length(self.to_taffy(rem_size, scale_factor))
}
}
impl ToTaffy<taffy::style::LengthPercentageAuto> for AbsoluteLength {
fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::LengthPercentageAuto {
taffy::style::LengthPercentageAuto::length(self.to_taffy(rem_size, scale_factor))
}
}
impl ToTaffy<taffy::style::Dimension> for AbsoluteLength {
fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::Dimension {
taffy::style::Dimension::length(self.to_taffy(rem_size, scale_factor))
}
}
impl<T, T2> From<TaffyPoint<T>> for Point<T2>
where
T: Into<T2>,
T2: Clone + Debug + Default + PartialEq,
{
fn from(point: TaffyPoint<T>) -> Point<T2> {
Point {
x: point.x.into(),
y: point.y.into(),
}
}
}
impl<T, T2> From<Point<T>> for TaffyPoint<T2>
where
T: Into<T2> + Clone + Debug + Default + PartialEq,
{
fn from(val: Point<T>) -> Self {
TaffyPoint {
x: val.x.into(),
y: val.y.into(),
}
}
}
impl<T, U> ToTaffy<TaffySize<U>> for Size<T>
where
T: ToTaffy<U> + Clone + Debug + Default + PartialEq,
{
fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> TaffySize<U> {
TaffySize {
width: self.width.to_taffy(rem_size, scale_factor),
height: self.height.to_taffy(rem_size, scale_factor),
}
}
}
impl<T, U> ToTaffy<TaffyRect<U>> for Edges<T>
where
T: ToTaffy<U> + Clone + Debug + Default + PartialEq,
{
fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> TaffyRect<U> {
TaffyRect {
top: self.top.to_taffy(rem_size, scale_factor),
right: self.right.to_taffy(rem_size, scale_factor),
bottom: self.bottom.to_taffy(rem_size, scale_factor),
left: self.left.to_taffy(rem_size, scale_factor),
}
}
}
impl<T, U> From<TaffySize<T>> for Size<U>
where
T: Into<U>,
U: Clone + Debug + Default + PartialEq,
{
fn from(taffy_size: TaffySize<T>) -> Self {
Size {
width: taffy_size.width.into(),
height: taffy_size.height.into(),
}
}
}
impl<T, U> From<Size<T>> for TaffySize<U>
where
T: Into<U> + Clone + Debug + Default + PartialEq,
{
fn from(size: Size<T>) -> Self {
TaffySize {
width: size.width.into(),
height: size.height.into(),
}
}
}
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)]
pub enum AvailableSpace {
Definite(Pixels),
#[default]
MinContent,
MaxContent,
}
impl AvailableSpace {
pub const fn min_size() -> Size<Self> {
Size {
width: Self::MinContent,
height: Self::MinContent,
}
}
}
impl From<AvailableSpace> for TaffyAvailableSpace {
fn from(space: AvailableSpace) -> TaffyAvailableSpace {
match space {
AvailableSpace::Definite(Pixels(value)) => TaffyAvailableSpace::Definite(value),
AvailableSpace::MinContent => TaffyAvailableSpace::MinContent,
AvailableSpace::MaxContent => TaffyAvailableSpace::MaxContent,
}
}
}
impl From<TaffyAvailableSpace> for AvailableSpace {
fn from(space: TaffyAvailableSpace) -> AvailableSpace {
match space {
TaffyAvailableSpace::Definite(value) => AvailableSpace::Definite(Pixels(value)),
TaffyAvailableSpace::MinContent => AvailableSpace::MinContent,
TaffyAvailableSpace::MaxContent => AvailableSpace::MaxContent,
}
}
}
impl From<Pixels> for AvailableSpace {
fn from(pixels: Pixels) -> Self {
AvailableSpace::Definite(pixels)
}
}
impl From<Size<Pixels>> for Size<AvailableSpace> {
fn from(size: Size<Pixels>) -> Self {
Size {
width: AvailableSpace::Definite(size.width),
height: AvailableSpace::Definite(size.height),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
AnyElement, AnyWindowHandle, AppContext as _, Context, Element, ElementId, GlobalElementId,
InspectorElementId, InteractiveElement as _, IntoElement, ParentElement as _, Render,
Styled as _, TestAppContext, div, px,
randomized_element_tree::{RandomizedElementTree, RandomizedElementTreeConfig},
rems,
};
use std::{cell::Cell, rc::Rc};
type Frame = Box<dyn Fn(&mut Window) -> AnyElement>;
struct FrameView {
frame: Rc<Cell<usize>>,
frames: Rc<Vec<Frame>>,
}
impl Render for FrameView {
fn render(&mut self, window: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
(self.frames[self.frame.get()])(window)
}
}
fn assert_frames_match_uncached(cx: &mut TestAppContext, frames: Vec<Frame>) {
assert_frames_match_uncached_with(cx, frames, |_| {}, |_| ());
}
fn assert_frames_match_uncached_with<T>(
cx: &mut TestAppContext,
frames: Vec<Frame>,
mut before_cached: impl FnMut(&Window),
mut after_cached: impl FnMut(&Window) -> T,
) -> Vec<T> {
let frames = Rc::new(frames);
let frame = Rc::new(Cell::new(0));
let windows = [false, true].map(|rebuild| {
let frames = frames.clone();
let frame = frame.clone();
let window = cx.open_window(size(px(800.), px(600.)), move |_, _| FrameView {
frame,
frames,
});
(AnyWindowHandle::from(window), rebuild)
});
let mut observations = Vec::new();
for index in 0..frames.len() {
frame.set(index);
let [cached, uncached] = windows.map(|(window, rebuild)| {
cx.update_window(window, |_, window, cx| {
if rebuild {
window.clear_layout_cache();
} else {
before_cached(window);
}
window.refresh();
window.draw(cx).clear(cx);
let observation = (!rebuild).then(|| after_cached(window));
let mut bounds = window
.rendered_frame
.debug_bounds
.iter()
.map(|(selector, bounds)| (selector.clone(), *bounds))
.collect::<Vec<_>>();
bounds.sort_by(|a, b| a.0.cmp(&b.0));
(bounds, observation)
})
.expect("window should remain open")
});
assert!(
!uncached.0.is_empty(),
"frame {index} has no debug selectors"
);
assert_eq!(cached.0, uncached.0, "frame {index}");
observations.push(cached.1.expect("cached window observation"));
}
observations
}
fn frame(render: impl Fn(&mut Window) -> AnyElement + 'static) -> Frame {
Box::new(render)
}
struct MeasuredLeaf {
size: Size<Pixels>,
measurements: Option<Rc<Cell<usize>>>,
}
impl MeasuredLeaf {
fn new(size: Size<Pixels>) -> Self {
Self {
size,
measurements: None,
}
}
fn with_counter(size: Size<Pixels>, measurements: Rc<Cell<usize>>) -> Self {
Self {
size,
measurements: Some(measurements),
}
}
}
impl Element for MeasuredLeaf {
type RequestLayoutState = ();
type PrepaintState = ();
fn id(&self) -> Option<ElementId> {
None
}
fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
window: &mut Window,
_: &mut App,
) -> (LayoutId, ()) {
let measured = self.size;
let measurements = self.measurements.clone();
let layout_id = window.request_keyed_measured_layout(
Style::default(),
measured,
move |_, _, _, _| {
if let Some(measurements) = &measurements {
measurements.set(measurements.get() + 1);
}
measured
},
);
(layout_id, ())
}
fn prepaint(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
_: Bounds<Pixels>,
_: &mut (),
_: &mut Window,
_: &mut App,
) {
}
fn paint(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
_: Bounds<Pixels>,
_: &mut (),
_: &mut (),
_: &mut Window,
_: &mut App,
) {
}
}
#[crate::test]
fn stretched_root_style_is_restored_when_its_node_is_reused(cx: &mut TestAppContext) {
assert_frames_match_uncached(
cx,
vec![
frame(|_| div().debug_selector(|| "root".into()).into_any_element()),
frame(|_| {
div()
.size(px(100.))
.child(div().debug_selector(|| "child".into()))
.into_any_element()
}),
],
);
}
#[crate::test]
fn root_is_detached_from_a_parent_no_longer_requested(cx: &mut TestAppContext) {
assert_frames_match_uncached(
cx,
vec![
frame(|_| {
div()
.p(px(10.))
.child(
div()
.p(px(7.))
.child(div().size(px(5.)).debug_selector(|| "leaf".into())),
)
.into_any_element()
}),
frame(|_| {
div()
.size(px(50.))
.debug_selector(|| "root".into())
.into_any_element()
}),
],
);
}
#[crate::test]
fn hidden_element_is_laid_out_again_when_shown(cx: &mut TestAppContext) {
let with_child_shown = |shown: bool| {
frame(move |_| {
let mut child = div().size(px(20.)).debug_selector(|| "child".into());
if !shown {
child.style().display = Some(crate::Display::None);
}
div()
.flex()
.child(div().debug_selector(|| "wrapper".into()).child(child))
.into_any_element()
})
};
assert_frames_match_uncached(
cx,
vec![
with_child_shown(false),
with_child_shown(false),
with_child_shown(true),
with_child_shown(false),
with_child_shown(true),
],
);
}
#[crate::test]
fn measured_leaf_is_measured_again_only_when_its_key_changes(cx: &mut TestAppContext) {
let counter = Rc::new(Cell::new(0));
let measured = |width: f32, height: f32| {
let counter = counter.clone();
frame(move |_| {
div()
.flex()
.child(div().debug_selector(|| "wrapper".into()).child(
MeasuredLeaf::with_counter(size(px(width), px(height)), counter.clone()),
))
.into_any_element()
})
};
let measurements = assert_frames_match_uncached_with(
cx,
vec![
measured(10., 10.),
measured(10., 10.),
measured(30., 20.),
measured(30., 20.),
measured(5., 40.),
],
|_| counter.set(0),
|_| counter.get(),
);
let measured = measurements
.iter()
.map(|count| *count > 0)
.collect::<Vec<_>>();
assert_eq!(
measured,
[false, false, true, false, true],
"measurements per frame: {measurements:?}"
);
}
#[crate::test]
fn rem_size_changes_resolve_lengths_again(cx: &mut TestAppContext) {
let with_rem_size = |rem_size: f32| {
frame(move |window| {
window.set_rem_size(px(rem_size));
div()
.flex()
.child(
div()
.w(rems(2.5))
.h(rems(1.25))
.debug_selector(|| "sized".into()),
)
.into_any_element()
})
};
assert_frames_match_uncached(
cx,
vec![with_rem_size(16.), with_rem_size(13.), with_rem_size(13.)],
);
}
#[crate::test]
fn element_ids_reused_by_different_elements(cx: &mut TestAppContext) {
let leaf = || {
frame(|_| {
div()
.flex()
.child(
div()
.id("reused")
.debug_selector(|| "reused".into())
.child(MeasuredLeaf::new(size(px(12.), px(9.)))),
)
.into_any_element()
})
};
assert_frames_match_uncached(
cx,
vec![
leaf(),
frame(|_| {
div()
.flex()
.child(div().size(px(3.)).debug_selector(|| "other".into()))
.into_any_element()
}),
frame(|_| {
div()
.flex()
.child(
div()
.id("reused")
.flex_col()
.p(px(4.))
.debug_selector(|| "reused".into())
.child(div().size(px(20.)))
.child(div().size(px(8.)).debug_selector(|| "inner".into())),
)
.into_any_element()
}),
leaf(),
],
);
}
#[crate::test]
fn text_is_measured_again_only_when_it_changes(cx: &mut TestAppContext) {
let text = |width: f32, text: &'static str, font_size: f32| {
frame(move |_| {
div()
.flex()
.child(
div()
.w(px(width))
.text_size(px(font_size))
.debug_selector(|| "wrapper".into())
.child(text),
)
.child(div().size(px(10.)).debug_selector(|| "sibling".into()))
.into_any_element()
})
};
let short = "Some text";
let same_length = "Same text";
let long = "Some longer text that wraps in narrow parents";
let frames = [
(text(200., short, 14.), false),
(text(200., short, 14.), false),
(text(200., same_length, 14.), true),
(text(200., long, 14.), true),
(text(200., long, 14.), false),
(text(60., long, 14.), true),
(text(60., long, 18.), true),
(text(60., long, 18.), false),
(text(200., long, 14.), true),
];
let expected = frames
.iter()
.map(|(_, measured)| *measured)
.collect::<Vec<_>>();
let measurements = assert_frames_match_uncached_with(
cx,
frames.into_iter().map(|(frame, _)| frame).collect(),
|_| {},
|window| window.text_system().last_frame_wrapped_line_count(),
);
let measured = measurements
.iter()
.map(|count| *count > 0)
.collect::<Vec<_>>();
assert_eq!(
measured, expected,
"text measurements per frame: {measurements:?}"
);
}
#[crate::test]
fn measured_leaf_updates_when_scale_factor_changes(cx: &mut TestAppContext) {
let at_scale = |scale_factor: f32| {
frame(move |window| {
window.set_scale_factor(scale_factor);
div()
.flex()
.child(
div()
.debug_selector(|| "wrapper".into())
.child(MeasuredLeaf::new(size(px(100.), px(40.)))),
)
.into_any_element()
})
};
assert_frames_match_uncached(cx, vec![at_scale(1.0), at_scale(2.0), at_scale(1.0)]);
}
#[crate::test]
fn text_is_measured_again_when_fonts_are_added(cx: &mut TestAppContext) {
let with_text = || {
frame(|_| {
div()
.flex()
.child(
div()
.w(px(200.))
.debug_selector(|| "wrapper".into())
.child("Some text"),
)
.into_any_element()
})
};
let mut frame_index = 0;
let measurements = assert_frames_match_uncached_with(
cx,
vec![with_text(), with_text(), with_text()],
|window| {
if frame_index == 1 {
window
.text_system()
.add_fonts(Vec::new())
.expect("noop text system accepts empty font list");
}
frame_index += 1;
},
|window| window.text_system().last_frame_wrapped_line_count(),
);
let measured = measurements
.iter()
.map(|count| *count > 0)
.collect::<Vec<_>>();
assert_eq!(
measured,
[false, true, false],
"text measurements per frame: {measurements:?}"
);
}
#[crate::test]
fn evicted_nodes_do_not_accumulate_when_ids_change_every_frame(cx: &mut TestAppContext) {
let window = cx.open_window(size(px(800.), px(600.)), |_, cx| {
RandomizedElementTree::new_with_config(RandomizedElementTreeConfig::new(3, 64), cx)
});
let root = window.root(cx).expect("window should have a root");
let mut node_counts = Vec::new();
for _ in 0..20 {
root.update(cx, |tree, cx| tree.churn_element_ids(cx));
let node_count = cx
.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
window.layout_node_count()
})
.expect("window should remain open");
node_counts.push(node_count);
}
assert!(
node_counts.windows(2).all(|pair| pair[0] == pair[1]),
"{node_counts:?}"
);
}
#[test]
fn border_widths_to_taffy_use_stroke_snapping() {
let border_widths = Edges {
top: Pixels(0.0).into(),
right: Pixels(0.4).into(),
bottom: Pixels(0.5).into(),
left: Pixels(1.6).into(),
};
let taffy_border = border_widths_to_taffy(&border_widths, Pixels(16.0), 1.0);
assert_eq!(
taffy_border.top,
taffy::style::LengthPercentage::length(0.0)
);
assert_eq!(
taffy_border.right,
taffy::style::LengthPercentage::length(1.0)
);
assert_eq!(
taffy_border.bottom,
taffy::style::LengthPercentage::length(1.0)
);
assert_eq!(
taffy_border.left,
taffy::style::LengthPercentage::length(2.0)
);
}
}