zed/crates/gpui/src/taffy.rs

499 lines
16 KiB
Rust
Raw Normal View History

use crate::{
AbsoluteLength, Bounds, DefiniteLength, Edges, Length, Pixels, Point, Size, Style,
WindowContext,
};
use collections::{FxHashMap, FxHashSet};
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::{
geometry::{Point as TaffyPoint, Rect as TaffyRect, Size as TaffySize},
style::AvailableSpace as TaffyAvailableSpace,
tree::NodeId,
gpui: Bump taffy to 0.4.3 again (#11655) We reverted bump to taffy 0.4.3 following an issue spotted by @maxdeviant where chat text input was not being rendered correctly: ![image](https://github.com/zed-industries/zed/assets/24362066/9d7e6444-47b1-4ac2-808f-bf10404377c0) This was an issue with the previous attempt to upgrade to taffy 0.4.0 as well. We bail early in `compute_auto_height_layout` due to a missing width: https://github.com/zed-industries/zed/blob/df190ea84621837c44fa50c62837bdbea04b9e22/crates/editor/src/element.rs#L5266 The same issue is visible in story for auto-height editor (or rather, the breakage is visible - the editor simply does not render at all there). I tracked down the breakage to https://github.com/DioxusLabs/taffy/pull/573 ; it looks like it specifically affects editors with auto-height. In taffy <0.4 which we were using previously, we'd eventually get a proper width for auto-height EditorElement after having initially computed the size. With taffy 0.4 however (and specifically that PR mentioned earlier), we're getting `Size::NONE` in layout phase [^1]. I've noticed though that even with taffy <0.3, the `known_dimensions.width` was always equal to `available_space.width` in layout phase. Hence, I went with falling back to `available_space.width` when it is a definite value and we don't have a `known_dimensions.width`. Done this way, both chat input and auto-height story render correctly. /cc @as-cii Related: https://github.com/zed-industries/zed/pull/11606 https://github.com/zed-industries/zed/pull/11622 https://github.com/zed-industries/zed/pull/7868 https://github.com/zed-industries/zed/pull/7896 [^1]: This could possibly be related to change in what gets passed in https://github.com/DioxusLabs/taffy/pull/573/files#diff-60c916e9b0c507925f032cecdde6ae163e41b84b8e4bc0a6c04f7d846b0aad9eR133 , though I'm not sure if editor is a leaf node in this case Release Notes: - N/A
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
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 {
gpui: Bump taffy to 0.4.3 again (#11655) We reverted bump to taffy 0.4.3 following an issue spotted by @maxdeviant where chat text input was not being rendered correctly: ![image](https://github.com/zed-industries/zed/assets/24362066/9d7e6444-47b1-4ac2-808f-bf10404377c0) This was an issue with the previous attempt to upgrade to taffy 0.4.0 as well. We bail early in `compute_auto_height_layout` due to a missing width: https://github.com/zed-industries/zed/blob/df190ea84621837c44fa50c62837bdbea04b9e22/crates/editor/src/element.rs#L5266 The same issue is visible in story for auto-height editor (or rather, the breakage is visible - the editor simply does not render at all there). I tracked down the breakage to https://github.com/DioxusLabs/taffy/pull/573 ; it looks like it specifically affects editors with auto-height. In taffy <0.4 which we were using previously, we'd eventually get a proper width for auto-height EditorElement after having initially computed the size. With taffy 0.4 however (and specifically that PR mentioned earlier), we're getting `Size::NONE` in layout phase [^1]. I've noticed though that even with taffy <0.3, the `known_dimensions.width` was always equal to `available_space.width` in layout phase. Hence, I went with falling back to `available_space.width` when it is a definite value and we don't have a `known_dimensions.width`. Done this way, both chat input and auto-height story render correctly. /cc @as-cii Related: https://github.com/zed-industries/zed/pull/11606 https://github.com/zed-industries/zed/pull/11622 https://github.com/zed-industries/zed/pull/7868 https://github.com/zed-industries/zed/pull/7896 [^1]: This could possibly be related to change in what gets passed in https://github.com/DioxusLabs/taffy/pull/573/files#diff-60c916e9b0c507925f032cecdde6ae163e41b84b8e4bc0a6c04f7d846b0aad9eR133 , though I'm not sure if editor is a leaf node in this case Release Notes: - N/A
2024-05-10 13:05:50 +00:00
taffy: TaffyTree<()>,
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 {
gpui: Bump taffy to 0.4.3 again (#11655) We reverted bump to taffy 0.4.3 following an issue spotted by @maxdeviant where chat text input was not being rendered correctly: ![image](https://github.com/zed-industries/zed/assets/24362066/9d7e6444-47b1-4ac2-808f-bf10404377c0) This was an issue with the previous attempt to upgrade to taffy 0.4.0 as well. We bail early in `compute_auto_height_layout` due to a missing width: https://github.com/zed-industries/zed/blob/df190ea84621837c44fa50c62837bdbea04b9e22/crates/editor/src/element.rs#L5266 The same issue is visible in story for auto-height editor (or rather, the breakage is visible - the editor simply does not render at all there). I tracked down the breakage to https://github.com/DioxusLabs/taffy/pull/573 ; it looks like it specifically affects editors with auto-height. In taffy <0.4 which we were using previously, we'd eventually get a proper width for auto-height EditorElement after having initially computed the size. With taffy 0.4 however (and specifically that PR mentioned earlier), we're getting `Size::NONE` in layout phase [^1]. I've noticed though that even with taffy <0.3, the `known_dimensions.width` was always equal to `available_space.width` in layout phase. Hence, I went with falling back to `available_space.width` when it is a definite value and we don't have a `known_dimensions.width`. Done this way, both chat input and auto-height story render correctly. /cc @as-cii Related: https://github.com/zed-industries/zed/pull/11606 https://github.com/zed-industries/zed/pull/11622 https://github.com/zed-industries/zed/pull/7868 https://github.com/zed-industries/zed/pull/7896 [^1]: This could possibly be related to change in what gets passed in https://github.com/DioxusLabs/taffy/pull/573/files#diff-60c916e9b0c507925f032cecdde6ae163e41b84b8e4bc0a6c04f7d846b0aad9eR133 , though I'm not sure if editor is a leaf node in this case Release Notes: - N/A
2024-05-10 13:05:50 +00:00
taffy: TaffyTree::new(),
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
}
pub fn clear(&mut self) {
self.taffy.clear();
self.children_to_parents.clear();
self.absolute_layout_bounds.clear();
self.computed_layouts.clear();
self.nodes_to_measure.clear();
self.styles.clear();
}
pub fn request_layout(
2023-09-19 19:19:22 +00:00
&mut self,
style: Style,
2023-09-19 19:19:22 +00:00
rem_size: Pixels,
children: &[LayoutId],
2023-10-11 04:14:47 +00:00
) -> LayoutId {
let taffy_style = style.to_taffy(rem_size);
let layout_id = if children.is_empty() {
self.taffy
.new_leaf(taffy_style)
.expect(EXPECT_MESSAGE)
.into()
2023-09-19 19:19:22 +00:00
} else {
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.
.new_with_children(taffy_style, unsafe { std::mem::transmute(children) })
2023-10-11 04:14:47 +00:00
.expect(EXPECT_MESSAGE)
.into();
self.children_to_parents
.extend(children.into_iter().map(|child_id| (*child_id, parent_id)));
parent_id
};
self.styles.insert(layout_id, style);
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,
measure: impl FnMut(Size<Option<Pixels>>, Size<AvailableSpace>, &mut WindowContext) -> Size<Pixels>
+ 'static,
2023-10-11 04:14:47 +00:00
) -> LayoutId {
let taffy_style = style.to_taffy(rem_size);
2023-09-20 20:32:55 +00:00
let layout_id = self
.taffy
.new_leaf_with_context(taffy_style, ())
2023-10-11 04:14:47 +00:00
.expect(EXPECT_MESSAGE)
.into();
self.nodes_to_measure.insert(layout_id, Box::new(measure));
self.styles.insert(layout_id, style);
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> {
let mut count = 0;
for child in self.taffy.children(parent.0)? {
// 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> {
println!(
"{parent:?} at depth {depth} has {} children",
gpui: Bump taffy to 0.4.3 again (#11655) We reverted bump to taffy 0.4.3 following an issue spotted by @maxdeviant where chat text input was not being rendered correctly: ![image](https://github.com/zed-industries/zed/assets/24362066/9d7e6444-47b1-4ac2-808f-bf10404377c0) This was an issue with the previous attempt to upgrade to taffy 0.4.0 as well. We bail early in `compute_auto_height_layout` due to a missing width: https://github.com/zed-industries/zed/blob/df190ea84621837c44fa50c62837bdbea04b9e22/crates/editor/src/element.rs#L5266 The same issue is visible in story for auto-height editor (or rather, the breakage is visible - the editor simply does not render at all there). I tracked down the breakage to https://github.com/DioxusLabs/taffy/pull/573 ; it looks like it specifically affects editors with auto-height. In taffy <0.4 which we were using previously, we'd eventually get a proper width for auto-height EditorElement after having initially computed the size. With taffy 0.4 however (and specifically that PR mentioned earlier), we're getting `Size::NONE` in layout phase [^1]. I've noticed though that even with taffy <0.3, the `known_dimensions.width` was always equal to `available_space.width` in layout phase. Hence, I went with falling back to `available_space.width` when it is a definite value and we don't have a `known_dimensions.width`. Done this way, both chat input and auto-height story render correctly. /cc @as-cii Related: https://github.com/zed-industries/zed/pull/11606 https://github.com/zed-industries/zed/pull/11622 https://github.com/zed-industries/zed/pull/7868 https://github.com/zed-industries/zed/pull/7896 [^1]: This could possibly be related to change in what gets passed in https://github.com/DioxusLabs/taffy/pull/573/files#diff-60c916e9b0c507925f032cecdde6ae163e41b84b8e4bc0a6c04f7d846b0aad9eR133 , though I'm not sure if editor is a leaf node in this case Release Notes: - N/A
2024-05-10 13:05:50 +00:00
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)
}
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)>> {
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)
}
pub fn compute_layout(
&mut self,
id: LayoutId,
available_space: Size<AvailableSpace>,
cx: &mut WindowContext,
) {
// 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)?);
// 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!("");
//
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(
self.taffy
.children(id.into())
.expect(EXPECT_MESSAGE)
.into_iter()
.map(Into::into),
);
}
}
2023-10-23 15:41:22 +00:00
// let started_at = std::time::Instant::now();
self.taffy
2023-11-19 04:30:33 +00:00
.compute_layout_with_measure(
id.into(),
available_space.into(),
|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),
};
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-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
}
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(),
};
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())
}
}
}
}
impl<T, T2> From<TaffyPoint<T>> for Point<T2>
2023-09-19 19:19:22 +00:00
where
T: Into<T2>,
T2: Clone + Default + Debug,
2023-09-19 19:19:22 +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
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 {
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
}
}
}
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
}
}
}
impl<T, U> ToTaffy<TaffyRect<U>> for Edges<T>
2023-09-19 19:19:22 +00:00
where
T: ToTaffy<U> + Clone + Default + Debug,
2023-09-19 19:19:22 +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
}
}
}
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(),
}
}
}
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 {
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
#[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
#[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),
}
}
}