Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,14 @@ - (void)boundsDidChange:(CGRect)newBounds
}

if (_state != nullptr) {
auto newState = ModalHostViewState{RCTSizeFromCGSize(newBounds.size)};
CGPoint viewportOffset = CGPointZero;
UIView *modalView = _viewController.view;
if (modalView && modalView.window) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this offset can get stale. It's only read when bounds change, so a sheet that moves without resizing (iPad rotation, keyboard) keeps the old value. The first read might also happen mid-animation or before the view has a window. Maybe read it again in viewDidAppear?

CGRect frameInWindow = [modalView convertRect:modalView.bounds toView:nil];
viewportOffset = frameInWindow.origin;
}
auto newState = ModalHostViewState{
RCTSizeFromCGSize(newBounds.size), facebook::react::Point{(Float)viewportOffset.x, (Float)viewportOffset.y}};
_state->updateState(std::move(newState));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ namespace facebook::react {

extern const char ModalHostViewComponentName[] = "ModalHostView";

Transform ModalHostViewShadowNode::getTransform() const {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<Modal ref> points at this host node, so measureInWindow() on it now adds the offset twice. The translate is applied here, then again by the main root.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed it!

auto viewportOffset = getStateData().viewportOffset;
return Transform::Translate(viewportOffset.x, viewportOffset.y, 0);
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class ModalHostViewShadowNode final : public ConcreteViewShadowNode<
traits.set(ShadowNodeTraits::Trait::Unstable_uncullableView);
return traits;
}

Transform getTransform() const override;
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ ModalHostViewState::ModalHostViewState()
#ifdef RN_SERIALIZABLE_STATE
folly::dynamic ModalHostViewState::getDynamic() const {
return folly::dynamic::object("screenWidth", screenSize.width)(
"screenHeight", screenSize.height);
"screenHeight", screenSize.height)("viewportOffsetX", viewportOffset.x)(
"viewportOffsetY", viewportOffset.y);
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ class ModalHostViewState final {

ModalHostViewState();
ModalHostViewState(Size screenSize_) : screenSize(screenSize_) {};
ModalHostViewState(Size screenSize_, Point viewportOffset_)
: screenSize(screenSize_), viewportOffset(viewportOffset_) {};

#ifdef RN_SERIALIZABLE_STATE
ModalHostViewState(const ModalHostViewState &previousState, folly::dynamic data)
: screenSize(
Size{.width = (Float)data["screenWidth"].getDouble(), .height = (Float)data["screenHeight"].getDouble()}) {
};
Size{.width = (Float)data["screenWidth"].getDouble(), .height = (Float)data["screenHeight"].getDouble()}),
viewportOffset(
Point{
.x = data.count("viewportOffsetX") ? (Float)data["viewportOffsetX"].getDouble() : 0.0f,
.y = data.count("viewportOffsetY") ? (Float)data["viewportOffsetY"].getDouble() : 0.0f}) {};
#endif

const Size screenSize{};
const Point viewportOffset{};

#ifdef RN_SERIALIZABLE_STATE
folly::dynamic getDynamic() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ LayoutMetrics LayoutableShadowNode::computeRelativeLayoutMetrics(
auto isRootNode = currentShadowNode->getTraits().check(
ShadowNodeTraits::Trait::RootNodeKind);

// The transform of a node with a `RootNodeKind` trait is its viewport
// offset, which only applies to its descendants. We don't apply it to the
// measured node because we measure it from an outside tree perspective.
auto shouldApplyTransformation = (policy.includeTransform && !isRootNode) ||
(policy.includeViewportOffset && isRootNode);
(policy.includeViewportOffset && isRootNode && i != 0);

// Move frame to the coordinate space of the current node.
resultFrame.origin += currentFrame.origin;
Expand All @@ -185,7 +188,10 @@ LayoutMetrics LayoutableShadowNode::computeRelativeLayoutMetrics(
(i != size - 1 || policy.clipSpecifiedAncestor)) {
auto overflowInset = currentShadowNode->getLayoutMetrics().overflowInset;
auto overflowRect = insetBy(
currentFrame * currentShadowNode->getTransform(), overflowInset);
shouldApplyTransformation
? currentFrame * currentShadowNode->getTransform()
: currentFrame,
overflowInset);
resultFrame = Rect::intersect(resultFrame, overflowRect);
if (resultFrame.size.width == 0 && resultFrame.size.height == 0) {
return EmptyLayoutMetrics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,135 @@ TEST(LayoutableShadowNodeTest, includeViewportOffset) {
EXPECT_EQ(layoutMetrics.frame.origin.y, 20);
}

/*
* ┌─────────────────────────────┐
* │<Root viewportOffset> │
* │ ┌──────────────────────────┐│
* │ │<Modal viewportOffset> ││
* │ │ ┌───────────┐ ││
* │ │ │<View> │ ││
* │ │ │ │ ││
* │ │ └───────────┘ ││
* │ └──────────────────────────┘│
* └─────────────────────────────┘
*/
// We're not running these on Android for the same reason as
// `relativeLayoutMetricsOnNodesCrossingRootKindNode`.
#ifndef ANDROID
TEST(
LayoutableShadowNodeTest,
includeViewportOffsetOnNodesCrossingRootKindNode) {
auto builder = simpleComponentBuilder();
auto childShadowNode = std::shared_ptr<ViewShadowNode>{};

// clang-format off
auto element =
Element<RootShadowNode>()
.props([] {
auto sharedProps = std::make_shared<RootProps>();
sharedProps->layoutContext.viewportOffset = {.x=100, .y=200};
return sharedProps;
})
.children({
Element<ModalHostViewShadowNode>()
.finalize([](ModalHostViewShadowNode &shadowNode){
auto layoutMetrics = EmptyLayoutMetrics;
layoutMetrics.frame.origin = {.x=10, .y=20};
shadowNode.setLayoutMetrics(layoutMetrics);
shadowNode.setStateData(
ModalHostViewState{Size{}, Point{.x=30, .y=40}});
})
.children({
Element<ViewShadowNode>()
.reference(childShadowNode)
.finalize([](ViewShadowNode &shadowNode){
auto layoutMetrics = EmptyLayoutMetrics;
layoutMetrics.frame.origin = {.x=1, .y=2};
shadowNode.setLayoutMetrics(layoutMetrics);
})
})
});
// clang-format on

auto rootShadowNode = builder.build(element);

// The modal is a RootKindNode, so the view is only offset by the viewport
// offset of the modal, and not by the one of the root.
auto layoutMetrics = LayoutableShadowNode::computeRelativeLayoutMetrics(
childShadowNode->getFamily(),
*rootShadowNode,
{.includeTransform = true, .includeViewportOffset = true});
EXPECT_EQ(layoutMetrics.frame.origin.x, 31);
EXPECT_EQ(layoutMetrics.frame.origin.y, 42);

layoutMetrics = LayoutableShadowNode::computeRelativeLayoutMetrics(
childShadowNode->getFamily(),
*rootShadowNode,
{.includeTransform = true, .includeViewportOffset = false});
EXPECT_EQ(layoutMetrics.frame.origin.x, 1);
EXPECT_EQ(layoutMetrics.frame.origin.y, 2);
}

/*
* ┌─────────────────────────────┐
* │<Root viewportOffset> │
* │ ┌──────────────────────────┐│
* │ │<Modal viewportOffset> ││
* │ └──────────────────────────┘│
* └─────────────────────────────┘
*/
TEST(LayoutableShadowNodeTest, includeViewportOffsetOnRootKindNode) {
auto builder = simpleComponentBuilder();
auto modalShadowNode = std::shared_ptr<ModalHostViewShadowNode>{};

// clang-format off
auto element =
Element<RootShadowNode>()
.props([] {
auto sharedProps = std::make_shared<RootProps>();
sharedProps->layoutContext.viewportOffset = {.x=100, .y=200};
return sharedProps;
})
.children({
Element<ModalHostViewShadowNode>()
.reference(modalShadowNode)
.finalize([](ModalHostViewShadowNode &shadowNode){
auto layoutMetrics = EmptyLayoutMetrics;
layoutMetrics.frame.origin = {.x=10, .y=20};
layoutMetrics.frame.size = {.width=50, .height=60};
shadowNode.setLayoutMetrics(layoutMetrics);
shadowNode.setStateData(
ModalHostViewState{Size{}, Point{.x=30, .y=40}});
})
});
// clang-format on

auto rootShadowNode = builder.build(element);

// The modal is measured from an outside tree perspective, so it is offset by
// the viewport offset of the root, and not by its own one.
auto layoutMetrics = LayoutableShadowNode::computeRelativeLayoutMetrics(
modalShadowNode->getFamily(),
*rootShadowNode,
{.includeTransform = true, .includeViewportOffset = true});
EXPECT_EQ(layoutMetrics.frame.origin.x, 110);
EXPECT_EQ(layoutMetrics.frame.origin.y, 220);

// Its own viewport offset doesn't clip it either.
layoutMetrics = LayoutableShadowNode::computeRelativeLayoutMetrics(
modalShadowNode->getFamily(),
*rootShadowNode,
{.includeTransform = true,
.includeViewportOffset = true,
.enableOverflowClipping = true,
.clipSpecifiedAncestor = false});
EXPECT_EQ(layoutMetrics.frame.origin.x, 110);
EXPECT_EQ(layoutMetrics.frame.origin.y, 220);
EXPECT_EQ(layoutMetrics.frame.size.width, 50);
EXPECT_EQ(layoutMetrics.frame.size.height, 60);
}
#endif

/*
* ┌───────────────────────────────┐
* │ <View verticallyInverted> │
Expand Down
3 changes: 3 additions & 0 deletions scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -2768,12 +2768,15 @@ class facebook::react::ModalHostViewShadowNode : public facebook::react::Concret
public ModalHostViewShadowNode(const facebook::react::ShadowNode& sourceShadowNode, const facebook::react::ShadowNodeFragment& fragment);
public ModalHostViewShadowNode(const facebook::react::ShadowNodeFragment& fragment, const facebook::react::ShadowNodeFamily::Shared& family, facebook::react::ShadowNodeTraits traits);
public static constexpr facebook::react::ShadowNodeTraits BaseTraits();
public virtual facebook::react::Transform getTransform() const override;
}

class facebook::react::ModalHostViewState {
public ModalHostViewState();
public ModalHostViewState(const facebook::react::ModalHostViewState& previousState, folly::dynamic data);
public ModalHostViewState(facebook::react::Size screenSize_);
public ModalHostViewState(facebook::react::Size screenSize_, facebook::react::Point viewportOffset_);
public const facebook::react::Point viewportOffset;
public const facebook::react::Size screenSize;
public folly::dynamic getDynamic() const;
public using Shared = std::shared_ptr<const facebook::react::ModalHostViewState>;
Expand Down
3 changes: 3 additions & 0 deletions scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -4955,11 +4955,14 @@ class facebook::react::ModalHostViewShadowNode : public facebook::react::Concret
public ModalHostViewShadowNode(const facebook::react::ShadowNode& sourceShadowNode, const facebook::react::ShadowNodeFragment& fragment);
public ModalHostViewShadowNode(const facebook::react::ShadowNodeFragment& fragment, const facebook::react::ShadowNodeFamily::Shared& family, facebook::react::ShadowNodeTraits traits);
public static constexpr facebook::react::ShadowNodeTraits BaseTraits();
public virtual facebook::react::Transform getTransform() const override;
}

class facebook::react::ModalHostViewState {
public ModalHostViewState();
public ModalHostViewState(facebook::react::Size screenSize_);
public ModalHostViewState(facebook::react::Size screenSize_, facebook::react::Point viewportOffset_);
public const facebook::react::Point viewportOffset;
public const facebook::react::Size screenSize;
public using Shared = std::shared_ptr<const facebook::react::ModalHostViewState>;
}
Expand Down
3 changes: 3 additions & 0 deletions scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -1653,11 +1653,14 @@ class facebook::react::ModalHostViewShadowNode : public facebook::react::Concret
public ModalHostViewShadowNode(const facebook::react::ShadowNode& sourceShadowNode, const facebook::react::ShadowNodeFragment& fragment);
public ModalHostViewShadowNode(const facebook::react::ShadowNodeFragment& fragment, const facebook::react::ShadowNodeFamily::Shared& family, facebook::react::ShadowNodeTraits traits);
public static constexpr facebook::react::ShadowNodeTraits BaseTraits();
public virtual facebook::react::Transform getTransform() const override;
}

class facebook::react::ModalHostViewState {
public ModalHostViewState();
public ModalHostViewState(facebook::react::Size screenSize_);
public ModalHostViewState(facebook::react::Size screenSize_, facebook::react::Point viewportOffset_);
public const facebook::react::Point viewportOffset;
public const facebook::react::Size screenSize;
public using Shared = std::shared_ptr<const facebook::react::ModalHostViewState>;
}
Expand Down
Loading