Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.76] Add ability to override UpdateLayoutMetrics and VisualToMountChildrenInto #14188

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "add the new generated headers",
"packageName": "@react-native-windows/automation",
"email": "email not defined",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "add the new generated headers",
"packageName": "@react-native-windows/automation-channel",
"email": "email not defined",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "add the new generated headers",
"packageName": "@react-native-windows/automation-commands",
"email": "email not defined",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Add linter exception to generated headers for Office builds",
"packageName": "@react-native-windows/codegen",
"email": "email not defined",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Add ability to override UpdateLayoutMetrics and VisualToMountChildrenInto",
"packageName": "@react-native-windows/codegen",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Add ability to override UpdateLayoutMetrics and VisualToMountChildrenInto",
"packageName": "react-native-windows",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "add the new generated headers",
"packageName": "react-native-windows",
"email": "email not defined",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type FilesOutput = Map<string, string>;
const headerTemplate = `/*
* This file is auto-generated from ::_COMPONENT_NAME_::NativeComponent spec file in flow / TypeScript.
*/
// clang-format off
#pragma once

#include <NativeModules.h>
Expand Down Expand Up @@ -99,6 +100,13 @@ struct Base::_COMPONENT_NAME_:: {
m_props = newProps;
}

// UpdateLayoutMetrics will only be called if this method is overridden
virtual void UpdateLayoutMetrics(
const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
const winrt::Microsoft::ReactNative::LayoutMetrics &/*newLayoutMetrics*/,
const winrt::Microsoft::ReactNative::LayoutMetrics &/*oldLayoutMetrics*/) noexcept {
}

// UpdateState will only be called if this method is overridden
virtual void UpdateState(
const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
Expand Down Expand Up @@ -167,6 +175,13 @@ void Register::_COMPONENT_NAME_::NativeComponent(
userData->UpdateProps(view, newProps ? newProps.as<::_COMPONENT_NAME_::Props>() : nullptr, oldProps ? oldProps.as<::_COMPONENT_NAME_::Props>() : nullptr);
});

compBuilder.SetUpdateLayoutMetricsHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
const winrt::Microsoft::ReactNative::LayoutMetrics &newLayoutMetrics,
const winrt::Microsoft::ReactNative::LayoutMetrics &oldLayoutMetrics) noexcept {
auto userData = view.UserData().as<TUserData>();
userData->UpdateLayoutMetrics(view, newLayoutMetrics, oldLayoutMetrics);
});

builder.SetUpdateEventEmitterHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
const winrt::Microsoft::ReactNative::EventEmitter &eventEmitter) noexcept {
auto userData = view.UserData().as<TUserData>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const headerTemplate = `/*
* in a way that also verifies at compile time that the native module matches the interface required
* by the TurboModule JS spec.
*/
#pragma once`;
#pragma once
// clang-format off`;

const specTemplate = `::_MODULE_CUSTOM_TYPES_REFLECTION_::
struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {
Expand Down
1 change: 1 addition & 0 deletions packages/sample-apps/codegen/NativeMyModuleDataTypes.g.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* by the TurboModule JS spec.
*/
#pragma once
// clang-format off

#include <string>
#include <optional>
Expand Down
1 change: 1 addition & 0 deletions packages/sample-apps/codegen/NativeMyModuleSpec.g.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* by the TurboModule JS spec.
*/
#pragma once
// clang-format off

// #include "NativeMyModuleDataTypes.g.h" before this file to use the generated type definition
#include <NativeModules.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/*
* This file is auto-generated from DrawingIslandNativeComponent spec file in flow / TypeScript.
*/
// clang-format off
#pragma once

#include <NativeModules.h>
Expand Down Expand Up @@ -53,6 +54,13 @@ struct BaseDrawingIsland {
m_props = newProps;
}

// UpdateLayoutMetrics will only be called if this method is overridden
virtual void UpdateLayoutMetrics(
const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
const winrt::Microsoft::ReactNative::LayoutMetrics &/*newLayoutMetrics*/,
const winrt::Microsoft::ReactNative::LayoutMetrics &/*oldLayoutMetrics*/) noexcept {
}

// UpdateState will only be called if this method is overridden
virtual void UpdateState(
const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
Expand Down Expand Up @@ -117,6 +125,13 @@ void RegisterDrawingIslandNativeComponent(
userData->UpdateProps(view, newProps ? newProps.as<DrawingIslandProps>() : nullptr, oldProps ? oldProps.as<DrawingIslandProps>() : nullptr);
});

compBuilder.SetUpdateLayoutMetricsHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
const winrt::Microsoft::ReactNative::LayoutMetrics &newLayoutMetrics,
const winrt::Microsoft::ReactNative::LayoutMetrics &oldLayoutMetrics) noexcept {
auto userData = view.UserData().as<TUserData>();
userData->UpdateLayoutMetrics(view, newLayoutMetrics, oldLayoutMetrics);
});

builder.SetUpdateEventEmitterHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
const winrt::Microsoft::ReactNative::EventEmitter &eventEmitter) noexcept {
auto userData = view.UserData().as<TUserData>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/*
* This file is auto-generated from MovingLightNativeComponent spec file in flow / TypeScript.
*/
// clang-format off
#pragma once

#include <NativeModules.h>
Expand Down Expand Up @@ -94,6 +95,13 @@ struct BaseMovingLight {
m_props = newProps;
}

// UpdateLayoutMetrics will only be called if this method is overridden
virtual void UpdateLayoutMetrics(
const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
const winrt::Microsoft::ReactNative::LayoutMetrics &/*newLayoutMetrics*/,
const winrt::Microsoft::ReactNative::LayoutMetrics &/*oldLayoutMetrics*/) noexcept {
}

// UpdateState will only be called if this method is overridden
virtual void UpdateState(
const winrt::Microsoft::ReactNative::ComponentView &/*view*/,
Expand Down Expand Up @@ -170,6 +178,13 @@ void RegisterMovingLightNativeComponent(
userData->UpdateProps(view, newProps ? newProps.as<MovingLightProps>() : nullptr, oldProps ? oldProps.as<MovingLightProps>() : nullptr);
});

compBuilder.SetUpdateLayoutMetricsHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
const winrt::Microsoft::ReactNative::LayoutMetrics &newLayoutMetrics,
const winrt::Microsoft::ReactNative::LayoutMetrics &oldLayoutMetrics) noexcept {
auto userData = view.UserData().as<TUserData>();
userData->UpdateLayoutMetrics(view, newLayoutMetrics, oldLayoutMetrics);
});

builder.SetUpdateEventEmitterHandler([](const winrt::Microsoft::ReactNative::ComponentView &view,
const winrt::Microsoft::ReactNative::EventEmitter &eventEmitter) noexcept {
auto userData = view.UserData().as<TUserData>();
Expand Down
72 changes: 26 additions & 46 deletions vnext/Microsoft.ReactNative/Fabric/ComponentView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ struct RootComponentView;

namespace winrt::Microsoft::ReactNative::implementation {

ComponentView::ComponentView(facebook::react::Tag tag, winrt::Microsoft::ReactNative::ReactContext const &reactContext)
: m_tag(tag), m_reactContext(reactContext) {}

void ComponentView::MarkAsCustomComponent() noexcept {
m_customComponent = true;
ComponentView::ComponentView(
facebook::react::Tag tag,
winrt::Microsoft::ReactNative::ReactContext const &reactContext,
winrt::Microsoft::ReactNative::Composition::ReactCompositionViewComponentBuilder *builder)
: m_tag(tag), m_reactContext(reactContext) {
m_builder.copy_from(builder);
}

std::vector<facebook::react::ComponentDescriptorProvider>
Expand All @@ -52,18 +53,15 @@ void ComponentView::MountChildComponentView(
uint32_t index) noexcept {
m_children.InsertAt(index, childComponentView);
winrt::get_self<winrt::Microsoft::ReactNative::implementation::ComponentView>(childComponentView)->parent(*this);
if (m_mountChildComponentViewHandler) {
m_mountChildComponentViewHandler(*this, winrt::make<MountChildComponentViewArgs>(childComponentView, index));
if (m_builder && m_builder->MountChildComponentViewHandler()) {
m_builder->MountChildComponentViewHandler()(
*this, winrt::make<MountChildComponentViewArgs>(childComponentView, index));
}
if (m_mounted) {
winrt::get_self<winrt::Microsoft::ReactNative::implementation::ComponentView>(childComponentView)->onMounted();
}
}

void ComponentView::MountChildComponentViewHandler(const MountChildComponentViewDelegate &handler) noexcept {
m_mountChildComponentViewHandler = handler;
}

void ComponentView::onMounted() noexcept {
assert(!m_mounted);
m_mounted = true;
Expand All @@ -89,16 +87,14 @@ void ComponentView::Mounted(winrt::event_token const &token) noexcept {
void ComponentView::UnmountChildComponentView(
const winrt::Microsoft::ReactNative::ComponentView &childComponentView,
uint32_t index) noexcept {
if (m_mountChildComponentViewHandler) {
m_mountChildComponentViewHandler(*this, winrt::make<MountChildComponentViewArgs>(childComponentView, index));
if (m_builder && m_builder->UnmountChildComponentViewHandler()) {
m_builder->UnmountChildComponentViewHandler()(
*this, winrt::make<UnmountChildComponentViewArgs>(childComponentView, index));
}
m_children.RemoveAt(index);
winrt::get_self<winrt::Microsoft::ReactNative::implementation::ComponentView>(childComponentView)->parent(nullptr);
winrt::get_self<winrt::Microsoft::ReactNative::implementation::ComponentView>(childComponentView)->onUnmounted();
}
void ComponentView::UnmountChildComponentViewHandler(const UnmountChildComponentViewDelegate &handler) noexcept {
m_unmountChildComponentViewHandler = handler;
}

void ComponentView::onUnmounted() noexcept {
if (!m_mounted)
Expand Down Expand Up @@ -148,15 +144,11 @@ uint32_t UnmountChildComponentViewArgs::Index() const noexcept {
void ComponentView::updateProps(
facebook::react::Props::Shared const &props,
facebook::react::Props::Shared const &oldProps) noexcept {
if (m_updatePropsDelegate) {
m_updatePropsDelegate(*this, userProps(props), oldProps ? userProps(oldProps) : nullptr);
if (m_builder && m_builder->UpdatePropsHandler()) {
m_builder->UpdatePropsHandler()(*this, userProps(props), oldProps ? userProps(oldProps) : nullptr);
}
}

void ComponentView::UpdatePropsHandler(const UpdatePropsDelegate &handler) noexcept {
m_updatePropsDelegate = handler;
}

const winrt::Microsoft::ReactNative::IComponentProps ComponentView::userProps(
facebook::react::Props::Shared const &props) noexcept {
const auto &abiProps =
Expand All @@ -165,28 +157,20 @@ const winrt::Microsoft::ReactNative::IComponentProps ComponentView::userProps(
}

void ComponentView::updateEventEmitter(facebook::react::EventEmitter::Shared const &eventEmitter) noexcept {
if (m_updateEventEmitterHandler) {
m_updateEventEmitterHandler(*this, winrt::make<EventEmitter>(eventEmitter));
if (m_builder && m_builder->UpdateEventEmitterHandler()) {
m_builder->UpdateEventEmitterHandler()(*this, winrt::make<EventEmitter>(eventEmitter));
}
}

void ComponentView::UpdateEventEmitterHandler(const UpdateEventEmitterDelegate &handler) noexcept {
m_updateEventEmitterHandler = handler;
}

void ComponentView::updateState(
facebook::react::State::Shared const &state,
facebook::react::State::Shared const &oldState) noexcept {
// Avoid new-ing up a new AbiComponentState on every state change if we are not a custom component
if (m_updateStateDelegate) {
m_updateStateDelegate(*this, winrt::make<::Microsoft::ReactNative::AbiComponentState>(state));
if (m_builder && m_builder->UpdateStateHandler()) {
m_builder->UpdateStateHandler()(*this, winrt::make<::Microsoft::ReactNative::AbiComponentState>(state));
}
}

void ComponentView::UpdateStateHandler(const UpdateStateDelegate &handler) noexcept {
m_updateStateDelegate = handler;
}

LayoutMetricsChangedArgs::LayoutMetricsChangedArgs(
const winrt::Microsoft::ReactNative::LayoutMetrics &newLayoutMetrics,
const winrt::Microsoft::ReactNative::LayoutMetrics &oldLayoutMetrics)
Expand Down Expand Up @@ -216,6 +200,10 @@ void ComponentView::updateLayoutMetrics(
layoutMetrics.frame.size.height},
layoutMetrics.pointScaleFactor};

if (m_builder && m_builder->UpdateLayoutMetricsHandler()) {
m_builder->UpdateLayoutMetricsHandler()(*this, newMetrics, oldMetrics);
}

m_layoutMetrics = layoutMetrics;

m_layoutMetricsChangedEvent(*this, winrt::make<LayoutMetricsChangedArgs>(newMetrics, oldMetrics));
Expand All @@ -240,13 +228,9 @@ void ComponentView::LayoutMetricsChanged(winrt::event_token const &token) noexce
m_layoutMetricsChangedEvent.remove(token);
}

void ComponentView::FinalizeUpdateHandler(const UpdateFinalizerDelegate &handler) noexcept {
m_finalizeUpdateHandler = handler;
}

void ComponentView::FinalizeUpdates(winrt::Microsoft::ReactNative::ComponentViewUpdateMask updateMask) noexcept {
if (m_finalizeUpdateHandler) {
m_finalizeUpdateHandler(*this, updateMask);
if (m_builder && m_builder->FinalizeUpdateHandler()) {
m_builder->FinalizeUpdateHandler()(*this, updateMask);
}
}

Expand All @@ -257,13 +241,9 @@ facebook::react::Props::Shared ComponentView::props() noexcept {
return {};
}

void ComponentView::CustomCommandHandler(const HandleCommandDelegate &handler) noexcept {
m_customCommandHandler = handler;
}

void ComponentView::HandleCommand(const winrt::Microsoft::ReactNative::HandleCommandArgs &args) noexcept {
if (m_customCommandHandler) {
m_customCommandHandler(*this, args);
if (m_builder && m_builder->CustomCommandHandler()) {
m_builder->CustomCommandHandler()(*this, args);
}
}

Expand Down
Loading
Loading