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_info.h"
29 #include "panel_status_listener.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 struct LayoutParams {
49     Rosen::Rect landscapeRect;
50     Rosen::Rect portraitRect;
51 };
52 
53 struct PanelAdjustInfo {
54     int32_t top;
55     int32_t left;
56     int32_t right;
57     int32_t bottom;
58     bool operator==(const PanelAdjustInfo &panelAdjust) const
59     {
60         return (top == panelAdjust.top && left == panelAdjust.left && right == panelAdjust.right &&
61                 bottom == panelAdjust.bottom);
62     }
63 };
64 
65 class InputMethodPanel {
66 public:
67     static constexpr uint32_t INVALID_WINDOW_ID = 0;
68     using CallbackFunc = std::function<void(uint32_t, PanelFlag)>;
69     InputMethodPanel() = default;
70     ~InputMethodPanel();
71     int32_t SetUiContent(const std::string &contentInfo, napi_env env, std::shared_ptr<NativeReference> contentStorage);
72     int32_t CreatePanel(const std::shared_ptr<AbilityRuntime::Context> &context, const PanelInfo &panelInfo);
73     int32_t DestroyPanel();
74 
75     int32_t Resize(uint32_t width, uint32_t height);
76     int32_t MoveTo(int32_t x, int32_t y);
77     int32_t AdjustPanelRect(const PanelFlag panelFlag, const LayoutParams &layoutParams);
78     int32_t ParsePanelRect(const PanelFlag panelFlag, const LayoutParams &layoutParams);
79     int32_t GetSysPanelAdjust(const PanelFlag panelFlag,
80         std::tuple<std::vector<std::string>, std::vector<std::string>> &keys, const LayoutParams &layoutParams);
81     int32_t CalculatePanelRect(const PanelFlag panelFlag, PanelAdjustInfo &lanIterValue, PanelAdjustInfo &porIterValue,
82         const LayoutParams &layoutParams);
83     int32_t CalculateLandscapeRect(sptr<OHOS::Rosen::Display> &defaultDisplay, const LayoutParams &layoutParams,
84         PanelAdjustInfo &lanIterValue, float densityDpi);
85     std::tuple<std::vector<std::string>, std::vector<std::string>> GetScreenStatus(const PanelFlag panelFlag);
86     int32_t ChangePanelFlag(PanelFlag panelFlag);
87     PanelType GetPanelType();
88     PanelFlag GetPanelFlag();
89     int32_t ShowPanel();
90     int32_t HidePanel();
91     int32_t SizeChange(const WindowSize &size);
92     WindowSize GetKeyboardSize();
93     bool SetPanelStatusListener(std::shared_ptr<PanelStatusListener> statusListener, const std::string &type);
94     void ClearPanelListener(const std::string &type);
95     int32_t SetCallingWindow(uint32_t windowId);
96     int32_t GetCallingWindowInfo(CallingWindowInfo &windowInfo);
97     int32_t SetPrivacyMode(bool isPrivacyMode);
98     bool IsShowing();
99     bool IsDisplayPortrait();
100     int32_t SetTextFieldAvoidInfo(double positionY, double height);
101     void SetPanelHeightCallback(CallbackFunc heightCallback);
102     uint32_t windowId_ = INVALID_WINDOW_ID;
103 
104 private:
105     class KeyboardPanelInfoChangeListener : public Rosen::IKeyboardPanelInfoChangeListener {
106     public:
107         using ChangeHandler = std::function<void(const Rosen::KeyboardPanelInfo &keyboardPanelInfo)>;
KeyboardPanelInfoChangeListener(ChangeHandler handler)108         explicit KeyboardPanelInfoChangeListener(ChangeHandler handler) : handler_(std::move(handler))
109         {
110         }
111         ~KeyboardPanelInfoChangeListener() = default;
OnKeyboardPanelInfoChanged(const Rosen::KeyboardPanelInfo & keyboardPanelInfo)112         void OnKeyboardPanelInfoChanged(const Rosen::KeyboardPanelInfo &keyboardPanelInfo) override
113         {
114             if (handler_ == nullptr) {
115                 return;
116             }
117             handler_(keyboardPanelInfo);
118         }
119 
120     private:
121         ChangeHandler handler_ = nullptr;
122     };
123     void RegisterKeyboardPanelInfoChangeListener();
124     void UnregisterKeyboardPanelInfoChangeListener();
125     void HandleKbPanelInfoChange(const Rosen::KeyboardPanelInfo &keyboardPanelInfo);
126     bool IsHidden();
127     int32_t SetPanelProperties();
128     std::string GeneratePanelName();
129     void PanelStatusChange(const InputWindowStatus &status);
130     void PanelStatusChangeToImc(const InputWindowStatus &status, const Rosen::Rect &rect);
131     bool MarkListener(const std::string &type, bool isRegister);
132     static uint32_t GenerateSequenceId();
133     bool IsSizeValid(uint32_t width, uint32_t height);
134     bool IsSizeValid(PanelFlag panelFlag, uint32_t width, uint32_t height, int32_t displayWidth, int32_t displayHeight);
135     bool CheckSize(PanelFlag panelFlag, uint32_t width, uint32_t height, bool isDataPortrait);
136     bool GetDisplaySize(bool isPortrait, WindowSize &size);
137     int32_t CalculateFloatRect(const LayoutParams &layoutParams, PanelAdjustInfo &lanIterValue,
138         PanelAdjustInfo &porIterValue, float densityDpi);
139     int32_t CalculateNoConfigRect(const PanelFlag panelFlag, const LayoutParams &layoutParams);
140     void SetResizeParams(uint32_t width, uint32_t height);
141     LayoutParams GetResizeParams();
142 
143     sptr<OHOS::Rosen::Window> window_ = nullptr;
144     sptr<OHOS::Rosen::WindowOption> winOption_ = nullptr;
145     sptr<WindowChangeListenerImpl> windowChangeListenerImpl_ = nullptr;
146     PanelType panelType_ = PanelType::SOFT_KEYBOARD;
147     PanelFlag panelFlag_ = PanelFlag::FLG_FIXED;
148     bool showRegistered_ = false;
149     bool hideRegistered_ = false;
150     bool sizeChangeRegistered_ = false;
151     uint32_t invalidGravityPercent = 0;
152     std::shared_ptr<PanelStatusListener> panelStatusListener_ = nullptr;
153 
154     static std::atomic<uint32_t> sequenceId_;
155     sptr<Rosen::IKeyboardPanelInfoChangeListener> kbPanelInfoListener_{ nullptr };
156     bool isScbEnable_{ false };
157 
158     std::mutex panelAdjustLock_;
159     std::map<std::vector<std::string>, PanelAdjustInfo> panelAdjust_;
160 
161     std::mutex keyboardSizeLock_;
162     WindowSize keyboardSize_{ 0, 0 };
163     Rosen::KeyboardLayoutParams keyboardLayoutParams_;
164     std::mutex windowListenerLock_;
165     sptr<Rosen::IWindowChangeListener> windowChangedListener_ = nullptr;
166     CallbackFunc panelHeightCallback_ = nullptr;
167 
168     LayoutParams adjustPanelRectLayoutParams_;
169 
170     LayoutParams resizePanelFoldParams_ { // FoldDefaultValue
171         {FOLD_TOP, FOLD_LEFT, FOLD_RIGHT, FOLD_BOTTOM},
172         {FOLD_TOP, FOLD_LEFT, FOLD_RIGHT, COMMON_BOTTOM}
173     };
174     LayoutParams resizePanelUnfoldParams_ { // UnfoldDefaultValue
175         {UNFOLD_TOP, UNFOLD_LEFT, UNFOLD_RIGHT, UNFOLD_BOTTOM},
176         {UNFOLD_TOP, UNFOLD_LEFT, UNFOLD_RIGHT, COMMON_BOTTOM}
177     };
178     std::atomic<bool> isWaitSetUiContent_{true};
179 };
180 } // namespace MiscServices
181 } // namespace OHOS
182 
183 #endif //INPUT_METHOD_PANEL_H
184