@react-native-ohos/react-native-keyboard-controller 1.12.8-rc.7 → 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 (169) 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 -8
  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 +29 -0
  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 -90
  15. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentDescriptor.h +77 -77
  16. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentInstance.cpp +816 -158
  17. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentInstance.h +147 -74
  18. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewJSIBinder.h +43 -43
  19. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentDescriptor.h +64 -64
  20. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentInstance.cpp +88 -88
  21. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentInstance.h +63 -63
  22. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaJSIBinder.h +39 -39
  23. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardController.cpp +25 -25
  24. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardController.h +27 -27
  25. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardControllerPackage.cpp +43 -43
  26. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardControllerPackage.h +56 -56
  27. package/harmony/keyboard_controller/src/main/cpp/RNStatusBarManagerCompat.cpp +24 -24
  28. package/harmony/keyboard_controller/src/main/cpp/RNStatusBarManagerCompat.h +28 -28
  29. package/harmony/keyboard_controller/src/main/cpp/ViewHierarchyNavigator.cpp +186 -0
  30. package/harmony/keyboard_controller/src/main/cpp/ViewHierarchyNavigator.h +112 -0
  31. package/harmony/keyboard_controller/src/main/cpp/keyboardControllerPackage.cpp +43 -43
  32. package/harmony/keyboard_controller/src/main/cpp/keyboardControllerPackage.h +56 -56
  33. package/harmony/keyboard_controller/src/main/ets/Logger.ts +45 -45
  34. package/harmony/keyboard_controller/src/main/ets/RNKeyboardControllerPackage.ets +49 -49
  35. package/harmony/keyboard_controller/src/main/ets/RNKeyboardControllerTurboModule.ts +405 -210
  36. package/harmony/keyboard_controller/src/main/ets/RNStatusBarManagerCompatTurboModule.ts +107 -98
  37. package/harmony/keyboard_controller/src/main/ets/Type.ts +41 -41
  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 -8
  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 +2 -0
  70. package/lib/commonjs/constants.js.map +1 -1
  71. package/lib/commonjs/context.js.map +1 -1
  72. package/lib/commonjs/hooks/index.js.map +1 -1
  73. package/lib/commonjs/hooks/useWindowDimensions/index.android.js.map +1 -1
  74. package/lib/commonjs/hooks/useWindowDimensions/index.js.map +1 -1
  75. package/lib/commonjs/index.js.map +1 -1
  76. package/lib/commonjs/internal.js +18 -18
  77. package/lib/commonjs/internal.js.map +1 -1
  78. package/lib/commonjs/monkey-patch.android.js +4 -4
  79. package/lib/commonjs/monkey-patch.android.js.map +1 -1
  80. package/lib/commonjs/monkey-patch.js.map +1 -1
  81. package/lib/commonjs/reanimated.js.map +1 -1
  82. package/lib/commonjs/reanimated.native.js.map +1 -1
  83. package/lib/commonjs/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
  84. package/lib/commonjs/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
  85. package/lib/commonjs/specs/NativeKeyboardController.js.map +1 -1
  86. package/lib/commonjs/specs/NativeStatusBarManagerCompat.js.map +1 -1
  87. package/lib/commonjs/types.js.map +1 -1
  88. package/lib/commonjs/utils.js.map +1 -1
  89. package/lib/module/animated.js +5 -17
  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 +44 -42
  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 +2 -0
  114. package/lib/module/constants.js.map +1 -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/monkey-patch.android.js +4 -4
  123. package/lib/module/monkey-patch.android.js.map +1 -1
  124. package/lib/module/monkey-patch.js.map +1 -1
  125. package/lib/module/reanimated.js.map +1 -1
  126. package/lib/module/reanimated.native.js.map +1 -1
  127. package/lib/module/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
  128. package/lib/module/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
  129. package/lib/module/specs/NativeKeyboardController.js.map +1 -1
  130. package/lib/module/specs/NativeStatusBarManagerCompat.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/typescript/types.d.ts +1 -0
  134. package/package.json +181 -177
  135. package/src/animated.tsx +246 -253
  136. package/src/bindings.native.ts +57 -57
  137. package/src/bindings.ts +38 -38
  138. package/src/components/KeyboardAvoidingView/hooks.ts +58 -58
  139. package/src/components/KeyboardAvoidingView/index.tsx +156 -156
  140. package/src/components/KeyboardAwareScrollView/index.tsx +381 -373
  141. package/src/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.ts +133 -133
  142. package/src/components/KeyboardAwareScrollView/utils.ts +41 -41
  143. package/src/components/KeyboardStickyView/index.tsx +57 -57
  144. package/src/components/KeyboardToolbar/Arrow.tsx +80 -80
  145. package/src/components/KeyboardToolbar/Button.tsx +98 -98
  146. package/src/components/KeyboardToolbar/colors.native.ts +37 -37
  147. package/src/components/KeyboardToolbar/colors.ts +16 -16
  148. package/src/components/KeyboardToolbar/index.tsx +211 -211
  149. package/src/components/KeyboardToolbar/types.ts +40 -40
  150. package/src/components/hooks/useColorScheme.ts +5 -5
  151. package/src/components/index.ts +11 -11
  152. package/src/constants.ts +19 -19
  153. package/src/context.ts +65 -65
  154. package/src/hooks/index.ts +90 -90
  155. package/src/hooks/useWindowDimensions/index.android.ts +33 -33
  156. package/src/hooks/useWindowDimensions/index.ts +1 -1
  157. package/src/index.ts +21 -21
  158. package/src/internal.ts +84 -84
  159. package/src/monkey-patch.android.ts +44 -44
  160. package/src/monkey-patch.ts +4 -4
  161. package/src/reanimated.native.ts +132 -132
  162. package/src/reanimated.ts +29 -29
  163. package/src/specs/KeyboardControllerViewNativeComponent.ts +70 -70
  164. package/src/specs/KeyboardGestureAreaNativeComponent.ts +15 -15
  165. package/src/specs/NativeKeyboardController.ts +19 -19
  166. package/src/specs/NativeStatusBarManagerCompat.ts +14 -14
  167. package/src/types.ts +212 -211
  168. package/src/utils.ts +1 -1
  169. package/react-native-keyboard-controller.podspec +0 -61
@@ -1,210 +1,405 @@
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 } from '@kit.BasicServicesKit';
14
- import { JSON } from '@kit.ArkTS';
15
-
16
- declare function px2vp(px: number): number;
17
-
18
- interface RNKeyboardControllerSpec {
19
- getConstants(): {};
20
-
21
- setInputMode(mode: number): void;
22
-
23
- setDefaultMode(): void;
24
-
25
- dismiss(): void;
26
-
27
- setFocusTo(direction: string): void;
28
-
29
- addListener(eventName: string, listener: (event) => void,): void;
30
-
31
- removeListeners(count: number): void;
32
- }
33
-
34
- export class RNKeyboardControllerTurboModule extends TurboModule implements RNKeyboardControllerSpec {
35
- private context: common.UIAbilityContext;
36
- private keyboardHeight: number;
37
- private keyboardStatus: KeyboardStatusType;
38
- private eventListeners: KeyboardControllerEventName[];
39
- private currentWindow:window.Window;
40
- private enabled:boolean;
41
- constructor(ctx) {
42
- super(ctx);
43
- this.context = this.ctx.uiAbilityContext;
44
- this.eventListeners = this.supportListeners();
45
- }
46
-
47
- readonly getConstants: () => {};
48
-
49
- // set mode
50
- setInputMode(mode: number): void {
51
- Logger.info('harmonyOS not support setInputMode')
52
- }
53
-
54
- setDefaultMode(): void {
55
- Logger.info('harmonyOS not support setDefaultMode')
56
- }
57
-
58
- /**
59
- * @description 键盘隐藏
60
- * */
61
- dismiss(): void {
62
- let inputMethodController = inputMethod.getController();
63
- inputMethodController.stopInputSession()
64
-
65
- }
66
- /**
67
- * @description 键盘隐藏
68
- * */
69
- private show(): void {
70
- let inputMethodController = inputMethod.getController();
71
- inputMethodController.showSoftKeyboard()
72
-
73
- }
74
-
75
- setFocusTo(direction: string): void {
76
- this.ctx.rnInstance.postMessageToCpp('setFocusTo', direction);
77
- }
78
-
79
- /**
80
- * @description 添加监听键盘事件
81
- */
82
- addListener(eventName: KeyboardControllerEventName) {
83
- let supportEvents = this.supportListeners()
84
- let bo = supportEvents.includes(eventName)
85
- if (bo && !this.eventListeners.includes(eventName)) {
86
- this.eventListeners.push(eventName)
87
- }
88
- }
89
-
90
- /**
91
- * @description 删除监听事件
92
- * */
93
- removeListeners(count: number): void {
94
- let num = this.eventListeners.length;
95
- if (num>0) {
96
- this.eventListeners = [];
97
- }
98
- }
99
-
100
- private supportListeners() {
101
- return [
102
- KeyboardControllerEventName.KEYBOARD_WILL_SHOW,
103
- KeyboardControllerEventName.KEYBOARD_DID_SHOW,
104
- KeyboardControllerEventName.KEYBOARD_WILL_HIDE,
105
- KeyboardControllerEventName.KEYBOARD_DID_HIDE,
106
- KeyboardControllerEventName.FOCUS_DID_SET
107
- ];
108
- }
109
-
110
- private async setWindowSystemBarEnable(statusBarTranslucent: boolean, navigationBarTranslucent: boolean) {
111
-
112
- let windowInstance: window.Window | undefined = undefined;
113
- windowInstance = await window.getLastWindow(this.context);
114
- let systemBarProperty = windowInstance.getWindowSystemBarProperties();
115
- let newSystemBarProperty: window.SystemBarProperties = {
116
- ...systemBarProperty,
117
- statusBarColor: statusBarTranslucent ? "#00000000" : systemBarProperty.statusBarColor,
118
- navigationBarColor: navigationBarTranslucent ? "#00000000" : systemBarProperty.navigationBarColor
119
- };
120
- try {
121
- let promise = windowInstance.setWindowSystemBarProperties(newSystemBarProperty);
122
- promise.then(() => {
123
- Logger.info('Succeeded in setting the system bar properties.');
124
- }).catch((err: BusinessError) => {
125
- Logger.error(`Failed to set the system bar properties. Cause code: ${err.code}, message: ${err.message}`);
126
- });
127
- } catch (exception) {
128
- Logger.error(`Failed to set the system bar properties. Cause code: ${exception.code}, message: ${exception.message}`);
129
- }
130
- }
131
-
132
- private keyboardControllerEventHandle(keyboardStatus: number, height: number) {
133
- Logger.info('###turboModule keyboardControllerEventHandle', String(keyboardStatus) + ',' + String(height));
134
- if(!this.enabled){
135
- return
136
- }
137
-
138
- if (this.keyboardStatus == KeyboardStatusType.HIDE) {
139
- // 键盘隐藏
140
- Logger.info('###turboModule keyboardControllerEventHandle');
141
- this.eventListeners.includes(KeyboardControllerEventName.KEYBOARD_DID_HIDE) &&
142
- this.ctx.rnInstance.emitDeviceEvent(KeyboardControllerEventName.KEYBOARD_DID_HIDE, {
143
- duration: 0,
144
- timestamp: new Date().getTime(),
145
- target: 0,
146
- height: height,
147
- tag:0
148
- });
149
- }
150
- if (this.keyboardStatus == KeyboardStatusType.SHOW) {
151
- // 键盘显示
152
- Logger.info('###turboModule keyboardControllerEventHandle');
153
- this.eventListeners.includes(KeyboardControllerEventName.KEYBOARD_DID_SHOW) &&
154
- this.ctx.rnInstance.emitDeviceEvent(KeyboardControllerEventName.KEYBOARD_DID_SHOW, {
155
- duration: 0,
156
- timestamp: new Date().getTime(),
157
- target: 0,
158
- height:height,
159
- tag:0
160
- });
161
- }
162
- }
163
-
164
- private async startKeyboardObserver(open:boolean) {
165
- Logger.info("###turboModule startKeyboardObserver",String(open));
166
- this.enabled=open;
167
- this.currentWindow = await window.getLastWindow(this.context);
168
- if (!this.currentWindow) {
169
- throw ('windowInstance is null')
170
- return
171
- }
172
- if(!open){
173
- try {
174
- Logger.info("###turboModule Close KeyboardObserver");
175
- this.currentWindow.off('keyboardHeightChange', (data) => {
176
- this.keyboardStatus = KeyboardStatusType.HIDE;
177
- this.keyboardHeight = 0;
178
- Logger.info('### close keyboardHeightChange observer');
179
- });
180
- } catch (exception) {
181
- Logger.error('### Failed to close the listener for keyboard height changes. Cause: ' + JSON.stringify(exception));
182
- }
183
- }else{
184
- try {
185
- this.currentWindow.on('keyboardHeightChange', (data) => {
186
- const keyboardAvoidArea = this.currentWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_KEYBOARD).bottomRect;
187
- let height = Math.ceil(px2vp(keyboardAvoidArea.height))
188
- if(open){
189
- if(this.keyboardHeight == height ){
190
- return
191
- }
192
- if (height > 0) {
193
- this.keyboardStatus = KeyboardStatusType.SHOW;
194
- } else {
195
- this.keyboardStatus = KeyboardStatusType.HIDE;
196
- }
197
- this.keyboardHeight = height;
198
- this.keyboardControllerEventHandle(this.keyboardStatus, height);
199
- this.ctx.rnInstance.postMessageToCpp('keyboardHeightChange', height);
200
- }
201
-
202
- });
203
- } catch (exception) {
204
- Logger.error('Failed to enable the listener for keyboard height changes. Cause: ' + JSON.stringify(exception));
205
- }
206
- }
207
-
208
- }
209
-
210
- }
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
+ }