From e722e95f850595a73ab68081a6aebe65c4339aea Mon Sep 17 00:00:00 2001 From: Shawn Dempsey Date: Wed, 1 May 2024 14:43:50 -0700 Subject: [PATCH] [fabric] Revert to Core Cursor implementation --- .../View/RCTViewComponentView.mm | 127 ++++++------------ .../renderer/components/view/primitives.h | 21 ++- 2 files changed, 56 insertions(+), 92 deletions(-) diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm index 7750cbebaa402d..2f05b0ed36b8a8 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -32,71 +32,6 @@ using namespace facebook::react; -#if TARGET_OS_OSX // [macOS -inline NSCursor* RCTNSCursorFromCursor(facebook::react::Cursor cursorValue) { - NSCursor *cursor = nil; - - switch (cursorValue) { - case Cursor::Arrow: - cursor = [NSCursor arrowCursor]; - break; - case Cursor::ClosedHand: - cursor = [NSCursor closedHandCursor]; - break; - case Cursor::ContextualMenu: - cursor = [NSCursor contextualMenuCursor]; - break; - case Cursor::Crosshair: - cursor = [NSCursor crosshairCursor]; - break; - case Cursor::DisappearingItem: - cursor = [NSCursor disappearingItemCursor]; - break; - case Cursor::DragCopy: - cursor = [NSCursor dragCopyCursor]; - break; - case Cursor::DragLink: - cursor = [NSCursor dragLinkCursor]; - break; - case Cursor::IBeam: - cursor = [NSCursor IBeamCursor]; - break; - case Cursor::IBeamCursorForVerticalLayout: - cursor = [NSCursor IBeamCursorForVerticalLayout]; - break; - case Cursor::OpenHand: - cursor = [NSCursor openHandCursor]; - break; - case Cursor::OperationNotAllowed: - cursor = [NSCursor operationNotAllowedCursor]; - break; - case Cursor::PointingHand: - cursor = [NSCursor pointingHandCursor]; - break; - case Cursor::ResizeDown: - cursor = [NSCursor resizeDownCursor]; - break; - case Cursor::ResizeLeft: - cursor = [NSCursor resizeLeftCursor]; - break; - case Cursor::ResizeLeftRight: - cursor = [NSCursor resizeLeftRightCursor]; - break; - case Cursor::ResizeRight: - cursor = [NSCursor resizeRightCursor]; - break; - case Cursor::ResizeUp: - cursor = [NSCursor resizeUpCursor]; - break; - case Cursor::ResizeUpDown: - cursor = [NSCursor resizeUpDownCursor]; - break; - } - - return cursor; -} -#endif // macOS] - @implementation RCTViewComponentView { RCTUIColor *_backgroundColor; // [macOS] CALayer *_borderLayer; @@ -176,18 +111,6 @@ - (void)setBackgroundColor:(RCTUIColor *)backgroundColor // [macOS] _backgroundColor = backgroundColor; } -#if TARGET_OS_OSX // [macOS -- (void)resetCursorRects -{ - [self discardCursorRects]; - if (_props->cursor != Cursor::Auto) - { - NSCursor *cursor = NSCursorFromCursor(_props->cursor); - [self addCursorRect:self.bounds cursor:cursor]; - } -} -#endif // macOS] - #pragma mark - RCTComponentViewProtocol + (ComponentDescriptorProvider)componentDescriptorProvider @@ -347,7 +270,11 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared & // `cursor` if (oldViewProps.cursor != newViewProps.cursor) { - needsInvalidateLayer = YES; +#if !TARGET_OS_OSX // [macOS] + needsInvalidateLayer = YES; // `cursor` +#else // [macOS + _cursor = NSCursorFromCursor(newViewProps.cursor); +#endif // macOS] } // `shouldRasterize` @@ -544,13 +471,6 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared & } } - // `cursor` - if (oldViewProps.cursor != newViewProps.cursor) { - _cursor = nil; - if (newViewProps.cursor.has_value()) { - _cursor = RCTNSCursorFromCursor(newViewProps.cursor.value()); - } - } #endif // macOS] _needsInvalidateLayer = _needsInvalidateLayer || needsInvalidateLayer; @@ -753,13 +673,17 @@ static RCTBorderStyle RCTBorderStyleFromBorderStyle(BorderStyle borderStyle) } #if TARGET_OS_OSX // [macOS - static NSCursor *NSCursorFromCursor(Cursor cursor) +static NSCursor *NSCursorFromCursor(Cursor cursor) { switch (cursor) { - case Cursor::Auto: - return [NSCursor arrowCursor]; case Cursor::Alias: return [NSCursor dragLinkCursor]; + case Cursor::Arrow: + return [NSCursor arrowCursor]; + case Cursor::Auto: + return [NSCursor arrowCursor]; + case Cursor::ClosedHand: + return [NSCursor closedHandCursor]; case Cursor::ColumnResize: return [NSCursor resizeLeftRightCursor]; case Cursor::ContextualMenu: @@ -772,20 +696,34 @@ static RCTBorderStyle RCTBorderStyleFromBorderStyle(BorderStyle borderStyle) return [NSCursor arrowCursor]; case Cursor::DisappearingItem: return [NSCursor disappearingItemCursor]; + case Cursor::DragCopy: + return [NSCursor dragCopyCursor]; + case Cursor::DragLink: + return [NSCursor dragLinkCursor]; case Cursor::EastResize: return [NSCursor resizeRightCursor]; case Cursor::Grab: return [NSCursor openHandCursor]; case Cursor::Grabbing: return [NSCursor closedHandCursor]; + case Cursor::IBeam: + return [NSCursor IBeamCursor]; + case Cursor::IBeamCursorForVerticalLayout: + return [NSCursor IBeamCursorForVerticalLayout]; case Cursor::NorthResize: return [NSCursor resizeUpCursor]; case Cursor::NoDrop: return [NSCursor operationNotAllowedCursor]; case Cursor::NotAllowed: return [NSCursor operationNotAllowedCursor]; + case Cursor::OpenHand: + return [NSCursor openHandCursor]; + case Cursor::OperationNotAllowed: + return [NSCursor operationNotAllowedCursor]; case Cursor::Pointer: return [NSCursor pointingHandCursor]; + case Cursor::PointingHand: + return [NSCursor pointingHandCursor]; case Cursor::RowResize: return [NSCursor resizeUpDownCursor]; case Cursor::SouthResize: @@ -796,11 +734,22 @@ static RCTBorderStyle RCTBorderStyleFromBorderStyle(BorderStyle borderStyle) return [NSCursor IBeamCursorForVerticalLayout]; case Cursor::WestResize: return [NSCursor resizeLeftCursor]; + case Cursor::ResizeDown: + return [NSCursor resizeDownCursor]; + case Cursor::ResizeLeft: + return [NSCursor resizeLeftCursor]; + case Cursor::ResizeLeftRight: + return [NSCursor resizeLeftRightCursor]; + case Cursor::ResizeRight: + return [NSCursor resizeRightCursor]; + case Cursor::ResizeUp: + return [NSCursor resizeUpCursor]; + case Cursor::ResizeUpDown: + return [NSCursor resizeDownCursor]; } } #endif // macOS] - - (void)invalidateLayer { CALayer *layer = self.layer; diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/primitives.h b/packages/react-native/ReactCommon/react/renderer/components/view/primitives.h index b97035e7cde480..a1eab3f86e3015 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/primitives.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/primitives.h @@ -92,22 +92,37 @@ enum class BorderCurve : uint8_t { Circular, Continuous }; enum class BorderStyle : uint8_t { Solid, Dotted, Dashed }; // [macOS [visionOS] -enum class Cursor : uint8_t { - Auto, +enum class Cursor : uint8_t { Alias, + Arrow, + Auto, + ClosedHand, ColumnResize, ContextualMenu, Copy, Crosshair, Default, DisappearingItem, + DragCopy, + DragLink, EastResize, Grab, Grabbing, - NorthResize, + IBeam, + IBeamCursorForVerticalLayout, NoDrop, + NorthResize, NotAllowed, + OpenHand, + OperationNotAllowed, Pointer, + PointingHand, + ResizeDown, + ResizeLeft, + ResizeLeftRight, + ResizeRight, + ResizeUp, + ResizeUpDown, RowResize, SouthResize, Text,