perf: reuse rendered elements across repaints
Rust / build (macos-latest, stable) (push) Canceled after 0s
Rust / build (ubuntu-latest, stable) (push) Canceled after 0s
Rust / build (windows-latest, stable) (push) Canceled after 0s

This commit is contained in:
2026-11-25 05:00:06 +07:00
parent 3f64b36ff4
commit bf06d29c2e
21 changed files with 603 additions and 316 deletions
+71
View File
@@ -1,6 +1,8 @@
//! The panel stack: the main island fills the window; splitting makes two
//! half-width islands with a drag divider that resizes them.
use std::cell::Cell;
use std::rc::Rc;
use std::sync::Arc;
use gpui::{
@@ -10,6 +12,7 @@ use gpui::{
size,
};
use ui::common::v_flex;
use ui::input::{Input, InputState};
use ui::layout::panel::{Panel, PanelSlot, PanelStack, PanelView, SplitSide};
use ui::tabs::Tabs;
use ui::theme::Theme;
@@ -69,6 +72,28 @@ impl Render for PanelHarness {
}
}
/// A split panel that counts its renders around a field.
struct FieldView {
input: Entity<InputState>,
renders: Rc<Cell<usize>>,
}
impl Render for FieldView {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
self.renders.set(self.renders.get() + 1);
div()
.debug_selector(|| "panel-test-field".to_string())
.size_full()
.child(Input::new("split-field", self.input.clone()))
}
}
impl Panel for FieldView {
fn title(&self, _cx: &App) -> SharedString {
"Field".into()
}
}
/// A marker view.
fn tag(cx: &mut App, selector: &'static str) -> Entity<TagView> {
cx.new(|_| TagView::new(selector))
@@ -989,3 +1014,49 @@ fn zoom_ends_when_another_split_panel_is_shown(cx: &mut TestAppContext) {
"the split is back once another panel is shown"
);
}
/// The split panel is cached: it repaints for its own changes, not for other panels'.
#[gpui::test]
fn a_split_panel_repaints_for_its_own_changes_only(cx: &mut TestAppContext) {
let renders = Rc::new(Cell::new(0));
let (input, main, field) = cx.update(|cx| {
let input = InputState::new(cx);
let field = cx.new(|_| FieldView {
input: input.clone(),
renders: Rc::clone(&renders),
});
(input, tag(cx, "panel-test-main"), field)
});
let seeded_main = main.clone();
let seeded_field = field.clone();
let (mut cx, _stack) = stack_window(cx, move |stack, cx| {
stack.insert(PanelSlot::Main, "main", Arc::new(seeded_main), cx);
stack.insert(PanelSlot::Split, "split", Arc::new(seeded_field), cx);
});
let rendered = renders.get();
cx.update(|window, cx| input.update(cx, |input, cx| input.set_value("hey", window, cx)));
cx.run_until_parked();
let typed = cx.update(|_, cx| input.read(cx).value().to_string());
assert_eq!(typed, "hey");
assert!(
renders.get() > rendered,
"an edit in the split panel repaints it"
);
let rendered = renders.get();
cx.update(|_, cx| main.update(cx, |_, cx| cx.notify()));
cx.run_until_parked();
assert_eq!(
renders.get(),
rendered,
"another panel's change leaves the split panel as it was"
);
cx.update(|_, cx| field.update(cx, |_, cx| cx.notify()));
cx.run_until_parked();
assert!(
renders.get() > rendered,
"the split panel's own change repaints it"
);
}