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,341 @@
1
+ // <TextInput> on Win32.
2
+ //
3
+ // The native half is React Native's own *iOS* TextInput: its shadow node,
4
+ // props, state and event emitter are pure C++ and measure through a
5
+ // TextLayoutManager, which on this platform is the DirectWrite one. Android's
6
+ // variant includes fbjni and calls into a Java FabricUIManager, so it is
7
+ // unusable here; see docs/DECISIONS.md. The component name is therefore
8
+ // "TextInput", which is what packages/basalt-core's
9
+ // src/overrides/TextInput.js asks for -- the same file both other desktops use,
10
+ // because that override was never about Linux.
11
+ //
12
+ // The editing itself is a real EDIT control, not a caret drawn on a DirectWrite
13
+ // layout. That brings the IME, selection, the clipboard, undo, double-click
14
+ // word selection, Ctrl+arrow, and every other binding a Windows user expects --
15
+ // none of it worth reimplementing and all of it easy to get subtly wrong. GTK
16
+ // embeds a GtkText and AppKit an NSTextField for exactly the same reason.
17
+ //
18
+ // ## The peer is a window, and that is the whole difficulty
19
+ //
20
+ // On the other two desktops a React Native view *is* a widget, so a text field
21
+ // is a widget inside a widget and the toolkit handles the rest. Here every view
22
+ // is a plain C++ object painted with Direct2D into one HWND, and an EDIT is a
23
+ // real child window. Three consequences follow, and none of them has an
24
+ // equivalent on GTK or AppKit:
25
+ //
26
+ // - **It has to be positioned by hand.** The manager walks the view up to the
27
+ // surface root to find its rectangle in client coordinates, and has to redo
28
+ // that whenever anything moves it -- which includes a scroll, where no
29
+ // mutation arrives at all. `syncBounds` is that, and the host calls it after
30
+ // every transaction and every wheel.
31
+ //
32
+ // - **It is always on top.** Child windows paint above whatever Direct2D drew,
33
+ // in an order the React tree has no say in. A field cannot be covered by a
34
+ // later sibling, and it does not clip to a scrolled ancestor -- it is hidden
35
+ // when it leaves the ancestor's box instead, which is close enough to look
36
+ // right and is not the same thing.
37
+ //
38
+ // - **Its events arrive at the parent.** EN_CHANGE, EN_SETFOCUS and
39
+ // EN_KILLFOCUS are WM_COMMAND notifications to the host window, and the
40
+ // text and background colours are answered from WM_CTLCOLOREDIT. So the
41
+ // host forwards both, which is why this class has methods a mounting
42
+ // manager would not otherwise need.
43
+ //
44
+ // ## The controlled-value loop, which is not about a toolkit
45
+ //
46
+ // React Native's <TextInput> is controlled: JavaScript owns the value, the
47
+ // control reports every change, JavaScript re-renders and pushes the value back
48
+ // down as a prop. If applying that prop looks like the user typing, the two
49
+ // chase each other forever. If a prop that arrives late is applied anyway, a
50
+ // fast typist watches characters reorder themselves.
51
+ //
52
+ // `applying` breaks the first and `eventCount` the second, exactly as on both
53
+ // other desktops, because neither is a Windows problem.
54
+
55
+ #pragma once
56
+
57
+ #include "RnWin32View.h"
58
+
59
+ #include <react/renderer/components/iostextinput/TextInputShadowNode.h>
60
+ #include <react/renderer/components/textinput/TextInputEventEmitter.h>
61
+ #include <react/renderer/core/EventEmitter.h>
62
+ #include <react/renderer/mounting/ShadowView.h>
63
+
64
+ #include <windows.h>
65
+
66
+ #include <functional>
67
+ #include <optional>
68
+ #include <string>
69
+ #include <unordered_map>
70
+ #include <vector>
71
+
72
+ namespace basalt {
73
+
74
+ class Win32TextInputManager {
75
+ public:
76
+ using EmitterLookup = std::function<facebook::react::EventEmitter::Shared(facebook::react::Tag)>;
77
+
78
+ explicit Win32TextInputManager(EmitterLookup lookup);
79
+ ~Win32TextInputManager();
80
+
81
+ Win32TextInputManager(const Win32TextInputManager &) = delete;
82
+ Win32TextInputManager &operator=(const Win32TextInputManager &) = delete;
83
+ Win32TextInputManager(Win32TextInputManager &&) = delete;
84
+ Win32TextInputManager &operator=(Win32TextInputManager &&) = delete;
85
+
86
+ // The window every peer is a child of. Nothing can be created before this is
87
+ // set, which is why `update` tolerates being called without it: the mounting
88
+ // manager exists before the host's window does, and the tests never have one.
89
+ void setHostWindow(HWND window);
90
+
91
+ // Called for every mutation touching a TextInput. Creates the control the
92
+ // first time, then applies props.
93
+ void update(win32::RnWin32View *view, const facebook::react::ShadowView &shadowView);
94
+
95
+ void remove(facebook::react::Tag tag);
96
+
97
+ // Focuses whatever `autoFocus` asked for, once the transaction is on screen.
98
+ //
99
+ // Where the prop is read is too early on the other two hosts, and may be here:
100
+ // MountingWalk::create applies props straight after `createView`, before any
101
+ // Insert. An EDIT is a child of the real top-level window from the moment it is
102
+ // created, so Windows may get away with it, but the control is made without
103
+ // WS_VISIBLE and is only shown by syncBounds afterwards. Deferred here too, so
104
+ // all three hosts answer `autoFocus` the same way and the one that differs is
105
+ // not the one nobody tested.
106
+ void flushAutoFocus();
107
+
108
+ // focus, blur, and setTextAndSelection. Returns false for anything else.
109
+ bool dispatchCommand(facebook::react::Tag tag,
110
+ const std::string &name,
111
+ const folly::dynamic &args);
112
+
113
+ // Moves every peer to where its view now is, in client coordinates, and hides
114
+ // the ones that have gone out of sight. Called after each transaction and
115
+ // each scroll: a child window does not follow a view that has no widget.
116
+ void syncBounds(win32::RnWin32View *root);
117
+
118
+ // WM_COMMAND from the host: EN_CHANGE, EN_SETFOCUS, EN_KILLFOCUS. True when
119
+ // the notification was one of this manager's controls.
120
+ bool handleControlCommand(WPARAM wparam, LPARAM lparam);
121
+
122
+ // WM_CTLCOLOREDIT / WM_CTLCOLORSTATIC from the host. Returns the brush to
123
+ // paint the control's background with, or null when the control is not one of
124
+ // this manager's. An EDIT otherwise draws in the system's colours, which have
125
+ // nothing to do with the `style` the component was given -- on a dark field
126
+ // that is dark text on dark, the same trap GtkText has.
127
+ HBRUSH controlColor(HDC deviceContext, HWND control);
128
+
129
+ // The bitmap currently installed as the caret for `tag`, or null.
130
+ //
131
+ // Only the test suite asks, and it asks because Windows offers no way to read
132
+ // the caret back: `CreateCaret` hands a bitmap to the system and there is no
133
+ // `GetCaret`. So the only thing that can be measured is the bitmap this
134
+ // installed, which is why this is here rather than private -- see
135
+ // `win32_a_cursor_colour_installs_a_caret_of_that_colour`.
136
+ HBITMAP caretBitmapFor(facebook::react::Tag tag) const;
137
+
138
+ // True when the control is one of this manager's peers, which is how the host
139
+ // tells a key meant for a field from one meant for the window.
140
+ bool ownsControl(HWND control) const;
141
+
142
+ // Focuses the field under a point in the surface root's coordinates, if there
143
+ // is one. True when there was.
144
+ //
145
+ // Only BASALT_TEST_TAP needs this, and the reason is the one thing a
146
+ // synthesised tap cannot reproduce. A real click at these coordinates never
147
+ // reaches the host's window procedure at all: the peer is a child window, so
148
+ // USER32 routes the click to it and the control focuses itself. A tap
149
+ // injected at the touch dispatcher skips all of that, because the dispatcher
150
+ // is above the point where the two paths diverge.
151
+ bool focusAt(win32::RnWin32View *root, double x, double y);
152
+
153
+ // Types into whichever field has focus, for BASALT_TEST_TYPE. Real WM_CHAR
154
+ // messages to the real control, so what it skips is the keyboard driver and
155
+ // nothing above it.
156
+ bool typeIntoFocused(const std::string &text);
157
+
158
+ private:
159
+ // Set when a control is created with `autoFocus`, cleared by flushAutoFocus.
160
+ std::vector<facebook::react::Tag> pendingAutoFocus_;
161
+
162
+ struct Entry {
163
+ win32::RnWin32View *view{nullptr};
164
+ HWND control{nullptr};
165
+ HFONT font{nullptr};
166
+ facebook::react::Tag tag{0};
167
+ Win32TextInputManager *owner{nullptr};
168
+ // The subclass has to be able to find its way back, and SetWindowSubclass's
169
+ // reference data is the only channel; this is what is put there.
170
+
171
+ // React Native counts events so it can ignore a prop update older than what
172
+ // the user has since typed. Every emitted metric carries it.
173
+ int eventCount{0};
174
+
175
+ // True while a prop is being pushed into the control, so the EN_CHANGE it
176
+ // provokes is not reported back as the user typing.
177
+ bool applying{false};
178
+ // The selection last reported to JavaScript. A plain EDIT has no
179
+ // notification for the caret moving -- EN_SELCHANGE belongs to RichEdit --
180
+ // so the selection is read after anything that could have moved it and
181
+ // compared with this. See reportSelectionIfChanged.
182
+ facebook::react::AttributedString::Range lastReportedSelection{};
183
+
184
+ std::string lastReportedText;
185
+
186
+ // The last `text` prop actually seen, and whether one has been seen at all.
187
+ //
188
+ // This is what tells a *controlled* field from an uncontrolled one, which
189
+ // props alone cannot: React Native's TextInput.js sends
190
+ // `text={value ?? defaultValue}`, and an uncontrolled field with no default
191
+ // sends undefined, which arrives here as the empty string -- exactly what a
192
+ // controlled field that JavaScript has cleared sends. Applying it on every
193
+ // render therefore wipes an uncontrolled field the moment anything else in
194
+ // the tree re-renders, because RN re-sends `mostRecentEventCount` on every
195
+ // change and that alone produces an Update mutation.
196
+ //
197
+ // So the prop is applied when it *changes*, not when it differs from the
198
+ // control. A controlled field's value changes as the user types; an
199
+ // uncontrolled one's never does.
200
+ std::string lastPropText;
201
+ bool sawProps{false};
202
+
203
+ // The last `selection` prop seen, for the same reason and under the same
204
+ // rule: a controlled selection is applied when it changes, not whenever it
205
+ // differs from the control. Absent means the field has never sent one,
206
+ // which is what an uncontrolled selection looks like.
207
+ std::optional<facebook::react::Selection> lastPropSelection{};
208
+
209
+ COLORREF textColor{RGB(0, 0, 0)};
210
+ COLORREF backgroundColor{RGB(255, 255, 255)};
211
+ HBRUSH backgroundBrush{nullptr};
212
+ // Whether the view behind the control has a background of its own. With
213
+ // none, the control is painted over whatever Direct2D drew there -- which
214
+ // it cannot see, so the closest honest answer is the window's own ground.
215
+ bool hasBackground{false};
216
+
217
+ bool secure{false};
218
+
219
+ // The three colours React Native has for a field, none of which a plain
220
+ // EDIT has a property for. See `applyProps`, `drawPlaceholder` and
221
+ // `installCaret`.
222
+ //
223
+ // Each is held with a flag rather than a sentinel colour, because
224
+ // `SharedColor`'s unset value is zero: a field that asked for nothing would
225
+ // otherwise get a black caret and a transparent placeholder, which is the
226
+ // bug both other hosts had to be careful of for the same reason.
227
+ bool hasPlaceholderColour{false};
228
+ COLORREF placeholderColour{RGB(0, 0, 0)};
229
+ // The placeholder itself, held because with a colour this host draws it
230
+ // rather than handing it to `EM_SETCUEBANNER`, which has no colour.
231
+ std::wstring placeholder;
232
+ // `submitBehavior`, already resolved: React Native's `Default` means
233
+ // `newline` for a multiline field and `blurAndSubmit` for a single-line
234
+ // one, and `getNonDefaultSubmitBehavior()` is what says so. Read in the
235
+ // subclass, where Enter arrives.
236
+ facebook::react::SubmitBehavior submitBehavior{
237
+ facebook::react::SubmitBehavior::BlurAndSubmit};
238
+
239
+ // `contextMenuHidden`. Read by the subclass, which swallows WM_CONTEXTMENU.
240
+ bool contextMenuHidden{false};
241
+
242
+ // `caretHidden`, held beside the colour because it arrives through the same
243
+ // bitmap: see `installCaret`.
244
+ bool caretHidden{false};
245
+ bool hasCaretColour{false};
246
+ COLORREF caretColour{RGB(0, 0, 0)};
247
+ // The caret's shape, which is how its colour is set: a bitmap, XOR-ed onto
248
+ // the field by the system. Created when the field takes focus, because the
249
+ // colour it has to carry depends on the background it will be XOR-ed
250
+ // against, and destroyed when it loses it -- the documented contract for a
251
+ // caret is that the window owning it has the focus.
252
+ HBITMAP caret{nullptr};
253
+
254
+ // `clearTextOnFocus` and `selectTextOnFocus`: what happens when the field
255
+ // takes focus, which arrives long after the props do. See EN_SETFOCUS.
256
+ bool clearTextOnFocus{false};
257
+ bool selectTextOnFocus{false};
258
+
259
+ // Which kind of EDIT this is. `ES_MULTILINE` is read when the window is
260
+ // created and cannot be added afterwards, so this is what a change in the
261
+ // prop is compared against: a field that switches has its peer rebuilt.
262
+ bool multiline{false};
263
+
264
+ // Yoga resolves border and padding into contentInsets, so the same twelve
265
+ // points mean the same thing in a field as in a <View>. Applied by placing
266
+ // the control inside them rather than by any EDIT message: EM_SETRECT is
267
+ // documented as multiline-only and silently does nothing here, which is a
268
+ // afternoon nobody should spend twice.
269
+ RECT insets{0, 0, 0, 0};
270
+
271
+ // What the control's font actually occupies, measured rather than assumed:
272
+ // a family substitution changes it, and this is what the control's height
273
+ // is set to so that the text sits in the middle of the field.
274
+ LONG lineHeight{0};
275
+
276
+ // The input scope last installed for `keyboardType`, and whether one has
277
+ // been. `SetInputScope` replaces the window's whole scope list, so it is
278
+ // called on a change rather than on every props update -- a controlled
279
+ // field re-sends identical props on every keystroke.
280
+ int inputScope{0};
281
+ bool sawInputScope{false};
282
+ };
283
+
284
+ Entry *entryForControl(HWND control);
285
+ void applyProps(Entry &entry, const facebook::react::TextInputProps &props);
286
+ // `spellCheck`, `autoCorrect`, `autoCapitalize` and `keyboardType`: the four
287
+ // the three hosts honour different subsets of. Separate from `applyProps`
288
+ // because what this host can act on is one and a half of them and the comment
289
+ // explaining which is longer than the code.
290
+ void applyTextChecking(Entry &entry, const facebook::react::TextInputProps &props);
291
+
292
+ // Remembers the content box and the line height, which is what `syncBounds`
293
+ // needs to place the control and cannot work out from the view alone.
294
+ void measureShape(Entry &entry, const facebook::react::LayoutMetrics &metrics);
295
+
296
+ void destroyPeer(Entry &entry);
297
+
298
+ // The placeholder, drawn by this host rather than by the control.
299
+ //
300
+ // `EM_SETCUEBANNER` is Windows' placeholder and takes no colour, so a field
301
+ // that asked for one is drawn here instead: the control's own font, the
302
+ // asked-for colour, inside the formatting rectangle the control reports. Used
303
+ // from both WM_PAINT and WM_PRINTCLIENT, which is what lets a test render a
304
+ // field into a memory DC and read the pixels back.
305
+ void drawPlaceholder(const Entry &entry, HDC deviceContext) const;
306
+
307
+ // The caret, whose colour is a bitmap XOR-ed onto the field.
308
+ //
309
+ // Called after the control has handled WM_SETFOCUS, because an EDIT creates
310
+ // its own caret there and `CreateCaret` replaces whatever shape came before.
311
+ void installCaret(Entry &entry);
312
+ void releaseCaret(Entry &entry);
313
+
314
+ // Enter, and the keys an EDIT would otherwise beep at. Installed with
315
+ // SetWindowSubclass rather than SetWindowLongPtr so that a later subclass by
316
+ // anything else still chains correctly.
317
+ static LRESULT CALLBACK
318
+ editProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam, UINT_PTR id, DWORD_PTR data);
319
+
320
+ // Focus back to the host window, which is this platform's "nothing is
321
+ // focused": see the `blur` command, which says why. Used by that command and
322
+ // by `submitBehavior`'s `blurAndSubmit`.
323
+ void moveFocusToHost();
324
+
325
+ void reportChange(Entry &entry);
326
+ facebook::react::TextInputEventEmitter::Metrics metricsFor(const Entry &entry) const;
327
+ // Reads the caret and emits onSelectionChange if it moved. Called after any
328
+ // message that could have moved it, because Windows will not say.
329
+ void reportSelectionIfChanged(Entry &entry);
330
+ std::shared_ptr<const facebook::react::TextInputEventEmitter> emitterFor(
331
+ facebook::react::Tag tag) const;
332
+
333
+ EmitterLookup lookup_;
334
+ HWND host_{nullptr};
335
+ // Stable addresses: the subclass procedure holds an Entry pointer, so the map
336
+ // must not move its values. std::unordered_map does not, which is the reason
337
+ // it is this container and not a vector.
338
+ std::unordered_map<facebook::react::Tag, Entry> entries_;
339
+ };
340
+
341
+ } // namespace basalt