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.

Ignore foreign WM_USER messages (#65628)

John Ture462f873c622
diff --git a/crates/gpui_windows/src/events.rs b/crates/gpui_windows/src/events.rs
index c0e2cd472d5..809d688ed9b 100644
--- a/crates/gpui_windows/src/events.rs
+++ b/crates/gpui_windows/src/events.rs
@@ -27,7 +27,6 @@ pub(crate) const WM_GPUI_DOCK_MENU_ACTION: u32 = WM_USER + 4;
pub(crate) const WM_GPUI_FORCE_UPDATE_WINDOW: u32 = WM_USER + 5;
pub(crate) const WM_GPUI_KEYBOARD_LAYOUT_CHANGED: u32 = WM_USER + 6;
pub(crate) const WM_GPUI_GPU_DEVICE_LOST: u32 = WM_USER + 7;
-pub(crate) const WM_GPUI_KEYDOWN: u32 = WM_USER + 8;
pub(crate) const WM_GPUI_DISPLAYS_CHANGED: u32 = WM_USER + 10;
const SIZE_MOVE_LOOP_TIMER_ID: usize = 1;
@@ -149,7 +148,6 @@ impl WindowsWindowInner {
WM_MOUSEHWHEEL => self.handle_mouse_horizontal_wheel_msg(handle, wparam, lparam),
WM_SYSKEYUP => self.handle_syskeyup_msg(wparam, lparam),
WM_KEYUP => self.handle_keyup_msg(wparam, lparam),
- WM_GPUI_KEYDOWN => self.handle_keydown_msg(wparam, lparam),
WM_CHAR => self.handle_char_msg(wparam),
WM_IME_STARTCOMPOSITION => self.handle_ime_position(handle),
WM_IME_COMPOSITION => self.handle_ime_composition(handle, lparam),
@@ -157,11 +155,11 @@ impl WindowsWindowInner {
WM_SETTINGCHANGE => self.handle_system_settings_changed(handle, wparam, lparam),
WM_INPUTLANGCHANGE => self.handle_input_language_changed(),
WM_SHOWWINDOW => self.handle_window_visibility_changed(handle, wparam),
- WM_GPUI_CURSOR_STYLE_CHANGED => self.handle_cursor_changed(lparam),
- WM_GPUI_FORCE_UPDATE_WINDOW => self.draw_window(handle, true),
- WM_GPUI_GPU_DEVICE_LOST => self.handle_device_lost(lparam),
DM_POINTERHITTEST => self.handle_dm_pointer_hit_test(wparam),
WM_GETOBJECT => self.handle_wm_getobject(wparam, lparam),
+ WM_GPUI_CURSOR_STYLE_CHANGED
+ | WM_GPUI_FORCE_UPDATE_WINDOW
+ | WM_GPUI_GPU_DEVICE_LOST => self.handle_gpui_events(handle, msg, wparam, lparam),
_ => None,
};
if let Some(n) = handled {
@@ -414,7 +412,7 @@ impl WindowsWindowInner {
// It's a known bug that you can't trigger `ctrl-shift-0`. See:
// https://superuser.com/questions/1455762/ctrl-shift-number-key-combination-has-stopped-working-for-a-few-numbers
- fn handle_keydown_msg(&self, wparam: WPARAM, lparam: LPARAM) -> Option<isize> {
+ pub(crate) fn handle_keydown_msg(&self, wparam: WPARAM, lparam: LPARAM) -> Option<isize> {
let Some(input) = handle_key_event(
wparam,
lparam,
@@ -1293,7 +1291,28 @@ impl WindowsWindowInner {
.detach();
}
+ fn handle_gpui_events(
+ &self,
+ handle: HWND,
+ message: u32,
+ wparam: WPARAM,
+ lparam: LPARAM,
+ ) -> Option<isize> {
+ if wparam.0 != self.validation_number {
+ return None;
+ }
+ match message {
+ WM_GPUI_CURSOR_STYLE_CHANGED => self.handle_cursor_changed(lparam),
+ WM_GPUI_FORCE_UPDATE_WINDOW => self.draw_window(handle, true),
+ WM_GPUI_GPU_DEVICE_LOST => self.handle_device_lost(lparam),
+ _ => unreachable!(),
+ }
+ }
+
fn handle_device_lost(&self, lparam: LPARAM) -> Option<isize> {
+ if lparam.0 == 0 {
+ return None;
+ }
let devices = lparam.0 as *const DirectXDevices;
let devices = unsafe { &*devices };
if let Err(err) = self
diff --git a/crates/gpui_windows/src/platform.rs b/crates/gpui_windows/src/platform.rs
index 98009bf2654..5053bdbca18 100644
--- a/crates/gpui_windows/src/platform.rs
+++ b/crates/gpui_windows/src/platform.rs
@@ -288,11 +288,7 @@ impl WindowsPlatform {
}
pub(crate) fn window_from_hwnd(&self, hwnd: HWND) -> Option<Rc<WindowsWindowInner>> {
- self.raw_window_handles
- .read()
- .iter()
- .find(|entry| entry.as_raw() == hwnd)
- .and_then(|hwnd| window_from_hwnd(hwnd.as_raw()))
+ self.inner.window_from_hwnd(hwnd)
}
#[inline]
@@ -499,22 +495,6 @@ impl WindowsPlatform {
}
}
-fn translate_accelerator(msg: &MSG) -> Option<()> {
- if msg.message != WM_KEYDOWN && msg.message != WM_SYSKEYDOWN {
- return None;
- }
-
- let result = unsafe {
- SendMessageW(
- msg.hwnd,
- WM_GPUI_KEYDOWN,
- Some(msg.wParam),
- Some(msg.lParam),
- )
- };
- (result.0 == 0).then_some(())
-}
-
fn encode_restart_arguments(arguments: &[OsString]) -> OsString {
// `Start-Process` accepts a single native command line, so quote each argument according to
// the Windows argv parsing rules before passing the complete string through the environment.
@@ -603,7 +583,7 @@ impl Platform for WindowsPlatform {
let mut msg = MSG::default();
unsafe {
while GetMessageW(&mut msg, None, 0, 0).as_bool() {
- if translate_accelerator(&msg).is_none() {
+ if !self.inner.translate_accelerator(&msg) {
_ = TranslateMessage(&msg);
DispatchMessageW(&msg);
}
@@ -985,7 +965,7 @@ impl Platform for WindowsPlatform {
if self.inner.state.current_cursor.get().map(|c| c.0) != hcursor.map(|c| c.0) {
self.post_message(
WM_GPUI_CURSOR_STYLE_CHANGED,
- WPARAM(0),
+ WPARAM(self.inner.validation_number),
LPARAM(hcursor.map_or(0, |c| c.0 as isize)),
);
self.inner.state.current_cursor.set(hcursor);
@@ -1166,6 +1146,24 @@ impl WindowsPlatformInner {
}))
}
+ fn window_from_hwnd(&self, hwnd: HWND) -> Option<Rc<WindowsWindowInner>> {
+ self.raw_window_handles
+ .upgrade()?
+ .read()
+ .iter()
+ .find(|window| window.as_raw() == hwnd)
+ .and_then(|window| window_from_hwnd(window.as_raw()))
+ }
+
+ fn translate_accelerator(&self, msg: &MSG) -> bool {
+ if msg.message != WM_KEYDOWN && msg.message != WM_SYSKEYDOWN {
+ return false;
+ }
+
+ self.window_from_hwnd(msg.hwnd)
+ .is_some_and(|window| window.handle_keydown_msg(msg.wParam, msg.lParam) == Some(0))
+ }
+
/// Calls `project` to project to the corresponding callback field, removes it from callbacks, calls `f` with the callback and then puts the callback back.
fn with_callback<T>(
&self,
@@ -1295,7 +1293,7 @@ impl WindowsPlatformInner {
// if we don't we might not for example process window quit events
let mut msg = MSG::default();
let process_message = |msg: &_| {
- if translate_accelerator(msg).is_none() {
+ if !self.translate_accelerator(msg) {
_ = unsafe { TranslateMessage(msg) };
unsafe { DispatchMessageW(msg) };
}