basalt-win32 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/LICENSE +21 -0
  2. package/cli/runWindows.js +35 -0
  3. package/cli/shared.js +75 -0
  4. package/native/CMakeLists.txt +429 -0
  5. package/native/tests/test_win32_accessibility.cpp +585 -0
  6. package/native/tests/test_win32_animated_image.cpp +351 -0
  7. package/native/tests/test_win32_clipboard.cpp +121 -0
  8. package/native/tests/test_win32_focus.cpp +195 -0
  9. package/native/tests/test_win32_hittest.cpp +750 -0
  10. package/native/tests/test_win32_image.cpp +370 -0
  11. package/native/tests/test_win32_imageloader.cpp +208 -0
  12. package/native/tests/test_win32_input.cpp +511 -0
  13. package/native/tests/test_win32_key_events.cpp +84 -0
  14. package/native/tests/test_win32_mounting.cpp +757 -0
  15. package/native/tests/test_win32_paint.cpp +1799 -0
  16. package/native/tests/test_win32_scrollview.cpp +647 -0
  17. package/native/tests/test_win32_selection.cpp +215 -0
  18. package/native/tests/test_win32_text.cpp +1456 -0
  19. package/native/tests/test_win32_textinput.cpp +1594 -0
  20. package/native/tests/test_win32_titlebar.cpp +111 -0
  21. package/native/tests/test_win32_titlebar_actions.cpp +62 -0
  22. package/native/tests/test_win32_view.cpp +222 -0
  23. package/native/win32/ColorSchemeWin32.cpp +56 -0
  24. package/native/win32/ComponentRegistryWin32.cpp +156 -0
  25. package/native/win32/DirectWriteLayout.cpp +485 -0
  26. package/native/win32/DirectWriteLayout.h +77 -0
  27. package/native/win32/DirectWriteLayoutManager.cpp +154 -0
  28. package/native/win32/FontRegistryDirectWrite.cpp +117 -0
  29. package/native/win32/PlatformServicesWin32.cpp +744 -0
  30. package/native/win32/RnWin32Accessible.cpp +343 -0
  31. package/native/win32/RnWin32Accessible.h +130 -0
  32. package/native/win32/RnWin32Image.cpp +588 -0
  33. package/native/win32/RnWin32Image.h +139 -0
  34. package/native/win32/RnWin32TextLayout.cpp +1379 -0
  35. package/native/win32/RnWin32TextLayout.h +390 -0
  36. package/native/win32/RnWin32View.cpp +2956 -0
  37. package/native/win32/RnWin32View.h +926 -0
  38. package/native/win32/Win32AnimationChoreographer.cpp +76 -0
  39. package/native/win32/Win32AnimationChoreographer.h +61 -0
  40. package/native/win32/Win32Clip.h +209 -0
  41. package/native/win32/Win32Cursors.cpp +309 -0
  42. package/native/win32/Win32Cursors.h +121 -0
  43. package/native/win32/Win32DropTarget.cpp +567 -0
  44. package/native/win32/Win32DropTarget.h +42 -0
  45. package/native/win32/Win32Focus.cpp +262 -0
  46. package/native/win32/Win32Focus.h +109 -0
  47. package/native/win32/Win32ImageLoader.cpp +199 -0
  48. package/native/win32/Win32ImageLoader.h +133 -0
  49. package/native/win32/Win32InputScopes.h +92 -0
  50. package/native/win32/Win32KeyEvents.cpp +126 -0
  51. package/native/win32/Win32KeyEvents.h +20 -0
  52. package/native/win32/Win32MenuBar.cpp +382 -0
  53. package/native/win32/Win32MenuBar.h +16 -0
  54. package/native/win32/Win32MountingManager.cpp +1282 -0
  55. package/native/win32/Win32MountingManager.h +243 -0
  56. package/native/win32/Win32Offscreen.h +53 -0
  57. package/native/win32/Win32Packaging.cpp +161 -0
  58. package/native/win32/Win32Packaging.h +56 -0
  59. package/native/win32/Win32RunLoopObserver.cpp +48 -0
  60. package/native/win32/Win32RunLoopObserver.h +42 -0
  61. package/native/win32/Win32ScrollView.cpp +718 -0
  62. package/native/win32/Win32ScrollView.h +256 -0
  63. package/native/win32/Win32SkiaContext.cpp +221 -0
  64. package/native/win32/Win32SkiaContext.h +53 -0
  65. package/native/win32/Win32SkiaModule.cpp +80 -0
  66. package/native/win32/Win32SkiaModule.h +50 -0
  67. package/native/win32/Win32SkiaPeer.cpp +358 -0
  68. package/native/win32/Win32SkiaPeer.h +66 -0
  69. package/native/win32/Win32SkiaUnicode.cpp +42 -0
  70. package/native/win32/Win32Snapshot.cpp +201 -0
  71. package/native/win32/Win32Snapshot.h +66 -0
  72. package/native/win32/Win32Strings.h +54 -0
  73. package/native/win32/Win32TextInput.cpp +1296 -0
  74. package/native/win32/Win32TextInput.h +341 -0
  75. package/native/win32/Win32TitleBar.cpp +562 -0
  76. package/native/win32/Win32TitleBar.h +146 -0
  77. package/native/win32/Win32TitleBarLayout.cpp +46 -0
  78. package/native/win32/Win32TitleBarLayout.h +58 -0
  79. package/native/win32/Win32TouchDispatcher.cpp +577 -0
  80. package/native/win32/Win32TouchDispatcher.h +201 -0
  81. package/native/win32/Win32UiThread.h +27 -0
  82. package/native/win32/Win32WindowControl.cpp +329 -0
  83. package/native/win32/Win32WindowModule.cpp +209 -0
  84. package/native/win32/Win32WindowModule.h +72 -0
  85. package/native/win32/demo_layout_win32.cpp +232 -0
  86. package/native/win32/main_win32.cpp +2722 -0
  87. package/native/win32/mount_harness_win32.cpp +316 -0
  88. package/package.json +25 -4
  89. package/react-native.config.js +22 -0
  90. package/README.md +0 -3
@@ -0,0 +1,46 @@
1
+ #include "Win32TitleBarLayout.h"
2
+
3
+ #include "RnWin32View.h"
4
+
5
+ #include <cmath>
6
+ #include <string>
7
+
8
+ namespace basalt::win32 {
9
+
10
+ bool isTitleBarDragRegionAt(RnWin32View *root, float x, float y) {
11
+ for (RnWin32View *view = hitTest(root, x, y); view != nullptr; view = view->parent()) {
12
+ const std::string &id = view->nativeId();
13
+ if (id == kTitleBarDragRegionId) {
14
+ return true;
15
+ }
16
+ if (id == kTitleBarNoDragRegionId) {
17
+ return false;
18
+ }
19
+ }
20
+ return false;
21
+ }
22
+
23
+ CaptionMetrics captionMetricsForDpi(uint32_t dpi) {
24
+ const float scale = dpi == 0 ? 1.0f : static_cast<float>(dpi) / 96.0f;
25
+ return CaptionMetrics{std::round(kCaptionHeightDips * scale),
26
+ std::round(kCaptionButtonWidthDips * scale)};
27
+ }
28
+
29
+ CaptionButton captionButtonAt(float x, float y, float clientWidth, const CaptionMetrics &metrics) {
30
+ if (y < 0 || y >= metrics.height || x < 0 || x >= clientWidth || metrics.buttonWidth <= 0) {
31
+ return CaptionButton::None;
32
+ }
33
+ const float fromRight = clientWidth - x;
34
+ if (fromRight <= metrics.buttonWidth) {
35
+ return CaptionButton::Close;
36
+ }
37
+ if (fromRight <= 2 * metrics.buttonWidth) {
38
+ return CaptionButton::Maximize;
39
+ }
40
+ if (fromRight <= 3 * metrics.buttonWidth) {
41
+ return CaptionButton::Minimize;
42
+ }
43
+ return CaptionButton::None;
44
+ }
45
+
46
+ } // namespace basalt::win32
@@ -0,0 +1,58 @@
1
+ // Where a hidden title bar's pieces are: which caption button a point is over,
2
+ // how big the caption is at a DPI, and whether a point drags the window.
3
+ //
4
+ // The geometry half of Win32TitleBar, kept apart from it so that it needs no
5
+ // window, no DWM and no React Native -- only a view tree and a size -- and can
6
+ // be tested with none of them. A custom caption fails in ways a person notices
7
+ // at once and a test does not, unless the test asks exactly these questions.
8
+
9
+ #pragma once
10
+
11
+ #include "TitleBarRegions.h"
12
+
13
+ #include <cstdint>
14
+
15
+ namespace basalt::win32 {
16
+
17
+ class RnWin32View;
18
+
19
+ // What <TitleBar.DragRegion> and <TitleBar.NoDragRegion> set as a view's
20
+ // nativeID, under this namespace's own names so that everything already
21
+ // written against them keeps compiling.
22
+ //
23
+ // The values come from core/TitleBarRegions.h rather than being spelled again
24
+ // here: GTK reads the same two strings, and src/TitleBar.tsx writes them, so a
25
+ // literal in this file would be the third copy of something that only works
26
+ // when all of them agree.
27
+ inline constexpr const char *kTitleBarDragRegionId = basalt::kTitleBarDragRegionId;
28
+ inline constexpr const char *kTitleBarNoDragRegionId = basalt::kTitleBarNoDragRegionId;
29
+
30
+ // Whether a point, in the root's coordinates, drags the window.
31
+ //
32
+ // The deepest view under the point and then each of its ancestors are asked in
33
+ // turn, and the nearest one marked either way decides. So a button inside a
34
+ // drag region stays a button once it is marked no-drag, a label inside a drag
35
+ // region drags without being marked at all, and a point under nothing marked
36
+ // is ordinary client area.
37
+ bool isTitleBarDragRegionAt(RnWin32View *root, float x, float y);
38
+
39
+ enum class CaptionButton { None, Minimize, Maximize, Close };
40
+
41
+ // Windows 11's own caption sizes, in device-independent pixels.
42
+ inline constexpr float kCaptionHeightDips = 32.0f;
43
+ inline constexpr float kCaptionButtonWidthDips = 46.0f;
44
+
45
+ // Those sizes at a DPI, in client pixels -- which on this host are also the
46
+ // surface root's units, because the root is sized to the client rectangle.
47
+ struct CaptionMetrics {
48
+ float height{0};
49
+ float buttonWidth{0};
50
+ };
51
+ CaptionMetrics captionMetricsForDpi(uint32_t dpi);
52
+
53
+ // The caption button under a point in client coordinates, for a window
54
+ // `clientWidth` wide. The three sit flush right, in the order Windows puts
55
+ // them: minimise, maximise, close.
56
+ CaptionButton captionButtonAt(float x, float y, float clientWidth, const CaptionMetrics &metrics);
57
+
58
+ } // namespace basalt::win32
@@ -0,0 +1,577 @@
1
+ #include "Win32TouchDispatcher.h"
2
+
3
+ #include "Gestures.h"
4
+
5
+ #include <react/renderer/components/view/PointerEvent.h>
6
+ #include <react/renderer/components/view/TouchEvent.h>
7
+
8
+ #include <cstdint>
9
+ #include <string>
10
+
11
+ namespace basalt {
12
+
13
+ using facebook::react::HighResTimeStamp;
14
+ using facebook::react::Point;
15
+ using facebook::react::PointerEvent;
16
+ using facebook::react::Tag;
17
+ using facebook::react::Touch;
18
+ using facebook::react::TouchEvent;
19
+ using facebook::react::TouchEventEmitter;
20
+ using facebook::react::Touches;
21
+ using win32::RnWin32View;
22
+
23
+ namespace {
24
+
25
+ // A desktop pointer is one touch point, and React Native identifies touches by
26
+ // number. Zero is the first (and here only) finger. The same number identifies
27
+ // the pointer in pointer events, where React Native keys its hover tracking by
28
+ // it.
29
+ constexpr int kPointerIdentifier = 0;
30
+
31
+ // The payload for a hovering pointer, at a point in root coordinates with the
32
+ // target's origin in the same space.
33
+ PointerEvent hoverEvent(double x, double y, double originX, double originY) {
34
+ PointerEvent event{};
35
+ event.pointerId = kPointerIdentifier;
36
+ event.pointerType = "mouse";
37
+ event.clientPoint = Point{.x = static_cast<facebook::react::Float>(x),
38
+ .y = static_cast<facebook::react::Float>(y)};
39
+ event.screenPoint = event.clientPoint;
40
+ event.offsetPoint = Point{.x = static_cast<facebook::react::Float>(x - originX),
41
+ .y = static_cast<facebook::react::Float>(y - originY)};
42
+ event.width = 1;
43
+ event.height = 1;
44
+ // A hovering mouse presses nothing. `button` is -1 rather than 0 because 0 is
45
+ // the left button; -1 is W3C's "no button changed state".
46
+ event.button = -1;
47
+ event.buttons = 0;
48
+ event.isPrimary = true;
49
+ event.timeStamp = HighResTimeStamp::now();
50
+ return event;
51
+ }
52
+
53
+ // A point through a 2D affine matrix in Direct2D's Matrix3x2F order and its
54
+ // row-vector convention, which is the order `localToParent` writes.
55
+ void mapPoint(const float m[6], double x, double y, double &outX, double &outY) {
56
+ const double mappedX = m[0] * x + m[2] * y + m[4];
57
+ const double mappedY = m[1] * x + m[3] * y + m[5];
58
+ outX = mappedX;
59
+ outY = mappedY;
60
+ }
61
+
62
+ // The views under a point, innermost first, each with its origin in the
63
+ // surface root's coordinates. That is what a gesture recogniser needs and a
64
+ // touch does not: a touch is reported against one target, while a gesture may
65
+ // be attached to any ancestor of the view that was hit.
66
+ //
67
+ // The origin is carried *up* the chain rather than recomputed per view, so the
68
+ // walk stays linear and each view's contribution -- its own transform, then its
69
+ // parent's scroll offset -- is applied exactly once. AppKit gets the same
70
+ // answer from convertPoint:toView: and GTK from the widget's allocation; here
71
+ // there is no toolkit geometry, so the composition is written out. It is the
72
+ // mirror of what `hitTest` did on the way down, which is the only thing that
73
+ // makes the two agree about a rotated or scrolled ancestor.
74
+ //
75
+ // Built only when something is attached; see core/Gestures.h.
76
+ std::vector<basalt::HitView> hitChain(RnWin32View *root, double x, double y) {
77
+ std::vector<basalt::HitView> chain;
78
+ RnWin32View *hit = win32::hitTest(root, static_cast<float>(x), static_cast<float>(y));
79
+ if (hit == nullptr) {
80
+ return chain;
81
+ }
82
+
83
+ double originX = 0.0;
84
+ double originY = 0.0;
85
+ for (RnWin32View *view = hit; view != nullptr; view = view->parent()) {
86
+ chain.push_back(basalt::HitView{
87
+ .tag = static_cast<int>(view->tag()), .originX = originX, .originY = originY});
88
+ if (view == root) {
89
+ break;
90
+ }
91
+ RnWin32View *parent = view->parent();
92
+ if (parent == nullptr) {
93
+ break;
94
+ }
95
+ float local[6];
96
+ view->localToParent(local);
97
+ mapPoint(local, originX, originY, originX, originY);
98
+ originX -= parent->scrollX();
99
+ originY -= parent->scrollY();
100
+ }
101
+ return chain;
102
+ }
103
+
104
+ } // namespace
105
+
106
+ Win32TouchDispatcher::Win32TouchDispatcher(
107
+ Win32MountingManager *mountingManager,
108
+ RnWin32View *surfaceRoot)
109
+ : mountingManager_(mountingManager), surfaceRoot_(surfaceRoot) {
110
+ // What `Copy` in a menu and Ctrl+C copy when a paragraph has a selection.
111
+ // Neither of those can reach this object -- one is in the platform services
112
+ // and the other in the window procedure -- so this is where they ask. See
113
+ // core/TextSelection.h.
114
+ basalt::setSelectedTextProvider([this]() -> std::string {
115
+ return highlightView_ == nullptr ? std::string{} : highlightView_->selectedText();
116
+ });
117
+ }
118
+
119
+ Win32TouchDispatcher::~Win32TouchDispatcher() {
120
+ // The provider holds `this`: a Copy role performed after a reload would
121
+ // otherwise ask a destroyed dispatcher for the selection.
122
+ basalt::setSelectedTextProvider(nullptr);
123
+ selectionView_ = nullptr;
124
+ highlightView_ = nullptr;
125
+ }
126
+
127
+ std::pair<win32::RnWin32View *, int> Win32TouchDispatcher::selectableTextAt(
128
+ double x,
129
+ double y) const {
130
+ if (surfaceRoot_ == nullptr) {
131
+ return {nullptr, -1};
132
+ }
133
+ // From the resolved hit, like every other hit test here, so that a box-none
134
+ // ancestor that swallows a press swallows a selection too.
135
+ RnWin32View *hit = win32::hitTest(surfaceRoot_, static_cast<float>(x), static_cast<float>(y));
136
+ for (RnWin32View *view = hit; view != nullptr; view = view->parent()) {
137
+ if (view->textSelectable()) {
138
+ return {view, textIndexIn(view, x, y)};
139
+ }
140
+ if (view == surfaceRoot_) {
141
+ break;
142
+ }
143
+ }
144
+ return {nullptr, -1};
145
+ }
146
+
147
+ int Win32TouchDispatcher::textIndexIn(win32::RnWin32View *view, double x, double y) const {
148
+ if (view == nullptr || surfaceRoot_ == nullptr) {
149
+ return -1;
150
+ }
151
+ // The same inverse chain `hitTest` walks on the way down, so a selection and
152
+ // the press that started it cannot disagree about where a rotated or scrolled
153
+ // paragraph is.
154
+ float localX = 0.0f;
155
+ float localY = 0.0f;
156
+ if (!view->pageToLocal(
157
+ surfaceRoot_, static_cast<float>(x), static_cast<float>(y), localX, localY)) {
158
+ return -1;
159
+ }
160
+ return view->textIndexAtPoint(localX, localY);
161
+ }
162
+
163
+ void Win32TouchDispatcher::drawSelection() {
164
+ bool changed = false;
165
+
166
+ // The paragraph that was holding a highlight, when it is not this one: a
167
+ // press in a second <Text> ends the first one's selection, which is what
168
+ // every desktop does.
169
+ if (highlightView_ != nullptr && highlightView_ != selectionView_) {
170
+ changed = highlightView_->textSelectionLength() > 0;
171
+ highlightView_->setTextSelection(0, 0);
172
+ highlightView_ = nullptr;
173
+ }
174
+ if (selectionView_ != nullptr) {
175
+ const basalt::TextSelectionRange range = selection_.range();
176
+ changed = changed || selectionView_->textSelectionStart() != range.start ||
177
+ selectionView_->textSelectionLength() != range.length;
178
+ selectionView_->setTextSelection(range.start, range.length);
179
+ highlightView_ = range.empty() ? nullptr : selectionView_;
180
+ }
181
+
182
+ // This host repaints per mounted transaction and a selection is not one, so
183
+ // the thing that changed it asks -- and only when something changed. Every
184
+ // press comes through here, including the ones that select nothing, and a
185
+ // repaint of every window per click is a cost a click should not have. See
186
+ // setRepaintRequester.
187
+ if (changed && requestRepaint_) {
188
+ requestRepaint_();
189
+ }
190
+ }
191
+
192
+ void Win32TouchDispatcher::synthesiseTap(double x, double y) {
193
+ synthesiseTap(x, y, basalt::PointerButton::Primary);
194
+ }
195
+
196
+ void Win32TouchDispatcher::synthesiseTap(double x, double y, basalt::PointerButton button) {
197
+ dispatchTouchStart(x, y, button);
198
+ dispatchTouchEnd(x, y, button);
199
+ }
200
+
201
+ void Win32TouchDispatcher::synthesiseHover(double x, double y) {
202
+ if (x < 0 || y < 0) {
203
+ dispatchHoverLeave();
204
+ return;
205
+ }
206
+ dispatchHover(x, y);
207
+ }
208
+
209
+ void Win32TouchDispatcher::synthesiseDrag(
210
+ double fromX, double fromY, double toX, double toY, int steps) {
211
+ dispatchTouchStart(fromX, fromY, basalt::PointerButton::Primary);
212
+ const int count = steps < 1 ? 1 : steps;
213
+ for (int step = 1; step <= count; step++) {
214
+ const double progress = static_cast<double>(step) / count;
215
+ dispatchTouchMove(fromX + (toX - fromX) * progress, fromY + (toY - fromY) * progress);
216
+ }
217
+ dispatchTouchEnd(toX, toY, basalt::PointerButton::Primary);
218
+ }
219
+
220
+ // ---------------------------------------------------------------------------
221
+ // Hit testing
222
+ // ---------------------------------------------------------------------------
223
+
224
+ Tag hitTestTag(RnWin32View *root, double x, double y) {
225
+ RnWin32View *hit = win32::hitTest(root, static_cast<float>(x), static_cast<float>(y));
226
+ return hit == nullptr ? 0 : static_cast<Tag>(hit->tag());
227
+ }
228
+
229
+ // ---------------------------------------------------------------------------
230
+ // Dispatch
231
+ // ---------------------------------------------------------------------------
232
+
233
+ void Win32TouchDispatcher::dispatchTouchStart(double x, double y, basalt::PointerButton button) {
234
+ if (surfaceRoot_ == nullptr) {
235
+ return;
236
+ }
237
+
238
+ // The innermost view, and where inside it the press landed. Needed for the
239
+ // pointer event whichever button it was; PointerEventsProcessor walks up from
240
+ // there itself.
241
+ Tag target = 0;
242
+ double originX = 0;
243
+ double originY = 0;
244
+ for (const auto &view : hitChain(surfaceRoot_, x, y)) {
245
+ target = static_cast<Tag>(view.tag);
246
+ originX = view.originX;
247
+ originY = view.originY;
248
+ break;
249
+ }
250
+ if (target == 0) {
251
+ return;
252
+ }
253
+
254
+ // Every button produces a pointer event, which is where `button` can be said
255
+ // at all.
256
+ emitPointerButton(true, target, originX, originY, x, y, button);
257
+
258
+ // Only the primary one goes any further. A secondary click is not a press --
259
+ // the web fires no `click` for one and no desktop treats it as an activation
260
+ // -- so it must not reach the responder system, a gesture handler, or
261
+ // `onPress`. See core/PointerButtons.h.
262
+ //
263
+ // This host was the odd one out in the other direction: it handled only
264
+ // WM_LBUTTONDOWN, so a right-click did nothing whatsoever.
265
+ if (!basalt::isPressButton(button)) {
266
+ return;
267
+ }
268
+
269
+ // Selectable text remembers where the press landed and nothing more: a press
270
+ // is also how a <Pressable> around this text is pressed, so claiming here
271
+ // would stop every tap on a selectable label. The press *does* clear whatever
272
+ // was selected, which is how a click dismisses a highlight. See
273
+ // core/TextSelection.h.
274
+ {
275
+ const auto [paragraph, index] = selectableTextAt(x, y);
276
+ selectionView_ = paragraph;
277
+ selection_.press(paragraph != nullptr ? static_cast<Tag>(paragraph->tag()) : 0,
278
+ index < 0 ? 0 : index,
279
+ Point{.x = static_cast<facebook::react::Float>(x),
280
+ .y = static_cast<facebook::react::Float>(y)});
281
+ drawSelection();
282
+ }
283
+
284
+ if (!basalt::gestures().empty()) {
285
+ basalt::gestures().pointerDown(
286
+ hitChain(surfaceRoot_, x, y), x, y, basalt::monotonicMilliseconds());
287
+ }
288
+
289
+ activeTarget_ = target;
290
+ isDown_ = true;
291
+ emit(TouchKind::Start, target, x, y);
292
+
293
+ // A gesture that claimed the pointer on contact -- a native handler -- takes
294
+ // it away from React Native's responder system immediately.
295
+ yieldToGesture(x, y);
296
+ }
297
+
298
+ void Win32TouchDispatcher::dispatchTouchMove(double x, double y) {
299
+ // A selection that has claimed the pointer owns every move after it, and this
300
+ // is before the guards below on purpose: claiming *cancelled* the touch, so
301
+ // `isDown_` is false and `activeTarget_` is zero from then on, and a check
302
+ // after them would extend a selection exactly once. The gesture recognisers
303
+ // are not fed either -- they were sent a cancel with the touch.
304
+ if (selection_.dragging()) {
305
+ selection_.moveTo(textIndexIn(selectionView_, x, y),
306
+ Point{.x = static_cast<facebook::react::Float>(x),
307
+ .y = static_cast<facebook::react::Float>(y)});
308
+ drawSelection();
309
+ return;
310
+ }
311
+
312
+ if (!basalt::gestures().empty()) {
313
+ basalt::gestures().pointerMove(x, y, basalt::monotonicMilliseconds());
314
+ }
315
+
316
+ // Motion with no button down is hover, which the touch model has no place
317
+ // for. Reporting it would look to the responder system like a finger dragging
318
+ // across the screen at all times.
319
+ if (!isDown_ || activeTarget_ == 0) {
320
+ return;
321
+ }
322
+
323
+ // The move that turns a press inside selectable text into a selection: far
324
+ // enough from where it started to be a sweep rather than a tap.
325
+ if (selection_.moveTo(textIndexIn(selectionView_, x, y),
326
+ Point{.x = static_cast<facebook::react::Float>(x),
327
+ .y = static_cast<facebook::react::Float>(y)})) {
328
+ drawSelection();
329
+ dispatchTouchCancel();
330
+ return;
331
+ }
332
+
333
+ if (yieldToGesture(x, y)) {
334
+ return;
335
+ }
336
+ emit(TouchKind::Move, activeTarget_, x, y);
337
+ }
338
+
339
+ void Win32TouchDispatcher::dispatchTouchEnd(double x, double y, basalt::PointerButton button) {
340
+ // The release half of the pointer event, for every button. Reported against
341
+ // whatever is under the pointer now rather than what was under it on press,
342
+ // which is what a release means when nothing was captured.
343
+ if (surfaceRoot_ != nullptr) {
344
+ for (const auto &view : hitChain(surfaceRoot_, x, y)) {
345
+ emitPointerButton(
346
+ false, static_cast<Tag>(view.tag), view.originX, view.originY, x, y, button);
347
+ break;
348
+ }
349
+ }
350
+
351
+ // The pointer is up, so a selection stops growing. The highlight stays: a
352
+ // person lets go and expects the text to stay selected until they click.
353
+ const bool wasSelecting = selection_.dragging();
354
+ selection_.release();
355
+
356
+ // And nothing else for a button that never pressed anything -- there is no
357
+ // touch to end, no gesture to finish and no <Switch> to toggle.
358
+ if (!basalt::isPressButton(button)) {
359
+ return;
360
+ }
361
+
362
+ // A selection already cancelled the touch sequence, so there is no touch to
363
+ // end and nothing to press: the release belongs to the selection.
364
+ if (wasSelecting) {
365
+ isDown_ = false;
366
+ activeTarget_ = 0;
367
+ return;
368
+ }
369
+
370
+ if (!basalt::gestures().empty()) {
371
+ basalt::gestures().pointerUp(x, y, basalt::monotonicMilliseconds());
372
+ }
373
+
374
+ if (!isDown_ || activeTarget_ == 0) {
375
+ return;
376
+ }
377
+ const Tag target = activeTarget_;
378
+ isDown_ = false;
379
+ activeTarget_ = 0;
380
+ emit(TouchKind::End, target, x, y);
381
+
382
+ // A <Switch> on this host is painted rather than mounted, so nothing
383
+ // underneath it turns a click into a toggle -- GTK's GtkSwitch and AppKit's
384
+ // NSSwitch both do that for themselves. This is the one line that makes up
385
+ // the difference; it is a no-op for every view that is not a switch.
386
+ if (mountingManager_ != nullptr) {
387
+ mountingManager_->pressedView(target);
388
+ }
389
+ }
390
+
391
+ void Win32TouchDispatcher::dispatchTouchCancel() {
392
+ if (!basalt::gestures().empty()) {
393
+ basalt::gestures().pointerCancel();
394
+ }
395
+
396
+ if (!isDown_ || activeTarget_ == 0) {
397
+ return;
398
+ }
399
+ const Tag target = activeTarget_;
400
+ isDown_ = false;
401
+ activeTarget_ = 0;
402
+ emit(TouchKind::Cancel, target, 0, 0);
403
+ }
404
+
405
+ // ---------------------------------------------------------------------------
406
+ // Hover
407
+ // ---------------------------------------------------------------------------
408
+
409
+ void Win32TouchDispatcher::dispatchHover(double x, double y) {
410
+ // The same chain the gesture path uses -- this is the one place where the
411
+ // absence of a toolkit pays off, because `hitChain` already composes the
412
+ // transforms and scroll offsets that GTK and AppKit answer for themselves.
413
+ //
414
+ // Only the innermost view is reported against -- React Native's
415
+ // PointerEventsProcessor walks up from it itself -- but the whole chain is
416
+ // still needed, because a listener on any ancestor is reason to dispatch.
417
+ Tag target = 0;
418
+ double originX = 0;
419
+ double originY = 0;
420
+ std::uint16_t listeners = HoverListenerNone;
421
+ for (const auto &view : hitChain(surfaceRoot_, x, y)) {
422
+ if (target == 0) {
423
+ target = static_cast<Tag>(view.tag);
424
+ originX = view.originX;
425
+ originY = view.originY;
426
+ }
427
+ listeners |= mountingManager_->hoverListenersForTag(static_cast<Tag>(view.tag));
428
+ }
429
+
430
+ if (target == 0) {
431
+ dispatchHoverLeave();
432
+ return;
433
+ }
434
+ if (!hover_.admitMove(static_cast<int>(target), listeners)) {
435
+ return;
436
+ }
437
+ emitPointerMove(target, originX, originY, x, y);
438
+ }
439
+
440
+ void Win32TouchDispatcher::dispatchHoverLeave() {
441
+ const int target = hover_.admitLeave();
442
+ if (target == 0) {
443
+ return;
444
+ }
445
+ const auto emitter = std::dynamic_pointer_cast<const TouchEventEmitter>(
446
+ mountingManager_->eventEmitterForTag(static_cast<Tag>(target)));
447
+ if (emitter == nullptr) {
448
+ return;
449
+ }
450
+ // A leave from a platform is not forwarded: the processor reads it as the
451
+ // pointer being gone and unwinds the path it is holding. So the coordinates
452
+ // on it are never seen by an app, which is just as well -- WM_MOUSELEAVE
453
+ // reports no position at all.
454
+ emitter->onPointerLeave(hoverEvent(0, 0, 0, 0));
455
+ }
456
+
457
+ void Win32TouchDispatcher::emitPointerButton(bool down,
458
+ Tag target,
459
+ double originX,
460
+ double originY,
461
+ double x,
462
+ double y,
463
+ basalt::PointerButton button) {
464
+ const auto emitter = std::dynamic_pointer_cast<const TouchEventEmitter>(
465
+ mountingManager_->eventEmitterForTag(target));
466
+ if (emitter == nullptr) {
467
+ return;
468
+ }
469
+ PointerEvent event = hoverEvent(x, y, originX, originY);
470
+ event.button = static_cast<int>(button);
471
+ // Held *during* the event, which is a different number from `button` in the
472
+ // same event: on release nothing is held any more. See core/PointerButtons.h.
473
+ event.buttons = down ? static_cast<int>(basalt::buttonsMaskFor(button)) : 0;
474
+ if (down) {
475
+ emitter->onPointerDown(event);
476
+ } else {
477
+ emitter->onPointerUp(event);
478
+ }
479
+ }
480
+
481
+ void Win32TouchDispatcher::emitPointerMove(
482
+ Tag target, double originX, double originY, double x, double y) {
483
+ const auto emitter = std::dynamic_pointer_cast<const TouchEventEmitter>(
484
+ mountingManager_->eventEmitterForTag(target));
485
+ if (emitter == nullptr) {
486
+ return;
487
+ }
488
+ emitter->onPointerMove(hoverEvent(x, y, originX, originY));
489
+ }
490
+
491
+ // A gesture recogniser that has activated owns the pointer, and React Native's
492
+ // responder system must be told the touch it was following is gone -- otherwise
493
+ // panning across a <Pressable> pans *and* presses it. This is what RNGH's
494
+ // setJSResponder does on the platforms it was written for; here both sides are
495
+ // fed from this one place, so it is a cancel rather than a negotiation.
496
+ bool Win32TouchDispatcher::yieldToGesture(double x, double y) {
497
+ if (!isDown_ || activeTarget_ == 0 || !basalt::gestures().hasActiveHandler()) {
498
+ return false;
499
+ }
500
+ const Tag target = activeTarget_;
501
+ isDown_ = false;
502
+ activeTarget_ = 0;
503
+ emit(TouchKind::Cancel, target, x, y);
504
+ return true;
505
+ }
506
+
507
+ void Win32TouchDispatcher::emit(TouchKind kind, Tag target, double x, double y) {
508
+ if (mountingManager_ == nullptr) {
509
+ return;
510
+ }
511
+ const auto emitter = std::dynamic_pointer_cast<const TouchEventEmitter>(
512
+ mountingManager_->eventEmitterForTag(target));
513
+ if (emitter == nullptr) {
514
+ return;
515
+ }
516
+
517
+ const auto now = HighResTimeStamp::now();
518
+
519
+ Touch touch{};
520
+ touch.identifier = kPointerIdentifier;
521
+ touch.target = target;
522
+ // Coordinates arrive relative to the surface root, which is what React Native
523
+ // calls the page.
524
+ touch.pagePoint = Point{.x = static_cast<facebook::react::Float>(x),
525
+ .y = static_cast<facebook::react::Float>(y)};
526
+ touch.screenPoint = touch.pagePoint;
527
+ // Relative to the target, which is what `offsetPoint` means and what
528
+ // `locationX`/`locationY` are built from. It carried the page point until
529
+ // now: right only for a view at the surface's origin, and wrong by that
530
+ // view's position for every other.
531
+ //
532
+ // The page point is the fallback, which is what every touch carried before:
533
+ // the target unmounted between the press and this event, or a chain with no
534
+ // inverse. Refusing would drop the touch.
535
+ float offsetX = static_cast<float>(x);
536
+ float offsetY = static_cast<float>(y);
537
+ if (mountingManager_ != nullptr) {
538
+ if (RnWin32View *view = mountingManager_->viewForTag(target)) {
539
+ view->pageToLocal(surfaceRoot_, static_cast<float>(x), static_cast<float>(y),
540
+ offsetX, offsetY);
541
+ }
542
+ }
543
+ touch.offsetPoint = Point{.x = static_cast<facebook::react::Float>(offsetX),
544
+ .y = static_cast<facebook::react::Float>(offsetY)};
545
+ touch.force = 1.0F;
546
+ touch.timeStamp = now;
547
+
548
+ Touches changed{};
549
+ changed.insert(touch);
550
+
551
+ const bool isEnd = kind == TouchKind::End || kind == TouchKind::Cancel;
552
+
553
+ TouchEvent event{};
554
+ event.changedTouches = changed;
555
+ // On touchend the finger is gone, so it is no longer in `touches`. Getting
556
+ // this wrong leaves the responder system believing a touch is still active,
557
+ // which swallows the next press.
558
+ event.touches = isEnd ? Touches{} : changed;
559
+ event.targetTouches = event.touches;
560
+
561
+ switch (kind) {
562
+ case TouchKind::Start:
563
+ emitter->onTouchStart(event);
564
+ break;
565
+ case TouchKind::Move:
566
+ emitter->onTouchMove(event);
567
+ break;
568
+ case TouchKind::End:
569
+ emitter->onTouchEnd(event);
570
+ break;
571
+ case TouchKind::Cancel:
572
+ emitter->onTouchCancel(event);
573
+ break;
574
+ }
575
+ }
576
+
577
+ } // namespace basalt