2018-04-10 16:37:11 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTViewComponentView.h"
|
|
|
|
|
2018-07-15 16:46:31 -07:00
|
|
|
#import <fabric/components/view/ViewProps.h>
|
|
|
|
#import <fabric/components/view/ViewEventEmitter.h>
|
2018-05-22 15:48:24 -07:00
|
|
|
|
2018-06-15 11:25:26 -07:00
|
|
|
#import "RCTConversions.h"
|
2018-04-10 16:37:11 -07:00
|
|
|
|
|
|
|
using namespace facebook::react;
|
|
|
|
|
|
|
|
@implementation RCTViewComponentView
|
2018-06-22 11:53:48 -07:00
|
|
|
{
|
|
|
|
BOOL _isCoreAnimationBorderRenderingEnabled;
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:25:14 -07:00
|
|
|
- (void)setContentView:(UIView *)contentView
|
|
|
|
{
|
|
|
|
if (_contentView) {
|
|
|
|
[_contentView removeFromSuperview];
|
|
|
|
}
|
|
|
|
|
|
|
|
_contentView = contentView;
|
|
|
|
|
|
|
|
if (_contentView) {
|
|
|
|
[self addSubview:_contentView];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
|
|
|
|
|
|
|
if (_contentView) {
|
|
|
|
_contentView.frame = RCTCGRectFromRect(_layoutMetrics.getContentFrame());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:25:24 -07:00
|
|
|
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
|
|
|
|
{
|
|
|
|
if (UIEdgeInsetsEqualToEdgeInsets(self.hitTestEdgeInsets, UIEdgeInsetsZero)) {
|
|
|
|
return [super pointInside:point withEvent:event];
|
|
|
|
}
|
|
|
|
CGRect hitFrame = UIEdgeInsetsInsetRect(self.bounds, self.hitTestEdgeInsets);
|
|
|
|
return CGRectContainsPoint(hitFrame, point);
|
|
|
|
}
|
2018-04-10 16:37:11 -07:00
|
|
|
|
2018-05-22 15:48:24 -07:00
|
|
|
- (void)updateProps:(SharedProps)props
|
|
|
|
oldProps:(SharedProps)oldProps
|
2018-04-10 16:37:11 -07:00
|
|
|
{
|
|
|
|
if (!oldProps) {
|
2018-05-08 22:56:04 -07:00
|
|
|
oldProps = _props ?: std::make_shared<ViewProps>();
|
2018-04-10 16:37:11 -07:00
|
|
|
}
|
2018-05-08 22:56:04 -07:00
|
|
|
_props = props;
|
2018-04-10 16:37:11 -07:00
|
|
|
|
|
|
|
auto oldViewProps = *std::dynamic_pointer_cast<const ViewProps>(oldProps);
|
|
|
|
auto newViewProps = *std::dynamic_pointer_cast<const ViewProps>(props);
|
|
|
|
|
2018-06-15 11:25:26 -07:00
|
|
|
// `opacity`
|
|
|
|
if (oldViewProps.opacity != newViewProps.opacity) {
|
|
|
|
self.layer.opacity = (CGFloat)newViewProps.opacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
// `backgroundColor`
|
2018-05-14 15:43:28 -07:00
|
|
|
if (oldViewProps.backgroundColor != newViewProps.backgroundColor) {
|
2018-06-15 11:25:26 -07:00
|
|
|
self.backgroundColor = RCTUIColorFromSharedColor(newViewProps.backgroundColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// `foregroundColor`
|
|
|
|
if (oldViewProps.foregroundColor != newViewProps.foregroundColor) {
|
|
|
|
self.foregroundColor = RCTUIColorFromSharedColor(newViewProps.foregroundColor);
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:25:32 -07:00
|
|
|
// `shadowColor`
|
|
|
|
if (oldViewProps.shadowColor != newViewProps.shadowColor) {
|
|
|
|
CGColorRef shadowColor = RCTCGColorRefFromSharedColor(newViewProps.shadowColor);
|
|
|
|
self.layer.shadowColor = shadowColor;
|
|
|
|
CGColorRelease(shadowColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// `shadowOffset`
|
|
|
|
if (oldViewProps.shadowOffset != newViewProps.shadowOffset) {
|
|
|
|
self.layer.shadowOffset = RCTCGSizeFromSize(newViewProps.shadowOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// `shadowOpacity`
|
|
|
|
if (oldViewProps.shadowOpacity != newViewProps.shadowOpacity) {
|
|
|
|
self.layer.shadowOpacity = (CGFloat)newViewProps.shadowOpacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
// `shadowRadius`
|
|
|
|
if (oldViewProps.shadowRadius != newViewProps.shadowRadius) {
|
|
|
|
self.layer.shadowRadius = (CGFloat)newViewProps.shadowRadius;
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:25:26 -07:00
|
|
|
// `backfaceVisibility`
|
|
|
|
if (oldViewProps.backfaceVisibility != newViewProps.backfaceVisibility) {
|
|
|
|
self.layer.doubleSided = newViewProps.backfaceVisibility;
|
|
|
|
}
|
|
|
|
|
|
|
|
// `shouldRasterize`
|
|
|
|
if (oldViewProps.shouldRasterize != newViewProps.shouldRasterize) {
|
|
|
|
self.layer.shouldRasterize = newViewProps.shouldRasterize;
|
|
|
|
self.layer.rasterizationScale = newViewProps.shouldRasterize ? [UIScreen mainScreen].scale : 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// `pointerEvents`
|
|
|
|
if (oldViewProps.pointerEvents != newViewProps.pointerEvents) {
|
|
|
|
self.userInteractionEnabled = newViewProps.pointerEvents != PointerEventsMode::None;
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:25:34 -07:00
|
|
|
// `transform`
|
|
|
|
if (oldViewProps.transform != newViewProps.transform) {
|
|
|
|
self.layer.transform = RCTCATransform3DFromTransformMatrix(newViewProps.transform);
|
|
|
|
self.layer.allowsEdgeAntialiasing = newViewProps.transform != Transform::Identity();
|
2018-04-10 16:37:11 -07:00
|
|
|
}
|
|
|
|
|
2018-06-15 11:25:24 -07:00
|
|
|
// `hitSlop`
|
|
|
|
if (oldViewProps.hitSlop != newViewProps.hitSlop) {
|
|
|
|
self.hitTestEdgeInsets = RCTUIEdgeInsetsFromEdgeInsets(newViewProps.hitSlop);
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:25:26 -07:00
|
|
|
// `zIndex`
|
|
|
|
if (oldViewProps.zIndex != newViewProps.zIndex) {
|
|
|
|
self.layer.zPosition = (CGFloat)newViewProps.zIndex;
|
|
|
|
}
|
|
|
|
|
2018-06-22 11:53:48 -07:00
|
|
|
// `border`
|
|
|
|
if (
|
|
|
|
oldViewProps.borderWidth != newViewProps.borderWidth ||
|
|
|
|
oldViewProps.borderStyle != newViewProps.borderStyle ||
|
|
|
|
oldViewProps.borderRadius != newViewProps.borderRadius ||
|
|
|
|
oldViewProps.borderColor != newViewProps.borderColor
|
|
|
|
) {
|
|
|
|
[self invalidateBorder];
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:25:22 -07:00
|
|
|
// `nativeId`
|
|
|
|
if (oldViewProps.nativeId != newViewProps.nativeId) {
|
2018-06-22 11:53:44 -07:00
|
|
|
self.nativeId = RCTNSStringFromString(newViewProps.nativeId);
|
|
|
|
}
|
2018-06-22 11:53:46 -07:00
|
|
|
|
|
|
|
// `accessible`
|
|
|
|
if (oldViewProps.accessible != newViewProps.accessible) {
|
|
|
|
self.accessibilityElement.isAccessibilityElement = newViewProps.accessible;
|
2018-06-15 11:25:22 -07:00
|
|
|
}
|
2018-04-10 16:37:11 -07:00
|
|
|
}
|
|
|
|
|
2018-06-09 13:02:55 -07:00
|
|
|
- (void)updateEventEmitter:(SharedEventEmitter)eventEmitter
|
2018-05-22 15:48:24 -07:00
|
|
|
{
|
2018-06-09 13:02:55 -07:00
|
|
|
assert(std::dynamic_pointer_cast<const ViewEventEmitter>(eventEmitter));
|
|
|
|
_eventEmitter = std::static_pointer_cast<const ViewEventEmitter>(eventEmitter);
|
2018-05-22 15:48:24 -07:00
|
|
|
}
|
|
|
|
|
2018-05-08 18:50:05 -07:00
|
|
|
- (void)updateLayoutMetrics:(LayoutMetrics)layoutMetrics
|
|
|
|
oldLayoutMetrics:(LayoutMetrics)oldLayoutMetrics
|
|
|
|
{
|
2018-06-15 11:25:14 -07:00
|
|
|
_layoutMetrics = layoutMetrics;
|
|
|
|
|
2018-05-08 18:50:05 -07:00
|
|
|
[super updateLayoutMetrics:layoutMetrics oldLayoutMetrics:oldLayoutMetrics];
|
2018-06-22 11:53:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invalidateBorder
|
|
|
|
{
|
|
|
|
const auto &props = *std::dynamic_pointer_cast<const ViewProps>(_props);
|
|
|
|
|
|
|
|
bool useCoreAnimationBorderRendering =
|
|
|
|
props.borderStyle == BorderStyle::Solid &&
|
2018-06-22 11:53:48 -07:00
|
|
|
props.borderWidth.isUniform() &&
|
|
|
|
props.borderRadius.isUniform();
|
2018-06-22 11:53:46 -07:00
|
|
|
|
|
|
|
CALayer *layer = self.layer;
|
|
|
|
if (_isCoreAnimationBorderRenderingEnabled != useCoreAnimationBorderRendering) {
|
|
|
|
_isCoreAnimationBorderRenderingEnabled = useCoreAnimationBorderRendering;
|
|
|
|
if (!useCoreAnimationBorderRendering) {
|
|
|
|
layer.borderWidth = 0;
|
|
|
|
layer.borderColor = nil;
|
|
|
|
layer.cornerRadius = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (useCoreAnimationBorderRendering) {
|
|
|
|
layer.borderWidth = (CGFloat)props.borderWidth.left;
|
|
|
|
layer.borderColor = RCTCGColorRefFromSharedColor(props.borderColor);
|
|
|
|
layer.cornerRadius = (CGFloat)props.borderRadius.topLeft;
|
|
|
|
_contentView.layer.cornerRadius = (CGFloat)props.borderRadius.topLeft;
|
|
|
|
_contentView.layer.masksToBounds = YES;
|
|
|
|
} else {
|
|
|
|
// Not supported yet.
|
|
|
|
}
|
|
|
|
}
|
2018-05-08 18:50:05 -07:00
|
|
|
|
2018-06-22 11:53:46 -07:00
|
|
|
#pragma mark - Accessibility
|
|
|
|
|
|
|
|
- (NSObject *)accessibilityElement
|
|
|
|
{
|
|
|
|
return self;
|
2018-05-08 18:50:05 -07:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:48:24 -07:00
|
|
|
#pragma mark - Accessibility Events
|
|
|
|
|
2018-06-22 11:53:46 -07:00
|
|
|
- (NSArray<UIAccessibilityCustomAction *> *)accessibilityCustomActions
|
|
|
|
{
|
|
|
|
const auto &accessibilityProps = *std::dynamic_pointer_cast<const AccessibilityProps>(_props);
|
|
|
|
|
|
|
|
if (accessibilityProps.accessibilityActions.size() == 0) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSMutableArray<UIAccessibilityCustomAction *> *customActions = [NSMutableArray array];
|
|
|
|
for (const auto &accessibilityAction : accessibilityProps.accessibilityActions) {
|
|
|
|
[customActions addObject:[[UIAccessibilityCustomAction alloc] initWithName:RCTNSStringFromString(accessibilityAction)
|
|
|
|
target:self
|
|
|
|
selector:@selector(didActivateAccessibilityCustomAction:)]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [customActions copy];
|
|
|
|
}
|
|
|
|
|
2018-05-22 15:48:24 -07:00
|
|
|
- (BOOL)accessibilityActivate
|
|
|
|
{
|
2018-06-09 13:02:55 -07:00
|
|
|
_eventEmitter->onAccessibilityTap();
|
2018-05-22 15:48:24 -07:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)accessibilityPerformMagicTap
|
|
|
|
{
|
2018-06-09 13:02:55 -07:00
|
|
|
_eventEmitter->onAccessibilityMagicTap();
|
2018-05-22 15:48:24 -07:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)didActivateAccessibilityCustomAction:(UIAccessibilityCustomAction *)action
|
|
|
|
{
|
2018-06-22 11:53:44 -07:00
|
|
|
_eventEmitter->onAccessibilityAction(RCTStringFromNSString(action.name));
|
2018-05-22 15:48:24 -07:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2018-06-09 13:02:55 -07:00
|
|
|
- (SharedEventEmitter)touchEventEmitter
|
2018-06-08 20:16:19 -07:00
|
|
|
{
|
2018-06-09 13:02:55 -07:00
|
|
|
return _eventEmitter;
|
2018-06-08 20:16:19 -07:00
|
|
|
}
|
|
|
|
|
2018-04-10 16:37:11 -07:00
|
|
|
@end
|