2023-11-30 16:12:40 +00:00
|
|
|
use crate::{
|
|
|
|
AbsoluteLength, Bounds, DefiniteLength, Edges, Length, Pixels, Point, Size, Style,
|
|
|
|
WindowContext,
|
|
|
|
};
|
2024-02-07 12:16:22 +00:00
|
|
|
use collections::{FxHashMap, FxHashSet};
|
2023-11-09 17:43:26 +00:00
|
|
|
use smallvec::SmallVec;
|
2023-09-19 19:19:22 +00:00
|
|
|
use std::fmt::Debug;
|
2023-09-21 02:28:32 +00:00
|
|
|
use taffy::{
|
2024-02-16 13:30:31 +00:00
|
|
|
geometry::{Point as TaffyPoint, Rect as TaffyRect, Size as TaffySize},
|
|
|
|
style::AvailableSpace as TaffyAvailableSpace,
|
|
|
|
tree::NodeId,
|
2024-05-10 13:05:50 +00:00
|
|
|
TaffyTree, TraversePartialTree as _,
|
2023-09-21 02:28:32 +00:00
|
|
|
};
|
2023-09-19 19:19:22 +00:00
|
|
|
|
2024-01-21 23:35:47 +00:00
|
|
|
type NodeMeasureFn =
|
|
|
|
Box<dyn FnMut(Size<Option<Pixels>>, Size<AvailableSpace>, &mut WindowContext) -> Size<Pixels>>;
|
|
|
|
|
2023-09-21 18:18:09 +00:00
|
|
|
pub struct TaffyLayoutEngine {
|
2024-05-10 13:05:50 +00:00
|
|
|
taffy: TaffyTree<()>,
|
2024-02-07 12:16:22 +00:00
|
|
|
styles: FxHashMap<LayoutId, Style>,
|
|
|
|
children_to_parents: FxHashMap<LayoutId, LayoutId>,
|
|
|
|
absolute_layout_bounds: FxHashMap<LayoutId, Bounds<Pixels>>,
|
|
|
|
computed_layouts: FxHashSet<LayoutId>,
|
|
|
|
nodes_to_measure: FxHashMap<LayoutId, NodeMeasureFn>,
|
2023-09-21 02:28:32 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 12:15:57 +00:00
|
|
|
static EXPECT_MESSAGE: &str = "we should avoid taffy layout errors by construction if possible";
|
2023-10-11 04:14:47 +00:00
|
|
|
|
2023-09-19 19:19:22 +00:00
|
|
|
impl TaffyLayoutEngine {
|
|
|
|
pub fn new() -> Self {
|
2023-09-21 18:18:09 +00:00
|
|
|
TaffyLayoutEngine {
|
2024-05-10 13:05:50 +00:00
|
|
|
taffy: TaffyTree::new(),
|
2024-02-07 12:16:22 +00:00
|
|
|
styles: FxHashMap::default(),
|
|
|
|
children_to_parents: FxHashMap::default(),
|
|
|
|
absolute_layout_bounds: FxHashMap::default(),
|
|
|
|
computed_layouts: FxHashSet::default(),
|
|
|
|
nodes_to_measure: FxHashMap::default(),
|
2023-09-21 18:18:09 +00:00
|
|
|
}
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 17:43:26 +00:00
|
|
|
pub fn clear(&mut self) {
|
2024-01-16 15:44:24 +00:00
|
|
|
self.taffy.clear();
|
|
|
|
self.children_to_parents.clear();
|
2023-11-09 17:43:26 +00:00
|
|
|
self.absolute_layout_bounds.clear();
|
|
|
|
self.computed_layouts.clear();
|
2023-11-30 15:03:22 +00:00
|
|
|
self.nodes_to_measure.clear();
|
2024-01-08 18:07:20 +00:00
|
|
|
self.styles.clear();
|
|
|
|
}
|
|
|
|
|
2024-04-23 13:14:22 +00:00
|
|
|
pub fn request_layout(
|
2023-09-19 19:19:22 +00:00
|
|
|
&mut self,
|
2024-05-09 09:38:53 +00:00
|
|
|
style: Style,
|
2023-09-19 19:19:22 +00:00
|
|
|
rem_size: Pixels,
|
|
|
|
children: &[LayoutId],
|
2023-10-11 04:14:47 +00:00
|
|
|
) -> LayoutId {
|
2024-01-08 18:07:20 +00:00
|
|
|
let taffy_style = style.to_taffy(rem_size);
|
|
|
|
let layout_id = if children.is_empty() {
|
2024-01-16 15:44:24 +00:00
|
|
|
self.taffy
|
2024-01-08 18:07:20 +00:00
|
|
|
.new_leaf(taffy_style)
|
|
|
|
.expect(EXPECT_MESSAGE)
|
|
|
|
.into()
|
2023-09-19 19:19:22 +00:00
|
|
|
} else {
|
2024-01-16 15:44:24 +00:00
|
|
|
let parent_id = self
|
|
|
|
.taffy
|
2023-09-21 18:18:09 +00:00
|
|
|
// This is safe because LayoutId is repr(transparent) to taffy::tree::NodeId.
|
2024-01-08 18:07:20 +00:00
|
|
|
.new_with_children(taffy_style, unsafe { std::mem::transmute(children) })
|
2023-10-11 04:14:47 +00:00
|
|
|
.expect(EXPECT_MESSAGE)
|
2024-01-16 15:44:24 +00:00
|
|
|
.into();
|
2024-05-09 09:38:53 +00:00
|
|
|
self.children_to_parents
|
|
|
|
.extend(children.into_iter().map(|child_id| (*child_id, parent_id)));
|
2024-01-16 15:44:24 +00:00
|
|
|
parent_id
|
2024-01-08 18:07:20 +00:00
|
|
|
};
|
2024-05-09 09:38:53 +00:00
|
|
|
self.styles.insert(layout_id, style);
|
2024-01-08 18:07:20 +00:00
|
|
|
layout_id
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 20:32:55 +00:00
|
|
|
pub fn request_measured_layout(
|
|
|
|
&mut self,
|
|
|
|
style: Style,
|
|
|
|
rem_size: Pixels,
|
2023-11-30 16:12:40 +00:00
|
|
|
measure: impl FnMut(Size<Option<Pixels>>, Size<AvailableSpace>, &mut WindowContext) -> Size<Pixels>
|
|
|
|
+ 'static,
|
2023-10-11 04:14:47 +00:00
|
|
|
) -> LayoutId {
|
2024-01-08 18:07:20 +00:00
|
|
|
let taffy_style = style.to_taffy(rem_size);
|
2023-09-20 20:32:55 +00:00
|
|
|
|
2023-11-30 15:03:22 +00:00
|
|
|
let layout_id = self
|
2024-01-16 15:44:24 +00:00
|
|
|
.taffy
|
2024-01-08 18:07:20 +00:00
|
|
|
.new_leaf_with_context(taffy_style, ())
|
2023-10-11 04:14:47 +00:00
|
|
|
.expect(EXPECT_MESSAGE)
|
2023-11-30 15:03:22 +00:00
|
|
|
.into();
|
|
|
|
self.nodes_to_measure.insert(layout_id, Box::new(measure));
|
2024-05-09 09:38:53 +00:00
|
|
|
self.styles.insert(layout_id, style);
|
2023-11-30 15:03:22 +00:00
|
|
|
layout_id
|
2023-09-20 20:32:55 +00:00
|
|
|
}
|
|
|
|
|
2023-10-13 16:01:15 +00:00
|
|
|
// Used to understand performance
|
|
|
|
#[allow(dead_code)]
|
2023-10-12 14:27:50 +00:00
|
|
|
fn count_all_children(&self, parent: LayoutId) -> anyhow::Result<u32> {
|
2023-10-06 14:48:25 +00:00
|
|
|
let mut count = 0;
|
|
|
|
|
2024-01-16 15:44:24 +00:00
|
|
|
for child in self.taffy.children(parent.0)? {
|
2023-10-06 14:48:25 +00:00
|
|
|
// Count this child.
|
|
|
|
count += 1;
|
|
|
|
|
|
|
|
// Count all of this child's children.
|
|
|
|
count += self.count_all_children(LayoutId(child))?
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(count)
|
|
|
|
}
|
|
|
|
|
2023-10-13 16:01:15 +00:00
|
|
|
// Used to understand performance
|
|
|
|
#[allow(dead_code)]
|
2023-10-12 14:27:50 +00:00
|
|
|
fn max_depth(&self, depth: u32, parent: LayoutId) -> anyhow::Result<u32> {
|
2023-10-06 14:48:25 +00:00
|
|
|
println!(
|
|
|
|
"{parent:?} at depth {depth} has {} children",
|
2024-05-10 13:05:50 +00:00
|
|
|
self.taffy.child_count(parent.0)
|
2023-10-06 14:48:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut max_child_depth = 0;
|
|
|
|
|
2024-01-16 15:44:24 +00:00
|
|
|
for child in self.taffy.children(parent.0)? {
|
2023-10-06 14:48:25 +00:00
|
|
|
max_child_depth = std::cmp::max(max_child_depth, self.max_depth(0, LayoutId(child))?);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(depth + 1 + max_child_depth)
|
|
|
|
}
|
|
|
|
|
2023-10-13 16:01:15 +00:00
|
|
|
// Used to understand performance
|
|
|
|
#[allow(dead_code)]
|
2023-10-12 14:27:50 +00:00
|
|
|
fn get_edges(&self, parent: LayoutId) -> anyhow::Result<Vec<(LayoutId, LayoutId)>> {
|
2023-10-06 14:48:25 +00:00
|
|
|
let mut edges = Vec::new();
|
|
|
|
|
2024-01-16 15:44:24 +00:00
|
|
|
for child in self.taffy.children(parent.0)? {
|
2023-10-06 14:48:25 +00:00
|
|
|
edges.push((parent, LayoutId(child)));
|
|
|
|
|
|
|
|
edges.extend(self.get_edges(LayoutId(child))?);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(edges)
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:12:40 +00:00
|
|
|
pub fn compute_layout(
|
|
|
|
&mut self,
|
|
|
|
id: LayoutId,
|
|
|
|
available_space: Size<AvailableSpace>,
|
|
|
|
cx: &mut WindowContext,
|
|
|
|
) {
|
2023-11-09 17:43:26 +00:00
|
|
|
// Leaving this here until we have a better instrumentation approach.
|
2023-10-07 16:23:25 +00:00
|
|
|
// println!("Laying out {} children", self.count_all_children(id)?);
|
|
|
|
// println!("Max layout depth: {}", self.max_depth(0, id)?);
|
2023-10-06 14:48:25 +00:00
|
|
|
|
|
|
|
// Output the edges (branches) of the tree in Mermaid format for visualization.
|
|
|
|
// println!("Edges:");
|
|
|
|
// for (a, b) in self.get_edges(id)? {
|
|
|
|
// println!("N{} --> N{}", u64::from(a), u64::from(b));
|
|
|
|
// }
|
|
|
|
// println!("");
|
2023-11-09 17:43:26 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
if !self.computed_layouts.insert(id) {
|
|
|
|
let mut stack = SmallVec::<[LayoutId; 64]>::new();
|
|
|
|
stack.push(id);
|
|
|
|
while let Some(id) = stack.pop() {
|
|
|
|
self.absolute_layout_bounds.remove(&id);
|
|
|
|
stack.extend(
|
2024-01-16 15:44:24 +00:00
|
|
|
self.taffy
|
2023-11-09 17:43:26 +00:00
|
|
|
.children(id.into())
|
|
|
|
.expect(EXPECT_MESSAGE)
|
|
|
|
.into_iter()
|
|
|
|
.map(Into::into),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-10-06 14:48:25 +00:00
|
|
|
|
2023-10-23 15:41:22 +00:00
|
|
|
// let started_at = std::time::Instant::now();
|
2024-01-16 15:44:24 +00:00
|
|
|
self.taffy
|
2023-11-19 04:30:33 +00:00
|
|
|
.compute_layout_with_measure(
|
|
|
|
id.into(),
|
|
|
|
available_space.into(),
|
2023-11-30 15:03:22 +00:00
|
|
|
|known_dimensions, available_space, node_id, _context| {
|
|
|
|
let Some(measure) = self.nodes_to_measure.get_mut(&node_id.into()) else {
|
2023-11-19 04:30:33 +00:00
|
|
|
return taffy::geometry::Size::default();
|
|
|
|
};
|
|
|
|
|
|
|
|
let known_dimensions = Size {
|
|
|
|
width: known_dimensions.width.map(Pixels),
|
|
|
|
height: known_dimensions.height.map(Pixels),
|
|
|
|
};
|
|
|
|
|
2023-11-30 16:12:40 +00:00
|
|
|
measure(known_dimensions, available_space.into(), cx).into()
|
2023-11-19 04:30:33 +00:00
|
|
|
},
|
|
|
|
)
|
2023-10-11 04:14:47 +00:00
|
|
|
.expect(EXPECT_MESSAGE);
|
2023-11-30 15:03:22 +00:00
|
|
|
|
2023-10-23 13:09:22 +00:00
|
|
|
// println!("compute_layout took {:?}", started_at.elapsed());
|
2023-09-26 17:29:44 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 04:14:47 +00:00
|
|
|
pub fn layout_bounds(&mut self, id: LayoutId) -> Bounds<Pixels> {
|
2023-10-06 03:02:26 +00:00
|
|
|
if let Some(layout) = self.absolute_layout_bounds.get(&id).cloned() {
|
2023-10-11 04:14:47 +00:00
|
|
|
return layout;
|
2023-09-21 18:18:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 15:44:24 +00:00
|
|
|
let layout = self.taffy.layout(id.into()).expect(EXPECT_MESSAGE);
|
2023-10-06 03:02:26 +00:00
|
|
|
let mut bounds = Bounds {
|
|
|
|
origin: layout.location.into(),
|
|
|
|
size: layout.size.into(),
|
|
|
|
};
|
|
|
|
|
2024-01-16 15:44:24 +00:00
|
|
|
if let Some(parent_id) = self.children_to_parents.get(&id).copied() {
|
|
|
|
let parent_bounds = self.layout_bounds(parent_id);
|
2023-10-06 03:02:26 +00:00
|
|
|
bounds.origin += parent_bounds.origin;
|
2023-09-21 18:18:09 +00:00
|
|
|
}
|
2023-10-06 03:02:26 +00:00
|
|
|
self.absolute_layout_bounds.insert(id, bounds);
|
2023-09-21 18:18:09 +00:00
|
|
|
|
2023-10-11 04:14:47 +00:00
|
|
|
bounds
|
2023-09-21 18:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 22:26:45 +00:00
|
|
|
/// A unique identifier for a layout node, generated when requesting a layout from Taffy
|
2023-09-21 18:18:09 +00:00
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct LayoutId(NodeId);
|
|
|
|
|
|
|
|
impl std::hash::Hash for LayoutId {
|
|
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
|
|
u64::from(self.0).hash(state);
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:18:09 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 19:19:22 +00:00
|
|
|
trait ToTaffy<Output> {
|
|
|
|
fn to_taffy(&self, rem_size: Pixels) -> Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTaffy<taffy::style::Style> for Style {
|
|
|
|
fn to_taffy(&self, rem_size: Pixels) -> taffy::style::Style {
|
|
|
|
taffy::style::Style {
|
|
|
|
display: self.display,
|
2024-01-01 23:09:24 +00:00
|
|
|
overflow: self.overflow.into(),
|
2023-09-19 19:19:22 +00:00
|
|
|
scrollbar_width: self.scrollbar_width,
|
|
|
|
position: self.position,
|
|
|
|
inset: self.inset.to_taffy(rem_size),
|
|
|
|
size: self.size.to_taffy(rem_size),
|
|
|
|
min_size: self.min_size.to_taffy(rem_size),
|
|
|
|
max_size: self.max_size.to_taffy(rem_size),
|
|
|
|
aspect_ratio: self.aspect_ratio,
|
|
|
|
margin: self.margin.to_taffy(rem_size),
|
|
|
|
padding: self.padding.to_taffy(rem_size),
|
|
|
|
border: self.border_widths.to_taffy(rem_size),
|
|
|
|
align_items: self.align_items,
|
|
|
|
align_self: self.align_self,
|
|
|
|
align_content: self.align_content,
|
|
|
|
justify_content: self.justify_content,
|
|
|
|
gap: self.gap.to_taffy(rem_size),
|
|
|
|
flex_direction: self.flex_direction,
|
|
|
|
flex_wrap: self.flex_wrap,
|
|
|
|
flex_basis: self.flex_basis.to_taffy(rem_size),
|
|
|
|
flex_grow: self.flex_grow,
|
|
|
|
flex_shrink: self.flex_shrink,
|
|
|
|
..Default::default() // Ignore grid properties for now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTaffy<taffy::style::LengthPercentageAuto> for Length {
|
|
|
|
fn to_taffy(&self, rem_size: Pixels) -> taffy::prelude::LengthPercentageAuto {
|
|
|
|
match self {
|
|
|
|
Length::Definite(length) => length.to_taffy(rem_size),
|
|
|
|
Length::Auto => taffy::prelude::LengthPercentageAuto::Auto,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTaffy<taffy::style::Dimension> for Length {
|
|
|
|
fn to_taffy(&self, rem_size: Pixels) -> taffy::prelude::Dimension {
|
|
|
|
match self {
|
|
|
|
Length::Definite(length) => length.to_taffy(rem_size),
|
|
|
|
Length::Auto => taffy::prelude::Dimension::Auto,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTaffy<taffy::style::LengthPercentage> for DefiniteLength {
|
|
|
|
fn to_taffy(&self, rem_size: Pixels) -> taffy::style::LengthPercentage {
|
|
|
|
match self {
|
|
|
|
DefiniteLength::Absolute(length) => match length {
|
|
|
|
AbsoluteLength::Pixels(pixels) => {
|
|
|
|
taffy::style::LengthPercentage::Length(pixels.into())
|
|
|
|
}
|
|
|
|
AbsoluteLength::Rems(rems) => {
|
|
|
|
taffy::style::LengthPercentage::Length((*rems * rem_size).into())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
DefiniteLength::Fraction(fraction) => {
|
|
|
|
taffy::style::LengthPercentage::Percent(*fraction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTaffy<taffy::style::LengthPercentageAuto> for DefiniteLength {
|
|
|
|
fn to_taffy(&self, rem_size: Pixels) -> taffy::style::LengthPercentageAuto {
|
|
|
|
match self {
|
|
|
|
DefiniteLength::Absolute(length) => match length {
|
|
|
|
AbsoluteLength::Pixels(pixels) => {
|
|
|
|
taffy::style::LengthPercentageAuto::Length(pixels.into())
|
|
|
|
}
|
|
|
|
AbsoluteLength::Rems(rems) => {
|
|
|
|
taffy::style::LengthPercentageAuto::Length((*rems * rem_size).into())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
DefiniteLength::Fraction(fraction) => {
|
|
|
|
taffy::style::LengthPercentageAuto::Percent(*fraction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTaffy<taffy::style::Dimension> for DefiniteLength {
|
|
|
|
fn to_taffy(&self, rem_size: Pixels) -> taffy::style::Dimension {
|
|
|
|
match self {
|
|
|
|
DefiniteLength::Absolute(length) => match length {
|
|
|
|
AbsoluteLength::Pixels(pixels) => taffy::style::Dimension::Length(pixels.into()),
|
|
|
|
AbsoluteLength::Rems(rems) => {
|
|
|
|
taffy::style::Dimension::Length((*rems * rem_size).into())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
DefiniteLength::Fraction(fraction) => taffy::style::Dimension::Percent(*fraction),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToTaffy<taffy::style::LengthPercentage> for AbsoluteLength {
|
|
|
|
fn to_taffy(&self, rem_size: Pixels) -> taffy::style::LengthPercentage {
|
|
|
|
match self {
|
|
|
|
AbsoluteLength::Pixels(pixels) => taffy::style::LengthPercentage::Length(pixels.into()),
|
|
|
|
AbsoluteLength::Rems(rems) => {
|
|
|
|
taffy::style::LengthPercentage::Length((*rems * rem_size).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 18:41:00 +00:00
|
|
|
impl<T, T2> From<TaffyPoint<T>> for Point<T2>
|
2023-09-19 19:19:22 +00:00
|
|
|
where
|
|
|
|
T: Into<T2>,
|
2023-10-13 18:41:00 +00:00
|
|
|
T2: Clone + Default + Debug,
|
2023-09-19 19:19:22 +00:00
|
|
|
{
|
2023-10-13 18:41:00 +00:00
|
|
|
fn from(point: TaffyPoint<T>) -> Point<T2> {
|
2023-09-19 19:19:22 +00:00
|
|
|
Point {
|
|
|
|
x: point.x.into(),
|
|
|
|
y: point.y.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-01 23:09:24 +00:00
|
|
|
impl<T, T2> From<Point<T>> for TaffyPoint<T2>
|
2023-09-19 19:19:22 +00:00
|
|
|
where
|
2023-10-13 18:41:00 +00:00
|
|
|
T: Into<T2> + Clone + Default + Debug,
|
2023-09-19 19:19:22 +00:00
|
|
|
{
|
2024-01-01 23:09:24 +00:00
|
|
|
fn from(val: Point<T>) -> Self {
|
2023-10-13 18:41:00 +00:00
|
|
|
TaffyPoint {
|
2024-01-01 23:09:24 +00:00
|
|
|
x: val.x.into(),
|
|
|
|
y: val.y.into(),
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 18:41:00 +00:00
|
|
|
impl<T, U> ToTaffy<TaffySize<U>> for Size<T>
|
|
|
|
where
|
|
|
|
T: ToTaffy<U> + Clone + Default + Debug,
|
|
|
|
{
|
|
|
|
fn to_taffy(&self, rem_size: Pixels) -> TaffySize<U> {
|
|
|
|
TaffySize {
|
2024-01-01 23:09:24 +00:00
|
|
|
width: self.width.to_taffy(rem_size),
|
|
|
|
height: self.height.to_taffy(rem_size),
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 18:41:00 +00:00
|
|
|
impl<T, U> ToTaffy<TaffyRect<U>> for Edges<T>
|
2023-09-19 19:19:22 +00:00
|
|
|
where
|
2023-10-13 18:41:00 +00:00
|
|
|
T: ToTaffy<U> + Clone + Default + Debug,
|
2023-09-19 19:19:22 +00:00
|
|
|
{
|
2023-10-13 18:41:00 +00:00
|
|
|
fn to_taffy(&self, rem_size: Pixels) -> TaffyRect<U> {
|
|
|
|
TaffyRect {
|
2024-01-01 23:09:24 +00:00
|
|
|
top: self.top.to_taffy(rem_size),
|
|
|
|
right: self.right.to_taffy(rem_size),
|
|
|
|
bottom: self.bottom.to_taffy(rem_size),
|
|
|
|
left: self.left.to_taffy(rem_size),
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 18:41:00 +00:00
|
|
|
impl<T, U> From<TaffySize<T>> for Size<U>
|
|
|
|
where
|
|
|
|
T: Into<U>,
|
|
|
|
U: Clone + Default + Debug,
|
|
|
|
{
|
|
|
|
fn from(taffy_size: TaffySize<T>) -> Self {
|
2023-09-21 02:28:32 +00:00
|
|
|
Size {
|
|
|
|
width: taffy_size.width.into(),
|
|
|
|
height: taffy_size.height.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 18:41:00 +00:00
|
|
|
impl<T, U> From<Size<T>> for TaffySize<U>
|
|
|
|
where
|
|
|
|
T: Into<U> + Clone + Default + Debug,
|
|
|
|
{
|
2023-09-21 02:28:32 +00:00
|
|
|
fn from(size: Size<T>) -> Self {
|
2023-10-13 18:41:00 +00:00
|
|
|
TaffySize {
|
2023-09-21 02:28:32 +00:00
|
|
|
width: size.width.into(),
|
|
|
|
height: size.height.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-21 22:26:45 +00:00
|
|
|
/// The space available for an element to be laid out in
|
2023-11-09 17:43:26 +00:00
|
|
|
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)]
|
2023-09-26 17:29:44 +00:00
|
|
|
pub enum AvailableSpace {
|
|
|
|
/// The amount of space available is the specified number of pixels
|
|
|
|
Definite(Pixels),
|
|
|
|
/// The amount of space available is indefinite and the node should be laid out under a min-content constraint
|
2023-10-13 18:41:00 +00:00
|
|
|
#[default]
|
2023-09-26 17:29:44 +00:00
|
|
|
MinContent,
|
|
|
|
/// The amount of space available is indefinite and the node should be laid out under a max-content constraint
|
|
|
|
MaxContent,
|
|
|
|
}
|
|
|
|
|
Fix flickering (#9012)
See https://zed.dev/channel/gpui-536
Fixes https://github.com/zed-industries/zed/issues/9010
Fixes https://github.com/zed-industries/zed/issues/8883
Fixes https://github.com/zed-industries/zed/issues/8640
Fixes https://github.com/zed-industries/zed/issues/8598
Fixes https://github.com/zed-industries/zed/issues/8579
Fixes https://github.com/zed-industries/zed/issues/8363
Fixes https://github.com/zed-industries/zed/issues/8207
### Problem
After transitioning Zed to GPUI 2, we started noticing that interacting
with the mouse on many UI elements would lead to a pretty annoying
flicker. The main issue with the old approach was that hover state was
calculated based on the previous frame. That is, when computing whether
a given element was hovered in the current frame, we would use
information about the same element in the previous frame.
However, inspecting the previous frame tells us very little about what
should be hovered in the current frame, as elements in the current frame
may have changed significantly.
### Solution
This pull request's main contribution is the introduction of a new
`after_layout` phase when redrawing the window. The key idea is that
we'll give every element a chance to register a hitbox (see
`ElementContext::insert_hitbox`) before painting anything. Then, during
the `paint` phase, elements can determine whether they're the topmost
and draw their hover state accordingly.
We are also removing the ability to give an arbitrary z-index to
elements. Instead, we will follow the much simpler painter's algorithm.
That is, an element that gets painted after will be drawn on top of an
element that got painted earlier. Elements can still escape their
current "stacking context" by using the new `ElementContext::defer_draw`
method (see `Overlay` for an example). Elements drawn using this method
will still be logically considered as being children of their original
parent (for keybinding, focus and cache invalidation purposes) but their
layout and paint passes will be deferred until the currently-drawn
element is done.
With these changes we also reworked geometry batching within the
`Scene`. The new approach uses an AABB tree to determine geometry
occlusion, which allows the GPU to render non-overlapping geometry in
parallel.
### Performance
Performance is slightly better than on `main` even though this new
approach is more correct and we're maintaining an extra data structure
(the AABB tree).
![before_after](https://github.com/zed-industries/zed/assets/482957/c8120b07-1dbd-4776-834a-d040e569a71e)
Release Notes:
- Fixed a bug that was causing popovers to flicker.
---------
Co-authored-by: Nathan Sobo <nathan@zed.dev>
Co-authored-by: Thorsten <thorsten@zed.dev>
2024-03-11 09:45:57 +00:00
|
|
|
impl AvailableSpace {
|
|
|
|
/// Returns a `Size` with both width and height set to `AvailableSpace::MinContent`.
|
|
|
|
///
|
|
|
|
/// This function is useful when you want to create a `Size` with the minimum content constraints
|
|
|
|
/// for both dimensions.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let min_content_size = AvailableSpace::min_size();
|
|
|
|
/// assert_eq!(min_content_size.width, AvailableSpace::MinContent);
|
|
|
|
/// assert_eq!(min_content_size.height, AvailableSpace::MinContent);
|
|
|
|
/// ```
|
|
|
|
pub const fn min_size() -> Size<Self> {
|
|
|
|
Size {
|
|
|
|
width: Self::MinContent,
|
|
|
|
height: Self::MinContent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-26 17:29:44 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-21 02:28:32 +00:00
|
|
|
|
|
|
|
impl From<TaffyAvailableSpace> for AvailableSpace {
|
2023-09-26 17:29:44 +00:00
|
|
|
fn from(space: TaffyAvailableSpace) -> AvailableSpace {
|
2023-09-21 02:28:32 +00:00
|
|
|
match space {
|
|
|
|
TaffyAvailableSpace::Definite(value) => AvailableSpace::Definite(Pixels(value)),
|
|
|
|
TaffyAvailableSpace::MinContent => AvailableSpace::MinContent,
|
|
|
|
TaffyAvailableSpace::MaxContent => AvailableSpace::MaxContent,
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-26 17:29:44 +00:00
|
|
|
impl From<Pixels> for AvailableSpace {
|
|
|
|
fn from(pixels: Pixels) -> Self {
|
|
|
|
AvailableSpace::Definite(pixels)
|
|
|
|
}
|
|
|
|
}
|
2023-12-08 01:16:19 +00:00
|
|
|
|
|
|
|
impl From<Size<Pixels>> for Size<AvailableSpace> {
|
|
|
|
fn from(size: Size<Pixels>) -> Self {
|
|
|
|
Size {
|
|
|
|
width: AvailableSpace::Definite(size.width),
|
|
|
|
height: AvailableSpace::Definite(size.height),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|