Skip to content

Visual Studio Code, rebuilt in Rust

Rust

fix(gpui): run the retention check as the drawing flush ends

rokartur35ac1350ccae
diff --git a/native/crates/app/src/retained_check.rs b/native/crates/app/src/retained_check.rs
index 39ddfba64b3..a0f6a72a23d 100644
--- a/native/crates/app/src/retained_check.rs
+++ b/native/crates/app/src/retained_check.rs
@@ -2,7 +2,7 @@
//! a second time from scratch once it is settled and fails the test where the two differ (vendor gpui
//! `fast/retained.rs` `check_settled_frames`); this test checks that the comparison sees a view whose state changed without a notify.
-use gpui::{AppContext, Context, Entity, IntoElement, ParentElement, Render, Styled, TestAppContext, Window, div, px, rgb};
+use gpui::{App, AppContext, Bounds, Context, Entity, IntoElement, ParentElement, Pixels, Render, Styled, TestAppContext, Window, canvas, div, px, rgb};
use std::{cell::Cell, rc::Rc};
struct Swatch(Rc<Cell<u32>>);
@@ -43,3 +43,45 @@ fn a_view_changed_without_a_notify_differs_from_a_full_render(cx: &mut TestAppCo
cx.run_until_parked();
assert_eq!(cx.update(|window, cx| window.retained_frame_difference(cx)), None);
}
+
+/// Under GPUI_RETENTION_CHECK=1: a change made after the frame without a notify is stale in either mode, so the
+/// check compares the frame before the test runs on.
+#[gpui::test]
+fn a_change_after_the_settled_frame_is_not_a_retention_difference(cx: &mut TestAppContext) {
+ let color = Rc::new(Cell::new(0xFF0000));
+ let (view, cx) = cx.add_window_view(|_, _| Swatch(color.clone()));
+ view.update(cx, |_, cx| cx.notify());
+ color.set(0x0000FF);
+ view.update(cx, |_, _| {});
+}
+
+/// Shows the width its canvas measured on the last frame, as the editor sizes its minimap.
+struct MeasuredWidth(Option<Pixels>);
+
+impl Render for MeasuredWidth {
+ fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
+ let this = cx.entity();
+ let measure = move |bounds: Bounds<Pixels>, _: &mut Window, cx: &mut App| {
+ this.update(cx, |this, cx| {
+ if this.0 != Some(bounds.size.width) {
+ this.0 = Some(bounds.size.width);
+ cx.notify();
+ }
+ })
+ };
+ div()
+ .size_full()
+ .child(canvas(measure, |_, _, _, _| {}).absolute().size_full())
+ .children(self.0.map(|width| div().w(width / 2.).h(px(10.)).bg(rgb(0xFF0000))))
+ }
+}
+
+#[gpui::test]
+fn a_view_notified_while_drawing_draws_again(cx: &mut TestAppContext) {
+ let (view, cx) = cx.add_window_view(|_, _| MeasuredWidth(None));
+ cx.run_until_parked();
+ cx.simulate_resize(gpui::size(px(800.), px(600.)));
+ cx.run_until_parked();
+ assert_eq!(view.read_with(cx, |view, _| view.0), Some(px(800.)));
+ assert_eq!(cx.update(|window, cx| window.retained_frame_difference(cx)), None, "the measured width is drawn");
+}
diff --git a/native/vendor/gpui-fast/crates/gpui/src/app.rs b/native/vendor/gpui-fast/crates/gpui/src/app.rs
index d3f0408db7b..6bceceeb17c 100644
--- a/native/vendor/gpui-fast/crates/gpui/src/app.rs
+++ b/native/vendor/gpui-fast/crates/gpui/src/app.rs
@@ -2345,16 +2345,26 @@ impl App {
window.invalidator.is_dirty().then_some(window.handle)
})
.collect::<Vec<_>>();
- if dirty_windows.is_empty() {
- crate::fast::retained::check_settled_frames(self);
- }
+ let mut drawn_dirty_again = false;
for window in dirty_windows {
self.update_window(window, |_, window, cx| {
window.draw(cx).clear(cx);
crate::fast::retained::note_test_frame(window);
+ drawn_dirty_again |= window.invalidator.is_dirty();
})
.unwrap();
}
+ // VSRustium: a frame that notified views while drawing asks for the next one (`Window::draw`).
+ if drawn_dirty_again {
+ continue;
+ }
+ }
+
+ // VSRustium: checked here, before the test runs on, since a change the test makes without a notify
+ // after the frame would show in the full render alone, a stale frame in either mode.
+ #[cfg(any(test, feature = "test-support"))]
+ if self.pending_effects.is_empty() && matches!(self.mode, GpuiMode::Test { .. }) {
+ crate::fast::retained::check_settled_frames(self);
}
if self.pending_effects.is_empty() {