Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
windows: Fix main thread blocking when resizing or moving window (#10758)
张小白fee2065b64b7
diff --git a/crates/gpui/src/platform/windows/platform.rs b/crates/gpui/src/platform/windows/platform.rsindex 41baf4fe679..72772e871b0 100644--- a/crates/gpui/src/platform/windows/platform.rs+++ b/crates/gpui/src/platform/windows/platform.rs@@ -78,6 +78,13 @@ impl WindowsPlatformInner {.find(|entry| *entry == &hwnd).and_then(|hwnd| try_get_window_inner(*hwnd))}++ #[inline]+ pub fn run_foreground_tasks(&self) {+ for runnable in self.main_receiver.drain() {+ runnable.run();+ }+ }}#[derive(Default)]@@ -182,10 +189,9 @@ impl WindowsPlatform {Self { inner }}+ #[inline]fn run_foreground_tasks(&self) {- for runnable in self.inner.main_receiver.drain() {- runnable.run();- }+ self.inner.run_foreground_tasks();}fn redraw_all(&self) {diff --git a/crates/gpui/src/platform/windows/window.rs b/crates/gpui/src/platform/windows/window.rsindex 2cc6b06eed3..77b80eed2ed 100644--- a/crates/gpui/src/platform/windows/window.rs+++ b/crates/gpui/src/platform/windows/window.rs@@ -253,6 +253,9 @@ impl WindowsWindowInner {WM_CREATE => self.handle_create_msg(lparam),WM_MOVE => self.handle_move_msg(lparam),WM_SIZE => self.handle_size_msg(lparam),+ WM_ENTERSIZEMOVE | WM_ENTERMENULOOP => self.handle_size_move_loop(),+ WM_EXITSIZEMOVE | WM_EXITMENULOOP => self.handle_size_move_loop_exit(),+ WM_TIMER => self.handle_timer_msg(wparam),WM_NCCALCSIZE => self.handle_calc_client_size(wparam, lparam),WM_DPICHANGED => self.handle_dpi_changed_msg(wparam, lparam),WM_NCHITTEST => self.handle_hit_test_msg(msg, wparam, lparam),@@ -342,10 +345,38 @@ impl WindowsWindowInner {let logical_size = logical_size(new_physical_size, scale_factor);callback(logical_size, scale_factor);}- self.invalidate_client_area();Some(0)}+ fn handle_size_move_loop(&self) -> Option<isize> {+ unsafe {+ let ret = SetTimer(self.hwnd, SIZE_MOVE_LOOP_TIMER_ID, USER_TIMER_MINIMUM, None);+ if ret == 0 {+ log::error!(+ "unable to create timer: {}",+ std::io::Error::last_os_error()+ );+ }+ }+ None+ }++ fn handle_size_move_loop_exit(&self) -> Option<isize> {+ unsafe {+ KillTimer(self.hwnd, SIZE_MOVE_LOOP_TIMER_ID).log_err();+ }+ None+ }++ fn handle_timer_msg(&self, wparam: WPARAM) -> Option<isize> {+ if wparam.0 == SIZE_MOVE_LOOP_TIMER_ID {+ self.platform_inner.run_foreground_tasks();+ self.handle_paint_msg();+ return Some(0);+ }+ None+ }+fn handle_paint_msg(&self) -> Option<isize> {let mut paint_struct = PAINTSTRUCT::default();let _hdc = unsafe { BeginPaint(self.hwnd, &mut paint_struct) };@@ -1883,6 +1914,7 @@ const DRAGDROP_GET_FILES_COUNT: u32 = 0xFFFFFFFF;const DOUBLE_CLICK_INTERVAL: Duration = Duration::from_millis(500);// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetricsconst DOUBLE_CLICK_SPATIAL_TOLERANCE: i32 = 4;+const SIZE_MOVE_LOOP_TIMER_ID: usize = 1;#[cfg(test)]mod tests {