react-native-macos 0.0.0-8c5d0eced → 0.0.0-8f5b6f691

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/Libraries/Components/ScrollView/ScrollView.js +11 -45
  2. package/Libraries/Components/TextInput/TextInput.js +69 -1
  3. package/Libraries/Components/View/ReactNativeStyleAttributes.js +1 -0
  4. package/Libraries/Components/View/ReactNativeViewViewConfig.js +1 -0
  5. package/Libraries/Components/View/ReactNativeViewViewConfigMacOS.js +3 -3
  6. package/Libraries/Components/View/ViewPropTypes.js +10 -6
  7. package/Libraries/Core/ReactNativeVersion.js +1 -1
  8. package/Libraries/LinkingIOS/RCTLinkingManager.h +1 -0
  9. package/Libraries/LinkingIOS/macos/RCTLinkingManager.mm +12 -5
  10. package/Libraries/Lists/FlatList.js +25 -1
  11. package/Libraries/Lists/VirtualizedList.js +106 -113
  12. package/Libraries/Lists/VirtualizedSectionList.js +9 -5
  13. package/Libraries/StyleSheet/StyleSheetTypes.js +25 -0
  14. package/Libraries/Text/RCTTextAttributes.m +5 -5
  15. package/Libraries/Text/TextInput/Multiline/RCTMultilineTextInputView.m +3 -1
  16. package/Libraries/Text/TextInput/Multiline/RCTUITextView.m +68 -2
  17. package/Libraries/Text/TextInput/RCTBackedTextInputDelegate.h +12 -1
  18. package/Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m +12 -3
  19. package/Libraries/Text/TextInput/RCTBackedTextInputViewProtocol.h +1 -0
  20. package/Libraries/Text/TextInput/RCTBaseTextInputView.h +6 -0
  21. package/Libraries/Text/TextInput/RCTBaseTextInputView.m +77 -2
  22. package/Libraries/Text/TextInput/RCTBaseTextInputViewManager.m +7 -0
  23. package/Libraries/Text/TextInput/Singleline/RCTUITextField.m +24 -0
  24. package/Libraries/Types/CoreEventTypes.js +0 -1
  25. package/React/Base/RCTConvert.m +3 -0
  26. package/React/Base/RCTRootView.m +0 -9
  27. package/React/Base/RCTUtils.m +27 -0
  28. package/React/Base/RCTVersion.m +1 -1
  29. package/React/Base/macOS/RCTUIKit.m +7 -0
  30. package/React/CoreModules/RCTAppearance.mm +21 -5
  31. package/React/CoreModules/RCTPerfMonitor.mm +1 -1
  32. package/React/Modules/RCTUIManager.m +2 -1
  33. package/React/Views/RCTActivityIndicatorView.m +2 -1
  34. package/React/Views/RCTCursor.h +39 -0
  35. package/React/Views/RCTCursor.m +105 -0
  36. package/React/Views/RCTView.h +16 -0
  37. package/React/Views/RCTView.m +135 -190
  38. package/React/Views/RCTViewKeyboardEvent.h +6 -28
  39. package/React/Views/RCTViewKeyboardEvent.m +66 -61
  40. package/React/Views/RCTViewManager.m +17 -12
  41. package/React/Views/ScrollView/RCTScrollContentView.h +4 -0
  42. package/React/Views/ScrollView/RCTScrollContentView.m +18 -0
  43. package/React/Views/ScrollView/RCTScrollContentViewManager.m +2 -0
  44. package/React/Views/ScrollView/RCTScrollView.h +2 -1
  45. package/React/Views/ScrollView/RCTScrollView.m +69 -62
  46. package/React/Views/ScrollView/RCTScrollViewManager.m +1 -1
  47. package/React/Views/UIView+React.h +2 -0
  48. package/React/Views/UIView+React.m +22 -13
  49. package/ReactAndroid/gradle.properties +1 -1
  50. package/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/ReactNativeVersion.java +1 -1
  51. package/ReactCommon/cxxreact/ReactNativeVersion.h +1 -1
  52. package/package.json +1 -1
  53. package/scripts/generate-artifacts.js +21 -17
  54. package/scripts/react_native_pods.rb +0 -3
  55. package/template/Gemfile.lock +5 -5
  56. package/template/android/gradlew.bat +89 -89
  57. package/template/package.json +1 -1
  58. package/third-party-podspecs/boost-for-react-native.podspec +0 -20
@@ -165,6 +165,7 @@ static NSString *RCTRecursiveAccessibilityLabel(RCTUIView *view) // TODO(macOS I
165
165
  _hitTestEdgeInsets = UIEdgeInsetsZero;
166
166
  #if TARGET_OS_OSX // TODO(macOS GH#774)
167
167
  _transform3D = CATransform3DIdentity;
168
+ _shadowColor = nil;
168
169
  #endif // TODO(macOS GH#774)
169
170
 
170
171
  _backgroundColor = super.backgroundColor;
@@ -716,6 +717,51 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : unused)
716
717
  #endif // TODO(macOS GH#774)
717
718
 
718
719
  #if TARGET_OS_OSX // [TODO(macOS GH#774)
720
+ - (void)setShadowColor:(NSColor *)shadowColor
721
+ {
722
+ if (_shadowColor != shadowColor)
723
+ {
724
+ _shadowColor = shadowColor;
725
+ [self didUpdateShadow];
726
+ }
727
+ }
728
+
729
+ - (void)setShadowOffset:(CGSize)shadowOffset
730
+ {
731
+ if (!CGSizeEqualToSize(_shadowOffset, shadowOffset))
732
+ {
733
+ _shadowOffset = shadowOffset;
734
+ [self didUpdateShadow];
735
+ }
736
+ }
737
+
738
+ - (void)setShadowOpacity:(CGFloat)shadowOpacity
739
+ {
740
+ if (_shadowOpacity != shadowOpacity)
741
+ {
742
+ _shadowOpacity = shadowOpacity;
743
+ [self didUpdateShadow];
744
+ }
745
+ }
746
+
747
+ - (void)setShadowRadius:(CGFloat)shadowRadius
748
+ {
749
+ if (_shadowRadius != shadowRadius)
750
+ {
751
+ _shadowRadius = shadowRadius;
752
+ [self didUpdateShadow];
753
+ }
754
+ }
755
+
756
+ -(void)didUpdateShadow
757
+ {
758
+ NSShadow *shadow = [NSShadow new];
759
+ shadow.shadowColor = [[self shadowColor] colorWithAlphaComponent:[self shadowOpacity]];
760
+ shadow.shadowOffset = [self shadowOffset];
761
+ shadow.shadowBlurRadius = [self shadowRadius];
762
+ [self setShadow:shadow];
763
+ }
764
+
719
765
  - (void)viewDidMoveToWindow
720
766
  {
721
767
  // Subscribe to view bounds changed notification so that the view can be notified when a
@@ -733,6 +779,9 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : unused)
733
779
  name:NSViewBoundsDidChangeNotification
734
780
  object:[[self enclosingScrollView] contentView]];
735
781
  }
782
+
783
+ [self reactViewDidMoveToWindow]; // TODO(macOS GH#1412)
784
+
736
785
  [super viewDidMoveToWindow];
737
786
  }
738
787
 
@@ -742,16 +791,16 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : unused)
742
791
  // the mouseExited: event does not get called on the view where mouseEntered: was previously called.
743
792
  // This creates an unnatural pairing of mouse enter and exit events and can cause problems.
744
793
  // We therefore explicitly check for this here and handle them by calling the appropriate callbacks.
745
-
794
+
746
795
  if (!_hasMouseOver && self.onMouseEnter)
747
796
  {
748
797
  NSPoint locationInWindow = [[self window] mouseLocationOutsideOfEventStream];
749
798
  NSPoint locationInView = [self convertPoint:locationInWindow fromView:nil];
750
-
799
+
751
800
  if (NSPointInRect(locationInView, [self bounds]))
752
801
  {
753
802
  _hasMouseOver = YES;
754
-
803
+
755
804
  [self sendMouseEventWithBlock:self.onMouseEnter
756
805
  locationInfo:[self locationInfoFromDraggingLocation:locationInWindow]
757
806
  modifierFlags:0
@@ -762,11 +811,11 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : unused)
762
811
  {
763
812
  NSPoint locationInWindow = [[self window] mouseLocationOutsideOfEventStream];
764
813
  NSPoint locationInView = [self convertPoint:locationInWindow fromView:nil];
765
-
814
+
766
815
  if (!NSPointInRect(locationInView, [self bounds]))
767
816
  {
768
817
  _hasMouseOver = NO;
769
-
818
+
770
819
  [self sendMouseEventWithBlock:self.onMouseLeave
771
820
  locationInfo:[self locationInfoFromDraggingLocation:locationInWindow]
772
821
  modifierFlags:0
@@ -1365,6 +1414,15 @@ setBorderColor() setBorderColor(Top) setBorderColor(Right) setBorderColor(Bottom
1365
1414
  #pragma mark - macOS Event Handler
1366
1415
 
1367
1416
  #if TARGET_OS_OSX
1417
+ - (void)resetCursorRects
1418
+ {
1419
+ [self discardCursorRects];
1420
+ NSCursor *cursor = [RCTConvert NSCursor:self.cursor];
1421
+ if (cursor) {
1422
+ [self addCursorRect:self.bounds cursor:cursor];
1423
+ }
1424
+ }
1425
+
1368
1426
  - (void)setOnDoubleClick:(RCTDirectEventBlock)block
1369
1427
  {
1370
1428
  if (_onDoubleClick != block) {
@@ -1392,7 +1450,7 @@ setBorderColor() setBorderColor(Top) setBorderColor(Right) setBorderColor(Bottom
1392
1450
  if (_trackingArea) {
1393
1451
  [self removeTrackingArea:_trackingArea];
1394
1452
  }
1395
-
1453
+
1396
1454
  if (self.onMouseEnter || self.onMouseLeave) {
1397
1455
  _trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds
1398
1456
  options:NSTrackingActiveAlways|NSTrackingMouseEnteredAndExited
@@ -1400,7 +1458,7 @@ setBorderColor() setBorderColor(Top) setBorderColor(Right) setBorderColor(Bottom
1400
1458
  userInfo:nil];
1401
1459
  [self addTrackingArea:_trackingArea];
1402
1460
  }
1403
-
1461
+
1404
1462
  [super updateTrackingAreas];
1405
1463
  }
1406
1464
 
@@ -1426,7 +1484,7 @@ setBorderColor() setBorderColor(Top) setBorderColor(Right) setBorderColor(Bottom
1426
1484
  {
1427
1485
  NSPoint locationInWindow = event.locationInWindow;
1428
1486
  NSPoint locationInView = [self convertPoint:locationInWindow fromView:nil];
1429
-
1487
+
1430
1488
  return @{@"screenX": @(locationInWindow.x),
1431
1489
  @"screenY": @(locationInWindow.y),
1432
1490
  @"clientX": @(locationInView.x),
@@ -1457,25 +1515,21 @@ setBorderColor() setBorderColor(Top) setBorderColor(Right) setBorderColor(Bottom
1457
1515
  if (modifierFlags & NSEventModifierFlagCommand) {
1458
1516
  body[@"metaKey"] = @YES;
1459
1517
  }
1460
-
1518
+
1461
1519
  if (locationInfo) {
1462
1520
  [body addEntriesFromDictionary:locationInfo];
1463
1521
  }
1464
-
1522
+
1465
1523
  if (additionalData) {
1466
1524
  [body addEntriesFromDictionary:additionalData];
1467
1525
  }
1468
-
1526
+
1469
1527
  block(body);
1470
1528
  }
1471
1529
 
1472
- - (NSDictionary*)dataTransferInfoFromPastboard:(NSPasteboard*)pastboard
1530
+ - (NSDictionary*)dataTransferInfoFromPasteboard:(NSPasteboard*)pasteboard
1473
1531
  {
1474
- if (![pastboard.types containsObject:NSFilenamesPboardType]) {
1475
- return @{};
1476
- }
1477
-
1478
- NSArray *fileNames = [pastboard propertyListForType:NSFilenamesPboardType];
1532
+ NSArray *fileNames = [pasteboard propertyListForType:NSFilenamesPboardType] ?: @[];
1479
1533
  NSMutableArray *files = [[NSMutableArray alloc] initWithCapacity:fileNames.count];
1480
1534
  NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:fileNames.count];
1481
1535
  NSMutableArray *types = [[NSMutableArray alloc] initWithCapacity:fileNames.count];
@@ -1495,27 +1549,57 @@ setBorderColor() setBorderColor(Top) setBorderColor(Right) setBorderColor(Bottom
1495
1549
  MIMETypeString = (__bridge_transfer NSString *)MIMEType;
1496
1550
  }
1497
1551
  }
1498
-
1552
+
1499
1553
  NSNumber *fileSizeValue = nil;
1500
1554
  NSError *fileSizeError = nil;
1501
1555
  BOOL success = [fileURL getResourceValue:&fileSizeValue
1502
1556
  forKey:NSURLFileSizeKey
1503
1557
  error:&fileSizeError];
1504
-
1558
+
1559
+ NSNumber *width = nil;
1560
+ NSNumber *height = nil;
1561
+ if ([MIMETypeString hasPrefix:@"image/"]) {
1562
+ NSImage *image = [[NSImage alloc] initWithContentsOfURL:fileURL];
1563
+ width = @(image.size.width);
1564
+ height = @(image.size.height);
1565
+ }
1566
+
1505
1567
  [files addObject:@{@"name": RCTNullIfNil(fileURL.lastPathComponent),
1506
1568
  @"type": RCTNullIfNil(MIMETypeString),
1507
- @"uri": RCTNullIfNil(fileURL.absoluteString),
1508
- @"size": success ? fileSizeValue : (id)kCFNull
1569
+ @"uri": RCTNullIfNil(fileURL.path),
1570
+ @"size": success ? fileSizeValue : (id)kCFNull,
1571
+ @"width": RCTNullIfNil(width),
1572
+ @"height": RCTNullIfNil(height)
1509
1573
  }];
1510
-
1574
+
1511
1575
  [items addObject:@{@"kind": @"file",
1512
1576
  @"type": RCTNullIfNil(MIMETypeString),
1513
1577
  }];
1514
-
1578
+
1515
1579
  [types addObject:RCTNullIfNil(MIMETypeString)];
1516
1580
  }
1517
1581
  }
1518
-
1582
+
1583
+ NSPasteboardType imageType = [pasteboard availableTypeFromArray:@[NSPasteboardTypePNG, NSPasteboardTypeTIFF]];
1584
+ if (imageType && fileNames.count == 0) {
1585
+ NSString *MIMETypeString = imageType == NSPasteboardTypePNG ? @"image/png" : @"image/tiff";
1586
+ NSData *imageData = [pasteboard dataForType:imageType];
1587
+ NSImage *image = [[NSImage alloc] initWithData:imageData];
1588
+
1589
+ [files addObject:@{@"type": RCTNullIfNil(MIMETypeString),
1590
+ @"uri": RCTDataURL(MIMETypeString, imageData).absoluteString,
1591
+ @"size": @(imageData.length),
1592
+ @"width": @(image.size.width),
1593
+ @"height": @(image.size.height),
1594
+ }];
1595
+
1596
+ [items addObject:@{@"kind": @"image",
1597
+ @"type": RCTNullIfNil(MIMETypeString),
1598
+ }];
1599
+
1600
+ [types addObject:RCTNullIfNil(MIMETypeString)];
1601
+ }
1602
+
1519
1603
  return @{@"dataTransfer": @{@"files": files,
1520
1604
  @"items": items,
1521
1605
  @"types": types}};
@@ -1524,7 +1608,7 @@ setBorderColor() setBorderColor(Top) setBorderColor(Right) setBorderColor(Bottom
1524
1608
  - (NSDictionary*)locationInfoFromDraggingLocation:(NSPoint)locationInWindow
1525
1609
  {
1526
1610
  NSPoint locationInView = [self convertPoint:locationInWindow fromView:nil];
1527
-
1611
+
1528
1612
  return @{@"screenX": @(locationInWindow.x),
1529
1613
  @"screenY": @(locationInWindow.y),
1530
1614
  @"clientX": @(locationInView.x),
@@ -1536,13 +1620,13 @@ setBorderColor() setBorderColor(Top) setBorderColor(Right) setBorderColor(Bottom
1536
1620
  {
1537
1621
  NSPasteboard *pboard = sender.draggingPasteboard;
1538
1622
  NSDragOperation sourceDragMask = sender.draggingSourceOperationMask;
1539
-
1623
+
1540
1624
  [self sendMouseEventWithBlock:self.onDragEnter
1541
1625
  locationInfo:[self locationInfoFromDraggingLocation:sender.draggingLocation]
1542
1626
  modifierFlags:0
1543
- additionalData:[self dataTransferInfoFromPastboard:pboard]];
1544
-
1545
- if ([pboard.types containsObject:NSFilenamesPboardType]) {
1627
+ additionalData:[self dataTransferInfoFromPasteboard:pboard]];
1628
+
1629
+ if ([pboard availableTypeFromArray:self.registeredDraggedTypes]) {
1546
1630
  if (sourceDragMask & NSDragOperationLink) {
1547
1631
  return NSDragOperationLink;
1548
1632
  } else if (sourceDragMask & NSDragOperationCopy) {
@@ -1557,7 +1641,7 @@ setBorderColor() setBorderColor(Top) setBorderColor(Right) setBorderColor(Bottom
1557
1641
  [self sendMouseEventWithBlock:self.onDragLeave
1558
1642
  locationInfo:[self locationInfoFromDraggingLocation:sender.draggingLocation]
1559
1643
  modifierFlags:0
1560
- additionalData:[self dataTransferInfoFromPastboard:sender.draggingPasteboard]];
1644
+ additionalData:[self dataTransferInfoFromPasteboard:sender.draggingPasteboard]];
1561
1645
  }
1562
1646
 
1563
1647
  - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
@@ -1565,7 +1649,7 @@ setBorderColor() setBorderColor(Top) setBorderColor(Right) setBorderColor(Bottom
1565
1649
  [self sendMouseEventWithBlock:self.onDrop
1566
1650
  locationInfo:[self locationInfoFromDraggingLocation:sender.draggingLocation]
1567
1651
  modifierFlags:0
1568
- additionalData:[self dataTransferInfoFromPastboard:[sender draggingPasteboard]]];
1652
+ additionalData:[self dataTransferInfoFromPasteboard:sender.draggingPasteboard]];
1569
1653
  return YES;
1570
1654
  }
1571
1655
  #endif // ]TODO(macOS GH#774)
@@ -1573,177 +1657,38 @@ setBorderColor() setBorderColor(Top) setBorderColor(Right) setBorderColor(Bottom
1573
1657
  #pragma mark - Keyboard Events
1574
1658
 
1575
1659
  #if TARGET_OS_OSX
1576
- NSString* const leftArrowPressKey = @"ArrowLeft";
1577
- NSString* const rightArrowPressKey = @"ArrowRight";
1578
- NSString* const upArrowPressKey = @"ArrowUp";
1579
- NSString* const downArrowPressKey = @"ArrowDown";
1580
-
1581
- - (RCTViewKeyboardEvent*)keyboardEvent:(NSEvent*)event downPress:(BOOL)downPress {
1582
- // modifiers
1583
- BOOL capsLockKey = NO;
1584
- BOOL shiftKey = NO;
1585
- BOOL controlKey = NO;
1586
- BOOL optionKey = NO;
1587
- BOOL commandKey = NO;
1588
- BOOL numericPadKey = NO;
1589
- BOOL helpKey = NO;
1590
- BOOL functionKey = NO;
1591
- // commonly used key short-cuts
1592
- BOOL leftArrowKey = NO;
1593
- BOOL rightArrowKey = NO;
1594
- BOOL upArrowKey = NO;
1595
- BOOL downArrowKey = NO;
1596
- BOOL tabKeyPressed = NO;
1597
- BOOL escapeKeyPressed = NO;
1598
- NSString *key = event.charactersIgnoringModifiers;
1599
- if ([key length] == 0) {
1600
- return nil;
1601
- }
1602
- unichar const code = [key characterAtIndex:0];
1603
-
1604
- // detect arrow key presses
1605
- if (code == NSLeftArrowFunctionKey) {
1606
- leftArrowKey = YES;
1607
- } else if (code == NSRightArrowFunctionKey) {
1608
- rightArrowKey = YES;
1609
- } else if (code == NSUpArrowFunctionKey) {
1610
- upArrowKey = YES;
1611
- } else if (code == NSDownArrowFunctionKey) {
1612
- downArrowKey = YES;
1613
- }
1614
-
1615
- // detect special key presses via the key code
1616
- switch (event.keyCode) {
1617
- case 48: // Tab
1618
- tabKeyPressed = YES;
1619
- break;
1620
- case 53: // Escape
1621
- escapeKeyPressed = YES;
1622
- break;
1623
- default:
1624
- break;
1660
+ - (RCTViewKeyboardEvent*)keyboardEvent:(NSEvent*)event {
1661
+ BOOL keyDown = event.type == NSEventTypeKeyDown;
1662
+ NSArray<NSString *> *validKeys = keyDown ? self.validKeysDown : self.validKeysUp;
1663
+ NSString *key = [RCTViewKeyboardEvent keyFromEvent:event];
1664
+
1665
+ // Only post events for keys we care about
1666
+ if (![validKeys containsObject:key]) {
1667
+ return nil;
1625
1668
  }
1626
1669
 
1627
- // detect modifier flags
1628
- if (event.modifierFlags & NSEventModifierFlagCapsLock) {
1629
- capsLockKey = YES;
1630
- } else if (event.modifierFlags & NSEventModifierFlagShift) {
1631
- shiftKey = YES;
1632
- } else if (event.modifierFlags & NSEventModifierFlagControl) {
1633
- controlKey = YES;
1634
- } else if (event.modifierFlags & NSEventModifierFlagOption) {
1635
- optionKey = YES;
1636
- } else if (event.modifierFlags & NSEventModifierFlagCommand) {
1637
- commandKey = YES;
1638
- } else if (event.modifierFlags & NSEventModifierFlagNumericPad) {
1639
- numericPadKey = YES;
1640
- } else if (event.modifierFlags & NSEventModifierFlagHelp) {
1641
- helpKey = YES;
1642
- } else if (event.modifierFlags & NSEventModifierFlagFunction) {
1643
- functionKey = YES;
1644
- }
1645
-
1646
- RCTViewKeyboardEvent *keyboardEvent = nil;
1647
- // only post events for keys we care about
1648
- if (downPress) {
1649
- NSString *keyToReturn = [self keyIsValid:key left:leftArrowKey right:rightArrowKey up:upArrowKey down:downArrowKey tabKey:tabKeyPressed escapeKey:escapeKeyPressed validKeys:[self validKeysDown]];
1650
- if (keyToReturn != nil) {
1651
- keyboardEvent = [RCTViewKeyboardEvent keyDownEventWithReactTag:self.reactTag
1652
- capsLockKey:capsLockKey
1653
- shiftKey:shiftKey
1654
- ctrlKey:controlKey
1655
- altKey:optionKey
1656
- metaKey:commandKey
1657
- numericPadKey:numericPadKey
1658
- helpKey:helpKey
1659
- functionKey:functionKey
1660
- leftArrowKey:leftArrowKey
1661
- rightArrowKey:rightArrowKey
1662
- upArrowKey:upArrowKey
1663
- downArrowKey:downArrowKey
1664
- key:keyToReturn];
1665
- }
1666
- } else {
1667
- NSString *keyToReturn = [self keyIsValid:key left:leftArrowKey right:rightArrowKey up:upArrowKey down:downArrowKey tabKey:tabKeyPressed escapeKey:escapeKeyPressed validKeys:[self validKeysUp]];
1668
- if (keyToReturn != nil) {
1669
- keyboardEvent = [RCTViewKeyboardEvent keyUpEventWithReactTag:self.reactTag
1670
- capsLockKey:capsLockKey
1671
- shiftKey:shiftKey
1672
- ctrlKey:controlKey
1673
- altKey:optionKey
1674
- metaKey:commandKey
1675
- numericPadKey:numericPadKey
1676
- helpKey:helpKey
1677
- functionKey:functionKey
1678
- leftArrowKey:leftArrowKey
1679
- rightArrowKey:rightArrowKey
1680
- upArrowKey:upArrowKey
1681
- downArrowKey:downArrowKey
1682
- key:keyToReturn];
1670
+ return [RCTViewKeyboardEvent keyEventFromEvent:event reactTag:self.reactTag];
1671
+ }
1672
+
1673
+ - (BOOL)handleKeyboardEvent:(NSEvent *)event {
1674
+ if (event.type == NSEventTypeKeyDown ? self.onKeyDown : self.onKeyUp) {
1675
+ RCTViewKeyboardEvent *keyboardEvent = [self keyboardEvent:event];
1676
+ if (keyboardEvent) {
1677
+ [_eventDispatcher sendEvent:keyboardEvent];
1678
+ return YES;
1683
1679
  }
1684
1680
  }
1685
- return keyboardEvent;
1686
- }
1687
-
1688
- // check if the user typed key matches a key we need to send an event for
1689
- // translate key codes over to JS compatible keys
1690
- - (NSString*)keyIsValid:(NSString*)key left:(BOOL)leftArrowPressed right:(BOOL)rightArrowPressed up:(BOOL)upArrowPressed down:(BOOL)downArrowPressed tabKey:(BOOL)tabKeyPressed escapeKey:(BOOL)escapeKeyPressed validKeys:(NSArray<NSString*>*)validKeys {
1691
- NSString *keyToReturn = key;
1692
-
1693
- // Allow the flexibility of defining special keys in multiple ways
1694
- BOOL enterKeyValidityCheck = [key isEqualToString:@"\r"] && ([validKeys containsObject:@"Enter"] || [validKeys containsObject:@"\r"]);
1695
- BOOL tabKeyValidityCheck = tabKeyPressed && ([validKeys containsObject:@"Tab"]); // tab has to be checked via a key code so we can't just use the key itself here
1696
- BOOL escapeKeyValidityCheck = escapeKeyPressed && ([validKeys containsObject:@"Esc"] || [validKeys containsObject:@"Escape"]); // escape has to be checked via a key code so we can't just use the key itself here
1697
- BOOL leftArrowValidityCheck = [validKeys containsObject:leftArrowPressKey] && leftArrowPressed;
1698
- BOOL rightArrowValidityCheck = [validKeys containsObject:rightArrowPressKey] && rightArrowPressed;
1699
- BOOL upArrowValidityCheck = [validKeys containsObject:upArrowPressKey] && upArrowPressed;
1700
- BOOL downArrowValidityCheck = [validKeys containsObject:downArrowPressKey] && downArrowPressed;
1701
-
1702
- if (tabKeyValidityCheck) {
1703
- keyToReturn = @"Tab";
1704
- } else if (escapeKeyValidityCheck) {
1705
- keyToReturn = @"Escape";
1706
- } else if (enterKeyValidityCheck) {
1707
- keyToReturn = @"Enter";
1708
- } else if (leftArrowValidityCheck) {
1709
- keyToReturn = leftArrowPressKey;
1710
- } else if (rightArrowValidityCheck) {
1711
- keyToReturn = rightArrowPressKey;
1712
- } else if (upArrowValidityCheck) {
1713
- keyToReturn = upArrowPressKey;
1714
- } else if (downArrowValidityCheck) {
1715
- keyToReturn = downArrowPressKey;
1716
- } else if (![validKeys containsObject:key]) {
1717
- keyToReturn = nil;
1718
- }
1719
-
1720
- return keyToReturn;
1681
+ return NO;
1721
1682
  }
1722
1683
 
1723
1684
  - (void)keyDown:(NSEvent *)event {
1724
- if (self.onKeyDown == nil) {
1725
- [super keyDown:event];
1726
- return;
1727
- }
1728
-
1729
- RCTViewKeyboardEvent *keyboardEvent = [self keyboardEvent:event downPress:YES];
1730
- if (keyboardEvent != nil) {
1731
- [_eventDispatcher sendEvent:keyboardEvent];
1732
- } else {
1685
+ if (![self handleKeyboardEvent:event]) {
1733
1686
  [super keyDown:event];
1734
1687
  }
1735
1688
  }
1736
1689
 
1737
1690
  - (void)keyUp:(NSEvent *)event {
1738
- if (self.onKeyUp == nil) {
1739
- [super keyUp:event];
1740
- return;
1741
- }
1742
-
1743
- RCTViewKeyboardEvent *keyboardEvent = [self keyboardEvent:event downPress:NO];
1744
- if (keyboardEvent != nil) {
1745
- [_eventDispatcher sendEvent:keyboardEvent];
1746
- } else {
1691
+ if (![self handleKeyboardEvent:event]) {
1747
1692
  [super keyUp:event];
1748
1693
  }
1749
1694
  }
@@ -7,33 +7,11 @@
7
7
  #import <React/RCTComponentEvent.h>
8
8
 
9
9
  @interface RCTViewKeyboardEvent : RCTComponentEvent
10
- + (instancetype)keyDownEventWithReactTag:(NSNumber *)reactTag
11
- capsLockKey:(BOOL)capsLockKey
12
- shiftKey:(BOOL)shiftKey
13
- ctrlKey:(BOOL)controlKey
14
- altKey:(BOOL)optionKey
15
- metaKey:(BOOL)commandKey
16
- numericPadKey:(BOOL)numericPadKey
17
- helpKey:(BOOL)helpKey
18
- functionKey:(BOOL)functionKey
19
- leftArrowKey:(BOOL)leftArrowKey
20
- rightArrowKey:(BOOL)rightArrowKey
21
- upArrowKey:(BOOL)upArrowKey
22
- downArrowKey:(BOOL)downArrowKey
23
- key:(NSString *)key;
24
10
 
25
- + (instancetype)keyUpEventWithReactTag:(NSNumber *)reactTag
26
- capsLockKey:(BOOL)capsLockKey
27
- shiftKey:(BOOL)shiftKey
28
- ctrlKey:(BOOL)controlKey
29
- altKey:(BOOL)optionKey
30
- metaKey:(BOOL)commandKey
31
- numericPadKey:(BOOL)numericPadKey
32
- helpKey:(BOOL)helpKey
33
- functionKey:(BOOL)functionKey
34
- leftArrowKey:(BOOL)leftArrowKey
35
- rightArrowKey:(BOOL)rightArrowKey
36
- upArrowKey:(BOOL)upArrowKey
37
- downArrowKey:(BOOL)downArrowKey
38
- key:(NSString *)key;
11
+ #if TARGET_OS_OSX // TODO(macOS GH#774)
12
+ + (NSDictionary *)bodyFromEvent:(NSEvent *)event;
13
+ + (NSString *)keyFromEvent:(NSEvent *)event;
14
+ + (instancetype)keyEventFromEvent:(NSEvent *)event reactTag:(NSNumber *)reactTag;
15
+ #endif // TODO(macOS GH#774)
16
+
39
17
  @end
@@ -9,69 +9,74 @@
9
9
  #import <React/RCTAssert.h>
10
10
 
11
11
  @implementation RCTViewKeyboardEvent
12
- // Keyboard mappings are aligned cross-platform as much as possible as per this doc https://github.com/microsoft/react-native-windows/blob/master/vnext/proposals/active/keyboard-reconcile-desktop.md
13
- + (instancetype)keyDownEventWithReactTag:(NSNumber *)reactTag
14
- capsLockKey:(BOOL)capsLockKey
15
- shiftKey:(BOOL)shiftKey
16
- ctrlKey:(BOOL)controlKey
17
- altKey:(BOOL)optionKey
18
- metaKey:(BOOL)commandKey
19
- numericPadKey:(BOOL)numericPadKey
20
- helpKey:(BOOL)helpKey
21
- functionKey:(BOOL)functionKey
22
- leftArrowKey:(BOOL)leftArrowKey
23
- rightArrowKey:(BOOL)rightArrowKey
24
- upArrowKey:(BOOL)upArrowKey
25
- downArrowKey:(BOOL)downArrowKey
26
- key:(NSString *)key {
27
- RCTViewKeyboardEvent *event = [[self alloc] initWithName:@"keyDown"
28
- viewTag:reactTag
29
- body:@{ @"capsLockKey" : @(capsLockKey),
30
- @"shiftKey" : @(shiftKey),
31
- @"ctrlKey" : @(controlKey),
32
- @"altKey" : @(optionKey),
33
- @"metaKey" : @(commandKey),
34
- @"numericPadKey" : @(numericPadKey),
35
- @"helpKey" : @(helpKey),
36
- @"functionKey" : @(functionKey),
37
- @"ArrowLeft" : @(leftArrowKey),
38
- @"ArrowRight" : @(rightArrowKey),
39
- @"ArrowUp" : @(upArrowKey),
40
- @"ArrowDown" : @(downArrowKey),
41
- @"key" : key }];
42
- return event;
12
+
13
+ #if TARGET_OS_OSX // TODO(macOS GH#774)
14
+ + (NSDictionary *)bodyFromEvent:(NSEvent *)event
15
+ {
16
+ NSString *key = [self keyFromEvent:event];
17
+ NSEventModifierFlags modifierFlags = event.modifierFlags;
18
+
19
+ return @{
20
+ @"key" : key,
21
+ @"capsLockKey" : (modifierFlags & NSEventModifierFlagCapsLock) ? @YES : @NO,
22
+ @"shiftKey" : (modifierFlags & NSEventModifierFlagShift) ? @YES : @NO,
23
+ @"ctrlKey" : (modifierFlags & NSEventModifierFlagControl) ? @YES : @NO,
24
+ @"altKey" : (modifierFlags & NSEventModifierFlagOption) ? @YES : @NO,
25
+ @"metaKey" : (modifierFlags & NSEventModifierFlagCommand) ? @YES : @NO,
26
+ @"numericPadKey" : (modifierFlags & NSEventModifierFlagNumericPad) ? @YES : @NO,
27
+ @"helpKey" : (modifierFlags & NSEventModifierFlagHelp) ? @YES : @NO,
28
+ @"functionKey" : (modifierFlags & NSEventModifierFlagFunction) ? @YES : @NO,
29
+ };
43
30
  }
44
31
 
45
- +(instancetype)keyUpEventWithReactTag:(NSNumber *)reactTag
46
- capsLockKey:(BOOL)capsLockKey
47
- shiftKey:(BOOL)shiftKey
48
- ctrlKey:(BOOL)controlKey
49
- altKey:(BOOL)optionKey
50
- metaKey:(BOOL)commandKey
51
- numericPadKey:(BOOL)numericPadKey
52
- helpKey:(BOOL)helpKey
53
- functionKey:(BOOL)functionKey
54
- leftArrowKey:(BOOL)leftArrowKey
55
- rightArrowKey:(BOOL)rightArrowKey
56
- upArrowKey:(BOOL)upArrowKey
57
- downArrowKey:(BOOL)downArrowKey
58
- key:(NSString *)key {
59
- RCTViewKeyboardEvent *event = [[self alloc] initWithName:@"keyUp"
60
- viewTag:reactTag
61
- body:@{ @"capsLockKey" : @(capsLockKey),
62
- @"shiftKey" : @(shiftKey),
63
- @"ctrlKey" : @(controlKey),
64
- @"altKey" : @(optionKey),
65
- @"metaKey" : @(commandKey),
66
- @"numericPadKey" : @(numericPadKey),
67
- @"helpKey" : @(helpKey),
68
- @"functionKey" : @(functionKey),
69
- @"ArrowLeft" : @(leftArrowKey),
70
- @"ArrowRight" : @(rightArrowKey),
71
- @"ArrowUp" : @(upArrowKey),
72
- @"ArrowDown" : @(downArrowKey),
73
- @"key" : key }];
74
- return event;
32
+ + (NSString *)keyFromEvent:(NSEvent *)event
33
+ {
34
+ NSString *key = event.charactersIgnoringModifiers;
35
+ unichar const code = key.length > 0 ? [key characterAtIndex:0] : 0;
36
+
37
+ if (event.keyCode == 48) {
38
+ return @"Tab";
39
+ } else if (event.keyCode == 53) {
40
+ return @"Escape";
41
+ } else if (code == NSEnterCharacter || code == NSNewlineCharacter || code == NSCarriageReturnCharacter) {
42
+ return @"Enter";
43
+ } else if (code == NSLeftArrowFunctionKey) {
44
+ return @"ArrowLeft";
45
+ } else if (code == NSRightArrowFunctionKey) {
46
+ return @"ArrowRight";
47
+ } else if (code == NSUpArrowFunctionKey) {
48
+ return @"ArrowUp";
49
+ } else if (code == NSDownArrowFunctionKey) {
50
+ return @"ArrowDown";
51
+ } else if (code == NSBackspaceCharacter || code == NSDeleteCharacter) {
52
+ return @"Backspace";
53
+ } else if (code == NSDeleteFunctionKey) {
54
+ return @"Delete";
55
+ } else if (code == NSHomeFunctionKey) {
56
+ return @"Home";
57
+ } else if (code == NSEndFunctionKey) {
58
+ return @"End";
59
+ } else if (code == NSPageUpFunctionKey) {
60
+ return @"PageUp";
61
+ } else if (code == NSPageDownFunctionKey) {
62
+ return @"PageDown";
63
+ }
64
+
65
+ return key;
66
+ }
67
+
68
+ // Keyboard mappings are aligned cross-platform as much as possible as per this doc https://github.com/microsoft/react-native-windows/blob/master/vnext/proposals/active/keyboard-reconcile-desktop.md
69
+ + (instancetype)keyEventFromEvent:(NSEvent *)event reactTag:(NSNumber *)reactTag
70
+ {
71
+ // Ignore "dead keys" (key press that waits for another key to make a character)
72
+ if (!event.charactersIgnoringModifiers.length) {
73
+ return nil;
74
+ }
75
+
76
+ return [[self alloc] initWithName:(event.type == NSEventTypeKeyDown ? @"keyDown" : @"keyUp")
77
+ viewTag:reactTag
78
+ body:[self bodyFromEvent:event]];
75
79
  }
80
+ #endif // TODO(macOS GH#774)
76
81
 
77
82
  @end