Visual Studio Code, rebuilt in Rust
Rust
//! Retained Mode against full renders. `GPUI_RETENTION_CHECK=1 cargo test -p app` draws every frame each test draws
//! 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::{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>>);
impl Render for Swatch {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().size(px(10.)).bg(rgb(self.0.get()))
}
}
struct Pair {
stale: Entity<Swatch>,
other: Entity<Swatch>,
}
impl Render for Pair {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().flex().child(self.stale.clone()).child(self.other.clone())
}
}
#[gpui::test]
fn a_view_changed_without_a_notify_differs_from_a_full_render(cx: &mut TestAppContext) {
// The check under GPUI_RETENTION_CHECK=1 fails this test's deliberately stale frame first.
if std::env::var("GPUI_RETENTION_CHECK").is_ok_and(|value| value == "1") {
return;
}
let color = Rc::new(Cell::new(0xFF0000));
let stale = cx.new(|_| Swatch(color.clone()));
let other = cx.new(|_| Swatch(Rc::new(Cell::new(0x00FF00))));
let (_, cx) = cx.add_window_view(|_, _| Pair { stale: stale.clone(), other: other.clone() });
color.set(0x0000FF);
other.update(cx, |_, cx| cx.notify());
cx.run_until_parked();
let difference = cx.update(|window, cx| window.retained_frame_difference(cx));
assert!(difference.is_some(), "the swatch read its color outside GPUI and kept its last frame");
stale.update(cx, |_, cx| cx.notify());
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");
}