mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
1f4535c175
Summary: Changelog: [Internal] # Problem SafeAreaView is getting reused, its previous `safeAreaInsets` is top: 44, bottom: 34 so `safeAreaInsetsDidChange` doesn't get called because it doesn't change. Therefore state gets never updates because `safeAreaInsetsDidChange` is never called. # Solution Update state whenever a new state is assigned. Reviewed By: shergin Differential Revision: D20444198 fbshipit-source-id: 75d1458450c70d74647df4962ddad88d5f6a38d2
100 lines
2.5 KiB
Plaintext
100 lines
2.5 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/components/safeareaview/SafeAreaViewComponentDescriptor.h>
|
|
#import <react/components/safeareaview/SafeAreaViewState.h>
|
|
#import "FBRCTFabricComponentsPlugins.h"
|
|
#import "RCTConversions.h"
|
|
#import "RCTFabricComponentsPlugins.h"
|
|
|
|
using namespace facebook::react;
|
|
|
|
@implementation RCTSafeAreaViewComponentView {
|
|
SafeAreaViewShadowNode::ConcreteState::Shared _state;
|
|
}
|
|
|
|
- (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)layoutSubviews
|
|
{
|
|
[super layoutSubviews];
|
|
}
|
|
|
|
- (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 oldPadding = _state->getData().padding;
|
|
auto newPadding = RCTEdgeInsetsFromUIEdgeInsets(insets);
|
|
auto threshold = 1.0 / RCTScreenScale() + 0.01; // Size of a pixel plus some small threshold.
|
|
auto deltaPadding = newPadding - oldPadding;
|
|
|
|
if (std::abs(deltaPadding.left) < threshold && std::abs(deltaPadding.top) < threshold &&
|
|
std::abs(deltaPadding.right) < threshold && std::abs(deltaPadding.bottom) < threshold) {
|
|
return;
|
|
}
|
|
|
|
_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];
|
|
}
|
|
|
|
+ (ComponentDescriptorProvider)componentDescriptorProvider
|
|
{
|
|
return concreteComponentDescriptorProvider<SafeAreaViewComponentDescriptor>();
|
|
}
|
|
|
|
@end
|
|
|
|
Class<RCTComponentViewProtocol> RCTSafeAreaViewCls(void)
|
|
{
|
|
return RCTSafeAreaViewComponentView.class;
|
|
}
|