• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef INPUT_METHOD_PANEL_H
17 #define INPUT_METHOD_PANEL_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <map>
22 #include <string>
23 
24 #include "calling_window_info.h"
25 #include "display_manager.h"
26 #include "input_window_info.h"
27 #include "js_runtime_utils.h"
28 #include "panel_common.h"
29 #include "panel_info.h"
30 #include "window_change_listener_impl.h"
31 #include "wm_common.h"
32 
33 namespace OHOS {
34 namespace MiscServices {
35 
36 constexpr int FOLD_TOP = 0;
37 constexpr int FOLD_LEFT = 0;
38 constexpr int FOLD_RIGHT = 0;
39 constexpr int FOLD_BOTTOM = 606;
40 
41 constexpr int UNFOLD_TOP = 0;
42 constexpr int UNFOLD_LEFT = 0;
43 constexpr int UNFOLD_RIGHT = 0;
44 constexpr int UNFOLD_BOTTOM = 822;
45 
46 constexpr int COMMON_BOTTOM = 809;
47 
48 class InputMethodPanel {
49 public:
50     static constexpr uint32_t INVALID_WINDOW_ID = 0;
51     using CallbackFunc = std::function<void(uint32_t, PanelFlag)>;
52     InputMethodPanel() = default;
53     ~InputMethodPanel();
54     int32_t SetUiContent(const std::string &contentInfo, napi_env env, std::shared_ptr<NativeReference> contentStorage);
55     int32_t CreatePanel(const std::shared_ptr<AbilityRuntime::Context> &context, const PanelInfo &panelInfo);
56     int32_t DestroyPanel();
57 
58     int32_t Resize(uint32_t width, uint32_t height);
59     int32_t MoveTo(int32_t x, int32_t y);
60     int32_t StartMoving();
61     int32_t GetDisplayId(uint64_t &displayId);
62     int32_t AdjustPanelRect(const PanelFlag panelFlag, const LayoutParams &layoutParams, bool needUpdateRegion = true);
63     int32_t AdjustPanelRect(PanelFlag panelFlag, EnhancedLayoutParams params, HotAreas hotAreas);
64     int32_t UpdateRegion(std::vector<Rosen::Rect> region);
65     int32_t ParsePanelRect(const PanelFlag panelFlag, const LayoutParams &layoutParams);
66     int32_t GetSysPanelAdjust(const PanelFlag panelFlag,
67         std::tuple<std::vector<std::string>, std::vector<std::string>> &keys, const LayoutParams &layoutParams);
68     int32_t CalculatePanelRect(const PanelFlag panelFlag, PanelAdjustInfo &lanIterValue, PanelAdjustInfo &porIterValue,
69         const LayoutParams &layoutParams);
70     int32_t CalculateLandscapeRect(sptr<OHOS::Rosen::Display> &defaultDisplay, const LayoutParams &layoutParams,
71         PanelAdjustInfo &lanIterValue);
72     std::tuple<std::vector<std::string>, std::vector<std::string>> GetScreenStatus(const PanelFlag panelFlag);
73     int32_t ChangePanelFlag(PanelFlag panelFlag);
74     PanelType GetPanelType();
75     PanelFlag GetPanelFlag();
76     int32_t ShowPanel();
77     int32_t HidePanel();
78     int32_t SizeChange(const WindowSize &size);
79     WindowSize GetKeyboardSize();
80     bool SetPanelStatusListener(std::shared_ptr<PanelStatusListener> statusListener, const std::string &type);
81     void ClearPanelListener(const std::string &type);
82     int32_t SetCallingWindow(uint32_t windowId);
83     int32_t GetCallingWindowInfo(CallingWindowInfo &windowInfo);
84     int32_t SetPrivacyMode(bool isPrivacyMode);
85     bool IsShowing();
86     bool IsDisplayPortrait();
87     int32_t SetTextFieldAvoidInfo(double positionY, double height);
88     void SetPanelHeightCallback(CallbackFunc heightCallback);
89     int32_t IsEnhancedParamValid(PanelFlag panelFlag, EnhancedLayoutParams &params);
90     int32_t SetImmersiveMode(ImmersiveMode mode);
91     ImmersiveMode GetImmersiveMode();
92     uint32_t windowId_ = INVALID_WINDOW_ID;
93 
94 private:
95     class KeyboardPanelInfoChangeListener : public Rosen::IKeyboardPanelInfoChangeListener {
96     public:
97         using ChangeHandler = std::function<void(const Rosen::KeyboardPanelInfo &keyboardPanelInfo)>;
KeyboardPanelInfoChangeListener(ChangeHandler handler)98         explicit KeyboardPanelInfoChangeListener(ChangeHandler handler) : handler_(std::move(handler))
99         {
100         }
101         ~KeyboardPanelInfoChangeListener() = default;
OnKeyboardPanelInfoChanged(const Rosen::KeyboardPanelInfo & keyboardPanelInfo)102         void OnKeyboardPanelInfoChanged(const Rosen::KeyboardPanelInfo &keyboardPanelInfo) override
103         {
104             if (handler_ == nullptr) {
105                 return;
106             }
107             handler_(keyboardPanelInfo);
108         }
109 
110     private:
111         ChangeHandler handler_ = nullptr;
112     };
113     void RegisterKeyboardPanelInfoChangeListener();
114     void UnregisterKeyboardPanelInfoChangeListener();
115     void HandleKbPanelInfoChange(const Rosen::KeyboardPanelInfo &keyboardPanelInfo);
116     bool IsHidden();
117     int32_t SetPanelProperties();
118     std::string GeneratePanelName();
119     void PanelStatusChange(const InputWindowStatus &status);
120     void PanelStatusChangeToImc(const InputWindowStatus &status, const Rosen::Rect &rect);
121     bool MarkListener(const std::string &type, bool isRegister);
122     bool SetPanelSizeChangeListener(std::shared_ptr<PanelStatusListener> statusListener);
123     std::shared_ptr<PanelStatusListener> GetPanelListener();
124     static uint32_t GenerateSequenceId();
125     bool IsSizeValid(uint32_t width, uint32_t height);
126     bool IsSizeValid(PanelFlag panelFlag, uint32_t width, uint32_t height, int32_t displayWidth, int32_t displayHeight);
127     bool IsRectValid(const Rosen::Rect &rect, const WindowSize &displaySize);
128     bool CheckSize(PanelFlag panelFlag, uint32_t width, uint32_t height, bool isDataPortrait);
129     bool GetDisplaySize(bool isPortrait, WindowSize &size);
130     int32_t CalculateFloatRect(const LayoutParams &layoutParams, PanelAdjustInfo &lanIterValue,
131         PanelAdjustInfo &porIterValue);
132     int32_t CalculateNoConfigRect(const PanelFlag panelFlag, const LayoutParams &layoutParams);
133     int32_t GetResizeParams(Rosen::Rect &portrait, Rosen::Rect &landscape, uint32_t width, uint32_t height);
134 
135     int32_t ParseEnhancedParams(
136         PanelFlag panelFlag, const FullPanelAdjustInfo &adjustInfo, EnhancedLayoutParams &params);
137     int32_t RectifyRect(bool isFullScreen, EnhancedLayoutParam &layoutParam, const WindowSize &displaySize,
138         PanelFlag panelFlag, const PanelAdjustInfo &adjustInfo);
139     int32_t CalculateAvoidHeight(EnhancedLayoutParam &layoutParam, const WindowSize &displaySize, PanelFlag panelFlag,
140         const PanelAdjustInfo &adjustInfo);
141 
142     void CalculateHotAreas(const EnhancedLayoutParams &enhancedParams, const Rosen::KeyboardLayoutParams &params,
143         const FullPanelAdjustInfo &adjustInfo, HotAreas &hotAreas);
144     void CalculateDefaultHotArea(
145         const Rosen::Rect &keyboard, const Rosen::Rect &panel, const PanelAdjustInfo &adjustInfo, HotArea &hotArea);
146     void CalculateHotArea(
147         const Rosen::Rect &keyboard, const Rosen::Rect &panel, const PanelAdjustInfo &adjustInfo, HotArea &hotArea);
148     void CalculateEnhancedHotArea(
149         const EnhancedLayoutParam &layout, const PanelAdjustInfo &adjustInfo, HotArea &hotArea);
150     void RectifyAreas(const std::vector<Rosen::Rect> availableAreas, std::vector<Rosen::Rect> &areas);
151     Rosen::Rect GetRectIntersection(Rosen::Rect a, Rosen::Rect b);
152     uint32_t SafeSubtract(uint32_t minuend, uint32_t subtrahend);
153 
154     int32_t ResizePanel(uint32_t width, uint32_t height);
155     int32_t ResizeWithoutAdjust(uint32_t width, uint32_t height);
156     int32_t ResizeEnhancedPanel(uint32_t width, uint32_t height);
157     void UpdateRectParams(
158         Rosen::Rect &portrait, Rosen::Rect &landscape, uint32_t width, uint32_t height, const LayoutParams &currParams);
159     int32_t MovePanelRect(int32_t x, int32_t y);
160     int32_t MoveEnhancedPanelRect(int32_t x, int32_t y);
161 
162     bool IsDisplayUnfolded();
163     int32_t GetDisplaySize(DisplaySize &size);
164     int32_t GetDensityDpi(float &densityDpi);
165 
166     int32_t InitAdjustInfo();
167     int32_t GetAdjustInfo(PanelFlag panelFlag, FullPanelAdjustInfo &fullPanelAdjustInfo);
168 
169     void UpdateResizeParams();
170     void UpdateHotAreas();
171     void UpdateLayoutInfo(PanelFlag panelFlag, const LayoutParams &params, const EnhancedLayoutParams &enhancedParams,
172         const Rosen::KeyboardLayoutParams &wmsParams, bool isEnhanced);
173     Rosen::KeyboardLayoutParams ConvertToWMSParam(PanelFlag panelFlag, const EnhancedLayoutParams &layoutParams);
174     Rosen::KeyboardTouchHotAreas ConvertToWMSHotArea(const HotAreas &hotAreas);
175     void OnPanelHeightChange(const Rosen::KeyboardPanelInfo &keyboardPanelInfo);
176     int32_t GetKeyboardArea(PanelFlag panelFlag, const WindowSize &size, PanelAdjustInfo &keyboardArea);
177     int32_t GetWindowOrientation(PanelFlag panelFlag, uint32_t windowWidth, bool &isPortrait);
178 
179     void SetHotAreas(const HotAreas &hotAreas);
180     HotAreas GetHotAreas();
181 
182     sptr<OHOS::Rosen::Window> window_ = nullptr;
183     sptr<OHOS::Rosen::WindowOption> winOption_ = nullptr;
184     sptr<WindowChangeListenerImpl> windowChangeListenerImpl_ = nullptr;
185     PanelType panelType_ = PanelType::SOFT_KEYBOARD;
186     PanelFlag panelFlag_ = PanelFlag::FLG_FIXED;
187     bool showRegistered_ = false;
188     bool hideRegistered_ = false;
189     bool sizeChangeRegistered_ = false;
190     bool sizeUpdateRegistered_ = false;
191     uint32_t invalidGravityPercent = 0;
192     std::shared_ptr<PanelStatusListener> panelStatusListener_ = nullptr;
193 
194     static std::atomic<uint32_t> sequenceId_;
195     sptr<Rosen::IKeyboardPanelInfoChangeListener> kbPanelInfoListener_{ nullptr };
196     bool isScbEnable_{ false };
197 
198     std::mutex panelAdjustLock_;
199     std::map<std::vector<std::string>, PanelAdjustInfo> panelAdjust_;
200     std::mutex adjustInfoInitLock_;
201     std::atomic<bool> isAdjustInfoInitialized_{ false };
202 
203     std::mutex hotAreasLock_;
204     HotAreas hotAreas_;
205     EnhancedLayoutParams enhancedLayoutParams_;
206     Rosen::KeyboardLayoutParams keyboardLayoutParams_;
207 
208     std::mutex keyboardSizeLock_;
209     WindowSize keyboardSize_{ 0, 0 };
210     std::mutex windowListenerLock_;
211     sptr<Rosen::IWindowChangeListener> windowChangedListener_ = nullptr;
212     CallbackFunc panelHeightCallback_ = nullptr;
213 
214     LayoutParams resizePanelFoldParams_ { // FoldDefaultValue
215         {FOLD_TOP, FOLD_LEFT, FOLD_RIGHT, FOLD_BOTTOM},
216         {FOLD_TOP, FOLD_LEFT, FOLD_RIGHT, COMMON_BOTTOM}
217     };
218     LayoutParams resizePanelUnfoldParams_ { // UnfoldDefaultValue
219         {UNFOLD_TOP, UNFOLD_LEFT, UNFOLD_RIGHT, UNFOLD_BOTTOM},
220         {UNFOLD_TOP, UNFOLD_LEFT, UNFOLD_RIGHT, COMMON_BOTTOM}
221     };
222     std::atomic<bool> isWaitSetUiContent_{true};
223     std::atomic<bool> isInEnhancedAdjust_{ false };
224     ImmersiveMode immersiveMode_ { ImmersiveMode::NONE_IMMERSIVE };
225 };
226 } // namespace MiscServices
227 } // namespace OHOS
228 
229 #endif //INPUT_METHOD_PANEL_H
230