From c63af5efaae45c2ee7a39a261be703c1fd907a39 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 22 Sep 2024 10:16:33 -0700 Subject: [PATCH] macos: cannot call mouseEntered/Exited with blank NSEvent Fixes #2283 macOS <= 14 crash with an invalid NSEvent error. macOS 15 seems to just ignore the blank event. We just got lucky with this. Since we don't override mouseEntered/Exited anymore we can remove this completely. The regression was from: e89a4f74089b66c00043096589dc75fff5ab6674 --- .../Sources/Ghostty/SurfaceView_AppKit.swift | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/macos/Sources/Ghostty/SurfaceView_AppKit.swift b/macos/Sources/Ghostty/SurfaceView_AppKit.swift index 82bc547de..2aee1cd5a 100644 --- a/macos/Sources/Ghostty/SurfaceView_AppKit.swift +++ b/macos/Sources/Ghostty/SurfaceView_AppKit.swift @@ -233,25 +233,6 @@ extension Ghostty { // The size represents our final size we're going for. let scaledSize = self.convertToBacking(size) setSurfaceSize(width: UInt32(scaledSize.width), height: UInt32(scaledSize.height)) - - // Frame changes do not always call mouseEntered/mouseExited, so we do some - // calculations ourself to call those events. - if let window = self.window { - let mouseScreen = NSEvent.mouseLocation - let mouseWindow = window.convertPoint(fromScreen: mouseScreen) - let mouseView = self.convert(mouseWindow, from: nil) - let isEntered = self.isMousePoint(mouseView, in: bounds) - if (isEntered) { - mouseEntered(with: NSEvent()) - } else { - mouseExited(with: NSEvent()) - } - } else { - // If we don't have a window, then our mouse can NOT be in our view. - // When the window comes back, I believe this event fires again so - // we'll get a mouseEntered. - mouseExited(with: NSEvent()) - } } private func setSurfaceSize(width: UInt32, height: UInt32) { @@ -261,7 +242,13 @@ extension Ghostty { ghostty_surface_set_size(surface, width, height) // Update our cached size metrics - self.surfaceSize = ghostty_surface_size(surface) + let size = ghostty_surface_size(surface) + DispatchQueue.main.async { + // DispatchQueue required since this may be called by SwiftUI off + // the main thread and Published changes need to be on the main + // thread. This caused a crash on macOS <= 14. + self.surfaceSize = size + } } func setCursorShape(_ shape: ghostty_mouse_shape_e) {