|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "FocusOrderingHelper.h" |
| 9 | +#include <android/log.h> |
| 10 | +#include <react/renderer/uimanager/UIManager.h> |
| 11 | + |
| 12 | +namespace facebook::react { |
| 13 | + |
| 14 | +int majorAxisDistanceRaw( |
| 15 | + FocusDirection focusDirection, |
| 16 | + Rect source, |
| 17 | + Rect dest) { |
| 18 | + switch (focusDirection) { |
| 19 | + case FocusDirection::FocusLeft: |
| 20 | + return static_cast<int>( |
| 21 | + source.origin.x - (dest.origin.x + dest.size.width)); |
| 22 | + case FocusDirection::FocusRight: |
| 23 | + return static_cast<int>( |
| 24 | + dest.origin.x - (source.origin.x + source.size.width)); |
| 25 | + case FocusDirection::FocusUp: |
| 26 | + return static_cast<int>( |
| 27 | + source.origin.y - (dest.origin.y + dest.size.height)); |
| 28 | + case FocusDirection::FocusDown: |
| 29 | + return static_cast<int>( |
| 30 | + dest.origin.y - (source.origin.y + source.size.height)); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +int majorAxisDistance(FocusDirection focusDirection, Rect source, Rect dest) { |
| 35 | + return std::max(0, majorAxisDistanceRaw(focusDirection, source, dest)); |
| 36 | +} |
| 37 | + |
| 38 | +int minorAxisDistance(FocusDirection direction, Rect source, Rect dest) { |
| 39 | + switch (direction) { |
| 40 | + case FocusDirection::FocusLeft: |
| 41 | + case FocusDirection::FocusRight: |
| 42 | + // the distance between the center verticals |
| 43 | + return static_cast<int>(abs((source.getMidY() - dest.getMidY()))); |
| 44 | + case FocusDirection::FocusUp: |
| 45 | + case FocusDirection::FocusDown: |
| 46 | + // the distance between the center horizontals |
| 47 | + return static_cast<int>(abs((source.getMidX() - dest.getMidX()))); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// 13 is a magic number that comes from Android's implementation. We opt to use |
| 52 | +// this to get the same focus ordering as Android. See: |
| 53 | +// https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/core/java/android/view/FocusFinder.java;l=547 |
| 54 | +int getWeightedDistanceFor(int majorAxisDistance, int minorAxisDistance) { |
| 55 | + return 13 * majorAxisDistance * majorAxisDistance + |
| 56 | + minorAxisDistance * minorAxisDistance; |
| 57 | +} |
| 58 | + |
| 59 | +// Make sure dest rect is actually on the direction of focus |
| 60 | +bool isCandidate(Rect source, Rect dest, FocusDirection focusDirection) { |
| 61 | + switch (focusDirection) { |
| 62 | + case FocusDirection::FocusLeft: |
| 63 | + return ((source.origin.x + source.size.width) > |
| 64 | + (dest.origin.x + dest.size.width) || |
| 65 | + source.origin.x >= (dest.origin.x + dest.size.width)) && |
| 66 | + source.origin.x > dest.origin.x; |
| 67 | + case FocusDirection::FocusRight: |
| 68 | + return (source.origin.x < dest.origin.x || |
| 69 | + (source.origin.x + source.size.width) <= dest.origin.x) && |
| 70 | + (source.origin.x + source.size.width) < |
| 71 | + (dest.origin.x + dest.size.width); |
| 72 | + case FocusDirection::FocusUp: |
| 73 | + return ((source.origin.y + source.size.height) > |
| 74 | + (dest.origin.y + dest.size.height) || |
| 75 | + source.origin.y >= (dest.origin.y + dest.size.height)) && |
| 76 | + source.origin.y > dest.origin.y; |
| 77 | + case FocusDirection::FocusDown: |
| 78 | + return (source.origin.y < dest.origin.y || |
| 79 | + (source.origin.y + source.size.height) <= dest.origin.y) && |
| 80 | + ((source.origin.y + source.size.height) < |
| 81 | + (dest.origin.y + dest.size.height)); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +bool isBetterCandidate( |
| 86 | + FocusDirection focusDirection, |
| 87 | + Rect source, |
| 88 | + Rect current, |
| 89 | + Rect candidate) { |
| 90 | + if (!isCandidate(source, candidate, focusDirection)) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + int candidateWeightedDistance = getWeightedDistanceFor( |
| 95 | + majorAxisDistance(focusDirection, source, candidate), |
| 96 | + minorAxisDistance(focusDirection, source, candidate)); |
| 97 | + |
| 98 | + int currentWeightedDistance = getWeightedDistanceFor( |
| 99 | + majorAxisDistance(focusDirection, source, current), |
| 100 | + minorAxisDistance(focusDirection, source, current)); |
| 101 | + |
| 102 | + return candidateWeightedDistance < currentWeightedDistance; |
| 103 | +} |
| 104 | + |
| 105 | +void FocusOrderingHelper::traverseAndUpdateNextFocusableElement( |
| 106 | + const ShadowNode::Shared& parentShadowNode, |
| 107 | + const ShadowNode::Shared& focusedShadowNode, |
| 108 | + const ShadowNode::Shared& currNode, |
| 109 | + FocusDirection focusDirection, |
| 110 | + const UIManager& uimanager, |
| 111 | + Rect sourceRect, |
| 112 | + std::optional<Rect>& nextRect, |
| 113 | + ShadowNode::Shared& nextNode) { |
| 114 | + const auto* props = |
| 115 | + dynamic_cast<const ViewProps*>(currNode->getProps().get()); |
| 116 | + |
| 117 | + // We only care about focusable elements since only they can be both |
| 118 | + // focused and present in the hierarchy |
| 119 | + if (currNode->getTraits().check(ShadowNodeTraits::Trait::KeyboardFocusable) || |
| 120 | + (props != nullptr && |
| 121 | + (props->focusable || props->accessible || props->hasTVPreferredFocus))) { |
| 122 | + LayoutMetrics nodeLayoutMetrics = uimanager.getRelativeLayoutMetrics( |
| 123 | + *currNode, parentShadowNode.get(), {.includeTransform = true}); |
| 124 | + |
| 125 | + if (nextRect == std::nullopt && |
| 126 | + isCandidate(sourceRect, nodeLayoutMetrics.frame, focusDirection)) { |
| 127 | + nextNode = currNode; |
| 128 | + nextRect = nodeLayoutMetrics.frame; |
| 129 | + } else if ( |
| 130 | + nextRect != std::nullopt && |
| 131 | + isBetterCandidate( |
| 132 | + focusDirection, |
| 133 | + sourceRect, |
| 134 | + nextRect.value(), |
| 135 | + nodeLayoutMetrics.frame)) { |
| 136 | + nextNode = currNode; |
| 137 | + nextRect = nodeLayoutMetrics.frame; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + for (auto& child : currNode->getChildren()) { |
| 142 | + if (child->getTraits().check(ShadowNodeTraits::Trait::RootNodeKind)) { |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + traverseAndUpdateNextFocusableElement( |
| 147 | + parentShadowNode, |
| 148 | + focusedShadowNode, |
| 149 | + child, |
| 150 | + focusDirection, |
| 151 | + uimanager, |
| 152 | + sourceRect, |
| 153 | + nextRect, |
| 154 | + nextNode); |
| 155 | + }; |
| 156 | +}; |
| 157 | + |
| 158 | +ShadowNode::Shared FocusOrderingHelper::findShadowNodeByTagRecursively( |
| 159 | + const ShadowNode::Shared& parentShadowNode, |
| 160 | + Tag tag) { |
| 161 | + if (parentShadowNode->getTag() == tag) { |
| 162 | + return parentShadowNode; |
| 163 | + } |
| 164 | + |
| 165 | + for (auto& shadowNode : parentShadowNode->getChildren()) { |
| 166 | + if (auto result = findShadowNodeByTagRecursively(shadowNode, tag)) { |
| 167 | + return result; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return nullptr; |
| 172 | +} |
| 173 | + |
| 174 | +std::optional<FocusDirection> FocusOrderingHelper::resolveFocusDirection( |
| 175 | + int direction) { |
| 176 | + switch (static_cast<FocusDirection>(direction)) { |
| 177 | + case FocusDirection::FocusDown: |
| 178 | + case FocusDirection::FocusUp: |
| 179 | + case FocusDirection::FocusRight: |
| 180 | + case FocusDirection::FocusLeft: |
| 181 | + return static_cast<FocusDirection>(direction); |
| 182 | + } |
| 183 | + |
| 184 | + return std::nullopt; |
| 185 | +} |
| 186 | +} // namespace facebook::react |
0 commit comments