• 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 "native_engine/native_engine.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 constexpr int FOLD_TOP = 0;
36 constexpr int FOLD_LEFT = 0;
37 constexpr int FOLD_RIGHT = 0;
38 constexpr int FOLD_BOTTOM = 606;
39 constexpr int UNFOLD_TOP = 0;
40 constexpr int UNFOLD_LEFT = 0;
41 constexpr int UNFOLD_RIGHT = 0;
42 constexpr int UNFOLD_BOTTOM = 822;
43 constexpr int COMMON_BOTTOM = 809;
44 
45 class InputMethodPanel {
46 public:
47     static constexpr uint32_t INVALID_WINDOW_ID = 0;
48     using CallbackFunc = std::function<void(uint32_t, PanelFlag)>;
49     InputMethodPanel() = default;
50     ~InputMethodPanel();
51     int32_t SetUiContent(const std::string &contentInfo, napi_env env, std::shared_ptr<NativeReference> contentStorage);
52     int32_t CreatePanel(const std::shared_ptr<AbilityRuntime::Context> &context, const PanelInfo &panelInfo);
53     int32_t DestroyPanel();
54 
55     int32_t Resize(uint32_t width, uint32_t height);
56     int32_t MoveTo(int32_t x, int32_t y);
57     int32_t StartMoving();
58     int32_t GetDisplayId(uint64_t &displayId);
59     int32_t AdjustKeyboard();
60     int32_t AdjustPanelRect(const PanelFlag panelFlag, const LayoutParams &layoutParams, bool needUpdateRegion = true);
61     int32_t AdjustPanelRect(PanelFlag panelFlag, EnhancedLayoutParams params, HotAreas hotAreas);
62     int32_t UpdateRegion(std::vector<Rosen::Rect> region);
63     int32_t ParsePanelRect(const PanelFlag panelFlag, const LayoutParams &layoutParams);
64     int32_t GetSysPanelAdjust(const PanelFlag panelFlag,
65         std::tuple<std::vector<std::string>, std::vector<std::string>> &keys, const LayoutParams &layoutParams);
66     int32_t CalculatePanelRect(const PanelFlag panelFlag, PanelAdjustInfo &lanIterValue, PanelAdjustInfo &porIterValue,
67         const LayoutParams &layoutParams);
68     int32_t CalculateLandscapeRect(sptr<OHOS::Rosen::Display> &defaultDisplay, const LayoutParams &layoutParams,
69         PanelAdjustInfo &lanIterValue);
70     std::tuple<std::vector<std::string>, std::vector<std::string>> GetScreenStatus(const PanelFlag panelFlag);
71     int32_t ChangePanelFlag(PanelFlag panelFlag);
72     PanelType GetPanelType();
73     PanelFlag GetPanelFlag();
74     int32_t ShowPanel();
75     int32_t HidePanel();
76     int32_t SizeChange(const WindowSize &size);
77     WindowSize GetKeyboardSize();
78     bool SetPanelStatusListener(std::shared_ptr<PanelStatusListener> statusListener, const std::string &type);
79     void ClearPanelListener(const std::string &type);
80     int32_t SetCallingWindow(uint32_t windowId, bool needWait = false);
81     int32_t GetCallingWindowInfo(CallingWindowInfo &windowInfo);
82     int32_t SetPrivacyMode(bool isPrivacyMode);
83     bool IsShowing();
84     bool IsDisplayPortrait();
85     int32_t SetTextFieldAvoidInfo(double positionY, double height);
86     void SetPanelHeightCallback(CallbackFunc heightCallback);
87     int32_t IsEnhancedParamValid(PanelFlag panelFlag, EnhancedLayoutParams &params);
88     int32_t SetImmersiveMode(ImmersiveMode mode);
89     ImmersiveMode GetImmersiveMode();
90     bool IsInMainDisplay();
91     uint32_t windowId_ = INVALID_WINDOW_ID;
92 
93 private:
94     class KeyboardPanelInfoChangeListener : public Rosen::IKeyboardPanelInfoChangeListener {
95     public:
96         using ChangeHandler = std::function<void(const Rosen::KeyboardPanelInfo &keyboardPanelInfo)>;
KeyboardPanelInfoChangeListener(ChangeHandler handler)97         explicit KeyboardPanelInfoChangeListener(ChangeHandler handler) : handler_(std::move(handler)) { }
98         ~KeyboardPanelInfoChangeListener() = default;
OnKeyboardPanelInfoChanged(const Rosen::KeyboardPanelInfo & keyboardPanelInfo)99         void OnKeyboardPanelInfoChanged(const Rosen::KeyboardPanelInfo &keyboardPanelInfo) override
100         {
101             if (handler_ == nullptr) {
102                 return;
103             }
104             handler_(keyboardPanelInfo);
105         }
106 
107     private:
108         ChangeHandler handler_ = nullptr;
109     };
110     void RegisterKeyboardPanelInfoChangeListener();
111     void UnregisterKeyboardPanelInfoChangeListener();
112     void HandleKbPanelInfoChange(const Rosen::KeyboardPanelInfo &keyboardPanelInfo);
113     bool IsHidden();
114     int32_t SetPanelProperties();
115     std::string GeneratePanelName();
116     void NotifyPanelStatus(PanelFlag panelFlag);
117     void PanelStatusChange(const InputWindowStatus &status);
118     void PanelStatusChangeToImc(const InputWindowStatus &status, const Rosen::Rect &rect);
119     bool MarkListener(const std::string &type, bool isRegister);
120     bool SetPanelSizeChangeListener(std::shared_ptr<PanelStatusListener> statusListener);
121     std::shared_ptr<PanelStatusListener> GetPanelListener();
122     static uint32_t GenerateSequenceId();
123     bool IsSizeValid(uint32_t width, uint32_t height);
124     bool IsSizeValid(PanelFlag panelFlag, uint32_t width, uint32_t height, int32_t displayWidth, int32_t displayHeight);
125     bool IsRectValid(const Rosen::Rect &rect, const WindowSize &displaySize);
126     bool CheckSize(PanelFlag panelFlag, uint32_t width, uint32_t height, bool isDataPortrait);
127     bool GetDisplaySize(bool isPortrait, WindowSize &size);
128     int32_t CalculateFloatRect(const LayoutParams &layoutParams, PanelAdjustInfo &lanIterValue,
129         PanelAdjustInfo &porIterValue);
130     int32_t CalculateNoConfigRect(const PanelFlag panelFlag, const LayoutParams &layoutParams);
131     int32_t GetResizeParams(Rosen::Rect &portrait, Rosen::Rect &landscape, uint32_t width, uint32_t height);
132 
133     int32_t ParseEnhancedParams(
134         PanelFlag panelFlag, const FullPanelAdjustInfo &adjustInfo, EnhancedLayoutParams &params);
135     int32_t RectifyRect(bool isFullScreen, EnhancedLayoutParam &layoutParam, const WindowSize &displaySize,
136         PanelFlag panelFlag, const PanelAdjustInfo &adjustInfo);
137     int32_t CalculateAvoidHeight(EnhancedLayoutParam &layoutParam, const WindowSize &displaySize, PanelFlag panelFlag,
138         const PanelAdjustInfo &adjustInfo);
139 
140     void CalculateHotAreas(const EnhancedLayoutParams &enhancedParams, const Rosen::KeyboardLayoutParams &params,
141         const FullPanelAdjustInfo &adjustInfo, HotAreas &hotAreas);
142     void CalculateDefaultHotArea(
143         const Rosen::Rect &keyboard, const Rosen::Rect &panel, const PanelAdjustInfo &adjustInfo, HotArea &hotArea);
144     void CalculateHotArea(
145         const Rosen::Rect &keyboard, const Rosen::Rect &panel, const PanelAdjustInfo &adjustInfo, HotArea &hotArea);
146     void CalculateEnhancedHotArea(
147         const EnhancedLayoutParam &layout, const PanelAdjustInfo &adjustInfo, HotArea &hotArea);
148     void RectifyAreas(const std::vector<Rosen::Rect> availableAreas, std::vector<Rosen::Rect> &areas);
149     Rosen::Rect GetRectIntersection(Rosen::Rect a, Rosen::Rect b);
150     uint32_t SafeSubtract(uint32_t minuend, uint32_t subtrahend);
151 
152     int32_t ResizePanel(uint32_t width, uint32_t height);
153     int32_t ResizeWithoutAdjust(uint32_t width, uint32_t height);
154     int32_t ResizeEnhancedPanel(uint32_t width, uint32_t height);
155     void UpdateRectParams(
156         Rosen::Rect &portrait, Rosen::Rect &landscape, uint32_t width, uint32_t height, const LayoutParams &currParams);
157     int32_t MovePanelRect(int32_t x, int32_t y);
158     int32_t MoveEnhancedPanelRect(int32_t x, int32_t y);
159 
160     bool IsDisplayUnfolded();
161     int32_t GetDisplaySize(DisplaySize &size);
162     int32_t GetDensityDpi(float &densityDpi);
163 
164     int32_t InitAdjustInfo();
165     int32_t GetAdjustInfo(PanelFlag panelFlag, FullPanelAdjustInfo &fullPanelAdjustInfo);
166 
167     void UpdateResizeParams();
168     void UpdateHotAreas();
169     void UpdateLayoutInfo(PanelFlag panelFlag, const LayoutParams &params, const EnhancedLayoutParams &enhancedParams,
170         const Rosen::KeyboardLayoutParams &wmsParams, bool isEnhanced);
171     Rosen::KeyboardLayoutParams ConvertToWMSParam(PanelFlag panelFlag, const EnhancedLayoutParams &layoutParams);
172     Rosen::KeyboardTouchHotAreas ConvertToWMSHotArea(const HotAreas &hotAreas);
173     void OnPanelHeightChange(const Rosen::KeyboardPanelInfo &keyboardPanelInfo);
174     int32_t GetKeyboardArea(PanelFlag panelFlag, const WindowSize &size, PanelAdjustInfo &keyboardArea);
175     int32_t GetWindowOrientation(PanelFlag panelFlag, uint32_t windowWidth, bool &isPortrait);
176 
177     void SetHotAreas(const HotAreas &hotAreas);
178     HotAreas GetHotAreas();
179     sptr<Rosen::Display> GetCurDisplay();
180     bool IsNeedConfig();
181     sptr<OHOS::Rosen::Window> window_ = nullptr;
182     sptr<OHOS::Rosen::WindowOption> winOption_ = nullptr;
183     PanelType panelType_ = PanelType::SOFT_KEYBOARD;
184     PanelFlag panelFlag_ = PanelFlag::FLG_FIXED;
185     bool showRegistered_ = false;
186     bool hideRegistered_ = false;
187     bool sizeChangeRegistered_ = false;
188     bool sizeUpdateRegistered_ = false;
189     uint32_t invalidGravityPercent = 0;
190     std::shared_ptr<PanelStatusListener> panelStatusListener_ = nullptr;
191 
192     static std::atomic<uint32_t> sequenceId_;
193     sptr<Rosen::IKeyboardPanelInfoChangeListener> kbPanelInfoListener_ { nullptr };
194     bool isScbEnable_ { false };
195 
196     std::mutex panelAdjustLock_;
197     std::map<std::vector<std::string>, PanelAdjustInfo> panelAdjust_;
198     std::mutex adjustInfoInitLock_;
199     std::atomic<bool> isAdjustInfoInitialized_{ false };
200 
201     std::mutex hotAreasLock_;
202     HotAreas hotAreas_;
203     EnhancedLayoutParams enhancedLayoutParams_;
204     Rosen::KeyboardLayoutParams keyboardLayoutParams_;
205 
206     std::mutex keyboardSizeLock_;
207     WindowSize keyboardSize_ { 0, 0 };
208     std::mutex windowListenerLock_;
209     sptr<Rosen::IWindowChangeListener> windowChangedListener_ = nullptr;
210     CallbackFunc panelHeightCallback_ = nullptr;
211 
212     LayoutParams resizePanelFoldParams_ { // FoldDefaultValue
213         { FOLD_TOP, FOLD_LEFT, FOLD_RIGHT, FOLD_BOTTOM },
214         { FOLD_TOP, FOLD_LEFT, FOLD_RIGHT, COMMON_BOTTOM }
215     };
216 
217     LayoutParams resizePanelUnfoldParams_ { // UnfoldDefaultValue
218         { UNFOLD_TOP, UNFOLD_LEFT, UNFOLD_RIGHT, UNFOLD_BOTTOM },
219         { UNFOLD_TOP, UNFOLD_LEFT, UNFOLD_RIGHT, COMMON_BOTTOM }
220     };
221     std::atomic<bool> isWaitSetUiContent_ { true };
222     std::atomic<bool> isInEnhancedAdjust_{ false };
223     ImmersiveMode immersiveMode_ { ImmersiveMode::NONE_IMMERSIVE };
224 };
225 } // namespace MiscServices
226 } // namespace OHOS
227 
228 #endif // INPUT_METHOD_PANEL_H
229