-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathTouchEventHandler.cpp
608 lines (519 loc) · 22.6 KB
/
TouchEventHandler.cpp
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include <pch.h>
#include <Views/ShadowNodeBase.h>
#include "TouchEventHandler.h"
#include <Modules/NativeUIManager.h>
#include <Modules/PaperUIManagerModule.h>
#include <UI.Xaml.Controls.h>
#include <UI.Xaml.Input.h>
#include <UI.Xaml.Media.h>
#include <Utils/ValueUtils.h>
#include <winrt/Windows.ApplicationModel.Core.h>
#include <winrt/Windows.Devices.Input.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.UI.Core.h>
#include <winrt/Windows.UI.Input.h>
#ifdef USE_WINUI3
#include <winrt/Microsoft.UI.Input.h>
namespace input = winrt::Microsoft::UI::Input;
#else
namespace input = winrt::Windows::UI::Input;
#endif
namespace Microsoft::ReactNative {
std::vector<int64_t> GetTagsForBranch(INativeUIManagerHost *host, int64_t tag, int64_t rootTag);
TouchEventHandler::TouchEventHandler(const Mso::React::IReactContext &context)
: m_xamlView(nullptr),
m_rootView(nullptr),
m_context(&context),
m_batchingEventEmitter{
std::make_shared<winrt::Microsoft::ReactNative::BatchingEventEmitter>(Mso::CntPtr(&context))} {}
TouchEventHandler::~TouchEventHandler() {
RemoveTouchHandlers();
}
void TouchEventHandler::AddTouchHandlers(XamlView xamlView, XamlView rootView, bool handledEventsToo) {
auto uiElement(xamlView.try_as<xaml::UIElement>());
if (uiElement == nullptr) {
assert(false);
return;
}
RemoveTouchHandlers();
m_xamlView = xamlView;
m_rootView = rootView != nullptr ? rootView : xamlView;
m_pressedHandler = winrt::box_value(winrt::PointerEventHandler{this, &TouchEventHandler::OnPointerPressed});
m_releasedHandler = winrt::box_value(winrt::PointerEventHandler{this, &TouchEventHandler::OnPointerReleased});
m_canceledHandler = winrt::box_value(winrt::PointerEventHandler{this, &TouchEventHandler::OnPointerCanceled});
m_captureLostHandler = winrt::box_value(winrt::PointerEventHandler{this, &TouchEventHandler::OnPointerCaptureLost});
m_exitedHandler = winrt::box_value(winrt::PointerEventHandler{this, &TouchEventHandler::OnPointerExited});
m_movedHandler = winrt::box_value(winrt::PointerEventHandler{this, &TouchEventHandler::OnPointerMoved});
uiElement.AddHandler(xaml::UIElement::PointerPressedEvent(), m_pressedHandler, handledEventsToo);
uiElement.AddHandler(xaml::UIElement::PointerReleasedEvent(), m_releasedHandler, handledEventsToo);
uiElement.AddHandler(xaml::UIElement::PointerCanceledEvent(), m_canceledHandler, handledEventsToo);
uiElement.AddHandler(xaml::UIElement::PointerCaptureLostEvent(), m_captureLostHandler, handledEventsToo);
uiElement.AddHandler(xaml::UIElement::PointerExitedEvent(), m_exitedHandler, handledEventsToo);
uiElement.AddHandler(xaml::UIElement::PointerMovedEvent(), m_movedHandler, handledEventsToo);
}
void TouchEventHandler::RemoveTouchHandlers() {
if (m_xamlView) {
auto uiElement(m_xamlView.as<xaml::UIElement>());
uiElement.RemoveHandler(xaml::UIElement::PointerPressedEvent(), m_pressedHandler);
uiElement.RemoveHandler(xaml::UIElement::PointerReleasedEvent(), m_releasedHandler);
uiElement.RemoveHandler(xaml::UIElement::PointerCanceledEvent(), m_canceledHandler);
uiElement.RemoveHandler(xaml::UIElement::PointerCaptureLostEvent(), m_captureLostHandler);
uiElement.RemoveHandler(xaml::UIElement::PointerExitedEvent(), m_exitedHandler);
uiElement.RemoveHandler(xaml::UIElement::PointerMovedEvent(), m_movedHandler);
m_pressedHandler = nullptr;
m_releasedHandler = nullptr;
m_canceledHandler = nullptr;
m_captureLostHandler = nullptr;
m_exitedHandler = nullptr;
m_movedHandler = nullptr;
m_rootView = nullptr;
m_xamlView = nullptr;
}
}
winrt::Microsoft::ReactNative::BatchingEventEmitter &TouchEventHandler::BatchingEmitter() noexcept {
return *m_batchingEventEmitter;
}
void TouchEventHandler::OnPointerPressed(
const winrt::IInspectable & /*sender*/,
const winrt::PointerRoutedEventArgs &args) {
// Short circuit all of this if we are in an error state
if (m_context->State() == Mso::React::ReactInstanceState::HasError)
return;
if (IndexOfPointerWithId(args.Pointer().PointerId()) != std::nullopt) {
// A pointer with this ID already exists
assert(false);
return;
}
// Only if the view has a Tag can we process this
std::vector<int64_t> tagsForBranch;
xaml::UIElement sourceElement(nullptr);
const auto eventType = TouchEventType::Start;
const auto kind = GetPointerEventKind(eventType);
const auto reactArgs = winrt::make<winrt::Microsoft::ReactNative::implementation::ReactPointerEventArgs>(kind, args);
if (!PropagatePointerEventAndFindReactSourceBranch(reactArgs, &tagsForBranch, &sourceElement))
return;
// If this was caused by the user pressing the "back" hardware button, fire that event instead
if (args.GetCurrentPoint(sourceElement).Properties().PointerUpdateKind() ==
input::PointerUpdateKind::XButton1Pressed) {
args.Handled(DispatchBackEvent());
return;
}
if (reactArgs.AllowUncaptured() || m_xamlView.as<xaml::FrameworkElement>().CapturePointer(args.Pointer())) {
assert(!tagsForBranch.empty());
const auto tag = tagsForBranch.front();
size_t pointerIndex = AddReactPointer(args, tag, sourceElement);
DispatchTouchEvent(eventType, pointerIndex);
}
}
void TouchEventHandler::OnPointerReleased(
const winrt::IInspectable & /*sender*/,
const winrt::PointerRoutedEventArgs &args) {
OnPointerConcluded(TouchEventType::End, args);
}
void TouchEventHandler::OnPointerCanceled(
const winrt::IInspectable & /*sender*/,
const winrt::PointerRoutedEventArgs &args) {
OnPointerConcluded(TouchEventType::Cancel, args);
}
void TouchEventHandler::OnPointerCaptureLost(
const winrt::IInspectable & /*sender*/,
const winrt::PointerRoutedEventArgs &args) {
OnPointerConcluded(TouchEventType::CaptureLost, args);
}
void TouchEventHandler::OnPointerExited(
const winrt::IInspectable & /*sender*/,
const winrt::PointerRoutedEventArgs &args) {
// Short circuit all of this if we are in an error state
if (m_context->State() == Mso::React::ReactInstanceState::HasError)
return;
std::vector<int64_t> tagsForBranch;
UpdatePointersInViews(args, nullptr, std::move(tagsForBranch));
}
void TouchEventHandler::OnPointerMoved(
const winrt::IInspectable & /*sender*/,
const winrt::PointerRoutedEventArgs &args) {
// Short circuit all of this if we are in an error state
if (m_context->State() == Mso::React::ReactInstanceState::HasError)
return;
// Only if the view has a Tag can we process this
std::vector<int64_t> tagsForBranch;
xaml::UIElement sourceElement(nullptr);
const auto eventType = TouchEventType::Move;
const auto kind = GetPointerEventKind(eventType);
const auto reactArgs = winrt::make<winrt::Microsoft::ReactNative::implementation::ReactPointerEventArgs>(kind, args);
const auto hasReactTarget = PropagatePointerEventAndFindReactSourceBranch(reactArgs, &tagsForBranch, &sourceElement);
const auto optPointerIndex = IndexOfPointerWithId(args.Pointer().PointerId());
if (optPointerIndex) {
UpdateReactPointer(
m_pointers[*optPointerIndex], args, hasReactTarget ? sourceElement : m_rootView.as<xaml::UIElement>());
DispatchTouchEvent(eventType, *optPointerIndex);
}
// If we re-introduce onMouseMove to react-native-windows, we should add an
// argument to ensure we do not emit these events while the pointer is down.
UpdatePointersInViews(args, sourceElement, std::move(tagsForBranch));
}
void TouchEventHandler::OnPointerConcluded(TouchEventType eventType, const winrt::PointerRoutedEventArgs &args) {
// Short circuit all of this if we are in an error state
if (m_context->State() == Mso::React::ReactInstanceState::HasError)
return;
auto optPointerIndex = IndexOfPointerWithId(args.Pointer().PointerId());
if (!optPointerIndex)
return;
// if the view has a Tag, update the pointer info.
// Regardless of that, ensure we Dispatch & cleanup the pointer
std::vector<int64_t> tagsForBranch;
xaml::UIElement sourceElement(nullptr);
const auto kind = GetPointerEventKind(eventType);
const auto reactArgs = winrt::make<winrt::Microsoft::ReactNative::implementation::ReactPointerEventArgs>(kind, args);
const auto hasReactTarget = PropagatePointerEventAndFindReactSourceBranch(reactArgs, &tagsForBranch, &sourceElement);
UpdateReactPointer(
m_pointers[*optPointerIndex], args, hasReactTarget ? sourceElement : m_rootView.as<xaml::UIElement>());
// In case a PointerCaptureLost event should be treated as an "end" event,
// check the ReactPointerEventArgs Kind property before emitting the event.
const auto adjustedEventType = reactArgs.Kind() == winrt::Microsoft::ReactNative::PointerEventKind::End
? TouchEventType::End
: TouchEventType::Cancel;
DispatchTouchEvent(adjustedEventType, *optPointerIndex);
m_pointers.erase(cbegin(m_pointers) + *optPointerIndex);
if (m_pointers.size() == 0)
m_touchId = 0;
m_xamlView.as<xaml::FrameworkElement>().ReleasePointerCapture(args.Pointer());
}
size_t TouchEventHandler::AddReactPointer(
const winrt::PointerRoutedEventArgs &args,
int64_t tag,
xaml::UIElement sourceElement) {
ReactPointer pointer = CreateReactPointer(args, tag, sourceElement);
m_pointers.emplace_back(std::move(pointer));
return m_pointers.size() - 1;
}
TouchEventHandler::ReactPointer TouchEventHandler::CreateReactPointer(
const winrt::PointerRoutedEventArgs &args,
int64_t tag,
xaml::UIElement sourceElement) {
auto point = args.GetCurrentPoint(sourceElement);
auto props = point.Properties();
ReactPointer pointer{};
pointer.target = tag;
pointer.identifier = m_touchId++;
pointer.pointerId = point.PointerId();
#ifndef USE_WINUI3
pointer.deviceType = point.PointerDevice().PointerDeviceType();
#else
pointer.deviceType = point.PointerDeviceType();
#endif
pointer.isLeftButton = props.IsLeftButtonPressed();
pointer.isRightButton = props.IsRightButtonPressed();
pointer.isMiddleButton = props.IsMiddleButtonPressed();
pointer.isHorizontalScrollWheel = props.IsHorizontalMouseWheel();
pointer.isEraser = props.IsEraser();
UpdateReactPointer(pointer, args, sourceElement);
return pointer;
}
void TouchEventHandler::UpdateReactPointer(
ReactPointer &pointer,
const winrt::PointerRoutedEventArgs &args,
xaml::UIElement sourceElement) {
auto rootPoint = args.GetCurrentPoint(m_rootView.as<xaml::FrameworkElement>());
auto point = args.GetCurrentPoint(sourceElement);
auto props = point.Properties();
auto keyModifiers = static_cast<uint32_t>(args.KeyModifiers());
pointer.positionRoot = rootPoint.Position();
pointer.positionView = point.Position();
pointer.timestamp = point.Timestamp() / 1000; // us -> ms
pointer.pressure = props.Pressure();
pointer.isBarrelButton = props.IsBarrelButtonPressed();
pointer.shiftKey = 0 != (keyModifiers & static_cast<uint32_t>(winrt::Windows::System::VirtualKeyModifiers::Shift));
pointer.ctrlKey = 0 != (keyModifiers & static_cast<uint32_t>(winrt::Windows::System::VirtualKeyModifiers::Control));
pointer.altKey = 0 != (keyModifiers & static_cast<uint32_t>(winrt::Windows::System::VirtualKeyModifiers::Menu));
}
std::optional<size_t> TouchEventHandler::IndexOfPointerWithId(uint32_t pointerId) {
for (size_t i = 0; i < m_pointers.size(); ++i) {
if (m_pointers[i].pointerId == pointerId)
return i;
}
return std::nullopt;
}
void TouchEventHandler::UpdatePointersInViews(
const winrt::PointerRoutedEventArgs &args,
xaml::UIElement sourceElement,
std::vector<int64_t> &&newViews) {
if (auto nativeUiManager = GetNativeUIManager(*m_context).lock()) {
auto puiManagerHost = nativeUiManager->getHost();
int32_t pointerId = args.Pointer().PointerId();
// m_pointers is tracking the pointers that are 'down', for moves we usually
// don't have any pointers down and should reset the touchId back to zero
if (m_pointers.size() == 0)
m_touchId = 0;
// Get the results of the last time we calculated the path
auto it = m_pointersInViews.find(pointerId);
TagSet *existingViews;
if (it != m_pointersInViews.end()) {
existingViews = &it->second;
} else {
existingViews = nullptr;
}
// Short-circuit if the hierarchy hasn't changed
if ((existingViews == nullptr && newViews.size() == 0) ||
(existingViews != nullptr && existingViews->orderedTags == newViews))
return;
// Prep to fire pointer events
std::unordered_set<int64_t> newViewsSet(newViews.begin(), newViews.end());
ReactPointer pointer;
auto optPointerIndex = IndexOfPointerWithId(pointerId);
if (optPointerIndex) {
pointer = m_pointers[*optPointerIndex];
UpdateReactPointer(pointer, args, sourceElement);
} else {
// newViews is empty when UpdatePointersInViews is called from outside
// the root view, in this case use -1 for the JS event pointer target
const auto tag = !newViews.empty() ? newViews.front() : InvalidTag;
pointer = CreateReactPointer(args, tag, sourceElement);
}
// Walk through existingViews from innermost to outer, firing mouseLeave events if they are not in newViews
if (existingViews) {
for (int64_t existingTag : existingViews->orderedTags) {
if (newViewsSet.count(existingTag)) {
continue;
}
ShadowNodeBase *node = static_cast<ShadowNodeBase *>(puiManagerHost->FindShadowNodeForTag(existingTag));
if (node != nullptr && node->m_onMouseLeaveRegistered)
BatchingEmitter().DispatchEvent(
existingTag,
L"topMouseLeave",
winrt::Microsoft::ReactNative::MakeJSValueWriter(GetPointerJson(pointer, existingTag)));
}
}
// Walk through newViews from outermost to inner, firing mouseEnter events if they are not in existingViews
for (auto iter = newViews.rbegin(); iter != newViews.rend(); ++iter) {
int64_t newTag = *iter;
if (existingViews && existingViews->tags.count(newTag)) {
continue;
}
ShadowNodeBase *node = static_cast<ShadowNodeBase *>(puiManagerHost->FindShadowNodeForTag(newTag));
if (node != nullptr && node->m_onMouseEnterRegistered)
BatchingEmitter().DispatchEvent(
newTag,
L"topMouseEnter",
winrt::Microsoft::ReactNative::MakeJSValueWriter(GetPointerJson(pointer, newTag)));
}
m_pointersInViews[pointerId] = {std::move(newViewsSet), std::move(newViews)};
}
}
// defines button payload, follows https://developer.mozilla.org/docs/Web/API/MouseEvent/button
enum class MouseEventButtonKind { None = -1, Main = 0, Auxiliary = 1, Secondary = 2, Eraser = 5 };
winrt::Microsoft::ReactNative::JSValue TouchEventHandler::GetPointerJson(const ReactPointer &pointer, int64_t target) {
MouseEventButtonKind button = MouseEventButtonKind::None;
if (pointer.isLeftButton) {
button = MouseEventButtonKind::Main;
} else if (pointer.isMiddleButton) {
button = MouseEventButtonKind::Auxiliary;
} else if (pointer.isRightButton || pointer.isBarrelButton) {
button = MouseEventButtonKind::Secondary;
} else if (pointer.isEraser) {
button = MouseEventButtonKind::Eraser;
}
return winrt::Microsoft::ReactNative::JSValueObject{
{"target", target},
{"identifier", pointer.identifier},
{"pageX", pointer.positionRoot.X},
{"pageY", pointer.positionRoot.Y},
{"locationX", pointer.positionView.X},
{"locationY", pointer.positionView.Y},
{"timestamp", pointer.timestamp},
{
"pointerType",
GetPointerDeviceTypeName(pointer.deviceType),
},
{"force", pointer.pressure},
{"isLeftButton", pointer.isLeftButton},
{"isRightButton", pointer.isRightButton},
{"isMiddleButton", pointer.isMiddleButton},
{"isBarrelButtonPressed", pointer.isBarrelButton},
{"isHorizontalScrollWheel", pointer.isHorizontalScrollWheel},
{"isEraser", pointer.isEraser},
{"shiftKey", pointer.shiftKey},
{"ctrlKey", pointer.ctrlKey},
{"button", static_cast<int>(button)},
{"altKey", pointer.altKey}};
}
void TouchEventHandler::DispatchTouchEvent(TouchEventType eventType, size_t pointerIndex) {
winrt::Microsoft::ReactNative::JSValueArray changedIndices;
changedIndices.push_back(pointerIndex);
winrt::Microsoft::ReactNative::JSValueArray touches;
for (const auto &pointer : m_pointers) {
touches.push_back(GetPointerJson(pointer, pointer.target));
}
// Package up parameters and invoke the JS event emitter
const wchar_t *eventName = GetTouchEventTypeName(eventType);
if (eventName == nullptr)
return;
const auto paramsWriter = MakeJSValueArgWriter(eventName, std::move(touches), std::move(changedIndices));
if (eventType == TouchEventType::Move || eventType == TouchEventType::PointerMove) {
BatchingEmitter().EmitCoalescingJSEvent(
L"RCTEventEmitter", L"receiveTouches", std::move(eventName), m_pointers[pointerIndex].pointerId, paramsWriter);
} else {
BatchingEmitter().EmitJSEvent(L"RCTEventEmitter", L"receiveTouches", paramsWriter);
}
}
bool TouchEventHandler::DispatchBackEvent() {
if (m_context->State() != Mso::React::ReactInstanceState::Loaded)
return false;
BatchingEmitter().EmitJSEvent(
L"RCTDeviceEventEmitter", L"emit", winrt::Microsoft::ReactNative::MakeJSValueArgWriter(L"hardwareBackPress"));
return true;
}
const char *TouchEventHandler::GetPointerDeviceTypeName(PointerDeviceType deviceType) noexcept {
const char *deviceTypeName = "unknown";
switch (deviceType) {
case PointerDeviceType::Mouse:
deviceTypeName = "mouse";
break;
case PointerDeviceType::Pen:
deviceTypeName = "pen";
break;
case PointerDeviceType::Touch:
deviceTypeName = "touch";
break;
default:
break;
}
return deviceTypeName;
}
winrt::Microsoft::ReactNative::PointerEventKind TouchEventHandler::GetPointerEventKind(
TouchEventType eventType) noexcept {
auto kind = winrt::Microsoft::ReactNative::PointerEventKind::None;
switch (eventType) {
case TouchEventType::Start:
kind = winrt::Microsoft::ReactNative::PointerEventKind::Start;
break;
case TouchEventType::End:
kind = winrt::Microsoft::ReactNative::PointerEventKind::End;
break;
case TouchEventType::Move:
kind = winrt::Microsoft::ReactNative::PointerEventKind::Move;
break;
case TouchEventType::Cancel:
kind = winrt::Microsoft::ReactNative::PointerEventKind::Cancel;
break;
case TouchEventType::CaptureLost:
kind = winrt::Microsoft::ReactNative::PointerEventKind::CaptureLost;
break;
default:
assert(false);
break;
}
return kind;
}
const wchar_t *TouchEventHandler::GetTouchEventTypeName(TouchEventType eventType) noexcept {
const wchar_t *eventName = nullptr;
switch (eventType) {
case TouchEventType::Start:
eventName = L"topTouchStart";
break;
case TouchEventType::End:
eventName = L"topTouchEnd";
break;
case TouchEventType::Move:
eventName = L"topTouchMove";
break;
case TouchEventType::Cancel:
case TouchEventType::CaptureLost:
eventName = L"topTouchCancel";
break;
default:
assert(false);
break;
}
return eventName;
}
bool TouchEventHandler::PropagatePointerEventAndFindReactSourceBranch(
const winrt::Microsoft::ReactNative::ReactPointerEventArgs &args,
std::vector<int64_t> *pTagsForBranch,
xaml::UIElement *pSourceElement) {
assert(pTagsForBranch != nullptr);
assert(pSourceElement != nullptr);
if (const auto uiManager = GetNativeUIManager(*m_context).lock()) {
xaml::UIElement sourceElement = args.Args().OriginalSource().try_as<xaml::UIElement>();
ShadowNodeBase *node = nullptr;
std::vector<int64_t> tagsForBranch;
// Find the "deepest" React element that triggered the input event
while (sourceElement) {
node = static_cast<ShadowNodeBase *>(uiManager->getHost()->FindShadowNodeForTag(GetTag(sourceElement)));
if (node) {
args.Target(sourceElement);
break;
} else {
sourceElement = winrt::VisualTreeHelper::GetParent(sourceElement).try_as<xaml::UIElement>();
}
}
// Walk to root to find refined React target view
const auto argsImpl = winrt::get_self<winrt::Microsoft::ReactNative::implementation::ReactPointerEventArgs>(args);
while (node) {
if (args.Target() == nullptr) {
args.Target(node->GetView());
}
const auto previousTarget = args.Target();
node->GetViewManager()->OnPointerEvent(node, args);
const auto target = args.Target().try_as<XamlView>();
if (target != previousTarget) {
tagsForBranch.clear();
if (target) {
// We assume that if a ViewManager is going to change the target, it
// can only update the target to one of its descendants.
const auto tagsToCurrentTarget = GetTagsForBranch(uiManager->getHost(), GetTag(target), node->m_tag);
for (auto tag : tagsToCurrentTarget) {
tagsForBranch.push_back(tag);
}
}
}
if (target) {
tagsForBranch.push_back(node->m_tag);
}
// Stop traversing when we get to the root target
if (node->GetView() == m_xamlView) {
break;
}
node = static_cast<ShadowNodeBase *>(uiManager->getHost()->FindShadowNodeForTag(node->m_parent));
}
if (args.Target() != nullptr) {
sourceElement = args.Target().try_as<xaml::UIElement>();
// Find the first parent UIElement of the React target for pointer positioning
if (!sourceElement) {
node = static_cast<ShadowNodeBase *>(uiManager->getHost()->FindShadowNodeForTag(tagsForBranch.front()));
}
while (!sourceElement && node) {
node = static_cast<ShadowNodeBase *>(uiManager->getHost()->FindShadowNodeForTag(node->m_parent));
sourceElement = node->GetView().try_as<xaml::UIElement>();
}
if (sourceElement) {
*pTagsForBranch = std::move(tagsForBranch);
*pSourceElement = sourceElement;
return true;
}
}
}
// If the root view is not fully created, then the Tag property will never
// be set. This can happen, e.g., when the red box error box is shown.
return false;
}
//
// Retrieves the path of nodes from an element to the root.
// The order of the returned list is from child to parent.
//
std::vector<int64_t> GetTagsForBranch(INativeUIManagerHost *host, int64_t tag, int64_t rootTag) {
std::vector<int64_t> tags;
auto *shadowNode = host->FindShadowNodeForTag(tag);
while (shadowNode != nullptr && tag != InvalidTag) {
if (tag == rootTag) {
break;
}
tags.push_back(tag);
tag = shadowNode->m_parent;
shadowNode = host->FindShadowNodeForTag(tag);
}
return tags;
}
} // namespace Microsoft::ReactNative