forked from microsoft/react-native-windows
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFabricUIManagerModule.cpp
More file actions
440 lines (373 loc) · 18.6 KB
/
FabricUIManagerModule.cpp
File metadata and controls
440 lines (373 loc) · 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include <AsynchronousEventBeat.h>
#include <DynamicReader.h>
#include <DynamicWriter.h>
#include <Fabric/ComponentView.h>
#include <Fabric/Composition/CompositionUIService.h>
#include <Fabric/Composition/CompositionViewComponentView.h>
#include <Fabric/FabricUIManagerModule.h>
#include <Fabric/ReactNativeConfigProperties.h>
#include <Fabric/WindowsComponentDescriptorRegistry.h>
#include <ICompositionRootView.h>
#include <IReactContext.h>
#include <IReactRootView.h>
#include <JSI/jsi.h>
#include <SchedulerSettings.h>
#include <SynchronousEventBeat.h>
#include <UI.Xaml.Controls.h>
#include <react/components/rnwcore/ComponentDescriptors.h>
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
#include <react/renderer/core/DynamicPropsUtilities.h>
#include <react/renderer/core/EventBeat.h>
#include <react/renderer/runtimescheduler/RuntimeScheduler.h>
#include <react/renderer/scheduler/Scheduler.h>
#include <react/renderer/scheduler/SchedulerToolbox.h>
#include <react/utils/ContextContainer.h>
#include <react/utils/CoreFeatures.h>
#include <runtimeexecutor/ReactCommon/RuntimeExecutor.h>
#include <winrt/Windows.Graphics.Display.h>
#include <winrt/Windows.UI.Composition.Desktop.h>
#include "Unicode.h"
namespace Microsoft::ReactNative {
winrt::Microsoft::ReactNative::ReactPropertyId<
winrt::Microsoft::ReactNative::ReactNonAbiValue<std::shared_ptr<FabricUIManager>>>
FabicUIManagerProperty() noexcept {
winrt::Microsoft::ReactNative::ReactPropertyId<
winrt::Microsoft::ReactNative::ReactNonAbiValue<std::shared_ptr<FabricUIManager>>>
propId{L"ReactNative.Fabric", L"UIManager"};
return propId;
}
/*static*/ std::shared_ptr<FabricUIManager> FabricUIManager::FromProperties(
const winrt::Microsoft::ReactNative::ReactPropertyBag &props) {
return props.Get(FabicUIManagerProperty()).Value();
}
FabricUIManager::FabricUIManager() {
facebook::react::CoreFeatures::enablePropIteratorSetter = true;
}
FabricUIManager::~FabricUIManager() {}
void FabricUIManager::installFabricUIManager() noexcept {
std::shared_ptr<const facebook::react::ReactNativeConfig> config =
std::make_shared<const ReactNativeConfigProperties>(m_context);
std::lock_guard<std::mutex> schedulerLock(m_schedulerMutex);
facebook::react::ContextContainer::Shared contextContainer = std::make_shared<facebook::react::ContextContainer>();
// This allows access to our ReactContext from the contextContainer thats passed around the fabric codebase
contextContainer->insert("MSRN.ReactContext", m_context);
auto runtimeExecutor = SchedulerSettings::GetRuntimeExecutor(m_context.Properties());
auto toolbox = facebook::react::SchedulerToolbox{};
if (auto runtimeScheduler = SchedulerSettings::RuntimeSchedulerFromProperties(m_context.Properties())) {
contextContainer->insert("RuntimeScheduler", runtimeScheduler);
facebook::react::EventBeat::Factory synchronousBeatFactory =
[runtimeExecutor, context = m_context, runtimeScheduler](
facebook::react::EventBeat::SharedOwnerBox const &ownerBox) {
return std::make_unique<SynchronousEventBeat>(ownerBox, context, runtimeExecutor, runtimeScheduler);
};
toolbox.synchronousEventBeatFactory = synchronousBeatFactory;
} else {
facebook::react::EventBeat::Factory synchronousBeatFactory =
[runtimeExecutor, context = m_context](facebook::react::EventBeat::SharedOwnerBox const &ownerBox) {
return std::make_unique<AsynchronousEventBeat>(ownerBox, context, runtimeExecutor);
};
toolbox.synchronousEventBeatFactory = synchronousBeatFactory;
}
facebook::react::EventBeat::Factory asynchronousBeatFactory =
[runtimeExecutor, context = m_context](facebook::react::EventBeat::SharedOwnerBox const &ownerBox) {
return std::make_unique<AsynchronousEventBeat>(ownerBox, context, runtimeExecutor);
};
contextContainer->insert("ReactNativeConfig", config);
toolbox.contextContainer = contextContainer;
toolbox.componentRegistryFactory = [](facebook::react::EventDispatcher::Weak const &eventDispatcher,
facebook::react::ContextContainer::Shared const &contextContainer)
-> facebook::react::ComponentDescriptorRegistry::Shared {
auto providerRegistry =
WindowsComponentDescriptorRegistry::FromProperties(
contextContainer->at<winrt::Microsoft::ReactNative::ReactContext>("MSRN.ReactContext").Properties())
->GetProviderRegistry();
auto registry = providerRegistry->createComponentDescriptorRegistry({eventDispatcher, contextContainer});
auto mutableRegistry = std::const_pointer_cast<facebook::react::ComponentDescriptorRegistry>(registry);
mutableRegistry->setFallbackComponentDescriptor(
std::make_shared<facebook::react::UnimplementedNativeViewComponentDescriptor>(
facebook::react::ComponentDescriptorParameters{eventDispatcher, contextContainer, nullptr}));
return registry;
};
if (auto runtimeScheduler = SchedulerSettings::RuntimeSchedulerFromProperties(m_context.Properties())) {
toolbox.runtimeExecutor = [runtimeScheduler](std::function<void(facebook::jsi::Runtime & runtime)> &&callback) {
runtimeScheduler->scheduleWork(std::move(callback));
};
} else {
toolbox.runtimeExecutor = runtimeExecutor;
}
toolbox.asynchronousEventBeatFactory = asynchronousBeatFactory;
toolbox.backgroundExecutor = [context = m_context,
dispatcher = Mso::DispatchQueue::MakeLooperQueue()](std::function<void()> &&callback) {
if (context.UIDispatcher().HasThreadAccess()) {
callback();
return;
}
dispatcher.Post(std::move(callback));
};
m_scheduler = std::make_shared<facebook::react::Scheduler>(
toolbox, (/*animationDriver_ ? animationDriver_.get() :*/ nullptr), this);
m_surfaceManager = std::make_shared<facebook::react::SurfaceManager>(*m_scheduler);
}
const IComponentViewRegistry &FabricUIManager::GetViewRegistry() const noexcept {
return m_registry;
}
void FabricUIManager::startSurface(
const winrt::Microsoft::ReactNative::CompositionRootView &rootView,
facebook::react::SurfaceId surfaceId,
const std::string &moduleName,
const folly::dynamic &initialProps) noexcept {
m_surfaceRegistry.insert({surfaceId, {rootView.RootVisual(), rootView}});
m_context.UIDispatcher().Post([self = shared_from_this(), surfaceId]() {
auto &rootComponentViewDescriptor = self->m_registry.dequeueComponentViewWithComponentHandle(
facebook::react::RootShadowNode::Handle(), surfaceId, self->m_compContext);
self->m_surfaceRegistry.at(surfaceId).rootVisual.InsertAt(
static_cast<const CompositionBaseComponentView &>(*rootComponentViewDescriptor.view).OuterVisual(), 0);
});
facebook::react::LayoutContext context;
facebook::react::LayoutConstraints constraints;
context.pointScaleFactor = rootView.ScaleFactor();
context.fontSizeMultiplier = rootView.ScaleFactor();
constraints.minimumSize.height = rootView.Size().Height;
constraints.minimumSize.width = rootView.Size().Width;
constraints.maximumSize.height = rootView.Size().Height;
constraints.maximumSize.width = rootView.Size().Width;
constraints.layoutDirection = facebook::react::LayoutDirection::LeftToRight;
m_surfaceManager->startSurface(
surfaceId,
moduleName,
initialProps,
constraints, // layout constraints
context // layout context
);
}
void FabricUIManager::stopSurface(facebook::react::SurfaceId surfaceId) noexcept {
m_surfaceManager->stopSurface(surfaceId);
}
winrt::IInspectable FabricUIManager::GetUiaFragmentProvider(facebook::react::SurfaceId surfaceId) const noexcept {
if (auto strong = m_surfaceRegistry.at(surfaceId).wkRootView.get()) {
return strong.GetUiaProvider();
}
return nullptr;
}
facebook::react::Size FabricUIManager::measureSurface(
facebook::react::SurfaceId surfaceId,
const facebook::react::LayoutConstraints &layoutConstraints,
const facebook::react::LayoutContext &layoutContext) const noexcept {
return m_surfaceManager->measureSurface(surfaceId, layoutConstraints, layoutContext);
}
void FabricUIManager::constraintSurfaceLayout(
facebook::react::SurfaceId surfaceId,
const facebook::react::LayoutConstraints &layoutConstraints,
const facebook::react::LayoutContext &layoutContext) const noexcept {
m_surfaceManager->constraintSurfaceLayout(surfaceId, layoutConstraints, layoutContext);
}
void FabricUIManager::didMountComponentsWithRootTag(facebook::react::SurfaceId surfaceId) noexcept {}
void FabricUIManager::RCTPerformMountInstructions(
facebook::react::ShadowViewMutationList const &mutations,
// facebook::react::RCTComponentViewRegistry* registry,
// facebook::react::RCTMountingTransactionObserverCoordinator& observerCoordinator,
facebook::react::SurfaceId surfaceId) {
for (auto const &mutation : mutations) {
switch (mutation.type) {
case facebook::react::ShadowViewMutation::Create: {
auto &newChildShadowView = mutation.newChildShadowView;
auto &newChildViewDescriptor = m_registry.dequeueComponentViewWithComponentHandle(
newChildShadowView.componentHandle, newChildShadowView.tag, m_compContext);
// observerCoordinator.registerViewComponentDescriptor(newChildViewDescriptor, surfaceId);
break;
}
case facebook::react::ShadowViewMutation::Delete: {
auto &oldChildShadowView = mutation.oldChildShadowView;
auto &oldChildViewDescriptor = m_registry.componentViewDescriptorWithTag(oldChildShadowView.tag);
// observerCoordinator.unregisterViewComponentDescriptor(oldChildViewDescriptor, surfaceId);
m_registry.enqueueComponentViewWithComponentHandle(
oldChildShadowView.componentHandle, oldChildShadowView.tag, oldChildViewDescriptor);
break;
}
case facebook::react::ShadowViewMutation::Insert: {
auto &oldChildShadowView = mutation.oldChildShadowView;
auto &newChildShadowView = mutation.newChildShadowView;
auto &parentShadowView = mutation.parentShadowView;
auto &newChildViewDescriptor = m_registry.componentViewDescriptorWithTag(newChildShadowView.tag);
auto &parentViewDescriptor = m_registry.componentViewDescriptorWithTag(parentShadowView.tag);
auto &newChildComponentView = newChildViewDescriptor.view;
newChildComponentView->updateProps(newChildShadowView.props, oldChildShadowView.props);
newChildComponentView->updateEventEmitter(newChildShadowView.eventEmitter);
newChildComponentView->updateState(newChildShadowView.state, oldChildShadowView.state);
newChildComponentView->updateLayoutMetrics(newChildShadowView.layoutMetrics, oldChildShadowView.layoutMetrics);
newChildComponentView->finalizeUpdates(RNComponentViewUpdateMask::All);
parentViewDescriptor.view->mountChildComponentView(*newChildComponentView, mutation.index);
break;
}
case facebook::react::ShadowViewMutation::Remove: {
auto &oldChildShadowView = mutation.oldChildShadowView;
auto &parentShadowView = mutation.parentShadowView;
auto &oldChildViewDescriptor = m_registry.componentViewDescriptorWithTag(oldChildShadowView.tag);
auto &parentViewDescriptor = m_registry.componentViewDescriptorWithTag(parentShadowView.tag);
parentViewDescriptor.view->unmountChildComponentView(*oldChildViewDescriptor.view, mutation.index);
break;
}
case facebook::react::ShadowViewMutation::Update: {
auto &oldChildShadowView = mutation.oldChildShadowView;
auto &newChildShadowView = mutation.newChildShadowView;
auto &newChildViewDescriptor = m_registry.componentViewDescriptorWithTag(newChildShadowView.tag);
auto &newChildComponentView = newChildViewDescriptor.view;
auto mask = RNComponentViewUpdateMask{};
if (oldChildShadowView.props != newChildShadowView.props) {
newChildComponentView->updateProps(newChildShadowView.props, oldChildShadowView.props);
mask |= RNComponentViewUpdateMask::Props;
}
if (oldChildShadowView.eventEmitter != newChildShadowView.eventEmitter) {
newChildComponentView->updateEventEmitter(newChildShadowView.eventEmitter);
mask |= RNComponentViewUpdateMask::EventEmitter;
}
if (oldChildShadowView.state != newChildShadowView.state) {
newChildComponentView->updateState(newChildShadowView.state, oldChildShadowView.state);
mask |= RNComponentViewUpdateMask::State;
}
if (oldChildShadowView.layoutMetrics != newChildShadowView.layoutMetrics) {
newChildComponentView->updateLayoutMetrics(
newChildShadowView.layoutMetrics, oldChildShadowView.layoutMetrics);
mask |= RNComponentViewUpdateMask::LayoutMetrics;
}
if (mask != RNComponentViewUpdateMask::None) {
newChildComponentView->finalizeUpdates(mask);
}
break;
}
}
}
}
void FabricUIManager::performTransaction(facebook::react::MountingCoordinator::Shared const &mountingCoordinator) {
auto surfaceId = mountingCoordinator->getSurfaceId();
mountingCoordinator->getTelemetryController().pullTransaction(
[&](facebook::react::MountingTransaction const &transaction,
facebook::react::SurfaceTelemetry const &surfaceTelemetry) {
//[self.delegate mountingManager:self willMountComponentsWithRootTag:surfaceId];
// _observerCoordinator.notifyObserversMountingTransactionWillMount(transaction, surfaceTelemetry);
},
[&](facebook::react::MountingTransaction const &transaction,
facebook::react::SurfaceTelemetry const &surfaceTelemetry) {
RCTPerformMountInstructions(
transaction.getMutations(), /* _componentViewRegistry, _observerCoordinator,*/ surfaceId);
},
[&](facebook::react::MountingTransaction const &transaction,
facebook::react::SurfaceTelemetry const &surfaceTelemetry) {
//_observerCoordinator.notifyObserversMountingTransactionDidMount(transaction, surfaceTelemetry);
didMountComponentsWithRootTag(surfaceId);
//[self.delegate mountingManager:self didMountComponentsWithRootTag:surfaceId];
});
}
void FabricUIManager::initiateTransaction(facebook::react::MountingCoordinator::Shared mountingCoordinator) {
if (m_transactionInFlight) {
m_followUpTransactionRequired = true;
return;
}
do {
m_followUpTransactionRequired = false;
m_transactionInFlight = true;
performTransaction(mountingCoordinator);
m_transactionInFlight = false;
} while (m_followUpTransactionRequired);
}
void FabricUIManager::schedulerDidFinishTransaction(
const facebook::react::MountingCoordinator::Shared &mountingCoordinator) {
// Should cache this locally
if (m_context.UIDispatcher().HasThreadAccess()) {
initiateTransaction(mountingCoordinator);
} else {
m_context.UIDispatcher().Post(
[mountingCoordinator, self = shared_from_this()]() { self->initiateTransaction(mountingCoordinator); });
}
}
void FabricUIManager::schedulerDidRequestPreliminaryViewAllocation(
facebook::react::SurfaceId surfaceId,
const facebook::react::ShadowNode &shadowView) {
// iOS does not do this optimization, but Android does. It maybe that android's allocations are more expensive due to
// the Java boundary.
// TODO: We should do some perf tests to see if this is worth doing.
/* -- No sense doing this until we recycle views... since this just throws away the view currently
if (m_context.UIDispatcher().HasThreadAccess()) {
m_registry.dequeueComponentViewWithComponentHandle(shadowView.getComponentHandle(), surfaceId, m_compContext);
} else {
m_context.UIDispatcher().Post(
[componentHandle = shadowView.getComponentHandle(), surfaceId, self = shared_from_this()]() {
self->m_registry.dequeueComponentViewWithComponentHandle(componentHandle, surfaceId, self->m_compContext);
});
}
*/
}
void FabricUIManager::schedulerDidDispatchCommand(
facebook::react::ShadowView const &shadowView,
std::string const &commandName,
folly::dynamic const &arg) {
if (m_context.UIDispatcher().HasThreadAccess()) {
auto descriptor = m_registry.componentViewDescriptorWithTag(shadowView.tag);
descriptor.view->handleCommand(commandName, arg);
} else {
m_context.UIDispatcher().Post(
[wkThis = weak_from_this(), commandName, tag = shadowView.tag, args = folly::dynamic(arg)]() {
if (auto pThis = wkThis.lock()) {
auto view = pThis->m_registry.findComponentViewWithTag(tag);
if (view) {
view->handleCommand(commandName, args);
}
}
});
}
}
void FabricUIManager::schedulerDidSetIsJSResponder(
facebook::react::ShadowView const &shadowView,
bool isJSResponder,
bool blockNativeResponder) {}
void FabricUIManager::schedulerDidSendAccessibilityEvent(
const facebook::react::ShadowView &shadowView,
std::string const &eventType) {
assert(false);
}
void FabricUIManager::Initialize(winrt::Microsoft::ReactNative::ReactContext const &reactContext) noexcept {
m_context = reactContext;
m_compContext =
winrt::Microsoft::ReactNative::Composition::implementation::CompositionUIService::GetCompositionContext(
reactContext.Properties().Handle());
m_registry.Initialize(reactContext);
m_context.Properties().Set(FabicUIManagerProperty(), shared_from_this());
/*
EventBeatManager eventBeatManager = new EventBeatManager(mReactApplicationContext);
UIManagerModule nativeModule =
Assertions.assertNotNull(mReactApplicationContext.getNativeModule(UIManagerModule.class));
EventDispatcher eventDispatcher = nativeModule.getEventDispatcher();
FabricUIManager uiManager =
new FabricUIManager(
mReactApplicationContext,
nativeModule.getViewManagerRegistry_DO_NOT_USE(),
eventDispatcher,
eventBeatManager);
Binding binding = new Binding();
// TODO T31905686: remove this call
loadClasses();
MessageQueueThread jsMessageQueueThread =
mReactApplicationContext
.getCatalystInstance()
.getReactQueueConfiguration()
.getJSQueueThread();
*/
// binding.register( // This is register in java, which calls into Binding::installFabricUIManager
installFabricUIManager(
/*
mReactApplicationContext.getCatalystInstance().getRuntimeExecutor(),
uiManager,
eventBeatManager,
jsMessageQueueThread,
mComponentFactory,
mConfig
*/);
/*
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
return uiManager;
*/
}
} // namespace Microsoft::ReactNative