@fluentui-react-native/focus-zone 0.23.2 → 0.23.4
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.
- package/.cache/tsconfig.tsbuildinfo +1 -1
- package/CHANGELOG.md +14 -0
- package/RCTFocusZone.podspec +1 -1
- package/SPEC.md +42 -0
- package/lib/FocusZone.d.ts +2 -6
- package/lib/FocusZone.d.ts.map +1 -1
- package/lib/FocusZone.js +28 -45
- package/lib/FocusZone.js.map +1 -1
- package/lib/FocusZone.types.d.ts +16 -15
- package/lib/FocusZone.types.d.ts.map +1 -1
- package/lib/FocusZone.types.js.map +1 -1
- package/lib/FocusZoneNativeComponent.d.ts +5 -3
- package/lib/FocusZoneNativeComponent.d.ts.map +1 -1
- package/lib/FocusZoneNativeComponent.js +3 -1
- package/lib/FocusZoneNativeComponent.js.map +1 -1
- package/lib/FocusZoneNativeComponent.win32.d.ts +16 -0
- package/lib/FocusZoneNativeComponent.win32.d.ts.map +1 -0
- package/lib/FocusZoneNativeComponent.win32.js +3 -0
- package/lib/FocusZoneNativeComponent.win32.js.map +1 -0
- package/lib/__tests__/FocusZone.test.js +68 -70
- package/lib/__tests__/FocusZone.test.js.map +1 -1
- package/lib/__tests__/FocusZone.types.test.d.ts +2 -0
- package/lib/__tests__/FocusZone.types.test.d.ts.map +1 -0
- package/lib/__tests__/FocusZone.types.test.js +26 -0
- package/lib/__tests__/FocusZone.types.test.js.map +1 -0
- package/macos/RCTFocusZone.m +76 -17
- package/macos/RCTFocusZoneComponentView.h +14 -0
- package/macos/RCTFocusZoneComponentView.mm +201 -0
- package/package.json +30 -8
- package/react-native.config.cjs +15 -0
- package/src/FocusZone.tsx +50 -0
- package/src/FocusZone.types.ts +16 -16
- package/src/FocusZoneNativeComponent.ts +8 -6
- package/src/FocusZoneNativeComponent.win32.ts +18 -0
- package/src/__tests__/FocusZone.test.tsx +105 -125
- package/src/__tests__/FocusZone.types.test.ts +36 -0
- package/tsconfig.json +5 -7
- package/windows/ExperimentalFeatures.props +33 -0
- package/windows/FRNFocusZone/FRNFocusZone.def +3 -0
- package/windows/FRNFocusZone/FRNFocusZone.rc +0 -0
- package/windows/FRNFocusZone/FRNFocusZone.vcxproj +147 -0
- package/windows/FRNFocusZone/FRNFocusZone.vcxproj.filters +44 -0
- package/windows/FRNFocusZone/FocusZoneComponentView.cpp +590 -0
- package/windows/FRNFocusZone/FocusZoneComponentView.h +7 -0
- package/windows/FRNFocusZone/ReactPackageProvider.cpp +24 -0
- package/windows/FRNFocusZone/ReactPackageProvider.h +24 -0
- package/windows/FRNFocusZone/ReactPackageProvider.idl +9 -0
- package/windows/FRNFocusZone/codegen/.clang-format +2 -0
- package/windows/FRNFocusZone/codegen/react/components/FRNFocusZoneSpec/FocusZone.g.h +243 -0
- package/windows/FRNFocusZone/packages.lock.json +151 -0
- package/windows/FRNFocusZone/pch.cpp +1 -0
- package/windows/FRNFocusZone/pch.h +40 -0
- package/windows/FRNFocusZone/resource.h +5 -0
- package/windows/FRNFocusZone/targetver.h +8 -0
- package/windows/FRNFocusZone.sln +43 -0
- package/windows/NuGet.Config +13 -0
- package/src/FocusZone.ts +0 -57
- package/src/__tests__/__snapshots__/FocusZone.test.tsx.snap +0 -1516
|
@@ -0,0 +1,590 @@
|
|
|
1
|
+
#include "pch.h"
|
|
2
|
+
|
|
3
|
+
#include "FocusZoneComponentView.h"
|
|
4
|
+
|
|
5
|
+
#ifdef RNW_NEW_ARCH
|
|
6
|
+
|
|
7
|
+
#include "codegen/react/components/FRNFocusZoneSpec/FocusZone.g.h"
|
|
8
|
+
|
|
9
|
+
namespace winrt::FRNFocusZone {
|
|
10
|
+
|
|
11
|
+
namespace winrtComp = winrt::Microsoft::ReactNative::Composition;
|
|
12
|
+
namespace winrtInput = winrt::Microsoft::ReactNative::Composition::Input;
|
|
13
|
+
namespace winrtRN = winrt::Microsoft::ReactNative;
|
|
14
|
+
|
|
15
|
+
void CollectFocusableDescendants(
|
|
16
|
+
const winrtRN::ComponentView &view,
|
|
17
|
+
std::vector<winrtRN::ComponentView> &descendants) {
|
|
18
|
+
auto children = view.Children();
|
|
19
|
+
for (uint32_t index = 0; index < children.Size(); ++index) {
|
|
20
|
+
auto child = children.GetAt(index);
|
|
21
|
+
auto firstFocusable = winrtComp::FocusManager::FindFirstFocusableElement(child);
|
|
22
|
+
if (firstFocusable && firstFocusable.Tag() == child.Tag()) {
|
|
23
|
+
descendants.push_back(child);
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
CollectFocusableDescendants(child, descendants);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
struct FocusZoneComponentView
|
|
31
|
+
: winrt::implements<FocusZoneComponentView, winrt::Windows::Foundation::IInspectable>,
|
|
32
|
+
FRNFocusZoneCodegen::BaseFocusZone<FocusZoneComponentView> {
|
|
33
|
+
void Initialize(const winrtRN::ComponentView &view) noexcept override {
|
|
34
|
+
m_containerTag = view.Tag();
|
|
35
|
+
|
|
36
|
+
view.KeyDown(
|
|
37
|
+
[weakThis = get_weak()](
|
|
38
|
+
const winrt::Windows::Foundation::IInspectable &sender,
|
|
39
|
+
const winrtInput::KeyRoutedEventArgs &args) noexcept {
|
|
40
|
+
if (auto strongThis = weakThis.get()) {
|
|
41
|
+
if (auto componentView = sender.try_as<winrtRN::ComponentView>()) {
|
|
42
|
+
strongThis->HandleKeyDown(componentView, args);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
view.GotFocus(
|
|
48
|
+
[weakThis = get_weak()](
|
|
49
|
+
const winrt::Windows::Foundation::IInspectable &sender,
|
|
50
|
+
const winrtInput::RoutedEventArgs &) noexcept {
|
|
51
|
+
if (auto strongThis = weakThis.get()) {
|
|
52
|
+
if (auto componentView = sender.try_as<winrtRN::ComponentView>()) {
|
|
53
|
+
strongThis->HandleGotFocus(componentView);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
view.GettingFocus(
|
|
59
|
+
[weakThis = get_weak()](
|
|
60
|
+
const winrt::Windows::Foundation::IInspectable &sender,
|
|
61
|
+
const winrtRN::GettingFocusEventArgs &args) noexcept {
|
|
62
|
+
if (auto strongThis = weakThis.get()) {
|
|
63
|
+
if (auto componentView = sender.try_as<winrtRN::ComponentView>()) {
|
|
64
|
+
strongThis->HandleGettingFocus(componentView, args);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private:
|
|
71
|
+
void HandleKeyDown(const winrtRN::ComponentView &view, const winrtInput::KeyRoutedEventArgs &args) noexcept {
|
|
72
|
+
using winrt::Windows::System::VirtualKey;
|
|
73
|
+
|
|
74
|
+
if (Disabled()) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (args.Key() == VirtualKey::Tab) {
|
|
79
|
+
HandleTabKey(view, args);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
int direction = 0;
|
|
84
|
+
bool horizontalNavigation = false;
|
|
85
|
+
bool moveToStart = false;
|
|
86
|
+
bool moveToEnd = false;
|
|
87
|
+
|
|
88
|
+
switch (args.Key()) {
|
|
89
|
+
case VirtualKey::Down:
|
|
90
|
+
if (!IsVertical()) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
direction = 1;
|
|
94
|
+
horizontalNavigation = false;
|
|
95
|
+
break;
|
|
96
|
+
case VirtualKey::Up:
|
|
97
|
+
if (!IsVertical()) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
direction = -1;
|
|
101
|
+
horizontalNavigation = false;
|
|
102
|
+
break;
|
|
103
|
+
case VirtualKey::Right:
|
|
104
|
+
if (!IsHorizontal()) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
direction = 1;
|
|
108
|
+
horizontalNavigation = true;
|
|
109
|
+
break;
|
|
110
|
+
case VirtualKey::Left:
|
|
111
|
+
if (!IsHorizontal()) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
direction = -1;
|
|
115
|
+
horizontalNavigation = true;
|
|
116
|
+
break;
|
|
117
|
+
case VirtualKey::Home:
|
|
118
|
+
moveToStart = true;
|
|
119
|
+
break;
|
|
120
|
+
case VirtualKey::End:
|
|
121
|
+
moveToEnd = true;
|
|
122
|
+
break;
|
|
123
|
+
default:
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const bool moved =
|
|
128
|
+
Use2DNavigation() && direction != 0
|
|
129
|
+
? MoveFocus2D(view, direction, horizontalNavigation, ShouldWrap())
|
|
130
|
+
: MoveFocus(view, direction, moveToStart, moveToEnd, ShouldWrap());
|
|
131
|
+
if (moved) {
|
|
132
|
+
args.Handled(true);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
void HandleTabKey(const winrtRN::ComponentView &view, const winrtInput::KeyRoutedEventArgs &args) noexcept {
|
|
137
|
+
const bool forward = (::GetKeyState(VK_SHIFT) & 0x8000) == 0;
|
|
138
|
+
const auto tabNavigation = TabNavigation();
|
|
139
|
+
|
|
140
|
+
if (tabNavigation == "Normal") {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (tabNavigation == "NavigateWrap" || tabNavigation == "NavigateStopAtEnds") {
|
|
145
|
+
const bool wrap = tabNavigation == "NavigateWrap";
|
|
146
|
+
MoveFocus(view, forward ? 1 : -1, false, false, wrap);
|
|
147
|
+
args.Handled(true);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (FocusOutsideZone(view, forward)) {
|
|
152
|
+
args.Handled(true);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// If the manual island walk cannot find an outside target, place focus at
|
|
157
|
+
// the zone edge and let RNW's default Tab navigation complete the exit.
|
|
158
|
+
m_inTabTrap = true;
|
|
159
|
+
FocusEdgeDescendant(view, !forward);
|
|
160
|
+
m_inTabTrap = false;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
bool MoveFocus(
|
|
164
|
+
const winrtRN::ComponentView &view,
|
|
165
|
+
int direction,
|
|
166
|
+
bool moveToStart,
|
|
167
|
+
bool moveToEnd,
|
|
168
|
+
bool wrap) noexcept {
|
|
169
|
+
std::vector<winrtRN::ComponentView> candidates;
|
|
170
|
+
CollectFocusableDescendants(view, candidates);
|
|
171
|
+
if (candidates.empty()) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
auto root = view.as<winrtComp::ComponentView>().Root();
|
|
176
|
+
int64_t focusedTag = 0;
|
|
177
|
+
int focusedIndex = -1;
|
|
178
|
+
if (root) {
|
|
179
|
+
if (auto focused = root.GetFocusedComponent()) {
|
|
180
|
+
focusedTag = focused.Tag();
|
|
181
|
+
for (int index = 0; index < static_cast<int>(candidates.size()); ++index) {
|
|
182
|
+
if (candidates[index].Tag() == focusedTag) {
|
|
183
|
+
focusedIndex = index;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
auto tryFocusFrom = [&](int startIndex, int step) noexcept {
|
|
191
|
+
const int count = static_cast<int>(candidates.size());
|
|
192
|
+
for (int offset = 0; offset < count; ++offset) {
|
|
193
|
+
int index = startIndex + step * offset;
|
|
194
|
+
if (wrap) {
|
|
195
|
+
index = ((index % count) + count) % count;
|
|
196
|
+
} else if (index < 0 || index >= count) {
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (root) {
|
|
201
|
+
if (auto focused = root.GetFocusedComponent()) {
|
|
202
|
+
if (IsWithinContainer(focused, candidates[index]) ||
|
|
203
|
+
IsWithinContainer(candidates[index], focused) ||
|
|
204
|
+
OverlapsFocusedComponent(view, focused, candidates[index])) {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (!candidates[index].TryFocus(winrtRN::FocusState::Keyboard)) {
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
int64_t newFocusedTag = 0;
|
|
215
|
+
if (root) {
|
|
216
|
+
if (auto focused = root.GetFocusedComponent()) {
|
|
217
|
+
newFocusedTag = focused.Tag();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (focusedTag != 0 && newFocusedTag == focusedTag) {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
return false;
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const int count = static_cast<int>(candidates.size());
|
|
229
|
+
if (moveToStart) {
|
|
230
|
+
return tryFocusFrom(0, 1);
|
|
231
|
+
}
|
|
232
|
+
if (moveToEnd) {
|
|
233
|
+
return tryFocusFrom(count - 1, -1);
|
|
234
|
+
}
|
|
235
|
+
if (focusedIndex < 0) {
|
|
236
|
+
return direction > 0 ? tryFocusFrom(0, 1) : tryFocusFrom(count - 1, -1);
|
|
237
|
+
}
|
|
238
|
+
return tryFocusFrom(focusedIndex + direction, direction);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
struct RelativeRect {
|
|
242
|
+
double x;
|
|
243
|
+
double y;
|
|
244
|
+
double width;
|
|
245
|
+
double height;
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
static std::optional<RelativeRect> RelativeRectWithin(
|
|
249
|
+
const winrtRN::ComponentView &container,
|
|
250
|
+
const winrtRN::ComponentView &node) noexcept {
|
|
251
|
+
const auto metrics = node.LayoutMetrics();
|
|
252
|
+
RelativeRect rect{
|
|
253
|
+
metrics.Frame.X,
|
|
254
|
+
metrics.Frame.Y,
|
|
255
|
+
metrics.Frame.Width,
|
|
256
|
+
metrics.Frame.Height};
|
|
257
|
+
|
|
258
|
+
auto current = node.Parent();
|
|
259
|
+
while (current && current.Tag() != container.Tag()) {
|
|
260
|
+
const auto parentMetrics = current.LayoutMetrics();
|
|
261
|
+
rect.x += parentMetrics.Frame.X;
|
|
262
|
+
rect.y += parentMetrics.Frame.Y;
|
|
263
|
+
current = current.Parent();
|
|
264
|
+
}
|
|
265
|
+
return current ? std::optional<RelativeRect>{rect} : std::nullopt;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
static bool OverlapsFocusedComponent(
|
|
269
|
+
const winrtRN::ComponentView &container,
|
|
270
|
+
const winrtRN::ComponentView &focused,
|
|
271
|
+
const winrtRN::ComponentView &candidate) noexcept {
|
|
272
|
+
const auto focusedRect = RelativeRectWithin(container, focused);
|
|
273
|
+
const auto candidateRect = RelativeRectWithin(container, candidate);
|
|
274
|
+
if (!focusedRect || !candidateRect) {
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const double overlapWidth =
|
|
279
|
+
std::min(focusedRect->x + focusedRect->width, candidateRect->x + candidateRect->width) -
|
|
280
|
+
std::max(focusedRect->x, candidateRect->x);
|
|
281
|
+
const double overlapHeight =
|
|
282
|
+
std::min(focusedRect->y + focusedRect->height, candidateRect->y + candidateRect->height) -
|
|
283
|
+
std::max(focusedRect->y, candidateRect->y);
|
|
284
|
+
return overlapWidth > 0.5 && overlapHeight > 0.5;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
bool MoveFocus2D(
|
|
288
|
+
const winrtRN::ComponentView &view,
|
|
289
|
+
int direction,
|
|
290
|
+
bool horizontal,
|
|
291
|
+
bool wrap) noexcept {
|
|
292
|
+
auto root = view.as<winrtComp::ComponentView>().Root();
|
|
293
|
+
if (!root) {
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
auto focused = root.GetFocusedComponent();
|
|
298
|
+
if (!focused || !IsWithinContainer(view, focused)) {
|
|
299
|
+
return MoveFocus(view, direction, false, false, wrap);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const auto focusedRect = RelativeRectWithin(view, focused);
|
|
303
|
+
if (!focusedRect) {
|
|
304
|
+
return MoveFocus(view, direction, false, false, wrap);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const double focusedPrimary =
|
|
308
|
+
horizontal ? focusedRect->x + focusedRect->width / 2 : focusedRect->y + focusedRect->height / 2;
|
|
309
|
+
const double focusedCross =
|
|
310
|
+
horizontal ? focusedRect->y + focusedRect->height / 2 : focusedRect->x + focusedRect->width / 2;
|
|
311
|
+
|
|
312
|
+
struct Candidate {
|
|
313
|
+
winrtRN::ComponentView view;
|
|
314
|
+
double primary;
|
|
315
|
+
double cross;
|
|
316
|
+
double distance;
|
|
317
|
+
size_t order;
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
std::vector<winrtRN::ComponentView> descendants;
|
|
321
|
+
CollectFocusableDescendants(view, descendants);
|
|
322
|
+
std::vector<Candidate> directionalCandidates;
|
|
323
|
+
std::vector<Candidate> allCandidates;
|
|
324
|
+
allCandidates.reserve(descendants.size());
|
|
325
|
+
|
|
326
|
+
for (size_t order = 0; order < descendants.size(); ++order) {
|
|
327
|
+
const auto &candidate = descendants[order];
|
|
328
|
+
if (candidate.Tag() == focused.Tag()) {
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
const auto rect = RelativeRectWithin(view, candidate);
|
|
332
|
+
if (!rect) {
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const double primary = horizontal ? rect->x + rect->width / 2 : rect->y + rect->height / 2;
|
|
337
|
+
const double cross = horizontal ? rect->y + rect->height / 2 : rect->x + rect->width / 2;
|
|
338
|
+
const double primaryDelta = primary - focusedPrimary;
|
|
339
|
+
const double crossDelta = cross - focusedCross;
|
|
340
|
+
Candidate ranked{
|
|
341
|
+
candidate,
|
|
342
|
+
primary,
|
|
343
|
+
cross,
|
|
344
|
+
std::hypot(primaryDelta, crossDelta),
|
|
345
|
+
order};
|
|
346
|
+
allCandidates.push_back(ranked);
|
|
347
|
+
if (direction * primaryDelta > 0.5) {
|
|
348
|
+
directionalCandidates.push_back(ranked);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
auto tryRankedCandidates = [&](std::vector<Candidate> &candidates) noexcept {
|
|
353
|
+
std::sort(candidates.begin(), candidates.end(), [focusedCross](const Candidate &left, const Candidate &right) {
|
|
354
|
+
if (left.distance != right.distance) {
|
|
355
|
+
return left.distance < right.distance;
|
|
356
|
+
}
|
|
357
|
+
const double leftCross = std::abs(left.cross - focusedCross);
|
|
358
|
+
const double rightCross = std::abs(right.cross - focusedCross);
|
|
359
|
+
return leftCross != rightCross ? leftCross < rightCross : left.order < right.order;
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
for (const auto &candidate : candidates) {
|
|
363
|
+
if (!candidate.view.TryFocus(winrtRN::FocusState::Keyboard)) {
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
if (auto newlyFocused = root.GetFocusedComponent(); newlyFocused && newlyFocused.Tag() != focused.Tag()) {
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return false;
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
if (tryRankedCandidates(directionalCandidates)) {
|
|
374
|
+
return true;
|
|
375
|
+
}
|
|
376
|
+
if (!wrap || allCandidates.empty()) {
|
|
377
|
+
return false;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const auto edge = std::minmax_element(
|
|
381
|
+
allCandidates.begin(),
|
|
382
|
+
allCandidates.end(),
|
|
383
|
+
[](const Candidate &left, const Candidate &right) { return left.primary < right.primary; });
|
|
384
|
+
const double wrapEdge = direction > 0 ? edge.first->primary : edge.second->primary;
|
|
385
|
+
for (auto &candidate : allCandidates) {
|
|
386
|
+
const double edgeDistance = std::abs(candidate.primary - wrapEdge);
|
|
387
|
+
const double crossDistance = std::abs(candidate.cross - focusedCross);
|
|
388
|
+
candidate.distance = edgeDistance * 10000 + crossDistance;
|
|
389
|
+
}
|
|
390
|
+
return tryRankedCandidates(allCandidates);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
static int SiblingIndex(const winrtRN::ComponentView &node, const winrtRN::ComponentView &parent) noexcept {
|
|
394
|
+
auto siblings = parent.Children();
|
|
395
|
+
for (uint32_t index = 0; index < siblings.Size(); ++index) {
|
|
396
|
+
if (siblings.GetAt(index).Tag() == node.Tag()) {
|
|
397
|
+
return static_cast<int>(index);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return -1;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
static winrtRN::ComponentView NextSkippingSubtree(const winrtRN::ComponentView &node) noexcept {
|
|
404
|
+
auto current = node;
|
|
405
|
+
while (current) {
|
|
406
|
+
auto parent = current.Parent();
|
|
407
|
+
if (!parent) {
|
|
408
|
+
return nullptr;
|
|
409
|
+
}
|
|
410
|
+
auto siblings = parent.Children();
|
|
411
|
+
const int index = SiblingIndex(current, parent);
|
|
412
|
+
if (index >= 0 && static_cast<uint32_t>(index + 1) < siblings.Size()) {
|
|
413
|
+
return siblings.GetAt(index + 1);
|
|
414
|
+
}
|
|
415
|
+
current = parent;
|
|
416
|
+
}
|
|
417
|
+
return nullptr;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
static winrtRN::ComponentView PreOrderNext(const winrtRN::ComponentView &node) noexcept {
|
|
421
|
+
auto children = node.Children();
|
|
422
|
+
return children.Size() > 0 ? children.GetAt(0) : NextSkippingSubtree(node);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
static winrtRN::ComponentView RightmostDescendant(const winrtRN::ComponentView &node) noexcept {
|
|
426
|
+
auto current = node;
|
|
427
|
+
while (true) {
|
|
428
|
+
auto children = current.Children();
|
|
429
|
+
if (children.Size() == 0) {
|
|
430
|
+
return current;
|
|
431
|
+
}
|
|
432
|
+
current = children.GetAt(children.Size() - 1);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
static winrtRN::ComponentView PreOrderPrevious(const winrtRN::ComponentView &node) noexcept {
|
|
437
|
+
auto parent = node.Parent();
|
|
438
|
+
if (!parent) {
|
|
439
|
+
return nullptr;
|
|
440
|
+
}
|
|
441
|
+
const int index = SiblingIndex(node, parent);
|
|
442
|
+
return index <= 0 ? parent : RightmostDescendant(parent.Children().GetAt(index - 1));
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
bool FocusOutsideZone(const winrtRN::ComponentView &container, bool forward) noexcept {
|
|
446
|
+
if (forward) {
|
|
447
|
+
for (auto node = NextSkippingSubtree(container); node; node = PreOrderNext(node)) {
|
|
448
|
+
auto candidate = winrtComp::FocusManager::FindFirstFocusableElement(node);
|
|
449
|
+
if (candidate && candidate.TryFocus(winrtRN::FocusState::Keyboard)) {
|
|
450
|
+
return true;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
} else {
|
|
454
|
+
for (auto node = PreOrderPrevious(container); node; node = PreOrderPrevious(node)) {
|
|
455
|
+
auto candidate = winrtComp::FocusManager::FindLastFocusableElement(node);
|
|
456
|
+
if (candidate && candidate.TryFocus(winrtRN::FocusState::Keyboard)) {
|
|
457
|
+
return true;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
return false;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
bool FocusEdgeDescendant(const winrtRN::ComponentView &view, bool last) noexcept {
|
|
465
|
+
auto candidate =
|
|
466
|
+
last
|
|
467
|
+
? winrtComp::FocusManager::FindLastFocusableElement(view)
|
|
468
|
+
: winrtComp::FocusManager::FindFirstFocusableElement(view);
|
|
469
|
+
return candidate && candidate.TryFocus(winrtRN::FocusState::Keyboard);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
void HandleGotFocus(const winrtRN::ComponentView &container) noexcept {
|
|
473
|
+
if (m_inTabTrap) {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
if (auto root = container.as<winrtComp::ComponentView>().Root()) {
|
|
477
|
+
if (auto focused = root.GetFocusedComponent()) {
|
|
478
|
+
if (focused.Tag() != m_containerTag && IsWithinContainer(container, focused)) {
|
|
479
|
+
m_lastFocusedTag = focused.Tag();
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
void HandleGettingFocus(
|
|
486
|
+
const winrtRN::ComponentView &container,
|
|
487
|
+
const winrtRN::GettingFocusEventArgs &args) noexcept {
|
|
488
|
+
if (Disabled()) {
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const int64_t preferredTag = m_lastFocusedTag != 0 ? m_lastFocusedTag : DefaultTabbableTag();
|
|
493
|
+
if (preferredTag == 0) {
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
if (auto oldFocus = args.OldFocusedComponent(); oldFocus && IsWithinContainer(container, oldFocus)) {
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
if (auto newFocus = args.NewFocusedComponent(); newFocus && newFocus.Tag() == preferredTag) {
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
std::vector<winrtRN::ComponentView> candidates;
|
|
504
|
+
CollectFocusableDescendants(container, candidates);
|
|
505
|
+
for (const auto &candidate : candidates) {
|
|
506
|
+
if (candidate.Tag() == preferredTag) {
|
|
507
|
+
args.TrySetNewFocusedComponent(candidate);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
static bool IsWithinContainer(
|
|
514
|
+
const winrtRN::ComponentView &container,
|
|
515
|
+
const winrtRN::ComponentView &node) noexcept {
|
|
516
|
+
auto current = node;
|
|
517
|
+
while (current) {
|
|
518
|
+
if (current.Tag() == container.Tag()) {
|
|
519
|
+
return true;
|
|
520
|
+
}
|
|
521
|
+
current = current.Parent();
|
|
522
|
+
}
|
|
523
|
+
return false;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
bool Disabled() const noexcept {
|
|
527
|
+
auto props = Props();
|
|
528
|
+
return props && props->disabled.value_or(false);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
std::string Direction() const noexcept {
|
|
532
|
+
auto props = Props();
|
|
533
|
+
return props && props->focusZoneDirection ? *props->focusZoneDirection : "bidirectional";
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
bool IsVertical() const noexcept {
|
|
537
|
+
const auto direction = Direction();
|
|
538
|
+
return direction == "vertical" || direction == "bidirectional";
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
bool IsHorizontal() const noexcept {
|
|
542
|
+
const auto direction = Direction();
|
|
543
|
+
return direction == "horizontal" || direction == "bidirectional";
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
bool ShouldWrap() const noexcept {
|
|
547
|
+
auto props = Props();
|
|
548
|
+
return props && props->navigateAtEnd && *props->navigateAtEnd == "NavigateWrap";
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
bool Use2DNavigation() const noexcept {
|
|
552
|
+
auto props = Props();
|
|
553
|
+
return props && props->use2DNavigation.value_or(false);
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
std::string TabNavigation() const noexcept {
|
|
557
|
+
auto props = Props();
|
|
558
|
+
return props && props->tabKeyNavigation ? *props->tabKeyNavigation : "None";
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
int64_t DefaultTabbableTag() const noexcept {
|
|
562
|
+
auto props = Props();
|
|
563
|
+
if (!props) {
|
|
564
|
+
return 0;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const auto &value = props->defaultTabbableElement;
|
|
568
|
+
if (value.Type() == winrtRN::JSValueType::Int64) {
|
|
569
|
+
return value.AsInt64();
|
|
570
|
+
}
|
|
571
|
+
if (value.Type() == winrtRN::JSValueType::Double) {
|
|
572
|
+
return static_cast<int64_t>(value.AsDouble());
|
|
573
|
+
}
|
|
574
|
+
return 0;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
int64_t m_containerTag{0};
|
|
578
|
+
int64_t m_lastFocusedTag{0};
|
|
579
|
+
bool m_inTabTrap{false};
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
} // namespace winrt::FRNFocusZone
|
|
583
|
+
|
|
584
|
+
void RegisterFocusZoneComponentView(const winrt::Microsoft::ReactNative::IReactPackageBuilder &packageBuilder) noexcept {
|
|
585
|
+
using ComponentView = winrt::FRNFocusZone::FocusZoneComponentView;
|
|
586
|
+
|
|
587
|
+
FRNFocusZoneCodegen::RegisterFocusZoneNativeComponent<ComponentView>(packageBuilder, {});
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
#endif
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#include "pch.h"
|
|
2
|
+
|
|
3
|
+
#include "ReactPackageProvider.h"
|
|
4
|
+
#if __has_include("ReactPackageProvider.g.cpp")
|
|
5
|
+
#include "ReactPackageProvider.g.cpp"
|
|
6
|
+
#endif
|
|
7
|
+
|
|
8
|
+
#include "FocusZoneComponentView.h"
|
|
9
|
+
|
|
10
|
+
using namespace winrt::Microsoft::ReactNative;
|
|
11
|
+
|
|
12
|
+
namespace winrt::FRNFocusZone::implementation
|
|
13
|
+
{
|
|
14
|
+
|
|
15
|
+
void ReactPackageProvider::CreatePackage(IReactPackageBuilder const &packageBuilder) noexcept
|
|
16
|
+
{
|
|
17
|
+
#ifdef RNW_NEW_ARCH
|
|
18
|
+
RegisterFocusZoneComponentView(packageBuilder);
|
|
19
|
+
#else
|
|
20
|
+
UNREFERENCED_PARAMETER(packageBuilder);
|
|
21
|
+
#endif
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
} // namespace winrt::FRNFocusZone::implementation
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "ReactPackageProvider.g.h"
|
|
4
|
+
|
|
5
|
+
using namespace winrt::Microsoft::ReactNative;
|
|
6
|
+
|
|
7
|
+
namespace winrt::FRNFocusZone::implementation
|
|
8
|
+
{
|
|
9
|
+
|
|
10
|
+
struct ReactPackageProvider : ReactPackageProviderT<ReactPackageProvider>
|
|
11
|
+
{
|
|
12
|
+
ReactPackageProvider() = default;
|
|
13
|
+
|
|
14
|
+
void CreatePackage(IReactPackageBuilder const &packageBuilder) noexcept;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
} // namespace winrt::FRNFocusZone::implementation
|
|
18
|
+
|
|
19
|
+
namespace winrt::FRNFocusZone::factory_implementation
|
|
20
|
+
{
|
|
21
|
+
|
|
22
|
+
struct ReactPackageProvider : ReactPackageProviderT<ReactPackageProvider, implementation::ReactPackageProvider> {};
|
|
23
|
+
|
|
24
|
+
} // namespace winrt::FRNFocusZone::factory_implementation
|