1use crate::{
2 AbsoluteLength, App, Bounds, DefiniteLength, Edges, Length, Pixels, Point, Size, Style, Window,
3 point, size,
4};
5use collections::{FxHashMap, FxHashSet};
6use smallvec::SmallVec;
7use stacksafe::{StackSafe, stacksafe};
8use std::{fmt::Debug, ops::Range};
9use taffy::{
10 TaffyTree, TraversePartialTree as _,
11 geometry::{Point as TaffyPoint, Rect as TaffyRect, Size as TaffySize},
12 style::AvailableSpace as TaffyAvailableSpace,
13 tree::NodeId,
14};
15
16type NodeMeasureFn = StackSafe<
17 Box<
18 dyn FnMut(
19 Size<Option<Pixels>>,
20 Size<AvailableSpace>,
21 &mut Window,
22 &mut App,
23 ) -> Size<Pixels>,
24 >,
25>;
26
27struct NodeContext {
28 measure: NodeMeasureFn,
29}
30pub struct TaffyLayoutEngine {
31 taffy: TaffyTree<NodeContext>,
32 absolute_layout_bounds: FxHashMap<LayoutId, Bounds<Pixels>>,
33 computed_layouts: FxHashSet<LayoutId>,
34}
35
36const EXPECT_MESSAGE: &str = "we should avoid taffy layout errors by construction if possible";
37
38impl TaffyLayoutEngine {
39 pub fn new() -> Self {
40 let mut taffy = TaffyTree::new();
41 taffy.enable_rounding();
42 TaffyLayoutEngine {
43 taffy,
44 absolute_layout_bounds: FxHashMap::default(),
45 computed_layouts: FxHashSet::default(),
46 }
47 }
48
49 pub fn clear(&mut self) {
50 self.taffy.clear();
51 self.absolute_layout_bounds.clear();
52 self.computed_layouts.clear();
53 }
54
55 pub fn request_layout(
56 &mut self,
57 style: Style,
58 rem_size: Pixels,
59 scale_factor: f32,
60 children: &[LayoutId],
61 ) -> LayoutId {
62 let taffy_style = style.to_taffy(rem_size, scale_factor);
63
64 if children.is_empty() {
65 self.taffy
66 .new_leaf(taffy_style)
67 .expect(EXPECT_MESSAGE)
68 .into()
69 } else {
70 self.taffy
71 // This is safe because LayoutId is repr(transparent) to taffy::tree::NodeId.
72 .new_with_children(taffy_style, LayoutId::to_taffy_slice(children))
73 .expect(EXPECT_MESSAGE)
74 .into()
75 }
76 }
77
78 pub fn request_measured_layout(
79 &mut self,
80 style: Style,
81 rem_size: Pixels,
82 scale_factor: f32,
83 measure: impl FnMut(
84 Size<Option<Pixels>>,
85 Size<AvailableSpace>,
86 &mut Window,
87 &mut App,
88 ) -> Size<Pixels>
89 + 'static,
90 ) -> LayoutId {
91 let taffy_style = style.to_taffy(rem_size, scale_factor);
92
93 self.taffy
94 .new_leaf_with_context(
95 taffy_style,
96 NodeContext {
97 measure: StackSafe::new(Box::new(measure)),
98 },
99 )
100 .expect(EXPECT_MESSAGE)
101 .into()
102 }
103
104 // Used to understand performance
105 #[allow(dead_code)]
106 fn count_all_children(&self, parent: LayoutId) -> anyhow::Result<u32> {
107 let mut count = 0;
108
109 for child in self.taffy.children(parent.0)? {
110 // Count this child.
111 count += 1;
112
113 // Count all of this child's children.
114 count += self.count_all_children(LayoutId(child))?
115 }
116
117 Ok(count)
118 }
119
120 // Used to understand performance
121 #[allow(dead_code)]
122 fn max_depth(&self, depth: u32, parent: LayoutId) -> anyhow::Result<u32> {
123 println!(
124 "{parent:?} at depth {depth} has {} children",
125 self.taffy.child_count(parent.0)
126 );
127
128 let mut max_child_depth = 0;
129
130 for child in self.taffy.children(parent.0)? {
131 max_child_depth = std::cmp::max(max_child_depth, self.max_depth(0, LayoutId(child))?);
132 }
133
134 Ok(depth + 1 + max_child_depth)
135 }
136
137 // Used to understand performance
138 #[allow(dead_code)]
139 fn get_edges(&self, parent: LayoutId) -> anyhow::Result<Vec<(LayoutId, LayoutId)>> {
140 let mut edges = Vec::new();
141
142 for child in self.taffy.children(parent.0)? {
143 edges.push((parent, LayoutId(child)));
144
145 edges.extend(self.get_edges(LayoutId(child))?);
146 }
147
148 Ok(edges)
149 }
150
151 #[stacksafe]
152 pub fn compute_layout(
153 &mut self,
154 id: LayoutId,
155 available_space: Size<AvailableSpace>,
156 window: &mut Window,
157 cx: &mut App,
158 ) {
159 // Leaving this here until we have a better instrumentation approach.
160 // println!("Laying out {} children", self.count_all_children(id)?);
161 // println!("Max layout depth: {}", self.max_depth(0, id)?);
162
163 // Output the edges (branches) of the tree in Mermaid format for visualization.
164 // println!("Edges:");
165 // for (a, b) in self.get_edges(id)? {
166 // println!("N{} --> N{}", u64::from(a), u64::from(b));
167 // }
168 //
169
170 if !self.computed_layouts.insert(id) {
171 let mut stack = SmallVec::<[LayoutId; 64]>::new();
172 stack.push(id);
173 while let Some(id) = stack.pop() {
174 self.absolute_layout_bounds.remove(&id);
175 stack.extend(
176 self.taffy
177 .children(id.into())
178 .expect(EXPECT_MESSAGE)
179 .into_iter()
180 .map(Into::into),
181 );
182 }
183 }
184
185 let scale_factor = window.scale_factor();
186
187 let transform = |v: AvailableSpace| match v {
188 AvailableSpace::Definite(pixels) => {
189 AvailableSpace::Definite(Pixels(pixels.0 * scale_factor))
190 }
191 AvailableSpace::MinContent => AvailableSpace::MinContent,
192 AvailableSpace::MaxContent => AvailableSpace::MaxContent,
193 };
194 let available_space = size(
195 transform(available_space.width),
196 transform(available_space.height),
197 );
198
199 self.taffy
200 .compute_layout_with_measure(
201 id.into(),
202 available_space.into(),
203 |known_dimensions, available_space, _id, node_context, _style| {
204 let Some(node_context) = node_context else {
205 return taffy::geometry::Size::default();
206 };
207
208 let known_dimensions = Size {
209 width: known_dimensions.width.map(|e| Pixels(e / scale_factor)),
210 height: known_dimensions.height.map(|e| Pixels(e / scale_factor)),
211 };
212
213 let available_space: Size<AvailableSpace> = available_space.into();
214 let untransform = |ev: AvailableSpace| match ev {
215 AvailableSpace::Definite(pixels) => {
216 AvailableSpace::Definite(Pixels(pixels.0 / scale_factor))
217 }
218 AvailableSpace::MinContent => AvailableSpace::MinContent,
219 AvailableSpace::MaxContent => AvailableSpace::MaxContent,
220 };
221 let available_space = size(
222 untransform(available_space.width),
223 untransform(available_space.height),
224 );
225
226 let a: Size<Pixels> =
227 (node_context.measure)(known_dimensions, available_space, window, cx);
228 size(a.width.0 * scale_factor, a.height.0 * scale_factor).into()
229 },
230 )
231 .expect(EXPECT_MESSAGE);
232 }
233
234 pub fn layout_bounds(&mut self, id: LayoutId, scale_factor: f32) -> Bounds<Pixels> {
235 if let Some(layout) = self.absolute_layout_bounds.get(&id).cloned() {
236 return layout;
237 }
238
239 let layout = self.taffy.layout(id.into()).expect(EXPECT_MESSAGE);
240 let mut bounds = Bounds {
241 origin: point(
242 Pixels(layout.location.x / scale_factor),
243 Pixels(layout.location.y / scale_factor),
244 ),
245 size: size(
246 Pixels(layout.size.width / scale_factor),
247 Pixels(layout.size.height / scale_factor),
248 ),
249 };
250
251 if let Some(parent_id) = self.taffy.parent(id.0) {
252 let parent_bounds = self.layout_bounds(parent_id.into(), scale_factor);
253 bounds.origin += parent_bounds.origin;
254 }
255 self.absolute_layout_bounds.insert(id, bounds);
256
257 bounds
258 }
259}
260
261/// A unique identifier for a layout node, generated when requesting a layout from Taffy
262#[derive(Copy, Clone, Eq, PartialEq, Debug)]
263#[repr(transparent)]
264pub struct LayoutId(NodeId);
265
266impl LayoutId {
267 fn to_taffy_slice(node_ids: &[Self]) -> &[taffy::NodeId] {
268 // SAFETY: LayoutId is repr(transparent) to taffy::tree::NodeId.
269 unsafe { std::mem::transmute::<&[LayoutId], &[taffy::NodeId]>(node_ids) }
270 }
271}
272
273impl std::hash::Hash for LayoutId {
274 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
275 u64::from(self.0).hash(state);
276 }
277}
278
279impl From<NodeId> for LayoutId {
280 fn from(node_id: NodeId) -> Self {
281 Self(node_id)
282 }
283}
284
285impl From<LayoutId> for NodeId {
286 fn from(layout_id: LayoutId) -> NodeId {
287 layout_id.0
288 }
289}
290
291trait ToTaffy<Output> {
292 fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> Output;
293}
294
295impl ToTaffy<taffy::style::Style> for Style {
296 fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::Style {
297 use taffy::style_helpers::{fr, length, minmax, repeat};
298
299 fn to_grid_line(
300 placement: &Range<crate::GridPlacement>,
301 ) -> taffy::Line<taffy::GridPlacement> {
302 taffy::Line {
303 start: placement.start.into(),
304 end: placement.end.into(),
305 }
306 }
307
308 fn to_grid_repeat<T: taffy::style::CheapCloneStr>(
309 unit: &Option<u16>,
310 ) -> Vec<taffy::GridTemplateComponent<T>> {
311 // grid-template-columns: repeat(<number>, minmax(0, 1fr));
312 unit.map(|count| vec![repeat(count, vec![minmax(length(0.0), fr(1.0))])])
313 .unwrap_or_default()
314 }
315
316 taffy::style::Style {
317 display: self.display.into(),
318 overflow: self.overflow.into(),
319 scrollbar_width: self.scrollbar_width.to_taffy(rem_size, scale_factor),
320 position: self.position.into(),
321 inset: self.inset.to_taffy(rem_size, scale_factor),
322 size: self.size.to_taffy(rem_size, scale_factor),
323 min_size: self.min_size.to_taffy(rem_size, scale_factor),
324 max_size: self.max_size.to_taffy(rem_size, scale_factor),
325 aspect_ratio: self.aspect_ratio,
326 margin: self.margin.to_taffy(rem_size, scale_factor),
327 padding: self.padding.to_taffy(rem_size, scale_factor),
328 border: self.border_widths.to_taffy(rem_size, scale_factor),
329 align_items: self.align_items.map(|x| x.into()),
330 align_self: self.align_self.map(|x| x.into()),
331 align_content: self.align_content.map(|x| x.into()),
332 justify_content: self.justify_content.map(|x| x.into()),
333 gap: self.gap.to_taffy(rem_size, scale_factor),
334 flex_direction: self.flex_direction.into(),
335 flex_wrap: self.flex_wrap.into(),
336 flex_basis: self.flex_basis.to_taffy(rem_size, scale_factor),
337 flex_grow: self.flex_grow,
338 flex_shrink: self.flex_shrink,
339 grid_template_rows: to_grid_repeat(&self.grid_rows),
340 grid_template_columns: to_grid_repeat(&self.grid_cols),
341 grid_row: self
342 .grid_location
343 .as_ref()
344 .map(|location| to_grid_line(&location.row))
345 .unwrap_or_default(),
346 grid_column: self
347 .grid_location
348 .as_ref()
349 .map(|location| to_grid_line(&location.column))
350 .unwrap_or_default(),
351 ..Default::default()
352 }
353 }
354}
355
356impl ToTaffy<f32> for AbsoluteLength {
357 fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> f32 {
358 match self {
359 AbsoluteLength::Pixels(pixels) => {
360 let pixels: f32 = pixels.into();
361 pixels * scale_factor
362 }
363 AbsoluteLength::Rems(rems) => {
364 let pixels: f32 = (*rems * rem_size).into();
365 pixels * scale_factor
366 }
367 }
368 }
369}
370
371impl ToTaffy<taffy::style::LengthPercentageAuto> for Length {
372 fn to_taffy(
373 &self,
374 rem_size: Pixels,
375 scale_factor: f32,
376 ) -> taffy::prelude::LengthPercentageAuto {
377 match self {
378 Length::Definite(length) => length.to_taffy(rem_size, scale_factor),
379 Length::Auto => taffy::prelude::LengthPercentageAuto::auto(),
380 }
381 }
382}
383
384impl ToTaffy<taffy::style::Dimension> for Length {
385 fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::prelude::Dimension {
386 match self {
387 Length::Definite(length) => length.to_taffy(rem_size, scale_factor),
388 Length::Auto => taffy::prelude::Dimension::auto(),
389 }
390 }
391}
392
393impl ToTaffy<taffy::style::LengthPercentage> for DefiniteLength {
394 fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::LengthPercentage {
395 match self {
396 DefiniteLength::Absolute(length) => match length {
397 AbsoluteLength::Pixels(pixels) => {
398 let pixels: f32 = pixels.into();
399 taffy::style::LengthPercentage::length(pixels * scale_factor)
400 }
401 AbsoluteLength::Rems(rems) => {
402 let pixels: f32 = (*rems * rem_size).into();
403 taffy::style::LengthPercentage::length(pixels * scale_factor)
404 }
405 },
406 DefiniteLength::Fraction(fraction) => {
407 taffy::style::LengthPercentage::percent(*fraction)
408 }
409 }
410 }
411}
412
413impl ToTaffy<taffy::style::LengthPercentageAuto> for DefiniteLength {
414 fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::LengthPercentageAuto {
415 match self {
416 DefiniteLength::Absolute(length) => match length {
417 AbsoluteLength::Pixels(pixels) => {
418 let pixels: f32 = pixels.into();
419 taffy::style::LengthPercentageAuto::length(pixels * scale_factor)
420 }
421 AbsoluteLength::Rems(rems) => {
422 let pixels: f32 = (*rems * rem_size).into();
423 taffy::style::LengthPercentageAuto::length(pixels * scale_factor)
424 }
425 },
426 DefiniteLength::Fraction(fraction) => {
427 taffy::style::LengthPercentageAuto::percent(*fraction)
428 }
429 }
430 }
431}
432
433impl ToTaffy<taffy::style::Dimension> for DefiniteLength {
434 fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::Dimension {
435 match self {
436 DefiniteLength::Absolute(length) => match length {
437 AbsoluteLength::Pixels(pixels) => {
438 let pixels: f32 = pixels.into();
439 taffy::style::Dimension::length(pixels * scale_factor)
440 }
441 AbsoluteLength::Rems(rems) => {
442 taffy::style::Dimension::length((*rems * rem_size * scale_factor).into())
443 }
444 },
445 DefiniteLength::Fraction(fraction) => taffy::style::Dimension::percent(*fraction),
446 }
447 }
448}
449
450impl ToTaffy<taffy::style::LengthPercentage> for AbsoluteLength {
451 fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> taffy::style::LengthPercentage {
452 match self {
453 AbsoluteLength::Pixels(pixels) => {
454 let pixels: f32 = pixels.into();
455 taffy::style::LengthPercentage::length(pixels * scale_factor)
456 }
457 AbsoluteLength::Rems(rems) => {
458 let pixels: f32 = (*rems * rem_size).into();
459 taffy::style::LengthPercentage::length(pixels * scale_factor)
460 }
461 }
462 }
463}
464
465impl<T, T2> From<TaffyPoint<T>> for Point<T2>
466where
467 T: Into<T2>,
468 T2: Clone + Debug + Default + PartialEq,
469{
470 fn from(point: TaffyPoint<T>) -> Point<T2> {
471 Point {
472 x: point.x.into(),
473 y: point.y.into(),
474 }
475 }
476}
477
478impl<T, T2> From<Point<T>> for TaffyPoint<T2>
479where
480 T: Into<T2> + Clone + Debug + Default + PartialEq,
481{
482 fn from(val: Point<T>) -> Self {
483 TaffyPoint {
484 x: val.x.into(),
485 y: val.y.into(),
486 }
487 }
488}
489
490impl<T, U> ToTaffy<TaffySize<U>> for Size<T>
491where
492 T: ToTaffy<U> + Clone + Debug + Default + PartialEq,
493{
494 fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> TaffySize<U> {
495 TaffySize {
496 width: self.width.to_taffy(rem_size, scale_factor),
497 height: self.height.to_taffy(rem_size, scale_factor),
498 }
499 }
500}
501
502impl<T, U> ToTaffy<TaffyRect<U>> for Edges<T>
503where
504 T: ToTaffy<U> + Clone + Debug + Default + PartialEq,
505{
506 fn to_taffy(&self, rem_size: Pixels, scale_factor: f32) -> TaffyRect<U> {
507 TaffyRect {
508 top: self.top.to_taffy(rem_size, scale_factor),
509 right: self.right.to_taffy(rem_size, scale_factor),
510 bottom: self.bottom.to_taffy(rem_size, scale_factor),
511 left: self.left.to_taffy(rem_size, scale_factor),
512 }
513 }
514}
515
516impl<T, U> From<TaffySize<T>> for Size<U>
517where
518 T: Into<U>,
519 U: Clone + Debug + Default + PartialEq,
520{
521 fn from(taffy_size: TaffySize<T>) -> Self {
522 Size {
523 width: taffy_size.width.into(),
524 height: taffy_size.height.into(),
525 }
526 }
527}
528
529impl<T, U> From<Size<T>> for TaffySize<U>
530where
531 T: Into<U> + Clone + Debug + Default + PartialEq,
532{
533 fn from(size: Size<T>) -> Self {
534 TaffySize {
535 width: size.width.into(),
536 height: size.height.into(),
537 }
538 }
539}
540
541/// The space available for an element to be laid out in
542#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)]
543pub enum AvailableSpace {
544 /// The amount of space available is the specified number of pixels
545 Definite(Pixels),
546 /// The amount of space available is indefinite and the node should be laid out under a min-content constraint
547 #[default]
548 MinContent,
549 /// The amount of space available is indefinite and the node should be laid out under a max-content constraint
550 MaxContent,
551}
552
553impl AvailableSpace {
554 /// Returns a `Size` with both width and height set to `AvailableSpace::MinContent`.
555 ///
556 /// This function is useful when you want to create a `Size` with the minimum content constraints
557 /// for both dimensions.
558 ///
559 /// # Examples
560 ///
561 /// ```
562 /// use gpui::AvailableSpace;
563 /// let min_content_size = AvailableSpace::min_size();
564 /// assert_eq!(min_content_size.width, AvailableSpace::MinContent);
565 /// assert_eq!(min_content_size.height, AvailableSpace::MinContent);
566 /// ```
567 pub const fn min_size() -> Size<Self> {
568 Size {
569 width: Self::MinContent,
570 height: Self::MinContent,
571 }
572 }
573}
574
575impl From<AvailableSpace> for TaffyAvailableSpace {
576 fn from(space: AvailableSpace) -> TaffyAvailableSpace {
577 match space {
578 AvailableSpace::Definite(Pixels(value)) => TaffyAvailableSpace::Definite(value),
579 AvailableSpace::MinContent => TaffyAvailableSpace::MinContent,
580 AvailableSpace::MaxContent => TaffyAvailableSpace::MaxContent,
581 }
582 }
583}
584
585impl From<TaffyAvailableSpace> for AvailableSpace {
586 fn from(space: TaffyAvailableSpace) -> AvailableSpace {
587 match space {
588 TaffyAvailableSpace::Definite(value) => AvailableSpace::Definite(Pixels(value)),
589 TaffyAvailableSpace::MinContent => AvailableSpace::MinContent,
590 TaffyAvailableSpace::MaxContent => AvailableSpace::MaxContent,
591 }
592 }
593}
594
595impl From<Pixels> for AvailableSpace {
596 fn from(pixels: Pixels) -> Self {
597 AvailableSpace::Definite(pixels)
598 }
599}
600
601impl From<Size<Pixels>> for Size<AvailableSpace> {
602 fn from(size: Size<Pixels>) -> Self {
603 Size {
604 width: AvailableSpace::Definite(size.width),
605 height: AvailableSpace::Definite(size.height),
606 }
607 }
608}