From 76b0732de2a8419eea94d135eee3ade537cce919 Mon Sep 17 00:00:00 2001 From: Saad Najmi Date: Tue, 27 Feb 2024 12:00:33 -0800 Subject: [PATCH] PR feedback --- .../Libraries/Text/RCTTextAttributes.mm | 72 ------------------- .../View/RCTViewComponentView.mm | 63 ++++++---------- packages/react-native/React/Views/RCTCursor.h | 49 +++++++++++++ packages/react-native/React/Views/RCTView.m | 71 ------------------ .../renderer/components/view/conversions.h | 46 ++++++------ .../js/examples/Cursor/CursorExample.js | 2 + 6 files changed, 94 insertions(+), 209 deletions(-) diff --git a/packages/react-native/Libraries/Text/RCTTextAttributes.mm b/packages/react-native/Libraries/Text/RCTTextAttributes.mm index f33e23e6b2864d..fe9ccb5d47ba00 100644 --- a/packages/react-native/Libraries/Text/RCTTextAttributes.mm +++ b/packages/react-native/Libraries/Text/RCTTextAttributes.mm @@ -15,78 +15,6 @@ NSString *const RCTTextAttributesIsHighlightedAttributeName = @"RCTTextAttributesIsHighlightedAttributeName"; NSString *const RCTTextAttributesTagAttributeName = @"RCTTextAttributesTagAttributeName"; -#if TARGET_OS_OSX // [macOS -// Duplicate this function here as RCTTextAttributes doesn't depend on RCTView -static NSCursor *NSCursorFromRCTCursor(RCTCursor cursor) -{ - NSCursor *platformCursor; - - switch (cursor) { - case RCTCursorAlias: - platformCursor = [NSCursor dragLinkCursor]; - break; - case RCTCursorAuto: - platformCursor = [NSCursor arrowCursor]; - break; - case RCTCursorColumnResize: - platformCursor = [NSCursor resizeLeftRightCursor]; - break; - case RCTCursorContextualMenu: - platformCursor = [NSCursor contextualMenuCursor]; - break; - case RCTCursorCopy: - platformCursor = [NSCursor dragCopyCursor]; - break; - case RCTCursorCrosshair: - platformCursor = [NSCursor crosshairCursor]; - break; - case RCTCursorDefault: - platformCursor = [NSCursor arrowCursor]; - break; - case RCTCursorDisappearingItem: - platformCursor = [NSCursor disappearingItemCursor]; - break; - case RCTCursorEastResize: - platformCursor = [NSCursor resizeRightCursor]; - break; - case RCTCursorGrab: - platformCursor = [NSCursor openHandCursor]; - break; - case RCTCursorGrabbing: - platformCursor = [NSCursor closedHandCursor]; - break; - case RCTCursorNorthResize: - platformCursor = [NSCursor resizeUpCursor]; - break; - case RCTCursorNoDrop: - platformCursor = [NSCursor operationNotAllowedCursor]; - break; - case RCTCursorNotAllowed: - platformCursor = [NSCursor operationNotAllowedCursor]; - break; - case RCTCursorPointer: - platformCursor = [NSCursor pointingHandCursor]; - break; - case RCTCursorRowResize: - platformCursor = [NSCursor resizeUpDownCursor]; - break; - case RCTCursorSouthResize: - platformCursor = [NSCursor resizeDownCursor]; - break; - case RCTCursorText: - platformCursor = [NSCursor IBeamCursor]; - break; - case RCTCursorVerticalText: - platformCursor = [NSCursor IBeamCursorForVerticalLayout]; - break; - case RCTCursorWestResize: - platformCursor = [NSCursor resizeLeftCursor]; - break; - } - return platformCursor; -} -#endif // macOS] - @implementation RCTTextAttributes // [macOS 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 d92eeb22f0227f..198715cb468da3 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -603,71 +603,48 @@ static RCTBorderStyle RCTBorderStyleFromBorderStyle(BorderStyle borderStyle) #if TARGET_OS_OSX // [macOS static NSCursor *NSCursorFromCursor(Cursor cursor) { - NSCursor *platformCursor; - switch (cursor) { case Cursor::Alias: - platformCursor = [NSCursor dragLinkCursor]; - break; + return [NSCursor dragLinkCursor]; case Cursor::Auto: - platformCursor = [NSCursor arrowCursor]; - break; + return [NSCursor arrowCursor]; case Cursor::ColumnResize: - platformCursor = [NSCursor resizeLeftRightCursor]; - break; + return [NSCursor resizeLeftRightCursor]; case Cursor::ContextualMenu: - platformCursor = [NSCursor contextualMenuCursor]; - break; + return [NSCursor contextualMenuCursor]; case Cursor::Copy: - platformCursor = [NSCursor dragCopyCursor]; - break; + return [NSCursor dragCopyCursor]; case Cursor::Crosshair: - platformCursor = [NSCursor crosshairCursor]; - break; + return [NSCursor crosshairCursor]; case Cursor::Default: - platformCursor = [NSCursor arrowCursor]; - break; + return [NSCursor arrowCursor]; case Cursor::DisappearingItem: - platformCursor = [NSCursor disappearingItemCursor]; - break; + return [NSCursor disappearingItemCursor]; case Cursor::EastResize: - platformCursor = [NSCursor resizeRightCursor]; - break; + return [NSCursor resizeRightCursor]; case Cursor::Grab: - platformCursor = [NSCursor openHandCursor]; - break; + return [NSCursor openHandCursor]; case Cursor::Grabbing: - platformCursor = [NSCursor closedHandCursor]; - break; + return [NSCursor closedHandCursor]; case Cursor::NorthResize: - platformCursor = [NSCursor resizeUpCursor]; - break; + return [NSCursor resizeUpCursor]; case Cursor::NoDrop: - platformCursor = [NSCursor operationNotAllowedCursor]; - break; + return [NSCursor operationNotAllowedCursor]; case Cursor::NotAllowed: - platformCursor = [NSCursor operationNotAllowedCursor]; - break; + return [NSCursor operationNotAllowedCursor]; case Cursor::Pointer: - platformCursor = [NSCursor pointingHandCursor]; - break; + return [NSCursor pointingHandCursor]; case Cursor::RowResize: - platformCursor = [NSCursor resizeUpDownCursor]; - break; + return [NSCursor resizeUpDownCursor]; case Cursor::SouthResize: - platformCursor = [NSCursor resizeDownCursor]; - break; + return [NSCursor resizeDownCursor]; case Cursor::Text: - platformCursor = [NSCursor IBeamCursor]; - break; + return [NSCursor IBeamCursor]; case Cursor::VerticalText: - platformCursor = [NSCursor IBeamCursorForVerticalLayout]; - break; + return [NSCursor IBeamCursorForVerticalLayout]; case Cursor::WestResize: - platformCursor = [NSCursor resizeLeftCursor]; - break; + return [NSCursor resizeLeftCursor]; } - return platformCursor; } #endif // macOS] diff --git a/packages/react-native/React/Views/RCTCursor.h b/packages/react-native/React/Views/RCTCursor.h index a3890e69e10786..5d6727ca2cb1e5 100644 --- a/packages/react-native/React/Views/RCTCursor.h +++ b/packages/react-native/React/Views/RCTCursor.h @@ -29,3 +29,52 @@ typedef NS_ENUM(NSInteger, RCTCursor) { RCTCursorVerticalText, RCTCursorWestResize, }; + +#if TARGET_OS_OSX // [macOS +inline static NSCursor *NSCursorFromRCTCursor(RCTCursor cursor) +{ + switch (cursor) { + case RCTCursorAlias: + return [NSCursor dragLinkCursor]; + case RCTCursorAuto: + return [NSCursor arrowCursor]; + case RCTCursorColumnResize: + return [NSCursor resizeLeftRightCursor]; + case RCTCursorContextualMenu: + return [NSCursor contextualMenuCursor]; + case RCTCursorCopy: + return [NSCursor dragCopyCursor]; + case RCTCursorCrosshair: + return [NSCursor crosshairCursor]; + case RCTCursorDefault: + return [NSCursor arrowCursor]; + case RCTCursorDisappearingItem: + return [NSCursor disappearingItemCursor]; + case RCTCursorEastResize: + return [NSCursor resizeRightCursor]; + case RCTCursorGrab: + return [NSCursor openHandCursor]; + case RCTCursorGrabbing: + return [NSCursor closedHandCursor]; + case RCTCursorNorthResize: + return [NSCursor resizeUpCursor]; + case RCTCursorNoDrop: + return [NSCursor operationNotAllowedCursor]; + case RCTCursorNotAllowed: + return [NSCursor operationNotAllowedCursor]; + case RCTCursorPointer: + return [NSCursor pointingHandCursor]; + case RCTCursorRowResize: + return [NSCursor resizeUpDownCursor]; + case RCTCursorSouthResize: + return [NSCursor resizeDownCursor]; + case RCTCursorText: + return [NSCursor IBeamCursor]; + case RCTCursorVerticalText: + return [NSCursor IBeamCursorForVerticalLayout]; + case RCTCursorWestResize: + return [NSCursor resizeLeftCursor]; + } +} +#endif // macOS] + diff --git a/packages/react-native/React/Views/RCTView.m b/packages/react-native/React/Views/RCTView.m index d360bc6eded2e1..80d299ee55d84e 100644 --- a/packages/react-native/React/Views/RCTView.m +++ b/packages/react-native/React/Views/RCTView.m @@ -1379,77 +1379,6 @@ static void RCTUpdateHoverStyleForView(RCTView *view) } #endif // visionOS] -#if TARGET_OS_OSX // [macOS -static NSCursor *NSCursorFromRCTCursor(RCTCursor cursor) -{ - NSCursor *platformCursor; - - switch (cursor) { - case RCTCursorAlias: - platformCursor = [NSCursor dragLinkCursor]; - break; - case RCTCursorAuto: - platformCursor = [NSCursor arrowCursor]; - break; - case RCTCursorColumnResize: - platformCursor = [NSCursor resizeLeftRightCursor]; - break; - case RCTCursorContextualMenu: - platformCursor = [NSCursor contextualMenuCursor]; - break; - case RCTCursorCopy: - platformCursor = [NSCursor dragCopyCursor]; - break; - case RCTCursorCrosshair: - platformCursor = [NSCursor crosshairCursor]; - break; - case RCTCursorDefault: - platformCursor = [NSCursor arrowCursor]; - break; - case RCTCursorDisappearingItem: - platformCursor = [NSCursor disappearingItemCursor]; - break; - case RCTCursorEastResize: - platformCursor = [NSCursor resizeRightCursor]; - break; - case RCTCursorGrab: - platformCursor = [NSCursor openHandCursor]; - break; - case RCTCursorGrabbing: - platformCursor = [NSCursor closedHandCursor]; - break; - case RCTCursorNorthResize: - platformCursor = [NSCursor resizeUpCursor]; - break; - case RCTCursorNoDrop: - platformCursor = [NSCursor operationNotAllowedCursor]; - break; - case RCTCursorNotAllowed: - platformCursor = [NSCursor operationNotAllowedCursor]; - break; - case RCTCursorPointer: - platformCursor = [NSCursor pointingHandCursor]; - break; - case RCTCursorRowResize: - platformCursor = [NSCursor resizeUpDownCursor]; - break; - case RCTCursorSouthResize: - platformCursor = [NSCursor resizeDownCursor]; - break; - case RCTCursorText: - platformCursor = [NSCursor IBeamCursor]; - break; - case RCTCursorVerticalText: - platformCursor = [NSCursor IBeamCursorForVerticalLayout]; - break; - case RCTCursorWestResize: - platformCursor = [NSCursor resizeLeftCursor]; - break; - } - return platformCursor; -} -#endif // macOS] - - (void)updateClippingForLayer:(CALayer *)layer { CALayer *mask = nil; diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h b/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h index 57580704e8893c..43cbc7d4b871c2 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h +++ b/packages/react-native/ReactCommon/react/renderer/components/view/conversions.h @@ -696,84 +696,84 @@ inline void fromRawValue( if (!value.hasType()) { return; } - auto stringValue = (std::string)value; - if (stringValue == "alias") { - result = Cursor::Alias; - return; - } + auto stringValue = std::string_view{(std::string)value}; if (stringValue == "auto") { result = Cursor::Auto; return; } - if (stringValue == "col-resize") { + if (stringValue == "alias") { + result = Cursor::Alias; + return; + } + if (stringValue == "col-resize") { result = Cursor::ColumnResize; return; } - if (stringValue == "context-menu") { + if (stringValue == "context-menu") { result = Cursor::ContextualMenu; return; } - if (stringValue == "copy") { + if (stringValue == "copy") { result = Cursor::Copy; return; } - if (stringValue == "crosshair") { + if (stringValue == "crosshair") { result = Cursor::Crosshair; return; } - if (stringValue == "default") { + if (stringValue == "default") { result = Cursor::Default; return; } - if (stringValue == "disappearing-item") { + if (stringValue == "disappearing-item") { result = Cursor::DisappearingItem; return; } - if (stringValue == "e-resize") { + if (stringValue == "e-resize") { result = Cursor::EastResize; return; } - if (stringValue == "grab") { + if (stringValue == "grab") { result = Cursor::Grab; return; } - if (stringValue == "grabbing") { + if (stringValue == "grabbing") { result = Cursor::Grabbing; return; } - if (stringValue == "n-resize") { + if (stringValue == "n-resize") { result = Cursor::NorthResize; return; } - if (stringValue == "no-drop") { + if (stringValue == "no-drop") { result = Cursor::NoDrop; return; } - if (stringValue == "not-allowed") { + if (stringValue == "not-allowed") { result = Cursor::NotAllowed; return; } - if (stringValue == "pointer") { + if (stringValue == "pointer") { result = Cursor::Pointer; return; } - if (stringValue == "row-resize") { + if (stringValue == "row-resize") { result = Cursor::RowResize; return; } - if (stringValue == "s-resize") { + if (stringValue == "s-resize") { result = Cursor::SouthResize; return; } - if (stringValue == "text") { + if (stringValue == "text") { result = Cursor::Text; return; } - if (stringValue == "vertical-text") { + if (stringValue == "vertical-text") { result = Cursor::VerticalText; return; } - if (stringValue == "w-resize") { + if (stringValue == "w-resize") { result = Cursor::WestResize; return; } diff --git a/packages/rn-tester/js/examples/Cursor/CursorExample.js b/packages/rn-tester/js/examples/Cursor/CursorExample.js index 76baaa9f2f0816..5a39b4fb30752b 100644 --- a/packages/rn-tester/js/examples/Cursor/CursorExample.js +++ b/packages/rn-tester/js/examples/Cursor/CursorExample.js @@ -147,9 +147,11 @@ exports.examples = [ description: 'cursor: pointer', render: CursorExamplePointer, }, + // [macOS { title: 'macOS Cursors', description: 'macOS supports many more cursors', render: CursorExampleMacOS, }, + // macOS] ];