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

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