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.

editor: Reduce redundant invalidations (#56234)

Mikhail Pertsev593a78d3ffa3
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index 6718802d43a..51127285f1f 100644
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@ -1793,8 +1793,10 @@ impl Editor {
self.sticky_headers_task = cx.spawn(async move |this, cx| {
let sticky_headers = background_task.await;
this.update(cx, |this, cx| {
- this.sticky_headers = Some(sticky_headers);
- cx.notify();
+ if this.sticky_headers.as_ref() != Some(&sticky_headers) {
+ this.sticky_headers = Some(sticky_headers);
+ cx.notify();
+ }
})
.ok();
});
diff --git a/crates/editor/src/element/mouse.rs b/crates/editor/src/element/mouse.rs
index b3fc095dbbc..5c0709c8d2d 100644
--- a/crates/editor/src/element/mouse.rs
+++ b/crates/editor/src/element/mouse.rs
@@ -569,11 +569,15 @@ impl EditorElement {
if scroll_position != current_scroll_position {
editor.scroll(scroll_position, axis, window, cx);
cx.stop_propagation();
- } else if y < 0. {
+ } else if y < 0. && !forbid_vertical_scroll {
// Due to clamping, we may fail to detect cases of overscroll to the top;
// We want the scroll manager to get an update in such cases and detect the change of direction
// on the next frame.
- cx.notify();
+ if editor.scroll_manager.should_notify_top_overscroll(axis) {
+ cx.notify();
+ }
+ } else {
+ editor.scroll_manager.reset_top_overscroll_notification();
}
});
}
diff --git a/crates/editor/src/scroll.rs b/crates/editor/src/scroll.rs
index f067519e734..15dabd5dddd 100644
--- a/crates/editor/src/scroll.rs
+++ b/crates/editor/src/scroll.rs
@@ -217,6 +217,7 @@ pub struct ScrollManager {
visible_line_count: Option<f64>,
visible_column_count: Option<f64>,
forbid_vertical_scroll: bool,
+ notified_top_overscroll: bool,
minimap_thumb_state: Option<ScrollbarThumbState>,
_save_scroll_position_task: Task<()>,
}
@@ -240,6 +241,7 @@ impl ScrollManager {
visible_line_count: None,
visible_column_count: None,
forbid_vertical_scroll: false,
+ notified_top_overscroll: false,
minimap_thumb_state: None,
_save_scroll_position_task: Task::ready(()),
}
@@ -344,6 +346,21 @@ impl ScrollManager {
self.ongoing.axis = axis;
}
+ pub fn should_notify_top_overscroll(&mut self, axis: Option<Axis>) -> bool {
+ let now = Instant::now();
+ let new_scroll = now.duration_since(self.ongoing.last_event) > SCROLL_EVENT_SEPARATION;
+ let axis_changed = self.ongoing.axis != axis;
+ let should_notify = !self.notified_top_overscroll || new_scroll || axis_changed;
+ self.ongoing.last_event = now;
+ self.ongoing.axis = axis;
+ self.notified_top_overscroll = true;
+ should_notify
+ }
+
+ pub fn reset_top_overscroll_notification(&mut self) {
+ self.notified_top_overscroll = false;
+ }
+
pub fn scroll_position(
&self,
snapshot: &DisplaySnapshot,
@@ -445,6 +462,7 @@ impl ScrollManager {
return WasScrolled(false);
}
+ self.notified_top_overscroll = false;
self.anchor.update(cx, |shared, _| {
shared.scroll_anchor = adjusted_anchor;
shared.display_map_id = Some(display_map.display_map_id);
diff --git a/crates/ui/src/components/button/button.rs b/crates/ui/src/components/button/button.rs
index 5278ffa2118..bf19b09496d 100644
--- a/crates/ui/src/components/button/button.rs
+++ b/crates/ui/src/components/button/button.rs
@@ -385,6 +385,7 @@ impl RenderOnce for Button {
} else {
self.label_color.unwrap_or_default()
};
+ let loading_icon_id = (self.base.id().clone(), "loading");
self.base.child(
h_flex()
@@ -397,7 +398,7 @@ impl RenderOnce for Button {
Icon::new(IconName::LoadCircle)
.size(IconSize::Small)
.color(Color::Muted)
- .with_rotate_animation(2),
+ .with_keyed_rotate_animation(loading_icon_id, 2),
)
},
|this| {