Skip to content

Commit b54b8b2

Browse files
authored
SetProperties is not exposed on ReactNativeIsland (#14517)
* Add SetAppProperties method to ReactNativeIsland * Change files * rename SetAppProperties to SetProperties * change file * fix * fix
1 parent ec02bf4 commit b54b8b2

File tree

11 files changed

+113
-18
lines changed

11 files changed

+113
-18
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Add SetProperties method to ReactNativeIsland",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/playground/windows/playground-composition/Playground-Composition.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,15 @@ struct WindowData {
368368
}
369369
m_forceRTL = !m_forceRTL;
370370
}
371+
case IDM_SETPROPS: {
372+
m_compRootView.SetProperties([](const winrt::Microsoft::ReactNative::IJSValueWriter &writer) {
373+
static int value = 123;
374+
writer.WriteObjectBegin();
375+
winrt::Microsoft::ReactNative::WriteProperty(writer, L"testProp1", value++);
376+
winrt::Microsoft::ReactNative::WriteProperty(writer, L"testProp2", L"value2");
377+
writer.WriteObjectEnd();
378+
});
379+
}
371380
}
372381

373382
return 0;

packages/playground/windows/playground-composition/Playground-Composition.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ BEGIN
5959
MENUITEM "&Refresh\tF5", IDM_REFRESH
6060
MENUITEM "&Unload", IDM_UNLOAD
6161
MENUITEM "Toggle Layout &Direction", IDM_TOGGLE_LAYOUT_DIRECTION
62+
MENUITEM "Set Props to running Island", IDM_SETPROPS
6263
MENUITEM SEPARATOR
6364
MENUITEM "&Settings...\tAlt+S", IDM_SETTINGS
6465
MENUITEM SEPARATOR

packages/playground/windows/playground-composition/resource.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define IDC_SIZETOCONTENT 112
2727
#define IDM_UNLOAD 113
2828
#define IDM_TOGGLE_LAYOUT_DIRECTION 114
29+
#define IDM_SETPROPS 115
2930
#define IDI_ICON1 1008
3031

3132
// Next default values for new objects

vnext/Microsoft.ReactNative/Fabric/Composition/ReactNativeIsland.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ void ReactNativeIsland::ReactViewHost(winrt::Microsoft::ReactNative::IReactViewH
195195
return;
196196
}
197197

198+
m_props = nullptr;
199+
198200
if (m_reactViewHost) {
199201
UninitRootView();
200202
m_reactViewHost.DetachViewInstance();
@@ -577,7 +579,9 @@ void ReactNativeIsland::ShowInstanceLoaded() noexcept {
577579
winrt::Microsoft::ReactNative::ReactPropertyBag(m_context.Properties()));
578580

579581
m_rootTag = ::Microsoft::ReactNative::getNextRootViewTag();
580-
auto initProps = DynamicWriter::ToDynamic(Mso::Copy(m_reactViewOptions.InitialProps()));
582+
583+
auto initProps =
584+
m_props.isNull() ? m_props : DynamicWriter::ToDynamic(Mso::Copy(m_reactViewOptions.InitialProps()));
581585
if (initProps.isNull()) {
582586
initProps = folly::dynamic::object();
583587
}
@@ -965,6 +969,24 @@ void ReactNativeIsland::OnUnmounted() noexcept {
965969
}
966970
}
967971

972+
void ReactNativeIsland::SetProperties(winrt::Microsoft::ReactNative::JSValueArgWriter props) noexcept {
973+
auto initProps = DynamicWriter::ToDynamic(props);
974+
if (initProps.isNull()) {
975+
initProps = folly::dynamic::object();
976+
}
977+
978+
if (m_isJSViewAttached) {
979+
if (auto fabricuiManager = ::Microsoft::ReactNative::FabricUIManager::FromProperties(
980+
winrt::Microsoft::ReactNative::ReactPropertyBag(m_context.Properties()))) {
981+
initProps["concurrentRoot"] = true;
982+
fabricuiManager->setProps(static_cast<facebook::react::SurfaceId>(m_rootTag), initProps);
983+
return;
984+
}
985+
}
986+
987+
m_props = initProps;
988+
}
989+
968990
winrt::com_ptr<winrt::Microsoft::ReactNative::Composition::implementation::RootComponentView>
969991
ReactNativeIsland::GetComponentView() noexcept {
970992
if (auto portal = m_portal.get()) {

vnext/Microsoft.ReactNative/Fabric/Composition/ReactNativeIsland.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ struct ReactNativeIsland
7676
float ScaleFactor() noexcept;
7777
void ScaleFactor(float value) noexcept;
7878

79+
void SetProperties(winrt::Microsoft::ReactNative::JSValueArgWriter props) noexcept;
80+
7981
float FontSizeMultiplier() const noexcept;
8082

8183
winrt::event_token SizeChanged(
@@ -159,6 +161,9 @@ struct ReactNativeIsland
159161
IReactDispatcher m_uiDispatcher{nullptr};
160162
winrt::IInspectable m_uiaProvider{nullptr};
161163

164+
// If SetProps is called before the surface is loaded, store it locally to use on start
165+
folly::dynamic m_props;
166+
162167
// This is the surfaceId that this island belongs to.
163168
// In the case of portal content root, this will be the surfaceId that contains the portal.
164169
int64_t m_rootTag{-1};

vnext/Microsoft.ReactNative/Fabric/FabricUIManagerModule.cpp

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ void FabricUIManager::installFabricUIManager() noexcept {
107107

108108
m_scheduler = std::make_shared<facebook::react::Scheduler>(
109109
toolbox, (/*animationDriver_ ? animationDriver_.get() :*/ nullptr), this);
110-
m_surfaceManager = std::make_shared<facebook::react::SurfaceManager>(*m_scheduler);
111110
}
112111

113112
const IComponentViewRegistry &FabricUIManager::GetViewRegistry() const noexcept {
@@ -135,17 +134,38 @@ void FabricUIManager::startSurface(
135134
layoutContext.pointScaleFactor = rootView.ScaleFactor();
136135
layoutContext.fontSizeMultiplier = rootView.FontSizeMultiplier();
137136

138-
m_surfaceManager->startSurface(
139-
surfaceId,
140-
moduleName,
141-
initialProps,
142-
layoutConstraints,
143-
layoutContext // layout context
144-
);
137+
{
138+
std::unique_lock lock(m_handlerMutex);
139+
auto surfaceHandler = facebook::react::SurfaceHandler{moduleName, surfaceId};
140+
surfaceHandler.setContextContainer(m_scheduler->getContextContainer());
141+
m_handlerRegistry.emplace(surfaceId, std::move(surfaceHandler));
142+
}
143+
144+
visit(surfaceId, [&](const facebook::react::SurfaceHandler &surfaceHandler) {
145+
surfaceHandler.setProps(initialProps);
146+
surfaceHandler.constraintLayout(layoutConstraints, layoutContext);
147+
m_scheduler->registerSurface(surfaceHandler);
148+
surfaceHandler.start();
149+
});
150+
}
151+
152+
void FabricUIManager::setProps(facebook::react::SurfaceId surfaceId, const folly::dynamic &props) const noexcept {
153+
visit(surfaceId, [=](const facebook::react::SurfaceHandler &surfaceHandler) { surfaceHandler.setProps(props); });
145154
}
146155

147156
void FabricUIManager::stopSurface(facebook::react::SurfaceId surfaceId) noexcept {
148-
m_surfaceManager->stopSurface(surfaceId);
157+
visit(surfaceId, [&](const facebook::react::SurfaceHandler &surfaceHandler) {
158+
surfaceHandler.stop();
159+
m_scheduler->unregisterSurface(surfaceHandler);
160+
});
161+
162+
{
163+
std::unique_lock lock(m_handlerMutex);
164+
165+
auto iterator = m_handlerRegistry.find(surfaceId);
166+
m_handlerRegistry.erase(iterator);
167+
}
168+
149169
auto &rootDescriptor = m_registry.componentViewDescriptorWithTag(surfaceId);
150170
rootDescriptor.view.as<winrt::Microsoft::ReactNative::Composition::implementation::RootComponentView>()->stop();
151171
m_registry.enqueueComponentViewWithComponentHandle(
@@ -156,14 +176,36 @@ facebook::react::Size FabricUIManager::measureSurface(
156176
facebook::react::SurfaceId surfaceId,
157177
const facebook::react::LayoutConstraints &layoutConstraints,
158178
const facebook::react::LayoutContext &layoutContext) const noexcept {
159-
return m_surfaceManager->measureSurface(surfaceId, layoutConstraints, layoutContext);
179+
auto size = facebook::react::Size{};
180+
181+
visit(surfaceId, [&](const facebook::react::SurfaceHandler &surfaceHandler) {
182+
size = surfaceHandler.measure(layoutConstraints, layoutContext);
183+
});
184+
185+
return size;
160186
}
161187

162188
void FabricUIManager::constraintSurfaceLayout(
163189
facebook::react::SurfaceId surfaceId,
164190
const facebook::react::LayoutConstraints &layoutConstraints,
165191
const facebook::react::LayoutContext &layoutContext) const noexcept {
166-
m_surfaceManager->constraintSurfaceLayout(surfaceId, layoutConstraints, layoutContext);
192+
visit(surfaceId, [=](const facebook::react::SurfaceHandler &surfaceHandler) {
193+
surfaceHandler.constraintLayout(layoutConstraints, layoutContext);
194+
});
195+
}
196+
197+
void FabricUIManager::visit(
198+
facebook::react::SurfaceId surfaceId,
199+
const std::function<void(const facebook::react::SurfaceHandler &surfaceHandler)> &callback) const noexcept {
200+
std::shared_lock lock(m_handlerMutex);
201+
202+
auto iterator = m_handlerRegistry.find(surfaceId);
203+
204+
if (iterator == m_handlerRegistry.end()) {
205+
return;
206+
}
207+
208+
callback(iterator->second);
167209
}
168210

169211
winrt::Microsoft::ReactNative::ReactNotificationId<facebook::react::SurfaceId>

vnext/Microsoft.ReactNative/Fabric/FabricUIManagerModule.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
#include <NativeModules.h>
77
#include <React.h>
88
#include <react/renderer/scheduler/SchedulerDelegate.h>
9-
#include <react/renderer/scheduler/SurfaceManager.h>
109
#include <winrt/Windows.UI.Composition.h>
1110
#include "Composition/ComponentViewRegistry.h"
1211

1312
namespace facebook::react {
1413
class Scheduler;
1514
class ReactNativeConfig;
15+
class SurfaceHandler;
1616
} // namespace facebook::react
1717

1818
namespace Microsoft::ReactNative {
@@ -47,6 +47,8 @@ struct FabricUIManager final : public std::enable_shared_from_this<FabricUIManag
4747
const facebook::react::LayoutConstraints &layoutConstraints,
4848
const facebook::react::LayoutContext &layoutContext) const noexcept;
4949

50+
void setProps(facebook::react::SurfaceId surfaceId, const folly::dynamic &props) const noexcept;
51+
5052
const IComponentViewRegistry &GetViewRegistry() const noexcept;
5153

5254
static winrt::Microsoft::ReactNative::ReactNotificationId<facebook::react::SurfaceId> NotifyMountedId() noexcept;
@@ -62,10 +64,13 @@ struct FabricUIManager final : public std::enable_shared_from_this<FabricUIManag
6264
facebook::react::SurfaceId surfaceId);
6365
void didMountComponentsWithRootTag(facebook::react::SurfaceId surfaceId) noexcept;
6466

67+
void visit(
68+
facebook::react::SurfaceId surfaceId,
69+
const std::function<void(const facebook::react::SurfaceHandler &surfaceHandler)> &callback) const noexcept;
70+
6571
winrt::Microsoft::ReactNative::ReactContext m_context;
6672
winrt::Microsoft::ReactNative::Composition::Experimental::ICompositionContext m_compContext;
6773
std::shared_ptr<facebook::react::Scheduler> m_scheduler;
68-
std::shared_ptr<facebook::react::SurfaceManager> m_surfaceManager;
6974
std::mutex m_schedulerMutex; // Protect m_scheduler
7075
bool m_transactionInFlight{false};
7176
bool m_followUpTransactionRequired{false};
@@ -77,6 +82,9 @@ struct FabricUIManager final : public std::enable_shared_from_this<FabricUIManag
7782

7883
std::unordered_map<facebook::react::SurfaceId, SurfaceInfo> m_surfaceRegistry;
7984

85+
std::unordered_map<facebook::react::SurfaceId, facebook::react::SurfaceHandler> m_handlerRegistry{};
86+
mutable std::shared_mutex m_handlerMutex;
87+
8088
// Inherited via SchedulerDelegate
8189
virtual void schedulerDidFinishTransaction(
8290
const std::shared_ptr<const facebook::react::MountingCoordinator> &mountingCoordinator) override;

vnext/Microsoft.ReactNative/ReactNativeIsland.idl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ namespace Microsoft.ReactNative
119119
Microsoft.ReactNative.Composition.Theme Theme { get; };
120120
Int64 RootTag { get; };
121121

122+
DOC_STRING("Initial props should be set on ReactViewHost. This is used to update props after the initial props are set")
123+
void SetProperties(JSValueArgWriter props);
124+
122125
#ifdef USE_WINUI3
123126
Microsoft.UI.Content.ContentIsland Island { get; };
124127
#endif

vnext/Shared/Shared.vcxitems

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,6 @@
651651
<ClCompile Include="$(ReactNativeDir)\ReactCommon\react\renderer\observers\events\EventPerformanceLogger.cpp" />
652652
<ClCompile Include="$(ReactNativeDir)\ReactCommon\react\renderer\scheduler\Scheduler.cpp" />
653653
<ClCompile Include="$(ReactNativeDir)\ReactCommon\react\renderer\scheduler\SurfaceHandler.cpp" />
654-
<ClCompile Include="$(ReactNativeDir)\ReactCommon\react\renderer\scheduler\SurfaceManager.cpp" />
655654
<ClCompile Include="$(ReactNativeDir)\ReactCommon\react\renderer\telemetry\SurfaceTelemetry.cpp" />
656655
<ClCompile Include="$(ReactNativeDir)\ReactCommon\react\renderer\telemetry\TransactionTelemetry.cpp" />
657656
<ClCompile Include="$(ReactNativeDir)\ReactCommon\react\renderer\textlayoutmanager\TextMeasureCache.cpp" />

0 commit comments

Comments
 (0)