Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui: Repaint cached views when inherited opacity changes (#65951)

Anthony Eid8fa085e397fd
diff --git a/crates/gpui/src/elements/div.rs b/crates/gpui/src/elements/div.rs
index 109acd49d0a..9b34aa23469 100644
--- a/crates/gpui/src/elements/div.rs
+++ b/crates/gpui/src/elements/div.rs
@@ -2446,7 +2446,9 @@ impl Interactivity {
let scroll_offset =
self.clamp_scroll_position(bounds, &style, window, cx);
- let result = f(&style, scroll_offset, hitbox, window, cx);
+ let result = window.with_element_opacity(style.opacity, |window| {
+ f(&style, scroll_offset, hitbox, window, cx)
+ });
(result, element_state)
},
)
diff --git a/crates/gpui/src/view.rs b/crates/gpui/src/view.rs
index 76688b74326..b613805658d 100644
--- a/crates/gpui/src/view.rs
+++ b/crates/gpui/src/view.rs
@@ -331,6 +331,7 @@ struct ViewElementCacheKey {
bounds: Bounds<Pixels>,
content_mask: ContentMask<Pixels>,
text_style: TextStyle,
+ opacity: f32,
}
#[cfg(not(gpui_hot_reload))]
@@ -542,11 +543,15 @@ fn prepaint_view(
|element_state, window| {
let content_mask = window.content_mask();
let text_style = window.text_style();
+ let opacity = window.element_opacity();
+ // Opacity is compared by bits so that a NaN opacity still matches itself
+ // and the view keeps using its cache.
if let Some(mut element_state) = element_state
&& element_state.cache_key.bounds == bounds
&& element_state.cache_key.content_mask == content_mask
&& element_state.cache_key.text_style == text_style
+ && element_state.cache_key.opacity.to_bits() == opacity.to_bits()
&& !window.dirty_views.contains(&entity_id)
&& !window.refreshing
{
@@ -582,6 +587,7 @@ fn prepaint_view(
bounds,
content_mask,
text_style,
+ opacity,
},
},
)
@@ -621,13 +627,23 @@ fn paint_view(
let mut element_state = element_state.unwrap();
let paint_start = window.paint_index();
+ let opacity = window.element_opacity();
+ // Prepaint can only predict the opacity paint applies: an ancestor's
+ // hover or drag-over style depends on this frame's hit test, which
+ // isn't known until prepaint finishes. The key records what was
+ // actually painted, and output replayed under a different opacity is
+ // redrawn on the next frame.
if let Some(element) = element {
let refreshing = mem::replace(&mut window.refreshing, true);
element.paint(window, cx);
window.refreshing = refreshing;
+ element_state.cache_key.opacity = opacity;
} else {
window.reuse_paint(element_state.paint_range.clone());
+ if element_state.cache_key.opacity.to_bits() != opacity.to_bits() {
+ window.request_animation_frame();
+ }
}
let paint_end = window.paint_index();
diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs
index 44e47d2ca64..cf98dcf80f6 100644
--- a/crates/gpui/src/window.rs
+++ b/crates/gpui/src/window.rs
@@ -10127,6 +10127,111 @@ mod tests {
})
.unwrap();
}
+
+ #[gpui::test]
+ fn cached_view_follows_inherited_opacity(cx: &mut TestAppContext) {
+ struct Child {
+ renders: usize,
+ }
+
+ impl Render for Child {
+ fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
+ self.renders += 1;
+ div().size_full().bg(crate::rgb(0xff0000))
+ }
+ }
+
+ struct Root {
+ opacity: f32,
+ width: Pixels,
+ child: crate::Entity<Child>,
+ }
+
+ impl Render for Root {
+ fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
+ div()
+ .w(self.width)
+ .h(px(200.))
+ .opacity(self.opacity)
+ .hover(|style| style.opacity(1.))
+ .child(
+ self.child
+ .clone()
+ .cached(crate::StyleRefinement::default().size(px(100.))),
+ )
+ }
+ }
+
+ /// Updates the root, draws until no more frames are requested, and returns the
+ /// child's painted alpha and how many times it has rendered.
+ fn update_and_draw(
+ window: crate::WindowHandle<Root>,
+ child: &crate::Entity<Child>,
+ cx: &mut TestAppContext,
+ update: impl FnOnce(&mut Root, &mut Window, &mut Context<Root>),
+ ) -> (f32, usize) {
+ window
+ .update(cx, |root, window, cx| {
+ update(root, window, cx);
+ cx.notify();
+ })
+ .expect("update root");
+ for _ in 0..3 {
+ let (alpha, renders, requested_frames) = cx
+ .update_window(window.into(), |_, window, cx| {
+ window.draw(cx).clear(cx);
+ let quads = &window.rendered_frame.scene.quads;
+ assert_eq!(quads.len(), 1);
+ let alpha = quads.first().expect("child quad").background.solid.a;
+ (
+ alpha,
+ child.read(cx).renders,
+ window.simulate_next_frame(cx),
+ )
+ })
+ .expect("draw window");
+ if requested_frames == 0 {
+ return (alpha, renders);
+ }
+ }
+ panic!("cached child kept requesting frames");
+ }
+
+ let child = cx.new(|_| Child { renders: 0 });
+ let window = cx.add_window(|_, _| Root {
+ opacity: 1.,
+ width: px(200.),
+ child: child.clone(),
+ });
+ let outside = point(px(500.), px(500.));
+ let inside = point(px(50.), px(50.));
+ let beside = point(px(250.), px(50.));
+
+ // Changing the wrapper's opacity redraws the child; unchanged opacity reuses it.
+ let move_outside = |_: &mut Root, window: &mut Window, cx: &mut Context<Root>| {
+ window.simulate_mouse_move(outside, cx)
+ };
+ assert_eq!(update_and_draw(window, &child, cx, move_outside), (1., 1));
+ assert_eq!(update_and_draw(window, &child, cx, |_, _, _| {}), (1., 1));
+ let fade = |root: &mut Root, _: &mut Window, _: &mut Context<Root>| root.opacity = 0.5;
+ assert_eq!(update_and_draw(window, &child, cx, fade), (0.5, 2));
+ assert_eq!(update_and_draw(window, &child, cx, |_, _, _| {}), (0.5, 2));
+
+ // Hover opacity applies when the mouse enters and leaves.
+ let move_inside = |_: &mut Root, window: &mut Window, cx: &mut Context<Root>| {
+ window.simulate_mouse_move(inside, cx)
+ };
+ assert_eq!(update_and_draw(window, &child, cx, move_inside).0, 1.);
+ let move_beside = |_: &mut Root, window: &mut Window, cx: &mut Context<Root>| {
+ window.simulate_mouse_move(beside, cx)
+ };
+ assert_eq!(update_and_draw(window, &child, cx, move_beside).0, 0.5);
+
+ // Widening the wrapper puts it under a still mouse, so only paint's hit test sees
+ // the hover.
+ let widen = |root: &mut Root, _: &mut Window, _: &mut Context<Root>| root.width = px(300.);
+ assert_eq!(update_and_draw(window, &child, cx, widen).0, 1.);
+ }
}
#[cfg(all(test, any(feature = "inspector", debug_assertions)))]