Start on caching views
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
5904bcf1c2
commit
84c36066bc
7 changed files with 117 additions and 58 deletions
|
@ -2272,7 +2272,7 @@ impl From<f64> for GlobalPixels {
|
||||||
/// For example, if the root element's font-size is `16px`, then `1rem` equals `16px`. A length of `2rems` would then be `32px`.
|
/// For example, if the root element's font-size is `16px`, then `1rem` equals `16px`. A length of `2rems` would then be `32px`.
|
||||||
///
|
///
|
||||||
/// [set_rem_size]: crate::WindowContext::set_rem_size
|
/// [set_rem_size]: crate::WindowContext::set_rem_size
|
||||||
#[derive(Clone, Copy, Default, Add, Sub, Mul, Div, Neg)]
|
#[derive(Clone, Copy, Default, Add, Sub, Mul, Div, Neg, PartialEq)]
|
||||||
pub struct Rems(pub f32);
|
pub struct Rems(pub f32);
|
||||||
|
|
||||||
impl Mul<Pixels> for Rems {
|
impl Mul<Pixels> for Rems {
|
||||||
|
@ -2295,7 +2295,7 @@ impl Debug for Rems {
|
||||||
/// affected by the current font size, or a number of rems, which is relative to the font size of
|
/// affected by the current font size, or a number of rems, which is relative to the font size of
|
||||||
/// the root element. It is used for specifying dimensions that are either independent of or
|
/// the root element. It is used for specifying dimensions that are either independent of or
|
||||||
/// related to the typographic scale.
|
/// related to the typographic scale.
|
||||||
#[derive(Clone, Copy, Debug, Neg)]
|
#[derive(Clone, Copy, Debug, Neg, PartialEq)]
|
||||||
pub enum AbsoluteLength {
|
pub enum AbsoluteLength {
|
||||||
/// A length in pixels.
|
/// A length in pixels.
|
||||||
Pixels(Pixels),
|
Pixels(Pixels),
|
||||||
|
@ -2366,7 +2366,7 @@ impl Default for AbsoluteLength {
|
||||||
/// This enum represents lengths that have a specific value, as opposed to lengths that are automatically
|
/// This enum represents lengths that have a specific value, as opposed to lengths that are automatically
|
||||||
/// determined by the context. It includes absolute lengths in pixels or rems, and relative lengths as a
|
/// determined by the context. It includes absolute lengths in pixels or rems, and relative lengths as a
|
||||||
/// fraction of the parent's size.
|
/// fraction of the parent's size.
|
||||||
#[derive(Clone, Copy, Neg)]
|
#[derive(Clone, Copy, Neg, PartialEq)]
|
||||||
pub enum DefiniteLength {
|
pub enum DefiniteLength {
|
||||||
/// An absolute length specified in pixels or rems.
|
/// An absolute length specified in pixels or rems.
|
||||||
Absolute(AbsoluteLength),
|
Absolute(AbsoluteLength),
|
||||||
|
|
|
@ -146,7 +146,7 @@ pub enum WhiteSpace {
|
||||||
Nowrap,
|
Nowrap,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Refineable, Clone, Debug)]
|
#[derive(Refineable, Clone, Debug, PartialEq)]
|
||||||
#[refineable(Debug)]
|
#[refineable(Debug)]
|
||||||
pub struct TextStyle {
|
pub struct TextStyle {
|
||||||
pub color: Hsla,
|
pub color: Hsla,
|
||||||
|
|
|
@ -12,6 +12,7 @@ use taffy::{
|
||||||
|
|
||||||
pub struct TaffyLayoutEngine {
|
pub struct TaffyLayoutEngine {
|
||||||
tree: TaffyTree,
|
tree: TaffyTree,
|
||||||
|
styles: FxHashMap<LayoutId, Style>,
|
||||||
absolute_layout_bounds: FxHashMap<LayoutId, Bounds<Pixels>>,
|
absolute_layout_bounds: FxHashMap<LayoutId, Bounds<Pixels>>,
|
||||||
computed_layouts: FxHashSet<LayoutId>,
|
computed_layouts: FxHashSet<LayoutId>,
|
||||||
nodes_to_measure: FxHashMap<
|
nodes_to_measure: FxHashMap<
|
||||||
|
@ -32,6 +33,7 @@ impl TaffyLayoutEngine {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
TaffyLayoutEngine {
|
TaffyLayoutEngine {
|
||||||
tree: TaffyTree::new(),
|
tree: TaffyTree::new(),
|
||||||
|
styles: FxHashMap::default(),
|
||||||
absolute_layout_bounds: FxHashMap::default(),
|
absolute_layout_bounds: FxHashMap::default(),
|
||||||
computed_layouts: FxHashSet::default(),
|
computed_layouts: FxHashSet::default(),
|
||||||
nodes_to_measure: FxHashMap::default(),
|
nodes_to_measure: FxHashMap::default(),
|
||||||
|
@ -43,6 +45,11 @@ impl TaffyLayoutEngine {
|
||||||
self.absolute_layout_bounds.clear();
|
self.absolute_layout_bounds.clear();
|
||||||
self.computed_layouts.clear();
|
self.computed_layouts.clear();
|
||||||
self.nodes_to_measure.clear();
|
self.nodes_to_measure.clear();
|
||||||
|
self.styles.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn requested_style(&self, layout_id: LayoutId) -> Option<&Style> {
|
||||||
|
self.styles.get(&layout_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn request_layout(
|
pub fn request_layout(
|
||||||
|
@ -51,16 +58,21 @@ impl TaffyLayoutEngine {
|
||||||
rem_size: Pixels,
|
rem_size: Pixels,
|
||||||
children: &[LayoutId],
|
children: &[LayoutId],
|
||||||
) -> LayoutId {
|
) -> LayoutId {
|
||||||
let style = style.to_taffy(rem_size);
|
let taffy_style = style.to_taffy(rem_size);
|
||||||
if children.is_empty() {
|
let layout_id = if children.is_empty() {
|
||||||
self.tree.new_leaf(style).expect(EXPECT_MESSAGE).into()
|
self.tree
|
||||||
|
.new_leaf(taffy_style)
|
||||||
|
.expect(EXPECT_MESSAGE)
|
||||||
|
.into()
|
||||||
} else {
|
} else {
|
||||||
self.tree
|
self.tree
|
||||||
// This is safe because LayoutId is repr(transparent) to taffy::tree::NodeId.
|
// This is safe because LayoutId is repr(transparent) to taffy::tree::NodeId.
|
||||||
.new_with_children(style, unsafe { std::mem::transmute(children) })
|
.new_with_children(taffy_style, unsafe { std::mem::transmute(children) })
|
||||||
.expect(EXPECT_MESSAGE)
|
.expect(EXPECT_MESSAGE)
|
||||||
.into()
|
.into()
|
||||||
}
|
};
|
||||||
|
self.styles.insert(layout_id, style.clone());
|
||||||
|
layout_id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn request_measured_layout(
|
pub fn request_measured_layout(
|
||||||
|
@ -70,14 +82,16 @@ impl TaffyLayoutEngine {
|
||||||
measure: impl FnMut(Size<Option<Pixels>>, Size<AvailableSpace>, &mut WindowContext) -> Size<Pixels>
|
measure: impl FnMut(Size<Option<Pixels>>, Size<AvailableSpace>, &mut WindowContext) -> Size<Pixels>
|
||||||
+ 'static,
|
+ 'static,
|
||||||
) -> LayoutId {
|
) -> LayoutId {
|
||||||
let style = style.to_taffy(rem_size);
|
let style = style.clone();
|
||||||
|
let taffy_style = style.to_taffy(rem_size);
|
||||||
|
|
||||||
let layout_id = self
|
let layout_id = self
|
||||||
.tree
|
.tree
|
||||||
.new_leaf_with_context(style, ())
|
.new_leaf_with_context(taffy_style, ())
|
||||||
.expect(EXPECT_MESSAGE)
|
.expect(EXPECT_MESSAGE)
|
||||||
.into();
|
.into();
|
||||||
self.nodes_to_measure.insert(layout_id, Box::new(measure));
|
self.nodes_to_measure.insert(layout_id, Box::new(measure));
|
||||||
|
self.styles.insert(layout_id, style.clone());
|
||||||
layout_id
|
layout_id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
seal::Sealed, AnyElement, AnyModel, AnyWeakModel, AppContext, AvailableSpace, BorrowWindow,
|
seal::Sealed, AnyElement, AnyModel, AnyWeakModel, AppContext, AvailableSpace, BorrowWindow,
|
||||||
Bounds, Element, ElementId, Entity, EntityId, Flatten, FocusHandle, FocusableView, IntoElement,
|
Bounds, ContentMask, Element, ElementId, Entity, EntityId, Flatten, FocusHandle, FocusableView,
|
||||||
LayoutId, Model, Pixels, Point, Render, Size, ViewContext, VisualContext, WeakModel,
|
IntoElement, LayoutId, Model, Pixels, Point, Render, Size, StackingOrder, Style, TextStyle,
|
||||||
WindowContext,
|
ViewContext, VisualContext, WeakModel, WindowContext,
|
||||||
};
|
};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -17,6 +17,19 @@ pub struct View<V> {
|
||||||
|
|
||||||
impl<V> Sealed for View<V> {}
|
impl<V> Sealed for View<V> {}
|
||||||
|
|
||||||
|
pub struct AnyViewState {
|
||||||
|
root_style: Style,
|
||||||
|
cache_key: Option<ViewCacheKey>,
|
||||||
|
element: Option<AnyElement>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ViewCacheKey {
|
||||||
|
bounds: Bounds<Pixels>,
|
||||||
|
stacking_order: StackingOrder,
|
||||||
|
content_mask: ContentMask<Pixels>,
|
||||||
|
text_style: TextStyle,
|
||||||
|
}
|
||||||
|
|
||||||
impl<V: 'static> Entity<V> for View<V> {
|
impl<V: 'static> Entity<V> for View<V> {
|
||||||
type Weak = WeakView<V>;
|
type Weak = WeakView<V>;
|
||||||
|
|
||||||
|
@ -60,16 +73,6 @@ impl<V: 'static> View<V> {
|
||||||
self.model.read(cx)
|
self.model.read(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn render_with<E>(&self, component: E) -> RenderViewWith<E, V>
|
|
||||||
// where
|
|
||||||
// E: 'static + Element,
|
|
||||||
// {
|
|
||||||
// RenderViewWith {
|
|
||||||
// view: self.clone(),
|
|
||||||
// element: Some(component),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
pub fn focus_handle(&self, cx: &AppContext) -> FocusHandle
|
pub fn focus_handle(&self, cx: &AppContext) -> FocusHandle
|
||||||
where
|
where
|
||||||
V: FocusableView,
|
V: FocusableView,
|
||||||
|
@ -183,16 +186,20 @@ impl<V> Eq for WeakView<V> {}
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct AnyView {
|
pub struct AnyView {
|
||||||
model: AnyModel,
|
model: AnyModel,
|
||||||
layout: fn(&AnyView, &mut WindowContext) -> (LayoutId, AnyElement),
|
request_layout: fn(&AnyView, &mut WindowContext) -> (LayoutId, AnyElement),
|
||||||
paint: fn(&AnyView, &mut AnyElement, &mut WindowContext),
|
cache: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AnyView {
|
impl AnyView {
|
||||||
|
pub fn cached(mut self) -> Self {
|
||||||
|
self.cache = true;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
pub fn downgrade(&self) -> AnyWeakView {
|
pub fn downgrade(&self) -> AnyWeakView {
|
||||||
AnyWeakView {
|
AnyWeakView {
|
||||||
model: self.model.downgrade(),
|
model: self.model.downgrade(),
|
||||||
layout: self.layout,
|
layout: self.request_layout,
|
||||||
paint: self.paint,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,8 +208,8 @@ impl AnyView {
|
||||||
Ok(model) => Ok(View { model }),
|
Ok(model) => Ok(View { model }),
|
||||||
Err(model) => Err(Self {
|
Err(model) => Err(Self {
|
||||||
model,
|
model,
|
||||||
layout: self.layout,
|
request_layout: self.request_layout,
|
||||||
paint: self.paint,
|
cache: self.cache,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -222,9 +229,9 @@ impl AnyView {
|
||||||
cx: &mut WindowContext,
|
cx: &mut WindowContext,
|
||||||
) {
|
) {
|
||||||
cx.with_absolute_element_offset(origin, |cx| {
|
cx.with_absolute_element_offset(origin, |cx| {
|
||||||
let (layout_id, mut rendered_element) = (self.layout)(self, cx);
|
let (layout_id, mut rendered_element) = (self.request_layout)(self, cx);
|
||||||
cx.compute_layout(layout_id, available_space);
|
cx.compute_layout(layout_id, available_space);
|
||||||
(self.paint)(self, &mut rendered_element, cx);
|
rendered_element.paint(cx);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -233,30 +240,65 @@ impl<V: Render> From<View<V>> for AnyView {
|
||||||
fn from(value: View<V>) -> Self {
|
fn from(value: View<V>) -> Self {
|
||||||
AnyView {
|
AnyView {
|
||||||
model: value.model.into_any(),
|
model: value.model.into_any(),
|
||||||
layout: any_view::layout::<V>,
|
request_layout: any_view::request_layout::<V>,
|
||||||
paint: any_view::paint,
|
cache: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Element for AnyView {
|
impl Element for AnyView {
|
||||||
type State = Option<AnyElement>;
|
type State = AnyViewState;
|
||||||
|
|
||||||
fn request_layout(
|
fn request_layout(
|
||||||
&mut self,
|
&mut self,
|
||||||
_state: Option<Self::State>,
|
state: Option<Self::State>,
|
||||||
cx: &mut WindowContext,
|
cx: &mut WindowContext,
|
||||||
) -> (LayoutId, Self::State) {
|
) -> (LayoutId, Self::State) {
|
||||||
let (layout_id, state) = (self.layout)(self, cx);
|
if self.cache {
|
||||||
(layout_id, Some(state))
|
if let Some(state) = state {
|
||||||
|
let layout_id = cx.request_layout(&state.root_style, None);
|
||||||
|
return (layout_id, state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let (layout_id, element) = (self.request_layout)(self, cx);
|
||||||
|
let root_style = cx.layout_style(layout_id).unwrap().clone();
|
||||||
|
let state = AnyViewState {
|
||||||
|
root_style,
|
||||||
|
cache_key: None,
|
||||||
|
element: Some(element),
|
||||||
|
};
|
||||||
|
(layout_id, state)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn paint(&mut self, _: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
|
fn paint(&mut self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
|
||||||
debug_assert!(
|
if !self.cache {
|
||||||
state.is_some(),
|
state.element.take().unwrap().paint(cx);
|
||||||
"state is None. Did you include an AnyView twice in the tree?"
|
return;
|
||||||
);
|
}
|
||||||
(self.paint)(self, state.as_mut().unwrap(), cx)
|
|
||||||
|
if let Some(cache_key) = state.cache_key.as_mut() {
|
||||||
|
if cache_key.bounds == bounds
|
||||||
|
&& cache_key.content_mask == cx.content_mask()
|
||||||
|
&& cache_key.stacking_order == *cx.stacking_order()
|
||||||
|
&& cache_key.text_style == cx.text_style()
|
||||||
|
{
|
||||||
|
println!("could reuse geometry for view {}", self.entity_id());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut element = state
|
||||||
|
.element
|
||||||
|
.take()
|
||||||
|
.unwrap_or_else(|| (self.request_layout)(self, cx).1);
|
||||||
|
element.draw(bounds.origin, bounds.size.into(), cx);
|
||||||
|
|
||||||
|
state.cache_key = Some(ViewCacheKey {
|
||||||
|
bounds,
|
||||||
|
stacking_order: cx.stacking_order().clone(),
|
||||||
|
content_mask: cx.content_mask(),
|
||||||
|
text_style: cx.text_style(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -287,7 +329,6 @@ impl IntoElement for AnyView {
|
||||||
pub struct AnyWeakView {
|
pub struct AnyWeakView {
|
||||||
model: AnyWeakModel,
|
model: AnyWeakModel,
|
||||||
layout: fn(&AnyView, &mut WindowContext) -> (LayoutId, AnyElement),
|
layout: fn(&AnyView, &mut WindowContext) -> (LayoutId, AnyElement),
|
||||||
paint: fn(&AnyView, &mut AnyElement, &mut WindowContext),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AnyWeakView {
|
impl AnyWeakView {
|
||||||
|
@ -295,8 +336,8 @@ impl AnyWeakView {
|
||||||
let model = self.model.upgrade()?;
|
let model = self.model.upgrade()?;
|
||||||
Some(AnyView {
|
Some(AnyView {
|
||||||
model,
|
model,
|
||||||
layout: self.layout,
|
request_layout: self.layout,
|
||||||
paint: self.paint,
|
cache: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -305,8 +346,7 @@ impl<V: 'static + Render> From<WeakView<V>> for AnyWeakView {
|
||||||
fn from(view: WeakView<V>) -> Self {
|
fn from(view: WeakView<V>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
model: view.model.into(),
|
model: view.model.into(),
|
||||||
layout: any_view::layout::<V>,
|
layout: any_view::request_layout::<V>,
|
||||||
paint: any_view::paint,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -328,7 +368,7 @@ impl std::fmt::Debug for AnyWeakView {
|
||||||
mod any_view {
|
mod any_view {
|
||||||
use crate::{AnyElement, AnyView, IntoElement, LayoutId, Render, WindowContext};
|
use crate::{AnyElement, AnyView, IntoElement, LayoutId, Render, WindowContext};
|
||||||
|
|
||||||
pub(crate) fn layout<V: 'static + Render>(
|
pub(crate) fn request_layout<V: 'static + Render>(
|
||||||
view: &AnyView,
|
view: &AnyView,
|
||||||
cx: &mut WindowContext,
|
cx: &mut WindowContext,
|
||||||
) -> (LayoutId, AnyElement) {
|
) -> (LayoutId, AnyElement) {
|
||||||
|
@ -337,8 +377,4 @@ mod any_view {
|
||||||
let layout_id = element.request_layout(cx);
|
let layout_id = element.request_layout(cx);
|
||||||
(layout_id, element)
|
(layout_id, element)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn paint(_view: &AnyView, element: &mut AnyElement, cx: &mut WindowContext) {
|
|
||||||
element.paint(cx);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -754,6 +754,14 @@ impl<'a> WindowContext<'a> {
|
||||||
.request_measured_layout(style, rem_size, measure)
|
.request_measured_layout(style, rem_size, measure)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn layout_style(&self, layout_id: LayoutId) -> Option<&Style> {
|
||||||
|
self.window
|
||||||
|
.layout_engine
|
||||||
|
.as_ref()
|
||||||
|
.unwrap()
|
||||||
|
.requested_style(layout_id)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn compute_layout(&mut self, layout_id: LayoutId, available_space: Size<AvailableSpace>) {
|
pub fn compute_layout(&mut self, layout_id: LayoutId, available_space: Size<AvailableSpace>) {
|
||||||
let mut layout_engine = self.window.layout_engine.take().unwrap();
|
let mut layout_engine = self.window.layout_engine.take().unwrap();
|
||||||
layout_engine.compute_layout(layout_id, available_space, self);
|
layout_engine.compute_layout(layout_id, available_space, self);
|
||||||
|
@ -1313,6 +1321,7 @@ impl<'a> WindowContext<'a> {
|
||||||
|
|
||||||
/// Draw pixels to the display for this window based on the contents of its scene.
|
/// Draw pixels to the display for this window based on the contents of its scene.
|
||||||
pub(crate) fn draw(&mut self) -> Scene {
|
pub(crate) fn draw(&mut self) -> Scene {
|
||||||
|
println!("=====================");
|
||||||
self.window.dirty = false;
|
self.window.dirty = false;
|
||||||
self.window.drawing = true;
|
self.window.drawing = true;
|
||||||
|
|
||||||
|
|
|
@ -601,7 +601,7 @@ impl Render for Dock {
|
||||||
Axis::Horizontal => this.min_w(size).h_full(),
|
Axis::Horizontal => this.min_w(size).h_full(),
|
||||||
Axis::Vertical => this.min_h(size).w_full(),
|
Axis::Vertical => this.min_h(size).w_full(),
|
||||||
})
|
})
|
||||||
.child(entry.panel.to_any()),
|
.child(entry.panel.to_any().cached()),
|
||||||
)
|
)
|
||||||
.child(handle)
|
.child(handle)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -3,8 +3,8 @@ use anyhow::{anyhow, Result};
|
||||||
use call::{ActiveCall, ParticipantLocation};
|
use call::{ActiveCall, ParticipantLocation};
|
||||||
use collections::HashMap;
|
use collections::HashMap;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
point, size, AnyWeakView, Axis, Bounds, Entity as _, IntoElement, Model, Pixels, Point, View,
|
point, size, AnyView, AnyWeakView, Axis, Bounds, Entity as _, IntoElement, Model, Pixels,
|
||||||
ViewContext,
|
Point, View, ViewContext,
|
||||||
};
|
};
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use project::Project;
|
use project::Project;
|
||||||
|
@ -244,7 +244,7 @@ impl Member {
|
||||||
.relative()
|
.relative()
|
||||||
.flex_1()
|
.flex_1()
|
||||||
.size_full()
|
.size_full()
|
||||||
.child(pane.clone())
|
.child(AnyView::from(pane.clone()).cached())
|
||||||
.when_some(leader_border, |this, color| {
|
.when_some(leader_border, |this, color| {
|
||||||
this.child(
|
this.child(
|
||||||
div()
|
div()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue