-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathRCTFocusZone.m
More file actions
618 lines (529 loc) · 18.9 KB
/
RCTFocusZone.m
File metadata and controls
618 lines (529 loc) · 18.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#import "KeyCodes.h"
#import <React/RCTBaseTextInputView.h>
#import "RCTFocusZone.h"
#import "RCTi18nUtil.h"
typedef enum {
FocusZoneActionNone,
FocusZoneActionTab,
FocusZoneActionShiftTab,
FocusZoneActionRightArrow,
FocusZoneActionLeftArrow,
FocusZoneActionDownArrow,
FocusZoneActionUpArrow,
} FocusZoneAction;
typedef BOOL (^IsViewLeadingCandidateForNextFocus)(NSView *candidateView);
// Maximum vertical (or horizontal) displacement for a candidate view to be
// enumerated in the same row (or column) as the current focused view
static const CGFloat FocusZoneBuffer = 3;
@implementation RCTFocusZone
static inline CGFloat GetDistanceBetweenPoints(NSPoint point1, NSPoint point2)
{
NSPoint delta = NSMakePoint(point1.x - point2.x, point1.y - point2.y);
return sqrt(delta.x * delta.x + delta.y * delta.y);
}
static inline CGFloat GetDistanceBetweenRects(NSRect rect1, NSRect rect2)
{
// Get the top left corner of the rect, top right in RTL
bool isRTL = [[RCTI18nUtil sharedInstance] isRTL];
CGFloat rect1Offset = isRTL ? rect1.size.width : 0;
CGFloat rect2Offset = isRTL ? rect2.size.width : 0;
NSPoint rect1Corner = NSMakePoint(rect1.origin.x + rect1Offset , rect1.origin.y);
NSPoint rect2Corner = NSMakePoint(rect2.origin.x + rect2Offset , rect2.origin.y);
return GetDistanceBetweenPoints(rect1Corner, rect2Corner);
}
static inline CGFloat GetMinDistanceBetweenRectVerticesAndPoint(NSRect rect, NSPoint point)
{
return fmin(
fmin(GetDistanceBetweenPoints(point, NSMakePoint(NSMinX(rect), NSMinY(rect))),
GetDistanceBetweenPoints(point, NSMakePoint(NSMaxX(rect), NSMaxY(rect)))),
fmin(GetDistanceBetweenPoints(point, NSMakePoint(NSMaxX(rect), NSMinY(rect))),
GetDistanceBetweenPoints(point, NSMakePoint(NSMinX(rect), NSMaxY(rect))))
);
}
/// Performs a depth first search looking for the first view that accepts responder status in a parent view's view hierarchy.
/// This function does not take into account the geometric position of the view.
static NSView *GetFirstFocusableViewWithin(NSView *parentView)
{
if ([[parentView subviews] count] < 1)
{
return nil;
}
for (NSView *view in [parentView subviews]) {
if ([view acceptsFirstResponder]) {
if ([view isKindOfClass:[RCTBaseTextInputView class]]) {
RCTUIView *backedTextInputView = [(RCTBaseTextInputView *)view backedTextInputView];
if ([backedTextInputView acceptsFirstResponder]) {
return backedTextInputView;
}
} else {
return view;
}
}
NSView *match = GetFirstFocusableViewWithin(view);
if (match) {
return match;
}
}
return nil;
}
/// Performs a depth first search looking for the last that accepts responder status in a parent view's view hierarchy.
/// We find the last view by simply reversing the order of the subview array.
/// This function does not take into account the geometric position of the view.
static NSView *GetLastFocusableViewWithin(NSView *parentView)
{
for (NSView *view in [[parentView subviews] reverseObjectEnumerator]) {
if ([view acceptsFirstResponder]) {
return view;
}
NSView *match = GetLastFocusableViewWithin(view);
if (match) {
return match;
}
}
return nil;
}
static NSView *GetFirstResponder(NSWindow *window)
{
NSResponder *responder = [window firstResponder];
while (responder != nil && ![responder isKindOfClass:[NSView class]])
{
responder = [responder nextResponder];
}
return [responder isKindOfClass:[NSView class]] ? (NSView *)responder : nil;
}
static FocusZoneAction GetActionForEvent(NSEvent *event)
{
FocusZoneAction action = FocusZoneActionNone;
NSEventModifierFlags modifierFlags = [event modifierFlags]
& (NSEventModifierFlagShift | NSEventModifierFlagControl
| NSEventModifierFlagOption | NSEventModifierFlagCommand);
switch ([event keyCode])
{
case kVK_UpArrow:
action = FocusZoneActionUpArrow;
break;
case kVK_DownArrow:
action = FocusZoneActionDownArrow;
break;
case kVK_LeftArrow:
action = FocusZoneActionLeftArrow;
break;
case kVK_RightArrow:
action = FocusZoneActionRightArrow;
break;
case kVK_Tab:
if (modifierFlags == 0)
{
action = FocusZoneActionTab;
break;
}
else if (modifierFlags == NSEventModifierFlagShift)
{
action = FocusZoneActionShiftTab;
break;
}
}
return action;
}
static inline BOOL IsAdvanceWithinZoneAction(FocusZoneAction action)
{
return action == FocusZoneActionRightArrow || action == FocusZoneActionDownArrow;
}
static inline BOOL IsHorizontalNavigationWithinZoneAction(FocusZoneAction action)
{
return action == FocusZoneActionRightArrow || action == FocusZoneActionLeftArrow;
}
/// Returns the topmost view of the given view's superview heirarchy that is a FocusZone.
/// Returns nil if no FocusZone is found.
static RCTFocusZone *GetFocusZoneAncestor(NSView *view)
{
NSView *candidateView = view;
NSView *topLevelView = [[view window] contentView];
RCTFocusZone *focusZoneAncestor = nil;
while (candidateView != nil && candidateView != topLevelView)
{
if ([candidateView isKindOfClass:[RCTFocusZone class]])
{
focusZoneAncestor = (RCTFocusZone *)candidateView;
}
candidateView = [candidateView superview];
}
return focusZoneAncestor;
}
/// Bypass FocusZone if it's empty or has no focusable elements
static BOOL ShouldSkipFocusZone(NSView *view)
{
if([view isKindOfClass:[RCTFocusZone class]])
{
NSView *keyView = GetFirstFocusableViewWithin(view);
// FocusZone is empty or has no focusable elements
if (keyView == nil)
{
return YES;
}
}
return NO;
}
/// FocusZone bases its return to accessibilityChildrenInNavigationOrder on the navigationOrderInRenderOrder property.
/// If navigationOrderInRenderOrder is set to YES, the accessible children are returned in the same order as returned by [(NSView) accessibilityChildren].
/// If navigationOrderInRenderOrder is set to NO, the accessible children are returned in the order determined by [(NSView) accessibilityChildrenInNavigationOrder].
- (NSArray *)accessibilityChildrenInNavigationOrder {
if ([self navigationOrderInRenderOrder]) {
return [self accessibilityChildren];
}
return [super accessibilityChildrenInNavigationOrder];
}
/// Accept firstResponder on FocusZone itself in order to reassign it within the FocusZone.
- (BOOL)acceptsFirstResponder
{
// Reject first responder if FocusZone is disabled or should be skipped.
return !_disabled && !ShouldSkipFocusZone(self);
}
- (BOOL)becomeFirstResponder
{
NSView *keyView = _defaultResponder ?: GetFirstFocusableViewWithin(self);
return !_disabled && [[self window] makeFirstResponder:keyView];
}
- (NSView *)nextViewToFocusForCondition:(IsViewLeadingCandidateForNextFocus)isLeadingCandidate
{
NSView *nextViewToFocus;
NSMutableArray<NSView *> *queue = [NSMutableArray array];
[queue addObject:self];
while ([queue count] > 0)
{
NSView *candidateView = [queue firstObject];
[queue removeObjectAtIndex:0];
if ([candidateView isNotEqualTo:self] && [candidateView acceptsFirstResponder] && isLeadingCandidate(candidateView))
{
nextViewToFocus = candidateView;
}
[queue addObjectsFromArray:[candidateView subviews]];
}
return nextViewToFocus;
}
- (NSView *)nextViewToFocusForAction:(FocusZoneAction)action
{
BOOL isAdvance = IsAdvanceWithinZoneAction(action);
BOOL isHorizontal = IsHorizontalNavigationWithinZoneAction(action);
NSView *firstResponder = GetFirstResponder([self window]);
NSRect firstResponderRect = [firstResponder convertRect:[firstResponder bounds] toView:self];
NSScrollView * firstResponderEnclosingScrollView = [firstResponder enclosingScrollView];
__block CGFloat closestDistance = CGFLOAT_MAX;
__block CGFloat closestDistanceWithinEnclosingScrollView = CGFLOAT_MAX;
IsViewLeadingCandidateForNextFocus block = ^BOOL(NSView *candidateView)
{
BOOL isLeadingCandidate = NO;
BOOL skip = candidateView == firstResponder;
NSRect candidateRect = [candidateView convertRect:[candidateView bounds] toView:self];
BOOL subviewRelationExists = [candidateView isDescendantOf:firstResponder] || [firstResponder isDescendantOf:candidateView];
if (isHorizontal)
{
if (subviewRelationExists)
{
skip = skip
|| (isAdvance && NSMinX(candidateRect) < NSMinX(firstResponderRect))
|| (!isAdvance && NSMaxX(candidateRect) > NSMaxX(firstResponderRect))
|| NSMinY(candidateRect) > NSMaxY(firstResponderRect) - FocusZoneBuffer
|| NSMaxY(candidateRect) < NSMinY(firstResponderRect) + FocusZoneBuffer;
}
else
{
skip = skip
|| (isAdvance && NSMidX(candidateRect) < NSMidX(firstResponderRect))
|| (!isAdvance && NSMidX(candidateRect) > NSMidX(firstResponderRect))
|| NSMinY(candidateRect) > NSMaxY(firstResponderRect) - FocusZoneBuffer
|| NSMaxY(candidateRect) < NSMinY(firstResponderRect) + FocusZoneBuffer;
}
}
else
{
if (subviewRelationExists)
{
skip = skip
|| (isAdvance && NSMinY(candidateRect) < NSMinY(firstResponderRect))
|| (!isAdvance && NSMaxY(candidateRect) > NSMaxY(firstResponderRect))
|| NSMaxX(candidateRect) < NSMinX(firstResponderRect) + FocusZoneBuffer
|| NSMinX(candidateRect) > NSMaxX(firstResponderRect) - FocusZoneBuffer;
}
else
{
skip = skip
|| (isAdvance && NSMidY(candidateRect) < NSMidY(firstResponderRect))
|| (!isAdvance && NSMidY(candidateRect) > NSMidY(firstResponderRect))
|| NSMaxX(candidateRect) < NSMinX(firstResponderRect) + FocusZoneBuffer
|| NSMinX(candidateRect) > NSMaxX(firstResponderRect) - FocusZoneBuffer;
}
}
if (!skip)
{
CGFloat distance = GetDistanceBetweenRects(firstResponderRect, candidateRect);
// If there are other candidate views inside the same ScrollView as the firstResponder,
// prefer those views over other views outside the scrollview, even if they are closer.
if ([firstResponderEnclosingScrollView isEqualTo:[candidateView enclosingScrollView]])
{
if (closestDistanceWithinEnclosingScrollView > distance)
{
closestDistanceWithinEnclosingScrollView = distance;
isLeadingCandidate = YES;
}
}
else
{
if (closestDistance > distance)
{
closestDistance = distance;
isLeadingCandidate = YES;
}
}
}
return isLeadingCandidate;
};
return [self nextViewToFocusForCondition:block];
}
- (NSView *)nextViewToFocusForCircularAction:(FocusZoneAction)action
{
BOOL isAdvance = IsAdvanceWithinZoneAction(action);
BOOL isHorizontal = IsHorizontalNavigationWithinZoneAction(action);
NSView *firstResponder = GetFirstResponder([self window]);
NSRect firstResponderRect = [firstResponder convertRect:[firstResponder bounds] toView:self];
NSPoint anchorPoint = isHorizontal
? NSMakePoint(isAdvance ? 0 : [self bounds].size.width, NSMidY(firstResponderRect))
: NSMakePoint(NSMidX(firstResponderRect), isAdvance ? 0 : [self bounds].size.height);
__block CGFloat closestDistance = CGFLOAT_MAX;
IsViewLeadingCandidateForNextFocus block = ^BOOL(NSView *candidateView)
{
BOOL isLeadingCandidate = NO;
BOOL skip = NO;
NSRect candidateRect = [candidateView convertRect:[candidateView bounds] toView:self];
BOOL subviewRelationExists = [candidateView isDescendantOf:firstResponder] || [firstResponder isDescendantOf:candidateView];
if (isHorizontal)
{
if (subviewRelationExists)
{
skip = (isAdvance && NSMinX(candidateRect) > NSMinX(firstResponderRect))
|| (!isAdvance && NSMaxX(candidateRect) < NSMaxX(firstResponderRect));
}
else
{
skip = (isAdvance && NSMidX(candidateRect) > NSMidX(firstResponderRect))
|| (!isAdvance && NSMidX(candidateRect) < NSMidX(firstResponderRect));
}
}
else
{
if (subviewRelationExists)
{
skip = (isAdvance && NSMinY(candidateRect) > NSMinY(firstResponderRect))
|| (!isAdvance && NSMaxY(candidateRect) < NSMaxY(firstResponderRect));
}
else
{
skip = (isAdvance && NSMidY(candidateRect) > NSMidY(firstResponderRect))
|| (!isAdvance && NSMidY(candidateRect) < NSMidY(firstResponderRect));
}
}
if (!skip)
{
NSPoint candidatePoint = isHorizontal
? NSMakePoint(isAdvance ? NSMinX(candidateRect) : NSMaxX(candidateRect), NSMidY(candidateRect))
: NSMakePoint(NSMidX(candidateRect), isAdvance ? NSMinY(candidateRect) : NSMaxY(candidateRect));
CGFloat distance = GetDistanceBetweenPoints(anchorPoint, candidatePoint);
if (closestDistance > distance)
{
closestDistance = distance;
isLeadingCandidate = YES;
}
}
return isLeadingCandidate;
};
return [self nextViewToFocusForCondition:block];
}
- (NSView *)nextViewToFocusForHorizontalNavigation:(FocusZoneAction)action
{
BOOL isAdvance = IsAdvanceWithinZoneAction(action);
NSView *firstResponder = GetFirstResponder([self window]);
NSRect firstResponderRect = [firstResponder convertRect:[firstResponder bounds] toView:self];
__block CGFloat closestDistance = CGFLOAT_MAX;
NSRect selfBounds = [self bounds];
NSPoint targetPoint = isAdvance
? NSMakePoint(0, NSMaxY(firstResponderRect))
: NSMakePoint(selfBounds.size.width, NSMinY(firstResponderRect));
IsViewLeadingCandidateForNextFocus block = ^BOOL(NSView *candidateView)
{
BOOL isLeadingCandidate = NO;
NSRect candidateRect = [candidateView convertRect:[candidateView bounds] toView:self];
BOOL skip = candidateView == firstResponder
|| (isAdvance && NSMinY(candidateRect) < NSMaxY(firstResponderRect) - FocusZoneBuffer)
|| (!isAdvance && NSMaxY(candidateRect) > NSMinY(firstResponderRect) + FocusZoneBuffer);
if (!skip)
{
CGFloat distance = GetMinDistanceBetweenRectVerticesAndPoint(candidateRect, targetPoint);
if (closestDistance > distance)
{
closestDistance = distance;
isLeadingCandidate = YES;
}
}
return isLeadingCandidate;
};
return [self nextViewToFocusForCondition:block];
}
- (NSView *)nextViewToFocusWithFallback:(FocusZoneAction)action considerCircular:(BOOL)shouldTryCircular
{
// Special case if we're currently focused on self
NSView *firstResponder = GetFirstResponder([self window]);
if (self == firstResponder)
{
if (action == FocusZoneActionDownArrow)
{
return GetFirstFocusableViewWithin(self);
}
else if (action == FocusZoneActionUpArrow)
{
return GetLastFocusableViewWithin(self);
}
}
BOOL isHorizontal = IsHorizontalNavigationWithinZoneAction(action);
BOOL isAdvance = IsAdvanceWithinZoneAction(action);
NSView *nextViewToFocus = [self nextViewToFocusForAction:action];
if (nextViewToFocus == nil)
{
if (isHorizontal)
{
nextViewToFocus = [self nextViewToFocusForHorizontalNavigation:action];
}
else
{
FocusZoneAction horizontalAction = isAdvance ? FocusZoneActionRightArrow : FocusZoneActionLeftArrow;
nextViewToFocus = [self nextViewToFocusWithFallback:horizontalAction considerCircular:NO];
}
}
if (nextViewToFocus == nil && shouldTryCircular)
{
nextViewToFocus = [self nextViewToFocusForCircularAction:action];
if (nextViewToFocus == firstResponder)
{
nextViewToFocus = isHorizontal ?
[self nextViewToFocusForCircularAction:isAdvance ? FocusZoneActionDownArrow : FocusZoneActionUpArrow] :
[self nextViewToFocusForCircularAction:isAdvance ? FocusZoneActionRightArrow : FocusZoneActionLeftArrow];
}
if (nextViewToFocus == firstResponder)
{
nextViewToFocus = nil;
}
}
return nextViewToFocus;
}
- (NSView *)nextViewToFocusOutsideZone:(FocusZoneAction)action
{
NSView *nextViewToFocus;
// Find the first view outside the FocusZone (or any parent FocusZones) to place focus
RCTFocusZone *focusZoneAncestor = GetFocusZoneAncestor(self);
if (action == FocusZoneActionTab) // Advance to next zone
{
nextViewToFocus = [self nextValidKeyView];
while([nextViewToFocus isDescendantOf:focusZoneAncestor])
{
// there are no views left in the key view loop
if ([nextViewToFocus isEqual:focusZoneAncestor])
{
nextViewToFocus = nil;
break;
}
nextViewToFocus = [nextViewToFocus nextValidKeyView];
}
}
else if (action == FocusZoneActionShiftTab)
{
nextViewToFocus = [self previousValidKeyView];
while([nextViewToFocus isDescendantOf:focusZoneAncestor])
{
// there are no views left in the key view loop
if ([nextViewToFocus isEqual:focusZoneAncestor])
{
nextViewToFocus = nil;
break;
}
nextViewToFocus = [nextViewToFocus previousValidKeyView];
}
// If the previous view is in a FocusZone, focus on its defaultKeyView
// (For FocusZoneActionTab, this is handled by becomeFirstResponder).
focusZoneAncestor = GetFocusZoneAncestor(nextViewToFocus);
NSView *ancestorKeyView = [focusZoneAncestor defaultResponder];
if (ancestorKeyView != nil) {
nextViewToFocus = [focusZoneAncestor defaultResponder];
}
}
return nextViewToFocus;
}
- (NSView *)nextViewToFocusForTab:(FocusZoneAction)action
{
[[self window] recalculateKeyViewLoop];
NSString *tabKeyNavigation = [self tabKeyNavigation];
if (![@"NavigateWrap" isEqual:tabKeyNavigation] && ![@"NavigateStopAtEnds" isEqual:tabKeyNavigation]
&& ![@"Normal" isEqual:tabKeyNavigation])
{
return [self nextViewToFocusOutsideZone:action];
}
BOOL forward = action != FocusZoneActionShiftTab;
NSView *firstResponder = GetFirstResponder([self window]);
NSView *nextViewToFocus = forward ? [firstResponder nextValidKeyView] : [firstResponder previousValidKeyView];
if (nextViewToFocus == self)
nextViewToFocus = forward ? [nextViewToFocus nextValidKeyView] : [nextViewToFocus previousValidKeyView];;
if ([@"Normal" isEqual:tabKeyNavigation] || [nextViewToFocus isDescendantOf:self])
return nextViewToFocus;
if ([@"NavigateStopAtEnds" isEqual:tabKeyNavigation])
return nil;
// wrap around -- find first (tab) or last (shift+tab)
NSView *aView = firstResponder;
while (aView != self && [aView isDescendantOf:self])
{
nextViewToFocus = aView;
aView = forward ? [aView previousValidKeyView] : [aView nextValidKeyView];
}
return nextViewToFocus != firstResponder ? nextViewToFocus : nil;
}
- (BOOL)isFlipped
{
return YES;
}
- (void)keyDown:(NSEvent *)event
{
FocusZoneAction action = GetActionForEvent(event);
FocusZoneDirection focusZoneDirection = [self focusZoneDirection];
NSString *navigateAtEnd = [self navigateAtEnd];
BOOL passthrough = NO;
NSView *viewToFocus = nil;
if ([self disabled] || action == FocusZoneActionNone)
{
passthrough = YES;
}
else if (action == FocusZoneActionTab || action == FocusZoneActionShiftTab)
{
viewToFocus = [self nextViewToFocusForTab:action];
}
else if ((focusZoneDirection == FocusZoneDirectionVertical
&& (action == FocusZoneActionRightArrow || action == FocusZoneActionLeftArrow))
|| (focusZoneDirection == FocusZoneDirectionHorizontal
&& (action == FocusZoneActionUpArrow || action == FocusZoneActionDownArrow))
|| (focusZoneDirection == FocusZoneDirectionNone))
{
passthrough = YES;
}
else
{
viewToFocus = [self nextViewToFocusWithFallback:action considerCircular:[@"NavigateWrap" isEqual:navigateAtEnd]];
}
if (viewToFocus != nil)
{
[[self window] makeFirstResponder:viewToFocus];
[viewToFocus scrollRectToVisible:[viewToFocus bounds]];
}
else if (passthrough)
{
// Only call super if we explicitly want to passthrough the event
// I.E: FocusZone don't handle this specific key
[super keyDown:event];
}
}
@end