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.

Fix macOS mouse cursor flicker (#55068)

Agus Zubiagad010b06a77d2
diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs
index 4606ee8a5b8..4cea82bde7a 100644
--- a/crates/gpui/src/window.rs
+++ b/crates/gpui/src/window.rs
@@ -601,6 +601,22 @@ impl HitboxId {
if window.last_input_was_keyboard() {
return false;
}
+ self.hit_test(window)
+ }
+
+ /// Checks if the hitbox with this ID is currently hovered, regardless of the last
+ /// input modality used.
+ ///
+ /// See [`HitboxId::is_hovered`] for more details.
+ pub(crate) fn is_hovered_ignoring_last_input(self, window: &Window) -> bool {
+ // If this hitbox has captured the pointer, it's always considered hovered
+ if window.captured_hitbox == Some(self) {
+ return true;
+ }
+ self.hit_test(window)
+ }
+
+ fn hit_test(self, window: &Window) -> bool {
let hit_test = &window.mouse_hit_test;
for id in hit_test.ids.iter().take(hit_test.hover_hitbox_count) {
if self == *id {
@@ -877,9 +893,11 @@ impl Frame {
.rev()
.fold_while(None, |style, request| match request.hitbox_id {
None => Done(Some(request.style)),
- Some(hitbox_id) => Continue(
- style.or_else(|| hitbox_id.is_hovered(window).then_some(request.style)),
- ),
+ Some(hitbox_id) => Continue(style.or_else(|| {
+ hitbox_id
+ .is_hovered_ignoring_last_input(window)
+ .then_some(request.style)
+ })),
})
.into_inner()
}
diff --git a/crates/gpui_macos/src/platform.rs b/crates/gpui_macos/src/platform.rs
index 9444be19d7e..fa37597065f 100644
--- a/crates/gpui_macos/src/platform.rs
+++ b/crates/gpui_macos/src/platform.rs
@@ -1,6 +1,7 @@
use crate::{
BoolExt, MacDispatcher, MacDisplay, MacKeyboardLayout, MacKeyboardMapper, MacWindow,
events::key_to_native, ns_string, pasteboard::Pasteboard, renderer,
+ set_active_window_cursor_style,
};
use anyhow::{Context as _, anyhow};
use block::ConcreteBlock;
@@ -979,52 +980,7 @@ impl Platform for MacPlatform {
/// in macOS's [NSCursor](https://developer.apple.com/documentation/appkit/nscursor).
fn set_cursor_style(&self, style: CursorStyle) {
unsafe {
- if style == CursorStyle::None {
- let _: () = msg_send![class!(NSCursor), setHiddenUntilMouseMoves:YES];
- return;
- }
-
- let new_cursor: id = match style {
- CursorStyle::Arrow => msg_send![class!(NSCursor), arrowCursor],
- CursorStyle::IBeam => msg_send![class!(NSCursor), IBeamCursor],
- CursorStyle::Crosshair => msg_send![class!(NSCursor), crosshairCursor],
- CursorStyle::ClosedHand => msg_send![class!(NSCursor), closedHandCursor],
- CursorStyle::OpenHand => msg_send![class!(NSCursor), openHandCursor],
- CursorStyle::PointingHand => msg_send![class!(NSCursor), pointingHandCursor],
- CursorStyle::ResizeLeftRight => msg_send![class!(NSCursor), resizeLeftRightCursor],
- CursorStyle::ResizeUpDown => msg_send![class!(NSCursor), resizeUpDownCursor],
- CursorStyle::ResizeLeft => msg_send![class!(NSCursor), resizeLeftCursor],
- CursorStyle::ResizeRight => msg_send![class!(NSCursor), resizeRightCursor],
- CursorStyle::ResizeColumn => msg_send![class!(NSCursor), resizeLeftRightCursor],
- CursorStyle::ResizeRow => msg_send![class!(NSCursor), resizeUpDownCursor],
- CursorStyle::ResizeUp => msg_send![class!(NSCursor), resizeUpCursor],
- CursorStyle::ResizeDown => msg_send![class!(NSCursor), resizeDownCursor],
-
- // Undocumented, private class methods:
- // https://stackoverflow.com/questions/27242353/cocoa-predefined-resize-mouse-cursor
- CursorStyle::ResizeUpLeftDownRight => {
- msg_send![class!(NSCursor), _windowResizeNorthWestSouthEastCursor]
- }
- CursorStyle::ResizeUpRightDownLeft => {
- msg_send![class!(NSCursor), _windowResizeNorthEastSouthWestCursor]
- }
-
- CursorStyle::IBeamCursorForVerticalLayout => {
- msg_send![class!(NSCursor), IBeamCursorForVerticalLayout]
- }
- CursorStyle::OperationNotAllowed => {
- msg_send![class!(NSCursor), operationNotAllowedCursor]
- }
- CursorStyle::DragLink => msg_send![class!(NSCursor), dragLinkCursor],
- CursorStyle::DragCopy => msg_send![class!(NSCursor), dragCopyCursor],
- CursorStyle::ContextualMenu => msg_send![class!(NSCursor), contextualMenuCursor],
- CursorStyle::None => unreachable!(),
- };
-
- let old_cursor: id = msg_send![class!(NSCursor), currentCursor];
- if new_cursor != old_cursor {
- let _: () = msg_send![new_cursor, set];
- }
+ set_active_window_cursor_style(style);
}
}
diff --git a/crates/gpui_macos/src/window.rs b/crates/gpui_macos/src/window.rs
index d6e1d296ac7..b8dafbbc90b 100644
--- a/crates/gpui_macos/src/window.rs
+++ b/crates/gpui_macos/src/window.rs
@@ -25,11 +25,11 @@ use cocoa::{
};
use dispatch2::DispatchQueue;
use gpui::{
- AnyWindowHandle, BackgroundExecutor, Bounds, Capslock, ExternalPaths, FileDropEvent,
- ForegroundExecutor, KeyDownEvent, Keystroke, Modifiers, ModifiersChangedEvent, MouseButton,
- MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, PlatformAtlas, PlatformDisplay,
- PlatformInput, PlatformInputHandler, PlatformWindow, Point, PromptButton, PromptLevel,
- RequestFrameOptions, SharedString, Size, SystemWindowTab, WindowAppearance,
+ AnyWindowHandle, BackgroundExecutor, Bounds, Capslock, CursorStyle, ExternalPaths,
+ FileDropEvent, ForegroundExecutor, KeyDownEvent, Keystroke, Modifiers, ModifiersChangedEvent,
+ MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, PlatformAtlas,
+ PlatformDisplay, PlatformInput, PlatformInputHandler, PlatformWindow, Point, PromptButton,
+ PromptLevel, RequestFrameOptions, SharedString, Size, SystemWindowTab, WindowAppearance,
WindowBackgroundAppearance, WindowBounds, WindowControlArea, WindowKind, WindowParams, point,
px, size,
};
@@ -173,6 +173,10 @@ unsafe fn build_classes() {
sel!(mouseMoved:),
handle_view_event as extern "C" fn(&Object, Sel, id),
);
+ decl.add_method(
+ sel!(resetCursorRects),
+ reset_cursor_rects as extern "C" fn(&Object, Sel),
+ );
decl.add_method(
sel!(pressureChangeWithEvent:),
handle_view_event as extern "C" fn(&Object, Sel, id),
@@ -313,6 +317,35 @@ pub(crate) fn convert_mouse_position(position: NSPoint, window_height: Pixels) -
)
}
+/// Stores the cursor style on the active GPUI window and invalidates its cursor rects.
+///
+/// # Safety
+///
+/// This function is not thread safe. Callers must ensure this is called on the AppKit main
+/// thread because it reads the active AppKit window and updates GPUI window state associated
+/// with Objective-C objects.
+pub(crate) unsafe fn set_active_window_cursor_style(style: CursorStyle) {
+ // SAFETY: The caller guarantees AppKit main-thread access. The class check ensures the
+ // window has our WINDOW_STATE_IVAR before reading it.
+ unsafe {
+ let app = NSApplication::sharedApplication(nil);
+ let main_window: id = msg_send![app, mainWindow];
+ if main_window.is_null() || !msg_send![main_window, isKindOfClass: WINDOW_CLASS] {
+ return;
+ }
+
+ let window_state = get_window_state(&*main_window);
+ let mut window_state = window_state.lock();
+ if window_state.cursor_style != style {
+ window_state.cursor_style = style;
+ let _: () = msg_send![
+ window_state.native_window,
+ invalidateCursorRectsForView: window_state.native_view.as_ptr()
+ ];
+ }
+ }
+}
+
unsafe fn build_window_class(name: &'static str, superclass: &Class) -> *const Class {
unsafe {
let mut decl = ClassDecl::new(name, superclass).unwrap();
@@ -429,6 +462,8 @@ struct MacWindowState {
native_view: NonNull<Object>,
blurred_view: Option<id>,
background_appearance: WindowBackgroundAppearance,
+ cursor_style: CursorStyle,
+ cursor_hidden: bool,
display_link: Option<DisplayLink>,
renderer: renderer::Renderer,
request_frame_callback: Option<Box<dyn FnMut(RequestFrameOptions)>>,
@@ -746,6 +781,8 @@ impl MacWindow {
native_view: NonNull::new_unchecked(native_view),
blurred_view: None,
background_appearance: WindowBackgroundAppearance::Opaque,
+ cursor_style: CursorStyle::Arrow,
+ cursor_hidden: false,
display_link: None,
renderer: renderer::new_renderer(
renderer_context,
@@ -1767,6 +1804,79 @@ extern "C" fn dealloc_view(this: &Object, _: Sel) {
}
}
+extern "C" fn reset_cursor_rects(this: &Object, _: Sel) {
+ // SAFETY: AppKit invokes cursor-rect updates on the main thread for GPUIView instances,
+ // whose WINDOW_STATE_IVAR is initialized when the view is created. The cursor registered
+ // below is a valid NSCursor.
+ unsafe {
+ let _: () = msg_send![super(this, class!(NSView)), resetCursorRects];
+
+ let window_state = get_window_state(this);
+ let cursor_style;
+ let cursor_hidden;
+
+ {
+ let mut window_state = window_state.lock();
+
+ if matches!(window_state.cursor_style, CursorStyle::None) {
+ if !window_state.cursor_hidden {
+ let _: () = msg_send![class!(NSCursor), hide];
+ window_state.cursor_hidden = true;
+ }
+ return;
+ }
+
+ cursor_style = window_state.cursor_style;
+ cursor_hidden = window_state.cursor_hidden;
+ };
+
+ let cursor: id = match cursor_style {
+ CursorStyle::Arrow => msg_send![class!(NSCursor), arrowCursor],
+ CursorStyle::IBeam => msg_send![class!(NSCursor), IBeamCursor],
+ CursorStyle::Crosshair => msg_send![class!(NSCursor), crosshairCursor],
+ CursorStyle::ClosedHand => msg_send![class!(NSCursor), closedHandCursor],
+ CursorStyle::OpenHand => msg_send![class!(NSCursor), openHandCursor],
+ CursorStyle::PointingHand => msg_send![class!(NSCursor), pointingHandCursor],
+ CursorStyle::ResizeLeftRight => msg_send![class!(NSCursor), resizeLeftRightCursor],
+ CursorStyle::ResizeUpDown => msg_send![class!(NSCursor), resizeUpDownCursor],
+ CursorStyle::ResizeLeft => msg_send![class!(NSCursor), resizeLeftCursor],
+ CursorStyle::ResizeRight => msg_send![class!(NSCursor), resizeRightCursor],
+ CursorStyle::ResizeColumn => msg_send![class!(NSCursor), resizeLeftRightCursor],
+ CursorStyle::ResizeRow => msg_send![class!(NSCursor), resizeUpDownCursor],
+ CursorStyle::ResizeUp => msg_send![class!(NSCursor), resizeUpCursor],
+ CursorStyle::ResizeDown => msg_send![class!(NSCursor), resizeDownCursor],
+
+ // Undocumented, private class methods:
+ // https://stackoverflow.com/questions/27242353/cocoa-predefined-resize-mouse-cursor
+ CursorStyle::ResizeUpLeftDownRight => {
+ msg_send![class!(NSCursor), _windowResizeNorthWestSouthEastCursor]
+ }
+ CursorStyle::ResizeUpRightDownLeft => {
+ msg_send![class!(NSCursor), _windowResizeNorthEastSouthWestCursor]
+ }
+
+ CursorStyle::IBeamCursorForVerticalLayout => {
+ msg_send![class!(NSCursor), IBeamCursorForVerticalLayout]
+ }
+ CursorStyle::OperationNotAllowed => {
+ msg_send![class!(NSCursor), operationNotAllowedCursor]
+ }
+ CursorStyle::DragLink => msg_send![class!(NSCursor), dragLinkCursor],
+ CursorStyle::DragCopy => msg_send![class!(NSCursor), dragCopyCursor],
+ CursorStyle::ContextualMenu => msg_send![class!(NSCursor), contextualMenuCursor],
+ CursorStyle::None => unreachable!(),
+ };
+
+ if cursor_hidden {
+ let _: () = msg_send![class!(NSCursor), unhide];
+ window_state.lock().cursor_hidden = false;
+ }
+
+ let bounds = NSView::bounds(this as *const Object as id);
+ let _: () = msg_send![this, addCursorRect: bounds cursor: cursor];
+ }
+}
+
extern "C" fn handle_key_equivalent(this: &Object, _: Sel, native_event: id) -> BOOL {
handle_key_event(this, native_event, true)
}