Skip to content

Commit

Permalink
Refactor and dedupe some code
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Gleitman committed Jun 25, 2024
1 parent 387e28f commit 3e5ec21
Showing 1 changed file with 13 additions and 32 deletions.
45 changes: 13 additions & 32 deletions packages/react-native/Libraries/Text/Text/RCTTextView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -456,43 +456,32 @@ - (void)mouseEntered:(NSEvent *)event
// superclass invokes self.onMouseEnter, so do this first
[super mouseEntered:event];

// TODO: dedupe from mouseMoved
if (_descendantViews != nil) {
NSNumber *reactTagOfHoveredView = [self reactTagAtMouseLocationFromEvent:event];

RCTUIView *hoveredView = nil;
if ([reactTagOfHoveredView isEqualToNumber:self.reactTag]) {
// We're hovering over the root Text element
hoveredView = self;
} else {
// Maybe we're hovering over a child Text element?
NSUInteger index = [_descendantViews indexOfObjectPassingTest:^BOOL(RCTUIView * _Nonnull view, NSUInteger idx, BOOL * _Nonnull stop) {
*stop = [[view reactTag] isEqualToNumber:reactTagOfHoveredView];
return *stop;
}];
if (index != NSNotFound) {
hoveredView = _descendantViews[index];
}
}

[self setCurrentHoveredSubview:hoveredView withEvent:event];
}
[self updateHoveredSubviewWithEvent:event];
}

- (void)mouseExited:(NSEvent *)event
{
[self setCurrentHoveredSubview:nil withEvent:event];
[self updateHoveredSubviewWithEvent:event];

// superclass invokes self.onMouseLeave, so do this last
[super mouseExited:event];
}

- (void)mouseMoved:(NSEvent *)event
{
if (_descendantViews != nil) {
[self updateHoveredSubviewWithEvent:event];

// Web environments call mouse move events from the inside outwards, so do this last
[super mouseMoved:event];
}

- (void)updateHoveredSubviewWithEvent:(NSEvent *)event
{
RCTUIView *hoveredView = nil;

if ([event type] != NSEventTypeMouseExited && _descendantViews != nil) {
NSNumber *reactTagOfHoveredView = [self reactTagAtMouseLocationFromEvent:event];

RCTUIView *hoveredView = nil;
if ([reactTagOfHoveredView isEqualToNumber:self.reactTag]) {
// We're hovering over the root Text element
hoveredView = self;
Expand All @@ -506,16 +495,8 @@ - (void)mouseMoved:(NSEvent *)event
hoveredView = _descendantViews[index];
}
}

[self setCurrentHoveredSubview:hoveredView withEvent:event];
}

// Web environments call mouse move events from the inside outwards, so do this last
[super mouseMoved:event];
}

- (void)setCurrentHoveredSubview:(RCTUIView *)hoveredView withEvent:(NSEvent *)event
{
if (_currentHoveredSubview == hoveredView) {
return;
}
Expand Down

0 comments on commit 3e5ec21

Please sign in to comment.