forked from twitter-archive/twui
/
TUINSView.m
849 lines (679 loc) · 22.2 KB
/
TUINSView.m
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/*
Copyright 2011 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this work except in compliance with the License.
You may obtain a copy of the License in the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Portions of this code were taken from Velvet,
// which is copyright (c) 2012 Bitswift, Inc.
// See LICENSE.txt for more information.
#import "TUINSView.h"
#import "CALayer+TUIExtensions.h"
#import "TUIBridgedScrollView.h"
#import "TUINSHostView.h"
#import "TUINSView+Private.h"
#import "TUIViewNSViewContainer.h"
#import "TUIView+Private.h"
#import "TUIView+TUIBridgedView.h"
#import "TUITextRenderer+Event.h"
#import "TUITooltipWindow.h"
#import <CoreFoundation/CoreFoundation.h>
// If enabled, NSViews contained within TUIViewNSViewContainers will be clipped
// by any TwUI ancestors that enable clipping to bounds.
//
// This should really only be disabled for debugging.
#define ENABLE_NSVIEW_CLIPPING 1
static NSComparisonResult compareNSViewOrdering (NSView *viewA, NSView *viewB, void *context) {
TUIViewNSViewContainer *hostA = viewA.hostView;
TUIViewNSViewContainer *hostB = viewB.hostView;
// hosted NSViews should be on top of everything else
if (!hostA) {
if (!hostB) {
return NSOrderedSame;
} else {
return NSOrderedAscending;
}
} else if (!hostB) {
return NSOrderedDescending;
}
TUIView *ancestor = [hostA ancestorSharedWithView:(TUIView *)hostB];
NSCAssert2(ancestor, @"TwUI-hosted NSViews in the same TUINSView should share a TwUI ancestor: %@, %@", viewA, viewB);
__block NSInteger orderA = -1;
__block NSInteger orderB = -1;
[ancestor.subviews enumerateObjectsUsingBlock:^(TUIView *subview, NSUInteger index, BOOL *stop){
if ([hostA isDescendantOfView:subview]) {
orderA = (NSInteger)index;
} else if ([hostB isDescendantOfView:subview]) {
orderB = (NSInteger)index;
}
if (orderA >= 0 && orderB >= 0) {
*stop = YES;
}
}];
if (orderA < orderB) {
return NSOrderedAscending;
} else if (orderA > orderB) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}
@interface TUINSView ()
/*
* The layer-hosted view which actually holds the TwUI hierarchy.
*/
@property (nonatomic, readonly, strong) TUINSHostView *tuiHostView;
- (void)recalculateNSViewClipping;
- (void)recalculateNSViewOrdering;
/*
* A layer used to mask the rendering of NSView-owned layers added to the
* receiver.
*
* This masking will keep the rendering of a given NSView consistent with the
* clipping its TUIViewNSViewContainer would have in the TwUI hierarchy.
*/
@property (nonatomic, strong) CAShapeLayer *maskLayer;
/*
* Returns any existing AppKit-created focus ring layer for the given view, or
* nil if one could not be found.
*/
- (CALayer *)focusRingLayerForView:(NSView *)view;
/*
* Configures all the necessary properties on the receiver. This is outside of
* an initializer because NSView has no true designated initializer.
*/
- (void)setUp;
- (void)windowDidResignKey:(NSNotification *)notification;
- (void)windowDidBecomeKey:(NSNotification *)notification;
- (void)screenProfileOrBackingPropertiesDidChange:(NSNotification *)notification;
@end
@implementation TUINSView
// implemented by TUIView
@dynamic layer;
// these cannot be implicitly synthesized because they're from protocols/categories
@synthesize hostView = _hostView;
@synthesize appKitHostView = _appKitHostView;
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self == nil)
return nil;
[self setUp];
return self;
}
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self == nil)
return nil;
[self setUp];
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
_rootView.nsView = nil;
[_rootView removeFromSuperview];
_rootView = nil;
_hoverView = nil;
_trackingView = nil;
_trackingArea = nil;
}
- (void)resetCursorRects
{
NSRect f = [self frame];
f.origin = NSZeroPoint;
[self addCursorRect:f cursor:[NSCursor arrowCursor]];
}
- (void)ab_setIsOpaque:(BOOL)o
{
opaque = o;
}
- (void)tui_setOpaque:(BOOL)o
{
opaque = o;
}
- (BOOL)isOpaque
{
return opaque;
}
- (BOOL)mouseDownCanMoveWindow
{
return NO;
}
- (void)updateTrackingAreas
{
[super updateTrackingAreas];
if(_trackingArea) {
[self removeTrackingArea:_trackingArea];
}
NSRect r = [self frame];
r.origin = NSZeroPoint;
_trackingArea = [[NSTrackingArea alloc] initWithRect:r options:NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveAlways owner:self userInfo:nil];
[self addTrackingArea:_trackingArea];
}
- (void)viewWillStartLiveResize
{
[super viewWillStartLiveResize];
inLiveResize = YES;
[_rootView viewWillStartLiveResize];
}
- (BOOL)inLiveResize
{
return inLiveResize;
}
- (void)viewDidEndLiveResize
{
[super viewDidEndLiveResize];
inLiveResize = NO;
[_rootView viewDidEndLiveResize]; // will send to all subviews
if([[self window] respondsToSelector:@selector(ensureWindowRectIsOnScreen)])
[[self window] performSelector:@selector(ensureWindowRectIsOnScreen)];
}
- (void)setRootView:(TUIView *)v
{
v.autoresizingMask = TUIViewAutoresizingFlexibleSize;
_rootView.nsView = nil;
_rootView.hostView = nil;
_rootView = v;
_rootView.nsView = self;
_rootView.hostView = self;
[_rootView setNextResponder:self];
CGSize s = [self frame].size;
v.frame = CGRectMake(0, 0, s.width, s.height);
[self.tuiHostView.layer addSublayer:_rootView.layer];
[self _updateLayerScaleFactor];
}
- (void)setNextResponder:(NSResponder *)r
{
NSResponder *nextResponder = [self nextResponder];
if([nextResponder isKindOfClass:[NSViewController class]]) {
// keep view controller in chain
[nextResponder setNextResponder:r];
} else {
[super setNextResponder:r];
}
}
- (void)viewWillMoveToWindow:(NSWindow *)newWindow {
if(self.window != nil) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidBecomeKeyNotification object:self.window];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResignKeyNotification object:self.window];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidChangeScreenProfileNotification object:self.window];
}
CALayer *hostLayer = self.tuiHostView.layer;
if(newWindow != nil && _rootView.layer.superlayer != hostLayer) {
_rootView.layer.frame = hostLayer.bounds;
[hostLayer addSublayer:_rootView.layer];
}
[self.rootView willMoveToWindow:(TUINSWindow *) newWindow];
if(newWindow == nil) {
[_rootView removeFromSuperview];
}
}
- (void)viewDidMoveToWindow
{
[self _updateLayerScaleFactor];
[self.rootView didMoveToWindow];
if(self.window != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:self.window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:self.window];
// make sure the window will post NSWindowDidChangeScreenProfileNotification
[self.window setDisplaysWhenScreenProfileChanges:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenProfileOrBackingPropertiesDidChange:) name:NSWindowDidChangeScreenProfileNotification object:self.window];
}
}
- (void)_updateLayerScaleFactor {
if([self window] != nil) {
CGFloat scale = 1.0f;
if([[self window] respondsToSelector:@selector(backingScaleFactor)]) {
scale = [[self window] backingScaleFactor];
}
if([self.tuiHostView.layer respondsToSelector:@selector(setContentsScale:)]) {
if(fabs(self.tuiHostView.layer.contentsScale - scale) > 0.1f) {
self.tuiHostView.layer.contentsScale = scale;
}
}
[self.rootView _updateLayerScaleFactor];
}
}
- (void)screenProfileOrBackingPropertiesDidChange:(NSNotification *)notification
{
[self performSelector:@selector(_updateLayerScaleFactor) withObject:nil afterDelay:0.0]; // the window's backingScaleFactor doesn't update until after this notification fires (10.8) - so delay it a bit.
}
- (TUIView *)viewForLocalPoint:(NSPoint)p
{
return [_rootView hitTest:p withEvent:nil];
}
- (NSPoint)localPointForLocationInWindow:(NSPoint)locationInWindow
{
return [self convertPoint:locationInWindow fromView:nil];
}
- (TUIView *)viewForLocationInWindow:(NSPoint)locationInWindow
{
return [self viewForLocalPoint:[self localPointForLocationInWindow:locationInWindow]];
}
- (TUIView *)viewForEvent:(NSEvent *)event
{
return [self viewForLocationInWindow:[event locationInWindow]];
}
- (void)windowDidResignKey:(NSNotification *)notification
{
[TUITooltipWindow endTooltip];
if(![self isWindowKey]) {
[self.rootView windowDidResignKey];
}
}
- (void)windowDidBecomeKey:(NSNotification *)notification
{
[self.rootView windowDidBecomeKey];
}
- (BOOL)isWindowKey
{
if([self.window isKeyWindow]) return YES;
NSWindow *keyWindow = [NSApp keyWindow];
if(keyWindow == nil) return NO;
return keyWindow == [self.window attachedSheet];
}
- (void)viewWillMoveToSuperview:(NSView *)newSuperview
{
[super viewWillMoveToSuperview:newSuperview];
if(newSuperview == nil) {
[TUITooltipWindow endTooltip];
}
}
- (void)_updateHoverView:(TUIView *)_newHoverView withEvent:(NSEvent *)event
{
if(_hyperFocusView) {
if(![_newHoverView isDescendantOfView:_hyperFocusView]) {
_newHoverView = nil; // don't allow hover
}
}
if(_newHoverView != _hoverView) {
[_hoverView mouseExited:event];
[_newHoverView mouseEntered:event];
_hoverView = _newHoverView;
if([[self window] isKeyWindow]) {
[TUITooltipWindow updateTooltip:_hoverView.toolTip delay:_hoverView.toolTipDelay];
} else {
[TUITooltipWindow updateTooltip:nil delay:_hoverView.toolTipDelay];
}
} else {
[_hoverView mouseMoved:event];
}
}
- (void)_updateHoverViewWithEvent:(NSEvent *)event
{
TUIView *_newHoverView = [self viewForEvent:event];
if(![[self window] isKeyWindow]) {
if(![_newHoverView acceptsFirstMouse:event]) {
// in background, don't do hover for things that don't accept first mouse
_newHoverView = nil;
}
}
[self _updateHoverView:_newHoverView withEvent:event];
}
- (void)invalidateHover
{
[self _updateHoverView:nil withEvent:nil];
}
- (void)invalidateHoverForView:(TUIView *)v
{
if([_hoverView isDescendantOfView:v]) {
[self invalidateHover];
}
}
- (void)mouseDown:(NSEvent *)event
{
if(_hyperFocusView) {
TUIView *v = [self viewForEvent:event];
if([v isDescendantOfView:_hyperFocusView]) {
// activate it normally
[self endHyperFocus:NO]; // not cancelled
goto normal;
} else {
// dismiss hover, don't click anything
[self endHyperFocus:YES];
}
} else {
// normal case
normal:
;
_trackingView = [self viewForEvent:event];
[_trackingView mouseDown:event];
}
[TUITooltipWindow endTooltip];
}
- (void)mouseUp:(NSEvent *)event
{
TUIView *lastTrackingView = _trackingView;
_trackingView = nil;
[lastTrackingView mouseUp:event]; // after _trackingView set to nil, will call mouseUp:fromSubview:
[self _updateHoverViewWithEvent:event];
}
- (void)mouseDragged:(NSEvent *)event
{
[_trackingView mouseDragged:event];
}
- (void)mouseMoved:(NSEvent *)event
{
[self _updateHoverViewWithEvent:event];
}
-(void)mouseEntered:(NSEvent *)event {
[self _updateHoverViewWithEvent:event];
}
-(void)mouseExited:(NSEvent *)event {
[self _updateHoverViewWithEvent:event];
}
- (void)rightMouseDown:(NSEvent *)event
{
_trackingView = [self viewForEvent:event];
[_trackingView rightMouseDown:event];
[TUITooltipWindow endTooltip];
[super rightMouseDown:event]; // we need to send this up the responder chain so that -menuForEvent: will get called for two-finger taps
}
- (void)rightMouseUp:(NSEvent *)event
{
TUIView *lastTrackingView = _trackingView;
_trackingView = nil;
[lastTrackingView rightMouseUp:event]; // after _trackingView set to nil, will call mouseUp:fromSubview:
}
- (void)scrollWheel:(NSEvent *)event
{
[[self viewForEvent:event] scrollWheel:event];
[self _updateHoverView:nil withEvent:event]; // don't pop in while scrolling
}
- (void)beginGestureWithEvent:(NSEvent *)event
{
[[self viewForEvent:event] beginGestureWithEvent:event];
}
- (void)endGestureWithEvent:(NSEvent *)event
{
[[self viewForEvent:event] endGestureWithEvent:event];
}
- (void)magnifyWithEvent:(NSEvent *)event
{
if(!deliveringEvent) {
deliveringEvent = YES;
[[self viewForEvent:event] magnifyWithEvent:event];
deliveringEvent = NO;
}
}
- (void)rotateWithEvent:(NSEvent *)event
{
if(!deliveringEvent) {
deliveringEvent = YES;
[[self viewForEvent:event] rotateWithEvent:event];
deliveringEvent = NO;
}
}
- (void)swipeWithEvent:(NSEvent *)event
{
if(!deliveringEvent) {
deliveringEvent = YES;
[[self viewForEvent:event] swipeWithEvent:event];
deliveringEvent = NO;
}
}
- (void)keyDown:(NSEvent *)event
{
BOOL consumed = NO;
// TUIView uses -performKeyAction: in -keyDown: to do its key equivalents. If none of our TUIViews consumed the key down as a key action, we want to give our view controller a chance to handle the key down as a key equivalent.
if([[self nextResponder] isKindOfClass:[NSViewController class]]) {
consumed = [[self nextResponder] performKeyEquivalent:event];
}
if(!consumed) {
[super keyDown:event];
}
}
- (BOOL)performKeyEquivalent:(NSEvent *)event
{
return [_rootView performKeyEquivalent:event];
}
- (void)setEverythingNeedsDisplay
{
[_rootView setEverythingNeedsDisplay];
}
- (BOOL)isTrackingSubviewOfView:(TUIView *)v
{
return [_trackingView isDescendantOfView:v];
}
- (BOOL)isHoveringSubviewOfView:(TUIView *)v
{
return [_hoverView isDescendantOfView:v];
}
- (BOOL)isHoveringView:(TUIView *)v
{
return _hoverView == v;
}
- (BOOL)acceptsFirstMouse:(NSEvent *)event
{
return [[self viewForEvent:event] acceptsFirstMouse:event];
}
/* http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/MenuList/Articles/EnablingMenuItems.html
If the menu item’s target is not set and the NSMenu object is a contextual menu, NSMenu goes through the same steps as before but the search order for the responder chain is different:
- The responder chain for the window in which the view that triggered the context menu resides, starting with the view.
- The window itself.
- The window’s delegate.
- The NSApplication object.
- The NSApplication object’s delegate.
*/
- (NSResponder *)firstResponderForSelector:(SEL)action
{
if(!action)
return nil;
NSResponder *f = [[self window] firstResponder];
// NSLog(@"starting search at %@", f);
do {
if([f respondsToSelector:action])
return f;
} while((f = [f nextResponder]));
return nil;
}
- (void)_patchMenu:(NSMenu *)menu
{
for(NSMenuItem *item in [menu itemArray]) {
if(![item target]) {
// would normally travel the responder chain starting too high up, patch it to target what it would target if it hit the true responder chain
[item setTarget:[self firstResponderForSelector:[item action]]];
}
if([item submenu])
[self _patchMenu:[item submenu]]; // recurse
}
}
// the problem is for context menus the responder chain search starts with the NSView... we want it to start deeper, so we can patch up targets of a copy of the menu here
- (NSMenu *)menuWithPatchedItems:(NSMenu *)menu
{
NSData *d = [NSKeyedArchiver archivedDataWithRootObject:menu]; // this is bad - doesn't persist 'target'?
menu = [NSKeyedUnarchiver unarchiveObjectWithData:d];
[self _patchMenu:menu];
return menu;
}
- (NSMenu *)menuForEvent:(NSEvent *)event
{
TUIView *v = [self viewForEvent:event];
do {
NSMenu *m = [v menuForEvent:event];
if(m)
return m; // not patched
v = v.superview;
} while(v);
return nil;
}
- (void)setUp {
opaque = YES;
_maskLayer = [CAShapeLayer layer];
_maskLayer.frame = self.bounds;
_maskLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
// enable layer-backing for this view
self.wantsLayer = YES;
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawNever;
_tuiHostView = [[TUINSHostView alloc] initWithFrame:self.bounds];
_tuiHostView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[self addSubview:_tuiHostView];
_appKitHostView = [[NSView alloc] initWithFrame:self.bounds];
_appKitHostView.autoresizesSubviews = NO;
_appKitHostView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
_appKitHostView.wantsLayer = YES;
_appKitHostView.layerContentsRedrawPolicy = NSViewLayerContentsRedrawNever;
[self addSubview:_appKitHostView];
// set up masking on the AppKit host view, and make ourselves the layout
// manager, so that we'll know when new sublayers are added
self.appKitHostView.layer.layoutManager = self;
#if ENABLE_NSVIEW_CLIPPING
self.appKitHostView.layer.mask = self.maskLayer;
[self recalculateNSViewClipping];
#endif
}
- (void)didAddSubview:(NSView *)view {
NSAssert(view == self.tuiHostView || view == self.appKitHostView, @"Subviews should not be added to TUINSView %@: %@", self, view);
[super didAddSubview:view];
}
#define ENABLE_NSTEXT_INPUT_CLIENT
#import "TUINSView+NSTextInputClient.m"
#undef ENABLE_NSTEXT_INPUT_CLIENT
#pragma mark AppKit bridging
- (NSView *)hitTest:(NSPoint)point {
// convert point into our coordinate system, so it's ready to go for all
// subviews (which expect it in their superview's coordinate system)
point = [self convertPoint:point fromView:self.superview];
if (!CGRectContainsPoint(self.bounds, point))
return nil;
__block NSView *result = self;
// we need to avoid hitting any NSViews that are clipped by their
// corresponding TwUI views
[self.appKitHostView.subviews enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSView *view, NSUInteger index, BOOL *stop){
id<TUIBridgedView> hostView = view.hostView;
if (hostView) {
CGRect bounds = hostView.layer.bounds;
CGRect clippedBounds = [hostView.layer tui_convertAndClipRect:bounds toLayer:self.layer];
if (!CGRectContainsPoint(clippedBounds, point)) {
// skip this view
return;
}
}
NSView *hitTestedView = [view hitTest:point];
if (hitTestedView) {
result = hitTestedView;
*stop = YES;
}
}];
return result;
}
- (void)recalculateNSViewOrdering; {
NSAssert([NSThread isMainThread], @"");
[self.appKitHostView sortSubviewsUsingFunction:&compareNSViewOrdering context:NULL];
}
- (void)recalculateNSViewClipping; {
NSAssert([NSThread isMainThread], @"");
#if !ENABLE_NSVIEW_CLIPPING
return;
#endif
CGMutablePathRef path = CGPathCreateMutable();
for (NSView *view in self.appKitHostView.subviews) {
id<TUIBridgedView> hostView = view.hostView;
if (!hostView)
continue;
CALayer *focusRingLayer = [self focusRingLayerForView:view];
if (focusRingLayer) {
id<TUIBridgedScrollView> clippingView = hostView.ancestorScrollView;
if (clippingView && self.ancestorScrollView != clippingView) {
// set up a mask on the focus ring that clips to any ancestor scroll views
CAShapeLayer *maskLayer = (id)focusRingLayer.mask;
if (![maskLayer isKindOfClass:[CAShapeLayer class]]) {
maskLayer = [CAShapeLayer layer];
focusRingLayer.mask = maskLayer;
}
CGRect rect = [clippingView.layer tui_convertAndClipRect:clippingView.layer.visibleRect toLayer:focusRingLayer];
if (CGRectIsNull(rect) || CGRectIsInfinite(rect)) {
rect = CGRectZero;
}
CGPathRef focusRingPath = CGPathCreateWithRect(rect, NULL);
maskLayer.path = focusRingPath;
CGPathRelease(focusRingPath);
CGPathAddRect(path, NULL, [focusRingLayer tui_convertAndClipRect:rect toLayer:self.layer]);
} else {
focusRingLayer.mask = nil;
CGPathAddRect(path, NULL, [focusRingLayer tui_convertAndClipRect:focusRingLayer.bounds toLayer:self.layer]);
}
}
// clip the frame of each NSView using the TwUI hierarchy
CGRect rect = [hostView.layer tui_convertAndClipRect:hostView.layer.visibleRect toLayer:self.layer];
if (CGRectIsNull(rect) || CGRectIsInfinite(rect))
continue;
CGPathAddRect(path, NULL, rect);
}
// mask them all at once (so fast!)
self.maskLayer.path = path;
CGPathRelease(path);
}
#pragma mark CALayer delegate
- (void)layoutSublayersOfLayer:(CALayer *)layer {
if (layer == self.layer) {
// TUINSView.layer is being laid out
return;
}
// appKitHostView.layer is being laid out
//
// this often happens in response to AppKit adding a focus ring layer, so
// recalculate our clipping paths to take it into account
[self recalculateNSViewClipping];
}
- (CALayer *)focusRingLayerForView:(NSView *)view; {
CALayer *resultSoFar = nil;
for (CALayer *layer in self.appKitHostView.layer.sublayers) {
// don't return the layer of the view itself
if (layer == view.layer) {
continue;
}
// if the layer doesn't wrap around this view, it's not the focus ring
if (!CGRectContainsRect(layer.frame, view.frame)) {
continue;
}
// if resultSoFar matched more tightly than this layer, consider the
// former to be the focus ring
if (resultSoFar && !CGRectContainsRect(resultSoFar.frame, layer.frame)) {
continue;
}
resultSoFar = layer;
}
return resultSoFar;
}
#pragma mark TUIHostView
- (void)ancestorDidLayout; {
[super ancestorDidLayout];
[self.rootView ancestorDidLayout];
}
- (id<TUIBridgedView>)descendantViewAtPoint:(CGPoint)point {
if (!CGRectContainsPoint(self.bounds, point))
return nil;
return [self.rootView descendantViewAtPoint:point] ?: self;
}
- (id<TUIHostView>)hostView {
if (_hostView)
return _hostView;
else
return self.superview.hostView;
}
- (void)viewHierarchyDidChange {
[super viewHierarchyDidChange];
[self.rootView viewHierarchyDidChange];
}
- (void)willMoveToTUINSView:(TUINSView *)view; {
// despite the TUIBridgedView contract that says we should forward this
// message onto all subviews and our rootView, doing so could result in
// crazy behavior, since the TUINSView of those views is and will remain
// 'self' by definition
}
- (void)didMoveFromTUINSView:(TUINSView *)view; {
// despite the TUIBridgedView contract that says we should forward this
// message onto all subviews and our rootView, doing so could result in
// crazy behavior, since the TUINSView of those views is and will remain
// 'self' by definition
}
@end