chore: Avoid some unnecessary window.refresh call. (#617)

Ref
https://github.com/zed-industries/zed/discussions/24260#discussioncomment-12135749
This commit is contained in:
Jason Lee 2025-02-11 17:06:17 +08:00 committed by GitHub
parent 4a26776289
commit 0050685a58
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 51 additions and 56 deletions

View file

@ -1,4 +1,3 @@
use core::time;
use std::time::Duration; use std::time::Duration;
use fake::Fake; use fake::Fake;
@ -306,22 +305,19 @@ impl ListStory {
// Spawn a background to random refresh the list // Spawn a background to random refresh the list
cx.spawn(move |this, mut cx| async move { cx.spawn(move |this, mut cx| async move {
loop { this.update(&mut cx, |this, cx| {
Timer::after(time::Duration::from_secs_f64(0.5)).await; this.company_list.update(cx, |picker, _| {
this.update(&mut cx, |this, cx| { picker
this.company_list.update(cx, |picker, _| { .delegate_mut()
picker .companies
.delegate_mut() .iter_mut()
.companies .for_each(|company| {
.iter_mut() company.random_update();
.for_each(|company| { });
company.random_update(); });
}); cx.notify();
}); })
cx.notify(); .ok();
})
.ok();
}
}) })
.detach(); .detach();

View file

@ -3,10 +3,10 @@
use std::sync::Arc; use std::sync::Arc;
use gpui::{ use gpui::{
div, prelude::FluentBuilder as _, px, AnyView, App, AppContext, Axis, Context, Element, Empty, div, prelude::FluentBuilder as _, px, App, AppContext, Axis, Context, Element, Empty, Entity,
Entity, InteractiveElement as _, IntoElement, MouseMoveEvent, MouseUpEvent, ParentElement as _, InteractiveElement as _, IntoElement, MouseMoveEvent, MouseUpEvent, ParentElement as _, Pixels,
Pixels, Point, Render, StatefulInteractiveElement, Style, StyleRefinement, Styled as _, Point, Render, StatefulInteractiveElement, Style, StyleRefinement, Styled as _, WeakEntity,
WeakEntity, Window, Window,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -411,9 +411,7 @@ impl Render for Dock {
}) })
.map(|this| match &self.panel { .map(|this| match &self.panel {
DockItem::Split { view, .. } => this.child(view.clone()), DockItem::Split { view, .. } => this.child(view.clone()),
DockItem::Tabs { view, .. } => { DockItem::Tabs { view, .. } => this.child(view.clone()),
this.child(AnyView::from(view.clone()).cached(cache_style))
}
DockItem::Panel { view, .. } => this.child(view.clone().view().cached(cache_style)), DockItem::Panel { view, .. } => this.child(view.clone().view().cached(cache_style)),
// Not support to render Tiles and Tile into Dock // Not support to render Tiles and Tile into Dock
DockItem::Tiles { .. } => this, DockItem::Tiles { .. } => this,
@ -472,14 +470,20 @@ impl Element for DockElement {
_: &mut Self::RequestLayoutState, _: &mut Self::RequestLayoutState,
_: &mut Self::PrepaintState, _: &mut Self::PrepaintState,
window: &mut gpui::Window, window: &mut gpui::Window,
_: &mut App, cx: &mut App,
) { ) {
window.on_mouse_event({ window.on_mouse_event({
let view = self.view.clone(); let view = self.view.clone();
let is_resizing = view.read(cx).is_resizing;
move |e: &MouseMoveEvent, phase, window, cx| { move |e: &MouseMoveEvent, phase, window, cx| {
if phase.bubble() { if !is_resizing {
view.update(cx, |view, cx| view.resize(e.position, window, cx)) return;
} }
if !phase.bubble() {
return;
}
view.update(cx, |view, cx| view.resize(e.position, window, cx))
} }
}); });

View file

@ -143,7 +143,6 @@ impl PopupMenu {
scroll_state: Rc::new(Cell::new(ScrollbarState::default())), scroll_state: Rc::new(Cell::new(ScrollbarState::default())),
_subscriptions, _subscriptions,
}; };
window.refresh();
f(menu, window, cx) f(menu, window, cx)
}) })
} }

View file

@ -496,41 +496,38 @@ impl Element for ResizePanelGroupElement {
let axis = self.axis; let axis = self.axis;
let current_ix = view.read(cx).resizing_panel_ix; let current_ix = view.read(cx).resizing_panel_ix;
move |e: &MouseMoveEvent, phase, window, cx| { move |e: &MouseMoveEvent, phase, window, cx| {
if phase.bubble() { if !phase.bubble() {
if let Some(ix) = current_ix { return;
view.update(cx, |view, cx| {
let panel = view
.panels
.get(ix)
.expect("BUG: invalid panel index")
.read(cx);
match axis {
Axis::Horizontal => view.resize_panels(
ix,
e.position.x - panel.bounds.left(),
window,
cx,
),
Axis::Vertical => {
view.resize_panels(
ix,
e.position.y - panel.bounds.top(),
window,
cx,
);
}
}
})
}
} }
let Some(ix) = current_ix else { return };
view.update(cx, |view, cx| {
let panel = view
.panels
.get(ix)
.expect("BUG: invalid panel index")
.read(cx);
match axis {
Axis::Horizontal => {
view.resize_panels(ix, e.position.x - panel.bounds.left(), window, cx)
}
Axis::Vertical => {
view.resize_panels(ix, e.position.y - panel.bounds.top(), window, cx);
}
}
})
} }
}); });
// When any mouse up, stop dragging // When any mouse up, stop dragging
window.on_mouse_event({ window.on_mouse_event({
let view = self.view.clone(); let view = self.view.clone();
let current_ix = view.read(cx).resizing_panel_ix;
move |_: &MouseUpEvent, phase, window, cx| { move |_: &MouseUpEvent, phase, window, cx| {
if current_ix.is_none() {
return;
}
if phase.bubble() { if phase.bubble() {
view.update(cx, |view, cx| view.done_resizing(window, cx)); view.update(cx, |view, cx| view.done_resizing(window, cx));
} }

View file

@ -124,7 +124,6 @@ impl RenderOnce for WindowBorder {
.when(!tiling.bottom, |div| div.pb(SHADOW_SIZE)) .when(!tiling.bottom, |div| div.pb(SHADOW_SIZE))
.when(!tiling.left, |div| div.pl(SHADOW_SIZE)) .when(!tiling.left, |div| div.pl(SHADOW_SIZE))
.when(!tiling.right, |div| div.pr(SHADOW_SIZE)) .when(!tiling.right, |div| div.pr(SHADOW_SIZE))
.on_mouse_move(|_e, window, _| window.refresh())
.on_mouse_down(MouseButton::Left, move |_, window, _| { .on_mouse_down(MouseButton::Left, move |_, window, _| {
let size = window.window_bounds().get_bounds().size; let size = window.window_bounds().get_bounds().size;
let pos = window.mouse_position(); let pos = window.mouse_position();