@react-native-ohos/react-native-keyboard-controller 1.16.7-beta.2 → 1.16.8

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 (174) 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 +29 -0
  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 +833 -287
  16. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentInstance.h +169 -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 +43 -41
  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.map +1 -1
  67. package/lib/commonjs/hooks/index.js.map +1 -1
  68. package/lib/commonjs/hooks/useWindowDimensions/index.android.js.map +1 -1
  69. package/lib/commonjs/hooks/useWindowDimensions/index.js.map +1 -1
  70. package/lib/commonjs/index.js.map +1 -1
  71. package/lib/commonjs/internal.js +18 -18
  72. package/lib/commonjs/internal.js.map +1 -1
  73. package/lib/commonjs/module.js.map +1 -1
  74. package/lib/commonjs/monkey-patch.android.js +4 -4
  75. package/lib/commonjs/monkey-patch.android.js.map +1 -1
  76. package/lib/commonjs/monkey-patch.js.map +1 -1
  77. package/lib/commonjs/reanimated.js.map +1 -1
  78. package/lib/commonjs/reanimated.native.js.map +1 -1
  79. package/lib/commonjs/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
  80. package/lib/commonjs/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
  81. package/lib/commonjs/specs/NativeKeyboardController.js.map +1 -1
  82. package/lib/commonjs/specs/NativeStatusBarManagerCompat.js.map +1 -1
  83. package/lib/commonjs/specs/OverKeyboardViewNativeComponent.js.map +1 -1
  84. package/lib/commonjs/types.js.map +1 -1
  85. package/lib/commonjs/utils.js.map +1 -1
  86. package/lib/commonjs/views/OverKeyboardView/index.js.map +1 -1
  87. package/lib/commonjs/views/index.js.map +1 -1
  88. package/lib/module/animated.js +2 -2
  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/module.js.map +1 -1
  121. package/lib/module/monkey-patch.android.js +4 -4
  122. package/lib/module/monkey-patch.android.js.map +1 -1
  123. package/lib/module/monkey-patch.js.map +1 -1
  124. package/lib/module/reanimated.js.map +1 -1
  125. package/lib/module/reanimated.native.js.map +1 -1
  126. package/lib/module/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
  127. package/lib/module/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
  128. package/lib/module/specs/NativeKeyboardController.js.map +1 -1
  129. package/lib/module/specs/NativeStatusBarManagerCompat.js.map +1 -1
  130. package/lib/module/specs/OverKeyboardViewNativeComponent.js.map +1 -1
  131. package/lib/module/types.js.map +1 -1
  132. package/lib/module/utils.js.map +1 -1
  133. package/lib/module/views/OverKeyboardView/index.js.map +1 -1
  134. package/lib/module/views/index.js.map +1 -1
  135. package/lib/typescript/types.d.ts +1 -0
  136. package/package.json +177 -173
  137. package/src/animated.tsx +255 -255
  138. package/src/bindings.native.ts +62 -62
  139. package/src/bindings.ts +42 -42
  140. package/src/components/KeyboardAvoidingView/hooks.ts +123 -123
  141. package/src/components/KeyboardAvoidingView/index.tsx +188 -188
  142. package/src/components/KeyboardAwareScrollView/index.tsx +397 -389
  143. package/src/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.ts +133 -133
  144. package/src/components/KeyboardAwareScrollView/utils.ts +41 -41
  145. package/src/components/KeyboardStickyView/index.tsx +66 -66
  146. package/src/components/KeyboardToolbar/Arrow.tsx +80 -80
  147. package/src/components/KeyboardToolbar/Button.tsx +98 -98
  148. package/src/components/KeyboardToolbar/colors.native.ts +37 -37
  149. package/src/components/KeyboardToolbar/colors.ts +16 -16
  150. package/src/components/KeyboardToolbar/index.tsx +223 -223
  151. package/src/components/KeyboardToolbar/types.ts +40 -40
  152. package/src/components/hooks/useColorScheme.ts +5 -5
  153. package/src/components/index.ts +11 -11
  154. package/src/constants.ts +19 -19
  155. package/src/context.ts +68 -68
  156. package/src/hooks/index.ts +90 -90
  157. package/src/hooks/useWindowDimensions/index.android.ts +38 -38
  158. package/src/hooks/useWindowDimensions/index.ts +1 -1
  159. package/src/index.ts +23 -23
  160. package/src/internal.ts +84 -84
  161. package/src/module.ts +52 -52
  162. package/src/monkey-patch.android.ts +44 -44
  163. package/src/monkey-patch.ts +4 -4
  164. package/src/reanimated.native.ts +132 -132
  165. package/src/reanimated.ts +29 -29
  166. package/src/specs/KeyboardControllerViewNativeComponent.ts +71 -71
  167. package/src/specs/KeyboardGestureAreaNativeComponent.ts +20 -20
  168. package/src/specs/NativeKeyboardController.ts +19 -19
  169. package/src/specs/NativeStatusBarManagerCompat.ts +14 -14
  170. package/src/specs/OverKeyboardViewNativeComponent.ts +12 -12
  171. package/src/types.ts +244 -243
  172. package/src/utils.ts +1 -1
  173. package/src/views/OverKeyboardView/index.tsx +43 -43
  174. package/src/views/index.ts +1 -1
@@ -1,273 +1,441 @@
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
- import { TurboModule } from "@rnoh/react-native-openharmony/ts";
29
- import common from '@ohos.app.ability.common';
30
- import window from '@ohos.window';
31
- import inputMethod from '@ohos.inputMethod';
32
- import Logger from './Logger';
33
- import { KeyboardControllerEventName, KeyboardStatusType } from './Type';
34
- import { BusinessError } from '@kit.BasicServicesKit';
35
- import { JSON } from '@kit.ArkTS';
36
- import { ConfigurationConstant } from '@kit.AbilityKit';
37
-
38
- declare function px2vp(px: number): number;
39
-
40
- interface RNKeyboardControllerSpec {
41
- getConstants(): {};
42
-
43
- setInputMode(mode: number): void;
44
-
45
- setDefaultMode(): void;
46
-
47
- dismiss(keepFocus: boolean): void;
48
-
49
- setFocusTo(direction: string): void;
50
-
51
- addListener(eventName: string, listener: (event) => void,): void;
52
-
53
- removeListeners(count: number): void;
54
- }
55
-
56
- export class RNKeyboardControllerTurboModule extends TurboModule implements RNKeyboardControllerSpec {
57
- private context: common.UIAbilityContext;
58
- private keyboardHeight: number;
59
- private keyboardStatus: KeyboardStatusType;
60
- private eventListeners: KeyboardControllerEventName[];
61
- private currentWindow:window.Window;
62
- private enabled:boolean;
63
- private cleanUpCallbacks: (() => void)[] = [];
64
- constructor(ctx) {
65
- super(ctx);
66
- this.context = this.ctx.uiAbilityContext;
67
- this.eventListeners = this.supportListeners();
68
-
69
- // 订阅 C++ 层发来的 focusDidSet 消息
70
- this.cleanUpCallbacks.push(
71
- this.ctx.rnInstance.cppEventEmitter.subscribe("focusDidSet", (payload: { current: number, count: number }) => {
72
- Logger.info('###turboModule received focusDidSet from cpp', String(payload.current) + ',' + String(payload.count));
73
- if (this.eventListeners.includes(KeyboardControllerEventName.FOCUS_DID_SET)) {
74
- this.ctx.rnInstance.emitDeviceEvent(KeyboardControllerEventName.FOCUS_DID_SET, {
75
- current: payload.current,
76
- count: payload.count
77
- });
78
- }
79
- })
80
- );
81
- }
82
-
83
- readonly getConstants: () => {};
84
-
85
- // set mode
86
- setInputMode(mode: number): void {
87
- Logger.info('harmonyOS not support setInputMode')
88
- }
89
-
90
- setDefaultMode(): void {
91
- Logger.info('harmonyOS not support setDefaultMode')
92
- }
93
-
94
- /**
95
- * @description 键盘隐藏
96
- * */
97
- dismiss(keepFocus: boolean): void {
98
- let inputMethodController = inputMethod.getController();
99
- inputMethodController.stopInputSession()
100
-
101
- }
102
- /**
103
- * @description 键盘隐藏
104
- * */
105
- private show(): void {
106
- let inputMethodController = inputMethod.getController();
107
- inputMethodController.showSoftKeyboard()
108
-
109
- }
110
-
111
- setFocusTo(direction: string): void {
112
- this.ctx.rnInstance.postMessageToCpp('setFocusTo', direction);
113
- }
114
-
115
- /**
116
- * @description 添加监听键盘事件
117
- */
118
- addListener(eventName: KeyboardControllerEventName) {
119
- let supportEvents = this.supportListeners()
120
- let bo = supportEvents.includes(eventName)
121
- if (bo && !this.eventListeners.includes(eventName)) {
122
- this.eventListeners.push(eventName)
123
- }
124
- }
125
-
126
- /**
127
- * @description 删除监听事件
128
- * */
129
- removeListeners(count: number): void {
130
- let num = this.eventListeners.length;
131
- if (num>0) {
132
- this.eventListeners = [];
133
- }
134
- }
135
-
136
- private supportListeners() {
137
- return [
138
- KeyboardControllerEventName.KEYBOARD_WILL_SHOW,
139
- KeyboardControllerEventName.KEYBOARD_DID_SHOW,
140
- KeyboardControllerEventName.KEYBOARD_WILL_HIDE,
141
- KeyboardControllerEventName.KEYBOARD_DID_HIDE,
142
- KeyboardControllerEventName.FOCUS_DID_SET
143
- ];
144
- }
145
-
146
- private async setWindowSystemBarEnable(statusBarTranslucent: boolean, navigationBarTranslucent: boolean) {
147
-
148
- let windowInstance: window.Window | undefined = undefined;
149
- windowInstance = await window.getLastWindow(this.context);
150
- let systemBarProperty = windowInstance.getWindowSystemBarProperties();
151
- let newSystemBarProperty: window.SystemBarProperties = {
152
- ...systemBarProperty,
153
- statusBarColor: statusBarTranslucent ? "#00000000" : systemBarProperty.statusBarColor,
154
- navigationBarColor: navigationBarTranslucent ? "#00000000" : systemBarProperty.navigationBarColor
155
- };
156
- try {
157
- let promise = windowInstance.setWindowSystemBarProperties(newSystemBarProperty);
158
- promise.then(() => {
159
- Logger.info('Succeeded in setting the system bar properties.');
160
- }).catch((err: BusinessError) => {
161
- Logger.error(`Failed to set the system bar properties. Cause code: ${err.code}, message: ${err.message}`);
162
- });
163
- } catch (exception) {
164
- Logger.error(`Failed to set the system bar properties. Cause code: ${exception.code}, message: ${exception.message}`);
165
- }
166
- }
167
-
168
- private keyboardControllerEventHandle(keyboardStatus: number, height: number) {
169
- Logger.info('###turboModule keyboardControllerEventHandle', String(keyboardStatus) + ',' + String(height));
170
- if(!this.enabled){
171
- return
172
- }
173
-
174
- if (this.keyboardStatus == KeyboardStatusType.HIDE) {
175
- // 键盘隐藏
176
- Logger.info('###turboModule keyboardControllerEventHandle');
177
- this.eventListeners.includes(KeyboardControllerEventName.KEYBOARD_DID_HIDE) &&
178
- this.ctx.rnInstance.emitDeviceEvent(KeyboardControllerEventName.KEYBOARD_DID_HIDE, {
179
- duration: 0,
180
- timestamp: new Date().getTime(),
181
- target: 0,
182
- height: height,
183
- tag:0,
184
- appearance:this.getKeyboardAppearance()
185
- });
186
- }
187
- if (this.keyboardStatus == KeyboardStatusType.SHOW) {
188
- // 键盘显示
189
- Logger.info('###turboModule keyboardControllerEventHandle');
190
- this.eventListeners.includes(KeyboardControllerEventName.KEYBOARD_DID_SHOW) &&
191
- this.ctx.rnInstance.emitDeviceEvent(KeyboardControllerEventName.KEYBOARD_DID_SHOW, {
192
- duration: 0,
193
- timestamp: new Date().getTime(),
194
- target: 0,
195
- height:height,
196
- tag:0,
197
- appearance:this.getKeyboardAppearance()
198
- });
199
- }
200
- }
201
-
202
- private async startKeyboardObserver(open:boolean) {
203
- Logger.info("###turboModule startKeyboardObserver",String(open));
204
- this.enabled=open;
205
- this.currentWindow = await window.getLastWindow(this.context);
206
- if (!this.currentWindow) {
207
- throw ('windowInstance is null')
208
- return
209
- }
210
- if(!open){
211
- try {
212
- Logger.info("###turboModule Close KeyboardObserver");
213
- this.currentWindow.off('keyboardHeightChange', (data) => {
214
- this.keyboardStatus = KeyboardStatusType.HIDE;
215
- this.keyboardHeight = 0;
216
- Logger.info('### close keyboardHeightChange observer');
217
- });
218
- } catch (exception) {
219
- Logger.error('### Failed to close the listener for keyboard height changes. Cause: ' + JSON.stringify(exception));
220
- }
221
- }else{
222
- try {
223
- this.currentWindow.on('keyboardHeightChange', (data) => {
224
- const keyboardAvoidArea = this.currentWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_KEYBOARD).bottomRect;
225
- let height = Math.ceil(px2vp(keyboardAvoidArea.height))
226
- if(open){
227
- if(this.keyboardHeight == height ){
228
- return
229
- }
230
- if (height > 0) {
231
- this.keyboardStatus = KeyboardStatusType.SHOW;
232
- } else {
233
- this.keyboardStatus = KeyboardStatusType.HIDE;
234
- }
235
- this.keyboardHeight = height;
236
- this.keyboardControllerEventHandle(this.keyboardStatus, height);
237
- this.ctx.rnInstance.postMessageToCpp('keyboardHeightChange', height);
238
- }
239
-
240
- });
241
- } catch (exception) {
242
- Logger.error('Failed to enable the listener for keyboard height changes. Cause: ' + JSON.stringify(exception));
243
- }
244
- }
245
-
246
- }
247
-
248
- private getKeyboardAppearance(): String {
249
- const colorMode = this.context.config.colorMode;
250
- if (colorMode == ConfigurationConstant.ColorMode.COLOR_MODE_DARK) {
251
- return "dark";
252
- }
253
- else if (colorMode == ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT) {
254
- return "light";
255
- }
256
- return "default";
257
- }
258
-
259
- private async setWindowLayoutFullScreen(preserveEdgeToEdge:boolean){
260
- let windowInstance: window.Window | undefined = undefined;
261
- windowInstance = await window.getLastWindow(this.context);
262
- try {
263
- let promise = windowInstance.setWindowLayoutFullScreen(preserveEdgeToEdge);
264
- promise.then(() => {
265
- Logger.info('Succeeded in setWindowLayoutFullScreen .' + preserveEdgeToEdge);
266
- }).catch((err: BusinessError) => {
267
- Logger.error(`Failed to setWindowLayoutFullScreen. Cause code: ${err.code}, message: ${err.message}`);
268
- });
269
- } catch (exception) {
270
- Logger.error(`Failed to setWindowLayoutFullScreen. Cause code: ${exception.code}, message: ${exception.message}`);
271
- }
272
- }
273
- }
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
+ import { TurboModule } from "@rnoh/react-native-openharmony/ts";
29
+ import common from '@ohos.app.ability.common';
30
+ import window from '@ohos.window';
31
+ import inputMethod from '@ohos.inputMethod';
32
+ import Logger from './Logger';
33
+ import { KeyboardControllerEventName, KeyboardStatusType } from './Type';
34
+ import { BusinessError, deviceInfo } from '@kit.BasicServicesKit';
35
+ import { JSON } from '@kit.ArkTS';
36
+ import { ConfigurationConstant } from '@kit.AbilityKit';
37
+ import { KeyboardAvoidMode } from '@kit.ArkUI';
38
+
39
+ declare function px2vp(px: number): number;
40
+
41
+ const NATIVE_KEYBOARD_WILL_EVENT_MIN_API = 20;
42
+ const LEGACY_KEYBOARD_ANIMATION_DURATION = 150;
43
+ const WINDOW_SESSION_MANAGER = 'SystemCapability.Window.SessionManager';
44
+
45
+ interface RNKeyboardControllerSpec {
46
+ getConstants(): {};
47
+
48
+ setInputMode(mode: number): void;
49
+
50
+ setDefaultMode(): void;
51
+
52
+ dismiss(keepFocus: boolean): void;
53
+
54
+ setFocusTo(direction: string): void;
55
+
56
+ addListener(eventName: string, listener: (event) => void,): void;
57
+
58
+ removeListeners(count: number): void;
59
+ }
60
+
61
+ export class RNKeyboardControllerTurboModule extends TurboModule implements RNKeyboardControllerSpec {
62
+ private context: common.UIAbilityContext;
63
+ private keyboardHeight: number = 0;
64
+ private keyboardStatus: KeyboardStatusType = KeyboardStatusType.HIDE;
65
+ // RN removes listeners by count only; event-name filtering remains in the JS emitter.
66
+ private listenerCount: number = 0;
67
+ private currentWindow: window.Window | undefined = undefined;
68
+ private enabled: boolean = false;
69
+ private cleanUpCallbacks: (() => void)[] = [];
70
+ private previousKeyboardAvoidMode: KeyboardAvoidMode | undefined = undefined;
71
+ private keyboardAvoidModeControlled: boolean = false;
72
+ private supportsNativeWillEvents: boolean = false;
73
+ private keyboardHeightListenerRegistered: boolean = false;
74
+ private nativeWillShowListenerRegistered: boolean = false;
75
+ private nativeWillHideListenerRegistered: boolean = false;
76
+ private observerGeneration: number = 0;
77
+ private focusedInputTarget: number = -1;
78
+ private focusedInputKeyboardType: string = 'default';
79
+ private currentKeyboardAnimationDuration: number = 0;
80
+
81
+ private readonly keyboardHeightChangeHandler = (_data: number): void => {
82
+ if (!this.enabled || !this.currentWindow) {
83
+ return;
84
+ }
85
+ try {
86
+ const keyboardAvoidArea = this.currentWindow.getWindowAvoidArea(window.AvoidAreaType.TYPE_KEYBOARD).bottomRect;
87
+ const height = Math.ceil(px2vp(keyboardAvoidArea.height));
88
+ if (this.keyboardHeight === height) {
89
+ return;
90
+ }
91
+ const wasVisible = this.keyboardHeight > 0;
92
+ const isVisible = height > 0;
93
+ this.keyboardStatus = height > 0 ? KeyboardStatusType.SHOW : KeyboardStatusType.HIDE;
94
+ this.keyboardHeight = height;
95
+ if (wasVisible !== isVisible) {
96
+ this.keyboardControllerEventHandle(this.keyboardStatus, height);
97
+ }
98
+ this.ctx.rnInstance.postMessageToCpp('keyboardHeightChange', height);
99
+ } catch (exception) {
100
+ Logger.error('Failed to handle keyboard height change. Cause: ' + JSON.stringify(exception));
101
+ }
102
+ };
103
+
104
+ private readonly keyboardWillShowHandler = (info: window.KeyboardInfo): void => {
105
+ if (!this.enabled) {
106
+ return;
107
+ }
108
+ const duration = this.getKeyboardDurationFromKeyboardInfo(info);
109
+ this.currentKeyboardAnimationDuration = duration;
110
+ const height = Math.ceil(px2vp(info.endRect.height));
111
+ this.emitKeyboardEvent(KeyboardControllerEventName.KEYBOARD_WILL_SHOW, height, duration);
112
+ };
113
+
114
+ private readonly keyboardWillHideHandler = (info: window.KeyboardInfo): void => {
115
+ if (!this.enabled) {
116
+ return;
117
+ }
118
+ const duration = this.getKeyboardDurationFromKeyboardInfo(info);
119
+ this.currentKeyboardAnimationDuration = duration;
120
+ this.emitKeyboardEvent(KeyboardControllerEventName.KEYBOARD_WILL_HIDE, 0, duration);
121
+ };
122
+ constructor(ctx) {
123
+ super(ctx);
124
+ this.context = this.ctx.uiAbilityContext;
125
+ // 订阅 C++ 层发来的 focusDidSet 消息
126
+ this.cleanUpCallbacks.push(
127
+ this.ctx.rnInstance.cppEventEmitter.subscribe("focusDidSet", (payload: { current: number, count: number }) => {
128
+ Logger.info('###turboModule received focusDidSet from cpp', String(payload.current) + ',' + String(payload.count));
129
+ if (this.listenerCount > 0) {
130
+ this.ctx.rnInstance.emitDeviceEvent(KeyboardControllerEventName.FOCUS_DID_SET, {
131
+ current: payload.current,
132
+ count: payload.count
133
+ });
134
+ }
135
+ })
136
+ );
137
+
138
+ this.cleanUpCallbacks.push(
139
+ this.ctx.rnInstance.cppEventEmitter.subscribe(
140
+ 'focusedInputChanged',
141
+ (payload: { target: number, type: string }) => {
142
+ this.focusedInputTarget = payload.target;
143
+ this.focusedInputKeyboardType = payload.type;
144
+ }
145
+ )
146
+ );
147
+ }
148
+
149
+ readonly getConstants: () => {};
150
+
151
+ // set mode
152
+ setInputMode(mode: number): void {
153
+ Logger.info('harmonyOS not support setInputMode')
154
+ }
155
+
156
+ setDefaultMode(): void {
157
+ Logger.info('harmonyOS not support setDefaultMode')
158
+ }
159
+
160
+ /**
161
+ * @description 键盘隐藏
162
+ * */
163
+ dismiss(keepFocus: boolean): void {
164
+ let inputMethodController = inputMethod.getController();
165
+ inputMethodController.stopInputSession()
166
+
167
+ }
168
+ /**
169
+ * @description 键盘隐藏
170
+ * */
171
+ private show(): void {
172
+ let inputMethodController = inputMethod.getController();
173
+ inputMethodController.showSoftKeyboard()
174
+
175
+ }
176
+
177
+ setFocusTo(direction: string): void {
178
+ this.ctx.rnInstance.postMessageToCpp('setFocusTo', direction);
179
+ }
180
+
181
+ /**
182
+ * @description 添加监听键盘事件
183
+ */
184
+ addListener(eventName: KeyboardControllerEventName) {
185
+ const supportEvents = this.supportListeners();
186
+ if (supportEvents.includes(eventName)) {
187
+ this.listenerCount += 1;
188
+ }
189
+ }
190
+
191
+ /**
192
+ * @description 删除监听事件
193
+ * */
194
+ removeListeners(count: number): void {
195
+ if (count > 0) {
196
+ this.listenerCount = Math.max(0, this.listenerCount - count);
197
+ }
198
+ }
199
+
200
+ private supportListeners() {
201
+ return [
202
+ KeyboardControllerEventName.KEYBOARD_WILL_SHOW,
203
+ KeyboardControllerEventName.KEYBOARD_DID_SHOW,
204
+ KeyboardControllerEventName.KEYBOARD_WILL_HIDE,
205
+ KeyboardControllerEventName.KEYBOARD_DID_HIDE,
206
+ KeyboardControllerEventName.FOCUS_DID_SET
207
+ ];
208
+ }
209
+
210
+ private async setWindowSystemBarEnable(statusBarTranslucent: boolean, navigationBarTranslucent: boolean) {
211
+
212
+ let windowInstance: window.Window | undefined = undefined;
213
+ windowInstance = await window.getLastWindow(this.context);
214
+ let systemBarProperty = windowInstance.getWindowSystemBarProperties();
215
+ let newSystemBarProperty: window.SystemBarProperties = {
216
+ ...systemBarProperty,
217
+ statusBarColor: statusBarTranslucent ? "#00000000" : systemBarProperty.statusBarColor,
218
+ navigationBarColor: navigationBarTranslucent ? "#00000000" : systemBarProperty.navigationBarColor
219
+ };
220
+ try {
221
+ let promise = windowInstance.setWindowSystemBarProperties(newSystemBarProperty);
222
+ promise.then(() => {
223
+ Logger.info('Succeeded in setting the system bar properties.');
224
+ }).catch((err: BusinessError) => {
225
+ Logger.error(`Failed to set the system bar properties. Cause code: ${err.code}, message: ${err.message}`);
226
+ });
227
+ } catch (exception) {
228
+ Logger.error(`Failed to set the system bar properties. Cause code: ${exception.code}, message: ${exception.message}`);
229
+ }
230
+ }
231
+
232
+ private keyboardControllerEventHandle(keyboardStatus: number, height: number) {
233
+ if (!this.enabled) {
234
+ return;
235
+ }
236
+
237
+ const legacyDuration = deviceInfo.sdkApiVersion < NATIVE_KEYBOARD_WILL_EVENT_MIN_API
238
+ ? LEGACY_KEYBOARD_ANIMATION_DURATION
239
+ : 0;
240
+ const duration = this.supportsNativeWillEvents ? this.currentKeyboardAnimationDuration : legacyDuration;
241
+ if (keyboardStatus === KeyboardStatusType.HIDE) {
242
+ if (!this.supportsNativeWillEvents) {
243
+ this.emitKeyboardEvent(KeyboardControllerEventName.KEYBOARD_WILL_HIDE, 0, duration);
244
+ }
245
+ this.emitKeyboardEvent(KeyboardControllerEventName.KEYBOARD_DID_HIDE, 0, duration);
246
+ this.currentKeyboardAnimationDuration = 0;
247
+ }
248
+ if (keyboardStatus === KeyboardStatusType.SHOW) {
249
+ if (!this.supportsNativeWillEvents) {
250
+ this.emitKeyboardEvent(KeyboardControllerEventName.KEYBOARD_WILL_SHOW, height, duration);
251
+ }
252
+ this.emitKeyboardEvent(KeyboardControllerEventName.KEYBOARD_DID_SHOW, height, duration);
253
+ }
254
+ }
255
+
256
+ private emitKeyboardEvent(eventName: KeyboardControllerEventName, height: number, duration: number): void {
257
+ if (!this.enabled || this.listenerCount <= 0) {
258
+ return;
259
+ }
260
+ this.ctx.rnInstance.emitDeviceEvent(eventName, {
261
+ height,
262
+ tag: this.focusedInputTarget,
263
+ duration,
264
+ timestamp: new Date().getTime(),
265
+ target: this.focusedInputTarget,
266
+ type: this.focusedInputKeyboardType,
267
+ appearance: this.getKeyboardAppearance(),
268
+ });
269
+ }
270
+
271
+ private async startKeyboardObserver(
272
+ open: boolean,
273
+ disableSystemKeyboardAvoidance: boolean = false
274
+ ): Promise<void> {
275
+ const generation = ++this.observerGeneration;
276
+ this.enabled = open;
277
+ if (!open) {
278
+ this.stopKeyboardObserver();
279
+ return;
280
+ }
281
+
282
+ let currentWindow: window.Window;
283
+ try {
284
+ currentWindow = await window.getLastWindow(this.context);
285
+ } catch (exception) {
286
+ Logger.error('Failed to get the current window. Cause: ' + JSON.stringify(exception));
287
+ return;
288
+ }
289
+ if (!this.enabled || generation !== this.observerGeneration) {
290
+ return;
291
+ }
292
+
293
+ this.currentWindow = currentWindow;
294
+ this.setKeyboardAvoidModeEnabled(disableSystemKeyboardAvoidance);
295
+ this.supportsNativeWillEvents =
296
+ deviceInfo.sdkApiVersion >= NATIVE_KEYBOARD_WILL_EVENT_MIN_API &&
297
+ canIUse(WINDOW_SESSION_MANAGER);
298
+ this.registerKeyboardHeightListener();
299
+ this.registerNativeWillListeners();
300
+ }
301
+
302
+ private stopKeyboardObserver(): void {
303
+ this.unregisterNativeWillListeners();
304
+ if (this.currentWindow && this.keyboardHeightListenerRegistered) {
305
+ try {
306
+ this.currentWindow.off('keyboardHeightChange', this.keyboardHeightChangeHandler);
307
+ } catch (exception) {
308
+ Logger.error('Failed to close keyboardHeightChange. Cause: ' + JSON.stringify(exception));
309
+ }
310
+ }
311
+ this.keyboardHeightListenerRegistered = false;
312
+ this.setKeyboardAvoidModeEnabled(false);
313
+ this.currentWindow = undefined;
314
+ this.keyboardHeight = 0;
315
+ this.keyboardStatus = KeyboardStatusType.HIDE;
316
+ this.currentKeyboardAnimationDuration = 0;
317
+ this.supportsNativeWillEvents = false;
318
+ }
319
+
320
+ private registerKeyboardHeightListener(): void {
321
+ if (!this.currentWindow || this.keyboardHeightListenerRegistered) {
322
+ return;
323
+ }
324
+ try {
325
+ this.currentWindow.on('keyboardHeightChange', this.keyboardHeightChangeHandler);
326
+ this.keyboardHeightListenerRegistered = true;
327
+ } catch (exception) {
328
+ Logger.error('Failed to enable keyboardHeightChange. Cause: ' + JSON.stringify(exception));
329
+ }
330
+ }
331
+
332
+ private registerNativeWillListeners(): void {
333
+ if (!this.currentWindow || !this.supportsNativeWillEvents ||
334
+ this.nativeWillShowListenerRegistered || this.nativeWillHideListenerRegistered) {
335
+ return;
336
+ }
337
+
338
+ try {
339
+ this.currentWindow.on('keyboardWillShow', this.keyboardWillShowHandler);
340
+ this.nativeWillShowListenerRegistered = true;
341
+ this.currentWindow.on('keyboardWillHide', this.keyboardWillHideHandler);
342
+ this.nativeWillHideListenerRegistered = true;
343
+ } catch (exception) {
344
+ this.unregisterNativeWillListeners();
345
+ this.supportsNativeWillEvents = false;
346
+ Logger.error('Native keyboard will events unavailable; using confirmed-state fallback. Cause: ' +
347
+ JSON.stringify(exception));
348
+ }
349
+ }
350
+
351
+ private unregisterNativeWillListeners(): void {
352
+ if (!this.currentWindow) {
353
+ this.nativeWillShowListenerRegistered = false;
354
+ this.nativeWillHideListenerRegistered = false;
355
+ return;
356
+ }
357
+ if (this.nativeWillShowListenerRegistered) {
358
+ try {
359
+ this.currentWindow.off('keyboardWillShow', this.keyboardWillShowHandler);
360
+ } catch (exception) {
361
+ Logger.error('Failed to close keyboardWillShow. Cause: ' + JSON.stringify(exception));
362
+ }
363
+ this.nativeWillShowListenerRegistered = false;
364
+ }
365
+ if (this.nativeWillHideListenerRegistered) {
366
+ try {
367
+ this.currentWindow.off('keyboardWillHide', this.keyboardWillHideHandler);
368
+ } catch (exception) {
369
+ Logger.error('Failed to close keyboardWillHide. Cause: ' + JSON.stringify(exception));
370
+ }
371
+ this.nativeWillHideListenerRegistered = false;
372
+ }
373
+ }
374
+
375
+ private getKeyboardDurationFromKeyboardInfo(info: window.KeyboardInfo): number {
376
+ if (info.animated === false) {
377
+ return 0;
378
+ }
379
+ const duration = info.config?.duration;
380
+ return typeof duration === 'number' && duration >= 0 ? duration : 0;
381
+ }
382
+
383
+ __onDestroy__(): void {
384
+ this.enabled = false;
385
+ ++this.observerGeneration;
386
+ this.stopKeyboardObserver();
387
+ this.cleanUpCallbacks.forEach((callback) => callback());
388
+ this.cleanUpCallbacks = [];
389
+ super.__onDestroy__();
390
+ }
391
+
392
+ private setKeyboardAvoidModeEnabled(enabled: boolean): void {
393
+ if (!this.currentWindow) {
394
+ return;
395
+ }
396
+ try {
397
+ const uiContext = this.currentWindow.getUIContext();
398
+ if (enabled) {
399
+ if (!this.keyboardAvoidModeControlled) {
400
+ this.previousKeyboardAvoidMode = uiContext.getKeyboardAvoidMode();
401
+ this.keyboardAvoidModeControlled = true;
402
+ }
403
+ uiContext.setKeyboardAvoidMode(KeyboardAvoidMode.NONE);
404
+ } else if (this.keyboardAvoidModeControlled) {
405
+ if (this.previousKeyboardAvoidMode !== undefined) {
406
+ uiContext.setKeyboardAvoidMode(this.previousKeyboardAvoidMode);
407
+ }
408
+ this.previousKeyboardAvoidMode = undefined;
409
+ this.keyboardAvoidModeControlled = false;
410
+ }
411
+ } catch (exception) {
412
+ Logger.error('Failed to update KeyboardAvoidMode. Cause: ' + JSON.stringify(exception));
413
+ }
414
+ }
415
+
416
+ private getKeyboardAppearance(): string {
417
+ const colorMode = this.context.config.colorMode;
418
+ if (colorMode == ConfigurationConstant.ColorMode.COLOR_MODE_DARK) {
419
+ return "dark";
420
+ }
421
+ else if (colorMode == ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT) {
422
+ return "light";
423
+ }
424
+ return "default";
425
+ }
426
+
427
+ private async setWindowLayoutFullScreen(preserveEdgeToEdge:boolean){
428
+ let windowInstance: window.Window | undefined = undefined;
429
+ windowInstance = await window.getLastWindow(this.context);
430
+ try {
431
+ let promise = windowInstance.setWindowLayoutFullScreen(preserveEdgeToEdge);
432
+ promise.then(() => {
433
+ Logger.info('Succeeded in setWindowLayoutFullScreen .' + preserveEdgeToEdge);
434
+ }).catch((err: BusinessError) => {
435
+ Logger.error(`Failed to setWindowLayoutFullScreen. Cause code: ${err.code}, message: ${err.message}`);
436
+ });
437
+ } catch (exception) {
438
+ Logger.error(`Failed to setWindowLayoutFullScreen. Cause code: ${exception.code}, message: ${exception.message}`);
439
+ }
440
+ }
441
+ }