Files
react-native/React/Fabric/Mounting/ComponentViews/SafeAreaView/RCTSafeAreaViewComponentView.mm
T
David Vacca 3093010ea5 move fabric to ReactCommon/react/renderer
Summary:
This diff moves fabric C++ code from ReactCommon/fabric to ReactCommon/react/renderer
As part of this diff I also refactored components, codegen and callsites on CatalystApp, FB4A and venice

Script: P137350694

changelog: [internal] internal refactor

Reviewed By: fkgozali

Differential Revision: D22852139

fbshipit-source-id: f85310ba858b6afd81abfd9cbe6d70b28eca7415
2020-07-31 13:34:29 -07:00

103 lines
2.7 KiB
Plaintext

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTSafeAreaViewComponentView.h"
#import <React/RCTUtils.h>
#import <react/renderer/components/safeareaview/SafeAreaViewComponentDescriptor.h>
#import <react/renderer/components/safeareaview/SafeAreaViewState.h>
#import "FBRCTFabricComponentsPlugins.h"
#import "RCTConversions.h"
#import "RCTFabricComponentsPlugins.h"
using namespace facebook::react;
@implementation RCTSafeAreaViewComponentView {
SafeAreaViewShadowNode::ConcreteState::Shared _state;
EdgeInsets _lastPaddingStateWasUpdatedWith;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static auto const defaultProps = std::make_shared<SafeAreaViewProps const>();
_props = defaultProps;
self.clipsToBounds = YES;
}
return self;
}
- (UIEdgeInsets)_safeAreaInsets
{
if (@available(iOS 11.0, tvOS 11.0, *)) {
return self.safeAreaInsets;
}
return UIEdgeInsetsZero;
}
- (void)safeAreaInsetsDidChange
{
[super safeAreaInsetsDidChange];
[self _updateStateIfNecessary];
}
- (void)_updateStateIfNecessary
{
if (!_state) {
return;
}
UIEdgeInsets insets = [self _safeAreaInsets];
insets.left = RCTRoundPixelValue(insets.left);
insets.top = RCTRoundPixelValue(insets.top);
insets.right = RCTRoundPixelValue(insets.right);
insets.bottom = RCTRoundPixelValue(insets.bottom);
auto newPadding = RCTEdgeInsetsFromUIEdgeInsets(insets);
auto threshold = 1.0 / RCTScreenScale() + 0.01; // Size of a pixel plus some small threshold.
auto deltaPadding = newPadding - _lastPaddingStateWasUpdatedWith;
if (std::abs(deltaPadding.left) < threshold && std::abs(deltaPadding.top) < threshold &&
std::abs(deltaPadding.right) < threshold && std::abs(deltaPadding.bottom) < threshold) {
return;
}
_lastPaddingStateWasUpdatedWith = newPadding;
_state->updateState(SafeAreaViewState{newPadding});
}
#pragma mark - RCTComponentViewProtocol
- (void)updateState:(facebook::react::State::Shared const &)state
oldState:(facebook::react::State::Shared const &)oldState
{
_state = std::static_pointer_cast<SafeAreaViewShadowNode::ConcreteState const>(state);
[self _updateStateIfNecessary];
}
- (void)prepareForRecycle
{
[super prepareForRecycle];
_state.reset();
_lastPaddingStateWasUpdatedWith = {};
}
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<SafeAreaViewComponentDescriptor>();
}
@end
Class<RCTComponentViewProtocol> RCTSafeAreaViewCls(void)
{
return RCTSafeAreaViewComponentView.class;
}