Skip to content
Browse files

Implemented <TUIBridgedView> on NSView

  • Loading branch information...
1 parent 9f49122 commit 6094ba6cb67b33e78b9d6ccf0edaff703c2d861f @jspahrsummers jspahrsummers committed Jul 17, 2012
Showing with 185 additions and 0 deletions.
  1. +16 −0 TwUI.xcodeproj/project.pbxproj
  2. +29 −0 lib/UIKit/NSView+TUIExtensions.h
  3. +139 −0 lib/UIKit/NSView+TUIExtensions.m
  4. +1 −0 lib/UIKit/TUIKit.h
View
16 TwUI.xcodeproj/project.pbxproj
@@ -287,6 +287,12 @@
D0C7652D15B6232100E7AC2C /* CATransaction+TUIExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7652315B6232100E7AC2C /* CATransaction+TUIExtensions.m */; };
D0C7652E15B6232100E7AC2C /* CATransaction+TUIExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7652315B6232100E7AC2C /* CATransaction+TUIExtensions.m */; };
D0C7652F15B6232100E7AC2C /* CATransaction+TUIExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7652315B6232100E7AC2C /* CATransaction+TUIExtensions.m */; };
+ D0C7653315B624D900E7AC2C /* NSView+TUIExtensions.h in Headers */ = {isa = PBXBuildFile; fileRef = D0C7653115B624D800E7AC2C /* NSView+TUIExtensions.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ D0C7653415B624D900E7AC2C /* NSView+TUIExtensions.h in Headers */ = {isa = PBXBuildFile; fileRef = D0C7653115B624D800E7AC2C /* NSView+TUIExtensions.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ D0C7653515B624D900E7AC2C /* NSView+TUIExtensions.h in Headers */ = {isa = PBXBuildFile; fileRef = D0C7653115B624D800E7AC2C /* NSView+TUIExtensions.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ D0C7653615B624D900E7AC2C /* NSView+TUIExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7653215B624D800E7AC2C /* NSView+TUIExtensions.m */; };
+ D0C7653715B624D900E7AC2C /* NSView+TUIExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7653215B624D800E7AC2C /* NSView+TUIExtensions.m */; };
+ D0C7653815B624D900E7AC2C /* NSView+TUIExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0C7653215B624D800E7AC2C /* NSView+TUIExtensions.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -440,6 +446,8 @@
D0C7652115B6232100E7AC2C /* CALayer+TUIExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "CALayer+TUIExtensions.m"; sourceTree = "<group>"; };
D0C7652215B6232100E7AC2C /* CATransaction+TUIExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "CATransaction+TUIExtensions.h"; sourceTree = "<group>"; };
D0C7652315B6232100E7AC2C /* CATransaction+TUIExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "CATransaction+TUIExtensions.m"; sourceTree = "<group>"; };
+ D0C7653115B624D800E7AC2C /* NSView+TUIExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSView+TUIExtensions.h"; sourceTree = "<group>"; };
+ D0C7653215B624D800E7AC2C /* NSView+TUIExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSView+TUIExtensions.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -598,6 +606,8 @@
CBB74C3D13BE6E1900C85CB5 /* UIKit */ = {
isa = PBXGroup;
children = (
+ D0C7653115B624D800E7AC2C /* NSView+TUIExtensions.h */,
+ D0C7653215B624D800E7AC2C /* NSView+TUIExtensions.m */,
CBB74C3E13BE6E1900C85CB5 /* TUIAccessibility.h */,
CBB74C3F13BE6E1900C85CB5 /* TUIAccessibility.m */,
CBB74C4013BE6E1900C85CB5 /* TUIActivityIndicatorView.h */,
@@ -734,6 +744,7 @@
D0C764ED15B611C200E7AC2C /* TUIBridgedView.h in Headers */,
D0C7650715B6156A00E7AC2C /* TUIHostView.h in Headers */,
D0C7651815B61E5A00E7AC2C /* TUIBridgedScrollView.h in Headers */,
+ D0C7653515B624D900E7AC2C /* NSView+TUIExtensions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -798,6 +809,7 @@
D0C764EB15B611C200E7AC2C /* TUIBridgedView.h in Headers */,
D0C7650515B6156A00E7AC2C /* TUIHostView.h in Headers */,
D0C7651615B61E5A00E7AC2C /* TUIBridgedScrollView.h in Headers */,
+ D0C7653315B624D900E7AC2C /* NSView+TUIExtensions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -818,6 +830,7 @@
D0C764EC15B611C200E7AC2C /* TUIBridgedView.h in Headers */,
D0C7650615B6156A00E7AC2C /* TUIHostView.h in Headers */,
D0C7651715B61E5A00E7AC2C /* TUIBridgedScrollView.h in Headers */,
+ D0C7653415B624D900E7AC2C /* NSView+TUIExtensions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1023,6 +1036,7 @@
D0C7651315B6189D00E7AC2C /* TUINSHostView.m in Sources */,
D0C7652915B6232100E7AC2C /* CALayer+TUIExtensions.m in Sources */,
D0C7652F15B6232100E7AC2C /* CATransaction+TUIExtensions.m in Sources */,
+ D0C7653815B624D900E7AC2C /* NSView+TUIExtensions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1092,6 +1106,7 @@
D0C7651115B6189D00E7AC2C /* TUINSHostView.m in Sources */,
D0C7652715B6232100E7AC2C /* CALayer+TUIExtensions.m in Sources */,
D0C7652D15B6232100E7AC2C /* CATransaction+TUIExtensions.m in Sources */,
+ D0C7653615B624D900E7AC2C /* NSView+TUIExtensions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1167,6 +1182,7 @@
D0C7651215B6189D00E7AC2C /* TUINSHostView.m in Sources */,
D0C7652815B6232100E7AC2C /* CALayer+TUIExtensions.m in Sources */,
D0C7652E15B6232100E7AC2C /* CATransaction+TUIExtensions.m in Sources */,
+ D0C7653715B624D900E7AC2C /* NSView+TUIExtensions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
29 lib/UIKit/NSView+TUIExtensions.h
@@ -0,0 +1,29 @@
+//
+// NSView+TUIExtensions.h
+// TwUI
+//
+// Created by Justin Spahr-Summers on 17.07.12.
+//
+// Portions of this code were taken from Velvet,
+// which is copyright (c) 2012 Bitswift, Inc.
+// See LICENSE.txt for more information.
+//
+
+#import <AppKit/AppKit.h>
+#import "TUIBridgedView.h"
+#import "TUIHostView.h"
+
+@class TUIViewNSViewContainer;
+
+/**
+ * Implements <TUIBridgedView> for NSView.
+ */
+@interface NSView (TUIExtensions) <TUIBridgedView>
+
+/**
+ * The TUIViewNSViewContainer that is hosting this view, or nil if it exists
+ * independently of a TwUI hierarchy.
+ */
+@property (nonatomic, unsafe_unretained) TUIViewNSViewContainer<TUIHostView> *hostView;
+
+@end
View
139 lib/UIKit/NSView+TUIExtensions.m
@@ -0,0 +1,139 @@
+//
+// NSView+TUIExtensions.m
+// TwUI
+//
+// Created by Justin Spahr-Summers on 17.07.12.
+//
+// Portions of this code were taken from Velvet,
+// which is copyright (c) 2012 Bitswift, Inc.
+// See LICENSE.txt for more information.
+//
+
+#import "NSView+TUIExtensions.h"
+#import "TUINSView.h"
+#import "TUIBridgedScrollView.h"
+#import <objc/runtime.h>
+
+@implementation NSView (TUIExtensions)
+
+// NSView already implements a layer property
+@dynamic layer;
+
+#pragma mark View hierarchy
+
+- (id<TUIHostView>)hostView {
+ id<TUIHostView> hostView = objc_getAssociatedObject(self, @selector(hostView));
+ if (hostView)
+ return hostView;
+ else
+ return self.superview.hostView;
+}
+
+- (id<TUIBridgedView>)immediateParentView {
+ id<TUIHostView> hostView = objc_getAssociatedObject(self, @selector(hostView));
+ if (hostView)
+ return hostView;
+ else
+ return self.superview;
+}
+
+- (void)setHostView:(id<TUIHostView>)hostView {
+ objc_setAssociatedObject(self, @selector(hostView), hostView, OBJC_ASSOCIATION_ASSIGN);
+}
+
+- (BOOL)isFocused {
+ return [objc_getAssociatedObject(self, @selector(isFocused)) boolValue];
+}
+
+- (void)setFocused:(BOOL)focused {
+ objc_setAssociatedObject(self, @selector(isFocused), [NSNumber numberWithBool:focused], OBJC_ASSOCIATION_COPY_NONATOMIC);
+
+ for (NSView *view in self.subviews) {
+ view.focused = focused;
+ }
+}
+
+- (void)ancestorDidLayout; {
+ [self.subviews makeObjectsPerformSelector:_cmd];
+}
+
+- (TUINSView *)ancestorTUINSView; {
+ NSView *view = self;
+
+ do {
+ if ([view isKindOfClass:[TUINSView class]])
+ return (id)view;
+
+ view = view.superview;
+ } while (view);
+
+ return nil;
+}
+
+- (id<TUIBridgedScrollView>)ancestorScrollView {
+ if ([self conformsToProtocol:@protocol(TUIBridgedScrollView)])
+ return (id)self;
+
+ return self.immediateParentView.ancestorScrollView;
+}
+
+#pragma mark Geometry
+
+- (CGPoint)convertFromWindowPoint:(CGPoint)point; {
+ NSPoint windowPoint = NSPointFromCGPoint(point);
+ NSPoint selfPoint = [self convertPoint:windowPoint fromView:nil];
+ return NSPointToCGPoint(selfPoint);
+}
+
+- (CGPoint)convertToWindowPoint:(CGPoint)point; {
+ NSPoint windowPoint = NSPointFromCGPoint(point);
+ NSPoint selfPoint = [self convertPoint:windowPoint toView:nil];
+ return NSPointToCGPoint(selfPoint);
+}
+
+- (CGRect)convertFromWindowRect:(CGRect)rect; {
+ NSRect windowRect = NSRectFromCGRect(rect);
+ NSRect selfRect = [self convertRect:windowRect fromView:nil];
+ return NSRectToCGRect(selfRect);
+}
+
+- (CGRect)convertToWindowRect:(CGRect)rect; {
+ NSRect windowRect = NSRectFromCGRect(rect);
+ NSRect selfRect = [self convertRect:windowRect toView:nil];
+ return NSRectToCGRect(selfRect);
+}
+
+#pragma mark Hit testing
+
+- (id<TUIBridgedView>)descendantViewAtPoint:(NSPoint)point {
+ NSPoint superviewPoint = [self convertPoint:point toView:self.superview];
+ id hitView = [self hitTest:superviewPoint];
+
+ if ([hitView isKindOfClass:[TUINSView class]]) {
+ NSPoint descendantPoint = [self convertPoint:point toView:hitView];
+ return [hitView descendantViewAtPoint:descendantPoint];
+ }
+
+ return hitView;
+}
+
+- (BOOL)pointInside:(CGPoint)point {
+ NSPoint superviewPoint = [self convertPoint:point toView:self.superview];
+ return [self hitTest:superviewPoint] ? YES : NO;
+}
+
+#pragma mark View hierarchy
+
+- (void)willMoveToTUINSView:(TUINSView *)view; {
+ [self.subviews makeObjectsPerformSelector:_cmd withObject:view];
+}
+
+- (void)didMoveFromTUINSView:(TUINSView *)view; {
+ [self.subviews makeObjectsPerformSelector:_cmd withObject:view];
+}
+
+- (void)viewHierarchyDidChange {
+ [self.subviews makeObjectsPerformSelector:_cmd];
+}
+
+@end
View
1 lib/UIKit/TUIKit.h
@@ -18,6 +18,7 @@
#import "CAAnimation+TUIExtensions.h"
#import "CoreText+Additions.h"
+#import "NSView+TUIExtensions.h"
#import "TUIActivityIndicatorView.h"
#import "TUIAttributedString.h"
#import "TUIBridgedScrollView.h"

0 comments on commit 6094ba6

Please sign in to comment.
Something went wrong with that request. Please try again.