@react-native-ohos/react-native-keyboard-controller 1.16.7 → 1.16.9-rc.1

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