@react-native-ohos/react-native-keyboard-controller 1.12.8 → 1.12.9

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 (166) hide show
  1. package/LICENSE +21 -21
  2. package/README.OpenSource +10 -10
  3. package/README.md +603 -19
  4. package/harmony/keyboard_controller/BuildProfile.ets +16 -16
  5. package/harmony/keyboard_controller/Index.ets +8 -29
  6. package/harmony/keyboard_controller/ResourceTable.txt +8 -8
  7. package/harmony/keyboard_controller/build-profile.json5 +31 -31
  8. package/harmony/keyboard_controller/hvigorfile.ts +6 -6
  9. package/harmony/keyboard_controller/obfuscation-rules.txt +17 -17
  10. package/harmony/keyboard_controller/oh-package-lock.json5 +28 -18
  11. package/harmony/keyboard_controller/oh-package.json5 +11 -11
  12. package/harmony/keyboard_controller/src/main/cpp/CMakeLists.txt +7 -7
  13. package/harmony/keyboard_controller/src/main/cpp/EventEmitters.cpp +119 -96
  14. package/harmony/keyboard_controller/src/main/cpp/EventEmitters.h +90 -111
  15. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentDescriptor.h +77 -98
  16. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentInstance.cpp +816 -274
  17. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentInstance.h +147 -101
  18. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewJSIBinder.h +43 -64
  19. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentDescriptor.h +64 -85
  20. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentInstance.cpp +88 -109
  21. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentInstance.h +63 -84
  22. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaJSIBinder.h +39 -60
  23. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardController.cpp +25 -46
  24. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardController.h +27 -48
  25. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardControllerPackage.cpp +43 -64
  26. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardControllerPackage.h +56 -77
  27. package/harmony/keyboard_controller/src/main/cpp/RNStatusBarManagerCompat.cpp +24 -45
  28. package/harmony/keyboard_controller/src/main/cpp/RNStatusBarManagerCompat.h +28 -49
  29. package/harmony/keyboard_controller/src/main/cpp/ViewHierarchyNavigator.cpp +186 -186
  30. package/harmony/keyboard_controller/src/main/cpp/ViewHierarchyNavigator.h +112 -112
  31. package/harmony/keyboard_controller/src/main/cpp/keyboardControllerPackage.cpp +43 -64
  32. package/harmony/keyboard_controller/src/main/cpp/keyboardControllerPackage.h +56 -77
  33. package/harmony/keyboard_controller/src/main/ets/Logger.ts +45 -66
  34. package/harmony/keyboard_controller/src/main/ets/RNKeyboardControllerPackage.ets +49 -70
  35. package/harmony/keyboard_controller/src/main/ets/RNKeyboardControllerTurboModule.ts +405 -245
  36. package/harmony/keyboard_controller/src/main/ets/RNStatusBarManagerCompatTurboModule.ts +107 -119
  37. package/harmony/keyboard_controller/src/main/ets/Type.ts +41 -62
  38. package/harmony/keyboard_controller/src/main/module.json +29 -29
  39. package/harmony/keyboard_controller/src/main/module.json5 +11 -11
  40. package/harmony/keyboard_controller/src/main/resources/base/element/string.json +8 -8
  41. package/harmony/keyboard_controller/src/main/resources/en_US/element/string.json +8 -8
  42. package/harmony/keyboard_controller/src/main/resources/zh_CN/element/string.json +8 -8
  43. package/harmony/keyboard_controller/ts.ets +8 -29
  44. package/harmony/keyboard_controller.har +0 -0
  45. package/lib/commonjs/animated.js +4 -16
  46. package/lib/commonjs/animated.js.map +1 -1
  47. package/lib/commonjs/bindings.js +3 -3
  48. package/lib/commonjs/bindings.js.map +1 -1
  49. package/lib/commonjs/bindings.native.js +3 -3
  50. package/lib/commonjs/bindings.native.js.map +1 -1
  51. package/lib/commonjs/components/KeyboardAvoidingView/hooks.js.map +1 -1
  52. package/lib/commonjs/components/KeyboardAvoidingView/index.js +3 -3
  53. package/lib/commonjs/components/KeyboardAvoidingView/index.js.map +1 -1
  54. package/lib/commonjs/components/KeyboardAwareScrollView/index.js +43 -41
  55. package/lib/commonjs/components/KeyboardAwareScrollView/index.js.map +1 -1
  56. package/lib/commonjs/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js +3 -3
  57. package/lib/commonjs/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js.map +1 -1
  58. package/lib/commonjs/components/KeyboardAwareScrollView/utils.js.map +1 -1
  59. package/lib/commonjs/components/KeyboardStickyView/index.js.map +1 -1
  60. package/lib/commonjs/components/KeyboardToolbar/Arrow.js.map +1 -1
  61. package/lib/commonjs/components/KeyboardToolbar/Button.js.map +1 -1
  62. package/lib/commonjs/components/KeyboardToolbar/colors.js.map +1 -1
  63. package/lib/commonjs/components/KeyboardToolbar/colors.native.js.map +1 -1
  64. package/lib/commonjs/components/KeyboardToolbar/index.js +3 -3
  65. package/lib/commonjs/components/KeyboardToolbar/index.js.map +1 -1
  66. package/lib/commonjs/components/KeyboardToolbar/types.js.map +1 -1
  67. package/lib/commonjs/components/hooks/useColorScheme.js.map +1 -1
  68. package/lib/commonjs/components/index.js.map +1 -1
  69. package/lib/commonjs/constants.js.map +1 -1
  70. package/lib/commonjs/context.js.map +1 -1
  71. package/lib/commonjs/hooks/index.js.map +1 -1
  72. package/lib/commonjs/hooks/useWindowDimensions/index.android.js.map +1 -1
  73. package/lib/commonjs/hooks/useWindowDimensions/index.js.map +1 -1
  74. package/lib/commonjs/index.js.map +1 -1
  75. package/lib/commonjs/internal.js +18 -18
  76. package/lib/commonjs/internal.js.map +1 -1
  77. package/lib/commonjs/monkey-patch.android.js +4 -4
  78. package/lib/commonjs/monkey-patch.android.js.map +1 -1
  79. package/lib/commonjs/monkey-patch.js.map +1 -1
  80. package/lib/commonjs/reanimated.js.map +1 -1
  81. package/lib/commonjs/reanimated.native.js.map +1 -1
  82. package/lib/commonjs/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
  83. package/lib/commonjs/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
  84. package/lib/commonjs/specs/NativeKeyboardController.js.map +1 -1
  85. package/lib/commonjs/specs/NativeStatusBarManagerCompat.js.map +1 -1
  86. package/lib/commonjs/types.js.map +1 -1
  87. package/lib/commonjs/utils.js.map +1 -1
  88. package/lib/module/animated.js +5 -17
  89. package/lib/module/animated.js.map +1 -1
  90. package/lib/module/bindings.js +3 -3
  91. package/lib/module/bindings.js.map +1 -1
  92. package/lib/module/bindings.native.js +3 -3
  93. package/lib/module/bindings.native.js.map +1 -1
  94. package/lib/module/components/KeyboardAvoidingView/hooks.js.map +1 -1
  95. package/lib/module/components/KeyboardAvoidingView/index.js +3 -3
  96. package/lib/module/components/KeyboardAvoidingView/index.js.map +1 -1
  97. package/lib/module/components/KeyboardAwareScrollView/index.js +44 -42
  98. package/lib/module/components/KeyboardAwareScrollView/index.js.map +1 -1
  99. package/lib/module/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js +3 -3
  100. package/lib/module/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js.map +1 -1
  101. package/lib/module/components/KeyboardAwareScrollView/utils.js.map +1 -1
  102. package/lib/module/components/KeyboardStickyView/index.js.map +1 -1
  103. package/lib/module/components/KeyboardToolbar/Arrow.js.map +1 -1
  104. package/lib/module/components/KeyboardToolbar/Button.js.map +1 -1
  105. package/lib/module/components/KeyboardToolbar/colors.js.map +1 -1
  106. package/lib/module/components/KeyboardToolbar/colors.native.js.map +1 -1
  107. package/lib/module/components/KeyboardToolbar/index.js +3 -3
  108. package/lib/module/components/KeyboardToolbar/index.js.map +1 -1
  109. package/lib/module/components/KeyboardToolbar/types.js.map +1 -1
  110. package/lib/module/components/hooks/useColorScheme.js.map +1 -1
  111. package/lib/module/components/index.js.map +1 -1
  112. package/lib/module/constants.js.map +1 -1
  113. package/lib/module/context.js.map +1 -1
  114. package/lib/module/hooks/index.js.map +1 -1
  115. package/lib/module/hooks/useWindowDimensions/index.android.js.map +1 -1
  116. package/lib/module/hooks/useWindowDimensions/index.js.map +1 -1
  117. package/lib/module/index.js.map +1 -1
  118. package/lib/module/internal.js +18 -18
  119. package/lib/module/internal.js.map +1 -1
  120. package/lib/module/monkey-patch.android.js +4 -4
  121. package/lib/module/monkey-patch.android.js.map +1 -1
  122. package/lib/module/monkey-patch.js.map +1 -1
  123. package/lib/module/reanimated.js.map +1 -1
  124. package/lib/module/reanimated.native.js.map +1 -1
  125. package/lib/module/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
  126. package/lib/module/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
  127. package/lib/module/specs/NativeKeyboardController.js.map +1 -1
  128. package/lib/module/specs/NativeStatusBarManagerCompat.js.map +1 -1
  129. package/lib/module/types.js.map +1 -1
  130. package/lib/module/utils.js.map +1 -1
  131. package/lib/typescript/types.d.ts +1 -0
  132. package/package.json +181 -178
  133. package/src/animated.tsx +246 -253
  134. package/src/bindings.native.ts +57 -57
  135. package/src/bindings.ts +38 -38
  136. package/src/components/KeyboardAvoidingView/hooks.ts +58 -58
  137. package/src/components/KeyboardAvoidingView/index.tsx +156 -156
  138. package/src/components/KeyboardAwareScrollView/index.tsx +381 -373
  139. package/src/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.ts +133 -133
  140. package/src/components/KeyboardAwareScrollView/utils.ts +41 -41
  141. package/src/components/KeyboardStickyView/index.tsx +57 -57
  142. package/src/components/KeyboardToolbar/Arrow.tsx +80 -80
  143. package/src/components/KeyboardToolbar/Button.tsx +98 -98
  144. package/src/components/KeyboardToolbar/colors.native.ts +37 -37
  145. package/src/components/KeyboardToolbar/colors.ts +16 -16
  146. package/src/components/KeyboardToolbar/index.tsx +211 -211
  147. package/src/components/KeyboardToolbar/types.ts +40 -40
  148. package/src/components/hooks/useColorScheme.ts +5 -5
  149. package/src/components/index.ts +11 -11
  150. package/src/constants.ts +19 -19
  151. package/src/context.ts +65 -65
  152. package/src/hooks/index.ts +90 -90
  153. package/src/hooks/useWindowDimensions/index.android.ts +33 -33
  154. package/src/hooks/useWindowDimensions/index.ts +1 -1
  155. package/src/index.ts +21 -21
  156. package/src/internal.ts +84 -84
  157. package/src/monkey-patch.android.ts +44 -44
  158. package/src/monkey-patch.ts +4 -4
  159. package/src/reanimated.native.ts +132 -132
  160. package/src/reanimated.ts +29 -29
  161. package/src/specs/KeyboardControllerViewNativeComponent.ts +70 -70
  162. package/src/specs/KeyboardGestureAreaNativeComponent.ts +15 -15
  163. package/src/specs/NativeKeyboardController.ts +19 -19
  164. package/src/specs/NativeStatusBarManagerCompat.ts +14 -14
  165. package/src/types.ts +212 -211
  166. package/src/utils.ts +1 -1
@@ -1,274 +1,816 @@
1
- /*
2
- * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
3
- */
4
- /**
5
- * MIT License
6
- *
7
- * Copyright (C) 2024 Huawei Device Co., Ltd.
8
- *
9
- * Permission is hereby granted, free of charge, to any person obtaining a copy
10
- * of this software and associated documentation files (the "Software"), to deal
11
- * in the Software without restriction, including without limitation the rights
12
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
- * copies of the Software, and to permit persons to whom the Software is
14
- * furnished to do so, subject to the following conditions:
15
- *
16
- * The above copyright notice and this permission notice shall be included in all
17
- * copies or substantial portions of the Software.
18
- *
19
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
- * SOFTWARE.
26
- */
27
-
28
- #include "KeyboardControllerViewComponentInstance.h"
29
- #include <folly/dynamic.h>
30
- #include <iostream>
31
- #include <arkui/native_interface_focus.h>
32
-
33
- namespace rnoh {
34
- using KeyboardControllerStatus = rnoh::KeyboardControllerStatus;
35
- KeyboardControllerViewComponentInstance::KeyboardControllerViewComponentInstance(Context context)
36
- : CppComponentInstance(std::move(context)), ArkTSMessageHub::Observer(m_deps->arkTSMessageHub) {
37
- DLOG(INFO) << "KeyboardControllerViewComponentInstance";
38
-
39
- m_customNode.setCustomNodeDelegate(this);
40
- }
41
- void KeyboardControllerViewComponentInstance::onChildInserted(ComponentInstance::Shared const &childComponentInstance,
42
- std::size_t index) {
43
- CppComponentInstance::onChildInserted(childComponentInstance, index);
44
- m_customNode.insertChild(childComponentInstance->getLocalRootArkUINode(), index + 1);
45
- DLOG(INFO) << "###111" << childComponentInstance->getComponentName();
46
- findTextInputComponents(childComponentInstance);
47
- }
48
-
49
- void KeyboardControllerViewComponentInstance::onChildRemoved(ComponentInstance::Shared const &childComponentInstance) {
50
- CppComponentInstance::onChildRemoved(childComponentInstance);
51
- m_customNode.removeChild(childComponentInstance->getLocalRootArkUINode());
52
- }
53
-
54
- void KeyboardControllerViewComponentInstance::onPropsChanged(SharedConcreteProps const &props) {
55
- DLOG(INFO) << "###onPropsChanged" << props->enabled << props->statusBarTranslucent
56
- << props->navigationBarTranslucent;
57
- CppComponentInstance::onPropsChanged(props);
58
- if (this->enabled != props->enabled) {
59
- this->enabled = props->enabled;
60
- this->startKeyboardObserver();
61
- }
62
- if (this->navigationBarTranslucent != props->navigationBarTranslucent) {
63
- this->navigationBarTranslucent = props->navigationBarTranslucent;
64
- this->setWindowSystemBarEnable();
65
- }
66
- if (this->statusBarTranslucent != props->statusBarTranslucent) {
67
- this->statusBarTranslucent = props->statusBarTranslucent;
68
- this->setWindowSystemBarEnable();
69
- }
70
- }
71
-
72
- void KeyboardControllerViewComponentInstance::onCommandReceived(std::string const &commandName,
73
- folly::dynamic const &args) {
74
- CppComponentInstance::onCommandReceived(commandName, args);
75
- }
76
-
77
- ArkUINode &KeyboardControllerViewComponentInstance::getLocalRootArkUINode() { return m_customNode; }
78
- void KeyboardControllerViewComponentInstance::onClick() {}
79
- void KeyboardControllerViewComponentInstance::onMessageReceived(ArkTSMessage const &message) {
80
- DLOG(INFO) << "onMessageReceived";
81
- if (message.name == "keyboardHeightChange") {
82
- double height = message.payload.getDouble();
83
- DLOG(INFO) << "keyboardHeightChange: " << height;
84
- if (height > 0) {
85
- this->keyboardStatus = KeyboardControllerStatus::SHOW;
86
- this->keyboardHeight = height;
87
- this->textInputVector.clear();
88
- this->focusDidSet();
89
- auto textInputVectorTemp = ViewHierarchyNavigator::getAllInputFields(this->shared_from_this());
90
- for (size_t i = 0; i < textInputVectorTemp.size(); ++i) {
91
- auto &input = textInputVectorTemp[i];
92
- findTextInputComponents(input);
93
- }
94
- }
95
- if (height == 0) {
96
- this->keyboardStatus = KeyboardControllerStatus::HIDE;
97
- }
98
- this->keyboardHeightChangeHandle();
99
- }
100
- if (message.name == "setFocusTo") {
101
- std::string direction = message.payload.getString();
102
- this->setFocusTo(direction);
103
- }
104
- }
105
- void KeyboardControllerViewComponentInstance::setWindowSystemBarEnable() {
106
- auto rnInstancePtr = this->m_deps->rnInstance.lock();
107
- if (rnInstancePtr != nullptr) {
108
- auto turboModule = rnInstancePtr->getTurboModule("KeyboardController");
109
- auto arkTsTurboModule = std::dynamic_pointer_cast<rnoh::ArkTSTurboModule>(turboModule);
110
- arkTsTurboModule->callSync("setWindowSystemBarEnable", {this->statusBarTranslucent, this->navigationBarTranslucent});
111
- }
112
- }
113
-
114
- void KeyboardControllerViewComponentInstance::startKeyboardObserver() {
115
- auto rnInstancePtr = this->m_deps->rnInstance.lock();
116
- if (rnInstancePtr != nullptr) {
117
- auto turboModule = rnInstancePtr->getTurboModule("KeyboardController");
118
- auto arkTsTurboModule = std::dynamic_pointer_cast<rnoh::ArkTSTurboModule>(turboModule);
119
- arkTsTurboModule->callSync("startKeyboardObserver", {this->enabled});
120
- }
121
- }
122
-
123
- void KeyboardControllerViewComponentInstance::keyboardHeightChangeHandle() {
124
- auto rnInstancePtr = this->m_deps->rnInstance.lock();
125
- if (rnInstancePtr != nullptr && this->enabled) {
126
- if (this->keyboardStatus == KeyboardControllerStatus::HIDE) {
127
- facebook::react::KeyboardControllerViewEventEmitter::MoveEvent start = {this->keyboardHeight, 0, 0, m_tag};
128
- facebook::react::KeyboardControllerViewEventEmitter::MoveEvent end = {0, 0, 0, m_tag};
129
- m_eventEmitter->onKeyboardMoveStart(start);
130
- m_eventEmitter->onKeyboardMove(end);
131
- m_eventEmitter->onKeyboardMoveEnd(end);
132
- } else {
133
- facebook::react::KeyboardControllerViewEventEmitter::MoveEvent start = {0, 0, 0, m_tag};
134
- facebook::react::KeyboardControllerViewEventEmitter::MoveEvent end = {this->keyboardHeight, 1, 0, m_tag};
135
- m_eventEmitter->onKeyboardMoveStart(start);
136
- m_eventEmitter->onKeyboardMove(end);
137
- m_eventEmitter->onKeyboardMoveEnd(end);
138
- }
139
- }
140
- }
141
-
142
- /**
143
- * 遍历查找TextInput
144
- * */
145
- void KeyboardControllerViewComponentInstance::findTextInputComponents(
146
- ComponentInstance::Shared const &childComponentInstance) {
147
- std::string name = childComponentInstance->getComponentName();
148
- DLOG(INFO) << "###" << name;
149
- if (name.find("TextInput") != std::string::npos || name.find("TextArea") != std::string::npos) {
150
- auto input = std::dynamic_pointer_cast<TextInputComponentInstance>(childComponentInstance);
151
- ArkUINode &node = input->getLocalRootArkUINode();
152
- TextInputNode *inputNodePtr = dynamic_cast<TextInputNode *>(&node);
153
- if (!inputNodePtr) {
154
- TextAreaNode *textAreaNodePtr = dynamic_cast<TextAreaNode *>(&node);
155
- textAreaNodePtr->setTextAreaNodeDelegate(this);
156
- } else {
157
- inputNodePtr->setTextInputNodeDelegate(this);
158
- }
159
- this->textInputVector.push_back(input);
160
- }
161
- auto childrenComponentInstances = childComponentInstance->getChildren();
162
- if (childrenComponentInstances.empty()) {
163
- return;
164
- }
165
-
166
- std::vector<ComponentInstance::Shared>::iterator it1;
167
- for (it1 = childrenComponentInstances.begin(); it1 != childrenComponentInstances.end(); ++it1) {
168
- ComponentInstance::Shared const &t = *it1;
169
- findTextInputComponents(t);
170
- }
171
- }
172
-
173
- void KeyboardControllerViewComponentInstance::onChange(std::string text) {
174
- facebook::react::KeyboardControllerViewEventEmitter::TextChangeEvent event = {text};
175
- if(this->enabled){
176
- m_eventEmitter->onFocusedInputTextChanged(event);
177
- }
178
-
179
- }
180
-
181
- void KeyboardControllerViewComponentInstance::onChange(std::string text, std::string extendStr) {
182
- onChange(std::move(text));
183
- }
184
-
185
- void KeyboardControllerViewComponentInstance::onTextSelectionChange(int32_t location, int32_t length) {
186
- DLOG(INFO) << " onKeyboardControllerView onTextSelectionChange";
187
- // to do
188
- };
189
-
190
- void KeyboardControllerViewComponentInstance::focusDidSet() {
191
- int currentIndex = -1;
192
- this->textInputVector = ViewHierarchyNavigator::getAllInputFields(this->shared_from_this());
193
- int count = static_cast<int>(this->textInputVector.size());
194
- for (size_t i = 0; i < this->textInputVector.size(); ++i) {
195
- auto& input = this->textInputVector[i];
196
- ArkUINode& node = input->getLocalRootArkUINode();
197
- if (node.isFocused()) {
198
- currentIndex = static_cast<int>(i);
199
- break;
200
- }
201
- }
202
- this->textInputVector.clear();
203
- // 发送 focusDidSet 事件到 JS 层
204
- if (currentIndex >= 0 && this->enabled) {
205
- auto rnInstancePtr = this->m_deps->rnInstance.lock();
206
- if (rnInstancePtr != nullptr) {
207
- folly::dynamic payload = folly::dynamic::object
208
- ("current", currentIndex)
209
- ("count", count);
210
- rnInstancePtr->postMessageToArkTS("focusDidSet", payload);
211
- }
212
- }
213
- }
214
-
215
- void KeyboardControllerViewComponentInstance::onFocus() {
216
- DLOG(INFO) << "onKeyboardControllerView onFocus";
217
- this->focusDidSet();
218
- }
219
-
220
- void KeyboardControllerViewComponentInstance::onBlur() { DLOG(INFO) << " onKeyboardControllerView onBlur"; }
221
-
222
- /**
223
- * 设置焦点到指定方向的输入框
224
- * 实现逻辑仿照 iOS ViewHierarchyNavigator.swift
225
- * @param direction "next" | "prev"
226
- */
227
- void KeyboardControllerViewComponentInstance::setFocusTo(const std::string& direction) {
228
- // 确定当前焦点组件
229
- this->textInputVector = ViewHierarchyNavigator::getAllInputFields(this->shared_from_this());
230
- ComponentInstance::Shared currentFocus = nullptr;
231
- for (size_t i = 0; i < this->textInputVector.size(); ++i) {
232
- auto& input = this->textInputVector[i];
233
- ArkUINode& node = input->getLocalRootArkUINode();
234
- if (node.isFocused()) {
235
- currentFocus = input;
236
- break;
237
- }
238
- }
239
- if (!currentFocus) {
240
- DLOG(INFO) << "no current focus available";
241
- return;
242
- }
243
- // 使用 ViewHierarchyNavigator 查找目标输入框
244
- auto targetInput = ViewHierarchyNavigator::setFocusTo(direction, currentFocus);
245
- if (targetInput) {
246
- DLOG(INFO) << "setFocusTo: found target, requesting focus, tag=" << targetInput->getTag();
247
- // 获取 ArkUI_NodeHandle 并请求焦点
248
- ArkUINode& node = targetInput->getLocalRootArkUINode();
249
- ArkUI_NodeHandle nodeHandle = node.getArkUINodeHandle();
250
- ArkUI_ErrorCode result = ARKUI_ERROR_CODE_NO_ERROR;
251
- high_lib_handle = dlopen("libace_ndk.z.so", RTLD_LAZY);
252
- if (high_lib_handle == NULL) {
253
- DLOG(INFO) << "libace_ndk.z.so not existed";
254
- dlclose(high_lib_handle);
255
- } else {
256
- focusRequestMethod = (ArkUI_ErrorCode(*)(ArkUI_NodeHandle node))dlsym(high_lib_handle, "OH_ArkUI_FocusRequest");
257
- bool isFocusRequestMethodExisted = focusRequestMethod != NULL;
258
- if (isFocusRequestMethodExisted) {
259
- DLOG(INFO) << "OH_ArkUI_FocusRequest method existed";
260
- result = focusRequestMethod(nodeHandle);
261
- } else {
262
- DLOG(INFO) << "OH_ArkUI_FocusRequest method not existed";
263
- }
264
- dlclose(high_lib_handle);
265
- }
266
- if (!(result == ARKUI_ERROR_CODE_NO_ERROR)) {
267
- DLOG(WARNING) << "setFocusTo: focus request failed with error code: " << result;
268
- }
269
- } else {
270
- DLOG(INFO) << "setFocusTo: no target input found in direction " << direction;
271
- }
272
- this->textInputVector.clear();
273
- }
274
- } // namespace rnoh
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+
7
+ #include "KeyboardControllerViewComponentInstance.h"
8
+ #include <algorithm>
9
+ #include <cmath>
10
+ #include <functional>
11
+ #include <folly/dynamic.h>
12
+ #include <iostream>
13
+ #include <unordered_map>
14
+ #include <arkui/native_interface_focus.h>
15
+ #include "RNOH/RNInstance.h"
16
+ #include "react/renderer/components/textinput/TextInputProps.h"
17
+
18
+ namespace rnoh {
19
+ using KeyboardControllerStatus = rnoh::KeyboardControllerStatus;
20
+
21
+ namespace {
22
+ constexpr int KBC_FOCUSED_INPUT_AREA_CHANGE_TARGET_ID = 0x4B4243;
23
+ struct InputObserver {
24
+ using Instance = std::shared_ptr<KeyboardControllerViewComponentInstance>;
25
+
26
+ std::weak_ptr<TextInputComponentInstance> input;
27
+ bool areaEventRegistered = false;
28
+ std::unordered_map<KeyboardControllerViewComponentInstance *,
29
+ std::weak_ptr<KeyboardControllerViewComponentInstance>> instances;
30
+
31
+ void addInstance(Instance const &target) {
32
+ instances[target.get()] = target;
33
+ }
34
+
35
+ void removeInstance(KeyboardControllerViewComponentInstance *target) {
36
+ instances.erase(target);
37
+ for (auto it = instances.begin(); it != instances.end();) {
38
+ if (it->second.expired()) {
39
+ it = instances.erase(it);
40
+ } else {
41
+ ++it;
42
+ }
43
+ }
44
+ }
45
+
46
+ std::vector<Instance> lockInstances() {
47
+ std::vector<Instance> result;
48
+ for (auto it = instances.begin(); it != instances.end();) {
49
+ auto instance = it->second.lock();
50
+ if (instance) {
51
+ result.push_back(instance);
52
+ ++it;
53
+ } else {
54
+ it = instances.erase(it);
55
+ }
56
+ }
57
+ return result;
58
+ }
59
+ };
60
+ std::unordered_map<ArkUI_NodeHandle, InputObserver> g_inputObservers;
61
+
62
+ static std::string keyboardTypeToString(facebook::react::KeyboardType type) {
63
+ switch (type) {
64
+ case facebook::react::KeyboardType::Default: return "default";
65
+ case facebook::react::KeyboardType::EmailAddress: return "email-address";
66
+ case facebook::react::KeyboardType::Numeric: return "numeric";
67
+ case facebook::react::KeyboardType::PhonePad: return "phone-pad";
68
+ case facebook::react::KeyboardType::NumberPad: return "number-pad";
69
+ case facebook::react::KeyboardType::DecimalPad: return "decimal-pad";
70
+ case facebook::react::KeyboardType::ASCIICapable: return "ascii-capable";
71
+ case facebook::react::KeyboardType::NumbersAndPunctuation: return "numbers-and-punctuation";
72
+ case facebook::react::KeyboardType::URL: return "url";
73
+ case facebook::react::KeyboardType::NamePhonePad: return "name-phone-pad";
74
+ case facebook::react::KeyboardType::Twitter: return "twitter";
75
+ case facebook::react::KeyboardType::WebSearch: return "web-search";
76
+ case facebook::react::KeyboardType::ASCIICapableNumberPad: return "ascii-capable-number-pad";
77
+ case facebook::react::KeyboardType::VisiblePassword: return "visible-password";
78
+ }
79
+ return "default";
80
+ }
81
+ } // namespace
82
+
83
+ KeyboardControllerViewComponentInstance::KeyboardControllerViewComponentInstance(Context context)
84
+ : CppComponentInstance(std::move(context)), ArkTSMessageHub::Observer(m_deps->arkTSMessageHub) {
85
+ DLOG(INFO) << "KeyboardControllerViewComponentInstance";
86
+
87
+ m_keyboardVsyncListener = std::make_shared<VSyncListener>("KeyboardControllerAnimation");
88
+ m_customNode.setCustomNodeDelegate(this);
89
+ }
90
+
91
+ KeyboardControllerViewComponentInstance::~KeyboardControllerViewComponentInstance() {
92
+ cancelKeyboardAnimation();
93
+ m_keyboardVsyncListener.reset();
94
+ clearFocusedInputLayoutObserver();
95
+ for (const auto &child : getChildren()) {
96
+ clearTextInputDelegates(child);
97
+ }
98
+ for (auto it = g_inputObservers.begin(); it != g_inputObservers.end();) {
99
+ auto handle = (it++)->first;
100
+ removeInputObserver(handle);
101
+ }
102
+ }
103
+ void KeyboardControllerViewComponentInstance::onChildInserted(ComponentInstance::Shared const &childComponentInstance,
104
+ std::size_t index) {
105
+ CppComponentInstance::onChildInserted(childComponentInstance, index);
106
+ m_customNode.insertChild(childComponentInstance->getLocalRootArkUINode(), index + 1);
107
+ DLOG(INFO) << "###111" << childComponentInstance->getComponentName();
108
+ findTextInputComponents(childComponentInstance);
109
+ }
110
+
111
+ void KeyboardControllerViewComponentInstance::onChildRemoved(ComponentInstance::Shared const &childComponentInstance) {
112
+ clearTextInputDelegates(childComponentInstance);
113
+ if (childComponentInstance->getLocalRootArkUINode().getArkUINodeHandle() == m_observedFocusedInputHandle) {
114
+ clearFocusedInputLayoutObserver();
115
+ }
116
+ CppComponentInstance::onChildRemoved(childComponentInstance);
117
+ m_customNode.removeChild(childComponentInstance->getLocalRootArkUINode());
118
+ }
119
+
120
+ void KeyboardControllerViewComponentInstance::onPropsChanged(SharedConcreteProps const &props) {
121
+ DLOG(INFO) << "###onPropsChanged" << props->enabled << props->statusBarTranslucent
122
+ << props->navigationBarTranslucent;
123
+ CppComponentInstance::onPropsChanged(props);
124
+ if (this->enabled != props->enabled) {
125
+ this->enabled = props->enabled;
126
+ this->startKeyboardObserver();
127
+ }
128
+ if (this->navigationBarTranslucent != props->navigationBarTranslucent) {
129
+ this->navigationBarTranslucent = props->navigationBarTranslucent;
130
+ this->setWindowSystemBarEnable();
131
+ }
132
+ if (this->statusBarTranslucent != props->statusBarTranslucent) {
133
+ this->statusBarTranslucent = props->statusBarTranslucent;
134
+ this->setWindowSystemBarEnable();
135
+ }
136
+ }
137
+
138
+ void KeyboardControllerViewComponentInstance::onCommandReceived(std::string const &commandName,
139
+ folly::dynamic const &args) {
140
+ CppComponentInstance::onCommandReceived(commandName, args);
141
+ if (commandName == "synchronizeFocusedInputLayout") {
142
+ syncUpLayout();
143
+ }
144
+ }
145
+
146
+ ArkUINode &KeyboardControllerViewComponentInstance::getLocalRootArkUINode() { return m_customNode; }
147
+ void KeyboardControllerViewComponentInstance::onClick() {}
148
+ void KeyboardControllerViewComponentInstance::onMessageReceived(ArkTSMessage const &message) {
149
+ DLOG(INFO) << "onMessageReceived";
150
+ if (message.name == "keyboardHeightChange") {
151
+ double height = message.payload.getDouble();
152
+ double previousKeyboardHeight = this->keyboardHeight;
153
+ DLOG(INFO) << "keyboardHeightChange: " << height;
154
+ if (height > 0) {
155
+ this->keyboardStatus = KeyboardControllerStatus::SHOW;
156
+ this->keyboardHeight = height;
157
+ this->textInputVector.clear();
158
+ this->focusDidSet();
159
+ auto textInputVectorTemp = ViewHierarchyNavigator::getAllInputFields(this->shared_from_this());
160
+ for (size_t i = 0; i < textInputVectorTemp.size(); ++i) {
161
+ auto &input = textInputVectorTemp[i];
162
+ findTextInputComponents(input);
163
+ }
164
+ }
165
+ if (height == 0) {
166
+ this->keyboardStatus = KeyboardControllerStatus::HIDE;
167
+ this->keyboardHeight = previousKeyboardHeight > 0 ? previousKeyboardHeight : 0;
168
+ m_lastKeyboardEventTarget = -1;
169
+ }
170
+ m_isHandlingKeyboardHeightChange = true;
171
+ if (this->enabled) {
172
+ syncUpLayout();
173
+ }
174
+ this->keyboardHeightChangeHandle(previousKeyboardHeight);
175
+ m_isHandlingKeyboardHeightChange = false;
176
+ if (height == 0) {
177
+ this->keyboardHeight = 0;
178
+ }
179
+ }
180
+ if (message.name == "setFocusTo") {
181
+ std::string direction = message.payload.getString();
182
+ this->setFocusTo(direction);
183
+ }
184
+ }
185
+ void KeyboardControllerViewComponentInstance::setWindowSystemBarEnable() {
186
+ auto rnInstancePtr = this->m_deps->rnInstance.lock();
187
+ if (rnInstancePtr != nullptr) {
188
+ auto turboModule = rnInstancePtr->getTurboModule("KeyboardController");
189
+ auto arkTsTurboModule = std::dynamic_pointer_cast<rnoh::ArkTSTurboModule>(turboModule);
190
+ arkTsTurboModule->callSync("setWindowSystemBarEnable", {this->statusBarTranslucent, this->navigationBarTranslucent});
191
+ }
192
+ }
193
+
194
+ void KeyboardControllerViewComponentInstance::startKeyboardObserver() {
195
+ auto rnInstancePtr = this->m_deps->rnInstance.lock();
196
+ if (rnInstancePtr != nullptr) {
197
+ auto turboModule = rnInstancePtr->getTurboModule("KeyboardController");
198
+ auto arkTsTurboModule = std::dynamic_pointer_cast<rnoh::ArkTSTurboModule>(turboModule);
199
+ arkTsTurboModule->callSync("startKeyboardObserver", {this->enabled, true});
200
+ }
201
+ }
202
+
203
+ void KeyboardControllerViewComponentInstance::keyboardHeightChangeHandle(double previousKeyboardHeight) {
204
+ auto rnInstancePtr = this->m_deps->rnInstance.lock();
205
+ if (rnInstancePtr != nullptr && this->enabled) {
206
+ int target = m_tag;
207
+ auto focusedInput = findFocusedTextInput();
208
+ if (focusedInput) {
209
+ target = static_cast<int>(focusedInput->getTag());
210
+ }
211
+ m_lastKeyboardEventTarget = target;
212
+ const bool willHide = this->keyboardStatus == KeyboardControllerStatus::HIDE;
213
+ startKeyboardAnimation(previousKeyboardHeight, willHide ? 0 : this->keyboardHeight, target);
214
+ }
215
+ }
216
+
217
+ TaskExecutor::Shared KeyboardControllerViewComponentInstance::getTaskExecutor() const {
218
+ auto rnInstance = m_deps->rnInstance.lock();
219
+ auto internalInstance = std::dynamic_pointer_cast<RNInstanceInternal>(rnInstance);
220
+ return internalInstance ? internalInstance->getTaskExecutor() : nullptr;
221
+ }
222
+
223
+ void KeyboardControllerViewComponentInstance::cancelKeyboardAnimation() {
224
+ ++m_keyboardAnimationGeneration;
225
+ auto taskExecutor = getTaskExecutor();
226
+ if (m_keyboardAnimationTask.has_value() && taskExecutor) {
227
+ taskExecutor->cancelDelayedTask(m_keyboardAnimationTask.value());
228
+ }
229
+ m_keyboardAnimationTask.reset();
230
+ m_keyboardAnimationStartTimestampNs.reset();
231
+ }
232
+
233
+ void KeyboardControllerViewComponentInstance::startKeyboardAnimation(double fromHeight, double toHeight, int target) {
234
+ cancelKeyboardAnimation();
235
+ const uint64_t generation = m_keyboardAnimationGeneration;
236
+ constexpr int durationMs = 150;
237
+ facebook::react::KeyboardControllerViewEventEmitter::MoveEvent start = {
238
+ toHeight > 0 ? toHeight : 0,
239
+ toHeight > 0 ? 1.0 : 0.0,
240
+ durationMs,
241
+ target};
242
+ m_eventEmitter->onKeyboardMoveStart(start);
243
+ requestKeyboardAnimationFrame(generation, fromHeight, toHeight, target);
244
+ }
245
+
246
+ void KeyboardControllerViewComponentInstance::requestKeyboardAnimationFrame(
247
+ uint64_t generation,
248
+ double fromHeight,
249
+ double toHeight,
250
+ int target) {
251
+ if (!m_keyboardVsyncListener || !m_eventEmitter || !this->enabled) {
252
+ return;
253
+ }
254
+ auto weakSelf = std::weak_ptr<KeyboardControllerViewComponentInstance>(
255
+ std::static_pointer_cast<KeyboardControllerViewComponentInstance>(shared_from_this()));
256
+ m_keyboardVsyncListener->requestFrame(
257
+ [weakSelf, generation, fromHeight, toHeight, target](long long timestampNs) {
258
+ auto self = weakSelf.lock();
259
+ if (!self || generation != self->m_keyboardAnimationGeneration) {
260
+ return;
261
+ }
262
+ auto taskExecutor = self->getTaskExecutor();
263
+ if (!taskExecutor) {
264
+ return;
265
+ }
266
+ auto frameTask = [weakSelf, generation, fromHeight, toHeight, target, timestampNs] {
267
+ auto self = weakSelf.lock();
268
+ if (!self) {
269
+ return;
270
+ }
271
+ self->handleKeyboardAnimationFrame(
272
+ generation, fromHeight, toHeight, target, static_cast<int64_t>(timestampNs));
273
+ };
274
+ if (taskExecutor->isOnTaskThread(TaskThread::MAIN)) {
275
+ frameTask();
276
+ } else {
277
+ taskExecutor->runTask(TaskThread::MAIN, std::move(frameTask));
278
+ }
279
+ });
280
+ }
281
+
282
+ void KeyboardControllerViewComponentInstance::handleKeyboardAnimationFrame(
283
+ uint64_t generation,
284
+ double fromHeight,
285
+ double toHeight,
286
+ int target,
287
+ int64_t timestampNs) {
288
+ if (generation != m_keyboardAnimationGeneration || !m_eventEmitter || !this->enabled) {
289
+ return;
290
+ }
291
+
292
+ if (!m_keyboardAnimationStartTimestampNs.has_value()) {
293
+ m_keyboardAnimationStartTimestampNs = timestampNs;
294
+ }
295
+
296
+ constexpr int durationMs = 150;
297
+ constexpr int64_t nanosecondsPerMillisecond = 1000000;
298
+ constexpr int64_t durationNs = durationMs * nanosecondsPerMillisecond;
299
+ const int64_t elapsedNs = std::max<int64_t>(0, timestampNs - m_keyboardAnimationStartTimestampNs.value());
300
+ const double animationFraction = std::clamp(
301
+ static_cast<double>(elapsedNs) / static_cast<double>(durationNs),
302
+ 0.0,
303
+ 1.0);
304
+ const double height = fromHeight + (toHeight - fromHeight) * animationFraction;
305
+ const double denominator = std::max(fromHeight, toHeight);
306
+ facebook::react::KeyboardControllerViewEventEmitter::MoveEvent move = {
307
+ height,
308
+ denominator > 0 ? std::abs(height / denominator) : 0.0,
309
+ durationMs,
310
+ target};
311
+ m_eventEmitter->onKeyboardMove(move);
312
+
313
+ if (animationFraction < 1.0) {
314
+ requestKeyboardAnimationFrame(generation, fromHeight, toHeight, target);
315
+ return;
316
+ }
317
+
318
+ constexpr int scrollSettleDelayMs = 16;
319
+ auto taskExecutor = getTaskExecutor();
320
+ if (!taskExecutor) {
321
+ return;
322
+ }
323
+ auto weakSelf = std::weak_ptr<KeyboardControllerViewComponentInstance>(
324
+ std::static_pointer_cast<KeyboardControllerViewComponentInstance>(shared_from_this()));
325
+ m_keyboardAnimationTask = taskExecutor->runDelayedTask(
326
+ TaskThread::MAIN,
327
+ [weakSelf, generation, toHeight, target] {
328
+ auto self = weakSelf.lock();
329
+ if (!self || generation != self->m_keyboardAnimationGeneration ||
330
+ !self->m_eventEmitter || !self->enabled) {
331
+ return;
332
+ }
333
+ constexpr int durationMs = 150;
334
+ facebook::react::KeyboardControllerViewEventEmitter::MoveEvent end = {
335
+ toHeight, toHeight > 0 ? 1.0 : 0.0, durationMs, target};
336
+ self->m_eventEmitter->onKeyboardMoveEnd(end);
337
+ self->m_keyboardAnimationTask.reset();
338
+ self->m_keyboardAnimationStartTimestampNs.reset();
339
+ },
340
+ scrollSettleDelayMs);
341
+ }
342
+
343
+ /**
344
+ * 遍历查找TextInput
345
+ * */
346
+ void KeyboardControllerViewComponentInstance::findTextInputComponents(
347
+ ComponentInstance::Shared const &childComponentInstance) {
348
+ std::string name = childComponentInstance->getComponentName();
349
+ if (name.find("TextInput") != std::string::npos || name.find("TextArea") != std::string::npos) {
350
+ auto input = std::dynamic_pointer_cast<TextInputComponentInstance>(childComponentInstance);
351
+ if (!input) {
352
+ return;
353
+ }
354
+ ArkUINode &node = input->getLocalRootArkUINode();
355
+ auto handle = node.getArkUINodeHandle();
356
+ auto observerIt = g_inputObservers.find(handle);
357
+ if (observerIt != g_inputObservers.end() && observerIt->second.input.expired()) {
358
+ g_inputObservers.erase(observerIt);
359
+ observerIt = g_inputObservers.end();
360
+ }
361
+ if (handle && observerIt == g_inputObservers.end()) {
362
+ auto result = NativeNodeApi::getInstance()->addNodeEventReceiver(
363
+ handle, KeyboardControllerViewComponentInstance::textSelectionEventReceiver);
364
+ if (result == ARKUI_ERROR_CODE_NO_ERROR) {
365
+ InputObserver observer;
366
+ observer.input = input;
367
+ g_inputObservers.emplace(handle, std::move(observer));
368
+ observerIt = g_inputObservers.find(handle);
369
+ }
370
+ }
371
+ if (observerIt != g_inputObservers.end()) {
372
+ observerIt->second.input = input;
373
+ observerIt->second.addInstance(
374
+ std::dynamic_pointer_cast<KeyboardControllerViewComponentInstance>(shared_from_this()));
375
+ }
376
+ this->textInputVector.push_back(input);
377
+ }
378
+ auto childrenComponentInstances = childComponentInstance->getChildren();
379
+ if (childrenComponentInstances.empty()) {
380
+ return;
381
+ }
382
+
383
+ std::vector<ComponentInstance::Shared>::iterator it1;
384
+ for (it1 = childrenComponentInstances.begin(); it1 != childrenComponentInstances.end(); ++it1) {
385
+ ComponentInstance::Shared const &t = *it1;
386
+ findTextInputComponents(t);
387
+ }
388
+ }
389
+
390
+ void KeyboardControllerViewComponentInstance::clearTextInputDelegates(
391
+ ComponentInstance::Shared const &component) {
392
+ if (!component) {
393
+ return;
394
+ }
395
+ ArkUINode &node = component->getLocalRootArkUINode();
396
+ if (node.getArkUINodeHandle() == m_observedFocusedInputHandle) {
397
+ clearFocusedInputLayoutObserver();
398
+ }
399
+ removeInputObserver(node.getArkUINodeHandle());
400
+ for (const auto &child : component->getChildren()) {
401
+ clearTextInputDelegates(child);
402
+ }
403
+ }
404
+
405
+ void KeyboardControllerViewComponentInstance::removeInputObserver(ArkUI_NodeHandle handle) {
406
+ auto observerIt = g_inputObservers.find(handle);
407
+ if (observerIt == g_inputObservers.end()) {
408
+ return;
409
+ }
410
+ observerIt->second.removeInstance(this);
411
+ if (!observerIt->second.instances.empty()) {
412
+ return;
413
+ }
414
+ if (observerIt->second.input.lock()) {
415
+ if (observerIt->second.areaEventRegistered) {
416
+ NativeNodeApi::getInstance()->unregisterNodeEvent(handle, NODE_EVENT_ON_AREA_CHANGE);
417
+ }
418
+ NativeNodeApi::getInstance()->removeNodeEventReceiver(
419
+ handle, KeyboardControllerViewComponentInstance::textSelectionEventReceiver);
420
+ }
421
+ g_inputObservers.erase(observerIt);
422
+ }
423
+
424
+ void KeyboardControllerViewComponentInstance::onChange(std::string text) {
425
+ onChange(std::move(text), findFocusedTextInput());
426
+ }
427
+
428
+ void KeyboardControllerViewComponentInstance::onChange(
429
+ std::string text,
430
+ TextInputComponentInstance::Shared const &focusedInput) {
431
+ facebook::react::KeyboardControllerViewEventEmitter::TextChangeEvent event = {text};
432
+ if (this->enabled && m_eventEmitter) {
433
+ syncUpLayout(focusedInput);
434
+ m_eventEmitter->onFocusedInputTextChanged(event);
435
+ }
436
+ }
437
+
438
+ void KeyboardControllerViewComponentInstance::onChange(std::string text, std::string extendStr) {
439
+ onChange(std::move(text));
440
+ }
441
+
442
+ void KeyboardControllerViewComponentInstance::onTextSelectionChange(int32_t location, int32_t length) {
443
+ if (!this->enabled) {
444
+ return;
445
+ }
446
+ auto focusedInput = findFocusedTextInput();
447
+ if (!focusedInput) {
448
+ return;
449
+ }
450
+ syncUpLayout();
451
+ dispatchSelectionToJS(
452
+ static_cast<int>(focusedInput->getTag()),
453
+ location,
454
+ location + length);
455
+ }
456
+
457
+ void KeyboardControllerViewComponentInstance::dispatchSelectionToJS(
458
+ int target, int32_t startPos, int32_t endPos) {
459
+ if (!m_eventEmitter || !this->enabled) {
460
+ return;
461
+ }
462
+ facebook::react::KeyboardControllerViewEventEmitter::InputSectionEvent event = {};
463
+ event.target = target;
464
+ event.selection.start.x = 0;
465
+ event.selection.start.y = 0;
466
+ event.selection.start.position = startPos;
467
+ event.selection.end.x = 0;
468
+ event.selection.end.y = 0;
469
+ event.selection.end.position = endPos;
470
+ m_eventEmitter->onFocusedInputSelectionChanged(event);
471
+ }
472
+
473
+ void KeyboardControllerViewComponentInstance::focusDidSet() {
474
+ auto focused = findFocusedTextInput();
475
+ if (!focused || !this->enabled) {
476
+ this->textInputVector.clear();
477
+ return;
478
+ }
479
+ this->textInputVector = ViewHierarchyNavigator::getAllInputFields(this->shared_from_this());
480
+ int count = static_cast<int>(this->textInputVector.size());
481
+ int currentIndex = -1;
482
+ for (size_t i = 0; i < this->textInputVector.size(); ++i) {
483
+ auto& input = this->textInputVector[i];
484
+ if (input && input->getTag() == focused->getTag()) {
485
+ currentIndex = static_cast<int>(i);
486
+ break;
487
+ }
488
+ }
489
+ this->textInputVector.clear();
490
+ // 发送 focusDidSet 事件到 JS 层
491
+ if (currentIndex >= 0) {
492
+ auto rnInstancePtr = this->m_deps->rnInstance.lock();
493
+ if (rnInstancePtr != nullptr) {
494
+ folly::dynamic payload = folly::dynamic::object
495
+ ("current", currentIndex)
496
+ ("count", count);
497
+ rnInstancePtr->postMessageToArkTS("focusDidSet", payload);
498
+ }
499
+ }
500
+ }
501
+
502
+ void KeyboardControllerViewComponentInstance::onFocus() {
503
+ postFocusedInputChanged();
504
+ this->focusDidSet();
505
+ syncUpLayout();
506
+ }
507
+
508
+ void KeyboardControllerViewComponentInstance::onBlur() {
509
+ clearFocusedInputLayoutObserver();
510
+ m_lastLayoutEvent = FocusedInputLayoutData{};
511
+ if (m_eventEmitter && this->enabled) {
512
+ facebook::react::KeyboardControllerViewEventEmitter::InputLayoutEvent payload = {};
513
+ payload.target = -1;
514
+ payload.parentScrollViewTarget = -1;
515
+ m_eventEmitter->onFocusedInputLayoutChanged(payload);
516
+ }
517
+ }
518
+
519
+ /**
520
+ * 设置焦点到指定方向的输入框
521
+ * 实现逻辑仿照 iOS ViewHierarchyNavigator.swift
522
+ * @param direction "next" | "prev"
523
+ */
524
+ void KeyboardControllerViewComponentInstance::setFocusTo(const std::string& direction) {
525
+ auto currentFocus = this->findFocusedTextInput();
526
+ if (!currentFocus) {
527
+ DLOG(INFO) << "no current focus available";
528
+ return;
529
+ }
530
+ // 使用 ViewHierarchyNavigator 查找目标输入框
531
+ auto targetInput = ViewHierarchyNavigator::setFocusTo(direction, currentFocus);
532
+ if (targetInput) {
533
+ DLOG(INFO) << "setFocusTo: found target, requesting focus, tag=" << targetInput->getTag();
534
+ // 获取 ArkUI_NodeHandle 并请求焦点
535
+ ArkUINode& node = targetInput->getLocalRootArkUINode();
536
+ ArkUI_NodeHandle nodeHandle = node.getArkUINodeHandle();
537
+ ArkUI_ErrorCode result = ARKUI_ERROR_CODE_NO_ERROR;
538
+ high_lib_handle = dlopen("libace_ndk.z.so", RTLD_LAZY);
539
+ if (high_lib_handle == NULL) {
540
+ DLOG(INFO) << "libace_ndk.z.so not existed";
541
+ } else {
542
+ focusRequestMethod = (ArkUI_ErrorCode(*)(ArkUI_NodeHandle node))dlsym(high_lib_handle, "OH_ArkUI_FocusRequest");
543
+ bool isFocusRequestMethodExisted = focusRequestMethod != NULL;
544
+ if (isFocusRequestMethodExisted) {
545
+ DLOG(INFO) << "OH_ArkUI_FocusRequest method existed";
546
+ result = focusRequestMethod(nodeHandle);
547
+ } else {
548
+ DLOG(INFO) << "OH_ArkUI_FocusRequest method not existed";
549
+ }
550
+ dlclose(high_lib_handle);
551
+ }
552
+ if (!(result == ARKUI_ERROR_CODE_NO_ERROR)) {
553
+ DLOG(WARNING) << "setFocusTo: focus request failed with error code: " << result;
554
+ }
555
+ } else {
556
+ DLOG(INFO) << "setFocusTo: no target input found in direction " << direction;
557
+ }
558
+ this->textInputVector.clear();
559
+ }
560
+
561
+ TextInputComponentInstance::Shared KeyboardControllerViewComponentInstance::findFocusedTextInput() {
562
+ std::function<TextInputComponentInstance::Shared(ComponentInstance::Shared)> findFocused;
563
+ findFocused = [&](ComponentInstance::Shared component) -> TextInputComponentInstance::Shared {
564
+ if (!component) {
565
+ return nullptr;
566
+ }
567
+ if (auto input = std::dynamic_pointer_cast<TextInputComponentInstance>(component)) {
568
+ if (input->getLocalRootArkUINode().isFocused()) {
569
+ return input;
570
+ }
571
+ }
572
+ for (const auto &child : component->getChildren()) {
573
+ if (auto input = findFocused(child)) {
574
+ return input;
575
+ }
576
+ }
577
+ return nullptr;
578
+ };
579
+ return findFocused(this->shared_from_this());
580
+ }
581
+
582
+ int KeyboardControllerViewComponentInstance::findParentScrollViewTarget(ComponentInstance::Shared const &input) {
583
+ auto parent = input->getParent().lock();
584
+ while (parent != nullptr) {
585
+ if (parent->getComponentName().find("ScrollView") != std::string::npos) {
586
+ return static_cast<int>(parent->getTag());
587
+ }
588
+ parent = parent->getParent().lock();
589
+ }
590
+ return -1;
591
+ }
592
+
593
+ double KeyboardControllerViewComponentInstance::pxToVp(double px) const {
594
+ auto pointScaleFactor = this->getLayoutMetrics().pointScaleFactor;
595
+ return pointScaleFactor == 0.0 ? px : px / pointScaleFactor;
596
+ }
597
+
598
+ void KeyboardControllerViewComponentInstance::textSelectionEventReceiver(ArkUI_NodeEvent *event) {
599
+ if (event == nullptr) {
600
+ return;
601
+ }
602
+ auto eventType = OH_ArkUI_NodeEvent_GetEventType(event);
603
+ auto handle = OH_ArkUI_NodeEvent_GetNodeHandle(event);
604
+ auto observerIt = g_inputObservers.find(handle);
605
+ if (observerIt == g_inputObservers.end()) {
606
+ return;
607
+ }
608
+ auto input = observerIt->second.input.lock();
609
+ if (!input) {
610
+ return;
611
+ }
612
+ auto instances = observerIt->second.lockInstances();
613
+ instances.erase(
614
+ std::remove_if(
615
+ instances.begin(),
616
+ instances.end(),
617
+ [](auto const &instance) { return !instance->enabled; }),
618
+ instances.end());
619
+
620
+ if (eventType == NODE_EVENT_ON_AREA_CHANGE &&
621
+ OH_ArkUI_NodeEvent_GetTargetId(event) == KBC_FOCUSED_INPUT_AREA_CHANGE_TARGET_ID) {
622
+ for (auto const &instance : instances) {
623
+ if (instance->m_observedFocusedInputHandle == handle) {
624
+ instance->syncUpLayout(input);
625
+ }
626
+ }
627
+ return;
628
+ }
629
+ if (eventType == NODE_ON_BLUR) {
630
+ for (auto const &instance : instances) {
631
+ if (instance->m_observedFocusedInputHandle == nullptr ||
632
+ instance->m_observedFocusedInputHandle == handle) {
633
+ instance->onBlur();
634
+ }
635
+ }
636
+ return;
637
+ }
638
+ if (eventType == NODE_ON_FOCUS) {
639
+ for (auto const &instance : instances) {
640
+ instance->postFocusedInputChanged();
641
+ instance->focusDidSet();
642
+ instance->syncUpLayout(input);
643
+ }
644
+ return;
645
+ }
646
+
647
+ const auto *focusStatus = NativeNodeApi::getInstance()->getAttribute(handle, NODE_FOCUS_STATUS);
648
+ if (focusStatus == nullptr || focusStatus->value == nullptr ||
649
+ focusStatus->size < 1 || focusStatus->value[0].i32 == 0) {
650
+ return;
651
+ }
652
+
653
+ if (eventType == NODE_TEXT_INPUT_ON_CHANGE ||
654
+ eventType == NODE_TEXT_INPUT_ON_CHANGE_WITH_PREVIEW_TEXT ||
655
+ eventType == NODE_TEXT_AREA_ON_CHANGE ||
656
+ eventType == NODE_TEXT_AREA_ON_CHANGE_WITH_PREVIEW_TEXT) {
657
+ auto textAttribute = eventType == NODE_TEXT_INPUT_ON_CHANGE ||
658
+ eventType == NODE_TEXT_INPUT_ON_CHANGE_WITH_PREVIEW_TEXT
659
+ ? NODE_TEXT_INPUT_TEXT
660
+ : NODE_TEXT_AREA_TEXT;
661
+ const auto *text = NativeNodeApi::getInstance()->getAttribute(handle, textAttribute);
662
+ if (text != nullptr && text->string != nullptr) {
663
+ for (auto const &instance : instances) {
664
+ instance->onChange(std::string(text->string), input);
665
+ }
666
+ }
667
+ return;
668
+ }
669
+
670
+ if (eventType != NODE_TEXT_INPUT_ON_TEXT_SELECTION_CHANGE &&
671
+ eventType != NODE_TEXT_AREA_ON_TEXT_SELECTION_CHANGE) {
672
+ return;
673
+ }
674
+ auto componentEvent = OH_ArkUI_NodeEvent_GetNodeComponentEvent(event);
675
+ if (componentEvent == nullptr) {
676
+ return;
677
+ }
678
+ for (auto const &instance : instances) {
679
+ instance->syncUpLayout(input);
680
+ instance->dispatchSelectionToJS(
681
+ static_cast<int>(input->getTag()),
682
+ componentEvent->data[0].i32,
683
+ componentEvent->data[1].i32);
684
+ }
685
+ }
686
+
687
+ void KeyboardControllerViewComponentInstance::clearFocusedInputLayoutObserver() {
688
+ m_observedFocusedInputHandle = nullptr;
689
+ }
690
+
691
+ void KeyboardControllerViewComponentInstance::updateFocusedInputLayoutObserver(
692
+ TextInputComponentInstance::Shared const &focusedInput) {
693
+ ArkUI_NodeHandle handle = focusedInput
694
+ ? focusedInput->getLocalRootArkUINode().getArkUINodeHandle()
695
+ : nullptr;
696
+ if (handle == m_observedFocusedInputHandle) {
697
+ return;
698
+ }
699
+ clearFocusedInputLayoutObserver();
700
+ if (handle == nullptr) {
701
+ return;
702
+ }
703
+ auto observerIt = g_inputObservers.find(handle);
704
+ if (observerIt == g_inputObservers.end()) {
705
+ findTextInputComponents(focusedInput);
706
+ observerIt = g_inputObservers.find(handle);
707
+ }
708
+ if (observerIt == g_inputObservers.end()) {
709
+ return;
710
+ }
711
+ auto *nodeApi = NativeNodeApi::getInstance();
712
+ auto registerRet = observerIt->second.areaEventRegistered
713
+ ? ARKUI_ERROR_CODE_NO_ERROR
714
+ : nodeApi->registerNodeEvent(
715
+ handle, NODE_EVENT_ON_AREA_CHANGE, KBC_FOCUSED_INPUT_AREA_CHANGE_TARGET_ID, nullptr);
716
+ if (registerRet != ARKUI_ERROR_CODE_NO_ERROR) {
717
+ return;
718
+ }
719
+ observerIt->second.areaEventRegistered = true;
720
+ m_observedFocusedInputHandle = handle;
721
+ }
722
+
723
+ void KeyboardControllerViewComponentInstance::syncUpLayout() {
724
+ auto focusedInput = findFocusedTextInput();
725
+ syncUpLayout(focusedInput);
726
+ }
727
+
728
+ void KeyboardControllerViewComponentInstance::syncUpLayout(
729
+ TextInputComponentInstance::Shared const &focusedInput) {
730
+ if (!focusedInput) {
731
+ clearFocusedInputLayoutObserver();
732
+ return;
733
+ }
734
+ updateFocusedInputLayoutObserver(focusedInput);
735
+ ArkUI_NodeHandle handle = focusedInput->getLocalRootArkUINode().getArkUINodeHandle();
736
+ ArkUI_IntOffset windowOffset{};
737
+ ArkUI_IntOffset localOffset{};
738
+ ArkUI_IntSize size{};
739
+ if (handle == nullptr ||
740
+ OH_ArkUI_NodeUtils_GetLayoutPositionInWindow(handle, &windowOffset) != ARKUI_ERROR_CODE_NO_ERROR ||
741
+ OH_ArkUI_NodeUtils_GetLayoutPosition(handle, &localOffset) != ARKUI_ERROR_CODE_NO_ERROR ||
742
+ OH_ArkUI_NodeUtils_GetLayoutSize(handle, &size) != ARKUI_ERROR_CODE_NO_ERROR) {
743
+ return;
744
+ }
745
+ FocusedInputLayoutData event;
746
+ event.absoluteX = pxToVp(windowOffset.x);
747
+ event.absoluteY = pxToVp(windowOffset.y);
748
+ event.x = pxToVp(localOffset.x);
749
+ event.y = pxToVp(localOffset.y);
750
+ event.width = pxToVp(size.width);
751
+ event.height = pxToVp(size.height);
752
+ event.target = static_cast<int>(focusedInput->getTag());
753
+ event.parentScrollViewTarget = findParentScrollViewTarget(focusedInput);
754
+ dispatchLayoutToJS(event);
755
+ dispatchKeyboardFocusChangedIfNeeded(focusedInput);
756
+ }
757
+
758
+ void KeyboardControllerViewComponentInstance::dispatchLayoutToJS(FocusedInputLayoutData const &event) {
759
+ if (event == m_lastLayoutEvent) {
760
+ return;
761
+ }
762
+ m_lastLayoutEvent = event;
763
+ if (!m_eventEmitter || !this->enabled) {
764
+ return;
765
+ }
766
+ facebook::react::KeyboardControllerViewEventEmitter::InputLayoutEvent payload = {};
767
+ payload.target = event.target;
768
+ payload.parentScrollViewTarget = event.parentScrollViewTarget;
769
+ payload.layout.absoluteX = event.absoluteX;
770
+ payload.layout.absoluteY = event.absoluteY;
771
+ payload.layout.x = event.x;
772
+ payload.layout.y = event.y;
773
+ payload.layout.width = event.width;
774
+ payload.layout.height = event.height;
775
+ m_eventEmitter->onFocusedInputLayoutChanged(payload);
776
+ }
777
+
778
+ void KeyboardControllerViewComponentInstance::dispatchKeyboardFocusChangedIfNeeded(
779
+ TextInputComponentInstance::Shared const &focusedInput) {
780
+ if (m_isHandlingKeyboardHeightChange) {
781
+ return;
782
+ }
783
+
784
+ if (!this->enabled || !m_eventEmitter || this->keyboardHeight <= 0) {
785
+ return;
786
+ }
787
+ int target = static_cast<int>(focusedInput->getTag());
788
+ if (m_lastKeyboardEventTarget == -1 || target == m_lastKeyboardEventTarget) {
789
+ return;
790
+ }
791
+ cancelKeyboardAnimation();
792
+ facebook::react::KeyboardControllerViewEventEmitter::MoveEvent event = {
793
+ this->keyboardHeight, 1.0, 0, target};
794
+ m_lastKeyboardEventTarget = target;
795
+ m_eventEmitter->onKeyboardMoveStart(event);
796
+ m_eventEmitter->onKeyboardMoveEnd(event);
797
+ }
798
+
799
+ void KeyboardControllerViewComponentInstance::postFocusedInputChanged() {
800
+ auto focusedInput = findFocusedTextInput();
801
+ int target = -1;
802
+ std::string type = "default";
803
+ if (focusedInput) {
804
+ target = static_cast<int>(focusedInput->getTag());
805
+ auto props = std::dynamic_pointer_cast<const facebook::react::TextInputProps>(focusedInput->getProps());
806
+ if (props) {
807
+ type = keyboardTypeToString(props->traits.keyboardType);
808
+ }
809
+ }
810
+ auto rnInstancePtr = this->m_deps->rnInstance.lock();
811
+ if (rnInstancePtr != nullptr) {
812
+ folly::dynamic payload = folly::dynamic::object("target", target)("type", type);
813
+ rnInstancePtr->postMessageToArkTS("focusedInputChanged", std::move(payload));
814
+ }
815
+ }
816
+ } // namespace rnoh