Skip to content

Visual Studio Code, rebuilt in Rust

Rust

perf(workbench): update the menu bar 200 ms after a frame, as menuUpdater does

rokarturec8a6221aa86
diff --git a/native/crates/app/src/workbench.rs b/native/crates/app/src/workbench.rs
index 8d4115bcd55..889acc51244 100644
--- a/native/crates/app/src/workbench.rs
+++ b/native/crates/app/src/workbench.rs
@@ -536,6 +536,8 @@ pub struct Workbench {
pub hovers: hover::Hovers,
/// What the application menu shows now.
installed_menubar: Option<menubar::Menubar>,
+ /// menubarControl.ts `menuUpdater`, pending.
+ menubar_update: Option<Task<()>>,
/// StorageScope.PROFILE of the current profile, shared with the extension host's MainThreadStorage.
profile_storage: Arc<Mutex<Storage>>,
/// StorageScope.APPLICATION: the default profile's storage, the same as `profile_storage` in that profile.
@@ -678,7 +680,7 @@ impl Render for Workbench {
let hover = if overlays_here { self.hover_widget(window, cx) } else { None };
let dialog = if overlays_here { self.custom_dialog_widget(window, cx) } else { None };
let accessible_view = self.accessible_view_widget(window, cx);
- self.update_menubar(window, cx);
+ self.schedule_menubar_update(window, cx);
self.with_group(main_group, |workbench| workbench.sync_window_title(window));
div()
.relative()
@@ -896,6 +898,7 @@ impl Workbench {
action_bars: Default::default(),
hovers: hover::Hovers::default(),
installed_menubar: None,
+ menubar_update: None,
profile_storage: Arc::new(Mutex::new(Storage::in_memory())),
application_storage: Arc::new(Mutex::new(Storage::in_memory())),
workspace_storage: Arc::new(Mutex::new(Storage::in_memory())),
diff --git a/native/crates/app/src/workbench/menubar.rs b/native/crates/app/src/workbench/menubar.rs
index 8599f4b13c7..d07eae75e56 100644
--- a/native/crates/app/src/workbench/menubar.rs
+++ b/native/crates/app/src/workbench/menubar.rs
@@ -155,6 +155,24 @@ impl Workbench {
Menubar { menus, keybindings, recent, recent_workspaces, always_on_top, update_item }
}
+ /// `updateMenubar`: `menuUpdater` updates the menu 200 ms later. Every render asks, so a frame does not build the
+ /// menus again; the first menu, and the menu of a window come to the front, install at once.
+ pub(super) fn schedule_menubar_update(&mut self, window: &Window, cx: &mut Context<Self>) {
+ if self.installed_menubar.is_none() {
+ return self.update_menubar(window, cx);
+ }
+ if self.menubar_update.is_some() {
+ return;
+ }
+ self.menubar_update = Some(cx.spawn_in(window, async move |workbench, cx| {
+ cx.background_executor().timer(std::time::Duration::from_millis(200)).await;
+ let _ = workbench.update_in(cx, |workbench, window, cx| {
+ workbench.menubar_update = None;
+ workbench.update_menubar(window, cx);
+ });
+ }));
+ }
+
/// Sets the application menu when what it shows changed, as `updateMenubar` does on menu changes.
pub(super) fn update_menubar(&mut self, window: &Window, cx: &mut Context<Self>) {
// The application menu is the focused window's (menubar.ts `onDidChangeWindowFocus`); a window in the
diff --git a/native/crates/app/src/workbench/menubar/tests.rs b/native/crates/app/src/workbench/menubar/tests.rs
index cb1ee705bbc..2f6ef65f980 100644
--- a/native/crates/app/src/workbench/menubar/tests.rs
+++ b/native/crates/app/src/workbench/menubar/tests.rs
@@ -329,3 +329,19 @@ fn the_folder_and_file_menu_bar_matches_the_reference(cx: &mut TestAppContext) {
assert_menubar_matches_the_reference(include_str!("reference-file.json"), Some(&folder), Some(&folder.join("notes.txt")), cx);
std::fs::remove_dir_all(folder).unwrap();
}
+
+#[gpui::test]
+fn a_frame_updates_the_menu_bar_200_ms_later(cx: &mut TestAppContext) {
+ let (workbench, cx) = workbench(None, cx);
+ let draw = |cx: &mut gpui::VisualTestContext| cx.update(|window, cx| window.draw(cx).clear(cx));
+ let checking = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.installed_menubar.as_ref().unwrap().update_item.as_ref().is_some_and(|(_, id, _)| *id == "update.checking"));
+ draw(cx);
+ assert!(!checking(cx));
+ workbench.update(cx, |workbench, _| workbench.update_state = crate::update::State::CheckingForUpdates { explicit: true });
+ draw(cx);
+ cx.run_until_parked();
+ assert!(!checking(cx));
+ cx.executor().advance_clock(std::time::Duration::from_millis(200));
+ cx.run_until_parked();
+ assert!(checking(cx));
+}
diff --git a/native/vendor/wu/crates/gpui/src/view.rs b/native/vendor/wu/crates/gpui/src/view.rs
index f8343be29a0..bcfddfe8a86 100644
--- a/native/vendor/wu/crates/gpui/src/view.rs
+++ b/native/vendor/wu/crates/gpui/src/view.rs
@@ -414,6 +414,10 @@ impl<V: View> Element for ViewElement<V> {
element
});
+ // A view made earlier in this frame is in the start set, so `detect_accessed_entities`
+ // drops it; a reuse of this state would then stop tracking the view's own notify.
+ let mut accessed_entities = accessed_entities;
+ accessed_entities.insert(entity_id);
let prepaint_end = window.prepaint_index();
window.refreshing = refreshing;