Re-introduce auto-height in editor2 (#3471)

Release Notes:

- N/A
This commit is contained in:
Antonio Scandurra
2023-12-01 12:02:45 +01:00
committed by GitHub
14 changed files with 211 additions and 698 deletions
+4 -6
View File
@@ -144,7 +144,6 @@ impl TextState {
runs: Option<Vec<TextRun>>,
cx: &mut WindowContext,
) -> LayoutId {
let text_system = cx.text_system().clone();
let text_style = cx.text_style();
let font_size = text_style.font_size.to_pixels(cx.rem_size());
let line_height = text_style
@@ -152,18 +151,16 @@ impl TextState {
.to_pixels(font_size.into(), cx.rem_size());
let text = SharedString::from(text);
let rem_size = cx.rem_size();
let runs = if let Some(runs) = runs {
runs
} else {
vec![text_style.to_run(text.len())]
};
let layout_id = cx.request_measured_layout(Default::default(), rem_size, {
let layout_id = cx.request_measured_layout(Default::default(), {
let element_state = self.clone();
move |known_dimensions, available_space| {
move |known_dimensions, available_space, cx| {
let wrap_width = if text_style.white_space == WhiteSpace::Normal {
known_dimensions.width.or(match available_space.width {
crate::AvailableSpace::Definite(x) => Some(x),
@@ -181,7 +178,8 @@ impl TextState {
}
}
let Some(lines) = text_system
let Some(lines) = cx
.text_system()
.shape_text(
&text, font_size, &runs, wrap_width, // Wrap if we know the width.
)
+1 -4
View File
@@ -109,7 +109,6 @@ impl Element for UniformList {
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
let max_items = self.item_count;
let rem_size = cx.rem_size();
let item_size = state
.as_ref()
.map(|s| s.item_size)
@@ -120,9 +119,7 @@ impl Element for UniformList {
.layout(state.map(|s| s.interactive), cx, |style, cx| {
cx.request_measured_layout(
style,
rem_size,
move |known_dimensions: Size<Option<Pixels>>,
available_space: Size<AvailableSpace>| {
move |known_dimensions, available_space, _cx| {
let desired_height = item_size.height * max_items;
let width =
known_dimensions
+34 -15
View File
@@ -1,4 +1,7 @@
use super::{AbsoluteLength, Bounds, DefiniteLength, Edges, Length, Pixels, Point, Size, Style};
use crate::{
AbsoluteLength, Bounds, DefiniteLength, Edges, Length, Pixels, Point, Size, Style,
WindowContext,
};
use collections::{HashMap, HashSet};
use smallvec::SmallVec;
use std::fmt::Debug;
@@ -9,13 +12,21 @@ use taffy::{
Taffy,
};
type Measureable = dyn Fn(Size<Option<Pixels>>, Size<AvailableSpace>) -> Size<Pixels> + Send + Sync;
pub struct TaffyLayoutEngine {
taffy: Taffy<Box<Measureable>>,
taffy: Taffy,
children_to_parents: HashMap<LayoutId, LayoutId>,
absolute_layout_bounds: HashMap<LayoutId, Bounds<Pixels>>,
computed_layouts: HashSet<LayoutId>,
nodes_to_measure: HashMap<
LayoutId,
Box<
dyn FnMut(
Size<Option<Pixels>>,
Size<AvailableSpace>,
&mut WindowContext,
) -> Size<Pixels>,
>,
>,
}
static EXPECT_MESSAGE: &'static str =
@@ -28,6 +39,7 @@ impl TaffyLayoutEngine {
children_to_parents: HashMap::default(),
absolute_layout_bounds: HashMap::default(),
computed_layouts: HashSet::default(),
nodes_to_measure: HashMap::default(),
}
}
@@ -36,6 +48,7 @@ impl TaffyLayoutEngine {
self.children_to_parents.clear();
self.absolute_layout_bounds.clear();
self.computed_layouts.clear();
self.nodes_to_measure.clear();
}
pub fn request_layout(
@@ -65,18 +78,18 @@ impl TaffyLayoutEngine {
&mut self,
style: Style,
rem_size: Pixels,
measure: impl Fn(Size<Option<Pixels>>, Size<AvailableSpace>) -> Size<Pixels>
+ Send
+ Sync
measure: impl FnMut(Size<Option<Pixels>>, Size<AvailableSpace>, &mut WindowContext) -> Size<Pixels>
+ 'static,
) -> LayoutId {
let style = style.to_taffy(rem_size);
let measurable = Box::new(measure);
self.taffy
.new_leaf_with_context(style, measurable)
let layout_id = self
.taffy
.new_leaf_with_context(style, ())
.expect(EXPECT_MESSAGE)
.into()
.into();
self.nodes_to_measure.insert(layout_id, Box::new(measure));
layout_id
}
// Used to understand performance
@@ -126,7 +139,12 @@ impl TaffyLayoutEngine {
Ok(edges)
}
pub fn compute_layout(&mut self, id: LayoutId, available_space: Size<AvailableSpace>) {
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.
// println!("Laying out {} children", self.count_all_children(id)?);
// println!("Max layout depth: {}", self.max_depth(0, id)?);
@@ -159,8 +177,8 @@ impl TaffyLayoutEngine {
.compute_layout_with_measure(
id.into(),
available_space.into(),
|known_dimensions, available_space, _node_id, context| {
let Some(measure) = context else {
|known_dimensions, available_space, node_id, _context| {
let Some(measure) = self.nodes_to_measure.get_mut(&node_id.into()) else {
return taffy::geometry::Size::default();
};
@@ -169,10 +187,11 @@ impl TaffyLayoutEngine {
height: known_dimensions.height.map(Pixels),
};
measure(known_dimensions, available_space.into()).into()
measure(known_dimensions, available_space.into(), cx).into()
},
)
.expect(EXPECT_MESSAGE);
// println!("compute_layout took {:?}", started_at.elapsed());
}
+1 -3
View File
@@ -209,9 +209,7 @@ impl AnyView {
) {
cx.with_absolute_element_offset(origin, |cx| {
let (layout_id, rendered_element) = (self.layout)(self, cx);
cx.window
.layout_engine
.compute_layout(layout_id, available_space);
cx.compute_layout(layout_id, available_space);
(self.paint)(self, rendered_element, cx);
})
}
+18 -11
View File
@@ -209,7 +209,7 @@ pub struct Window {
sprite_atlas: Arc<dyn PlatformAtlas>,
rem_size: Pixels,
viewport_size: Size<Pixels>,
pub(crate) layout_engine: TaffyLayoutEngine,
layout_engine: Option<TaffyLayoutEngine>,
pub(crate) root_view: Option<AnyView>,
pub(crate) element_id_stack: GlobalElementId,
pub(crate) previous_frame: Frame,
@@ -327,7 +327,7 @@ impl Window {
sprite_atlas,
rem_size: px(16.),
viewport_size: content_size,
layout_engine: TaffyLayoutEngine::new(),
layout_engine: Some(TaffyLayoutEngine::new()),
root_view: None,
element_id_stack: GlobalElementId::default(),
previous_frame: Frame::new(DispatchTree::new(cx.keymap.clone(), cx.actions.clone())),
@@ -606,9 +606,11 @@ impl<'a> WindowContext<'a> {
self.app.layout_id_buffer.extend(children.into_iter());
let rem_size = self.rem_size();
self.window
.layout_engine
.request_layout(style, rem_size, &self.app.layout_id_buffer)
self.window.layout_engine.as_mut().unwrap().request_layout(
style,
rem_size,
&self.app.layout_id_buffer,
)
}
/// Add a node to the layout tree for the current frame. Instead of taking a `Style` and children,
@@ -618,22 +620,25 @@ impl<'a> WindowContext<'a> {
/// The given closure is invoked at layout time with the known dimensions and available space and
/// returns a `Size`.
pub fn request_measured_layout<
F: Fn(Size<Option<Pixels>>, Size<AvailableSpace>) -> Size<Pixels> + Send + Sync + 'static,
F: FnMut(Size<Option<Pixels>>, Size<AvailableSpace>, &mut WindowContext) -> Size<Pixels>
+ 'static,
>(
&mut self,
style: Style,
rem_size: Pixels,
measure: F,
) -> LayoutId {
let rem_size = self.rem_size();
self.window
.layout_engine
.as_mut()
.unwrap()
.request_measured_layout(style, rem_size, measure)
}
pub fn compute_layout(&mut self, layout_id: LayoutId, available_space: Size<AvailableSpace>) {
self.window
.layout_engine
.compute_layout(layout_id, available_space)
let mut layout_engine = self.window.layout_engine.take().unwrap();
layout_engine.compute_layout(layout_id, available_space, self);
self.window.layout_engine = Some(layout_engine);
}
/// Obtain the bounds computed for the given LayoutId relative to the window. This method should not
@@ -643,6 +648,8 @@ impl<'a> WindowContext<'a> {
let mut bounds = self
.window
.layout_engine
.as_mut()
.unwrap()
.layout_bounds(layout_id)
.map(Into::into);
bounds.origin += self.element_offset();
@@ -1193,7 +1200,7 @@ impl<'a> WindowContext<'a> {
self.text_system().start_frame();
let window = &mut *self.window;
window.layout_engine.clear();
window.layout_engine.as_mut().unwrap().clear();
mem::swap(&mut window.previous_frame, &mut window.current_frame);
let frame = &mut window.current_frame;