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 IMF_ADAPTER_H
17 #define IMF_ADAPTER_H
18 
19 #include <string>
20 
21 namespace OHOS::NWeb {
22 enum class IMFAdapterTextInputType : int32_t {
23     NONE = -1,
24     TEXT = 0,
25     MULTILINE,
26     NUMBER,
27     PHONE,
28     DATETIME,
29     EMAIL_ADDRESS,
30     URL,
31     VISIBLE_PASSWORD,
32     NUMBER_PASSWORD,
33     SCREEN_LOCK_PASSWORD,
34     USER_NAME,
35     NEW_PASSWORD,
36     NUMBER_DECIMAL,
37 };
38 
39 enum class IMFAdapterEnterKeyType : int32_t {
40     UNSPECIFIED = 0,
41     NONE,
42     GO,
43     SEARCH,
44     SEND,
45     NEXT,
46     DONE,
47     PREVIOUS,
48     NEW_LINE,
49 };
50 
51 enum class IMFAdapterDirection : int32_t {
52     NONE = 0,
53     UP = 1,
54     DOWN,
55     LEFT,
56     RIGHT,
57 };
58 
59 class IMFCursorInfoAdapter {
60 public:
61     IMFCursorInfoAdapter() = default;
62 
63     virtual ~IMFCursorInfoAdapter() = default;
64 
65     virtual double GetLeft() = 0;
66 
67     virtual double GetTop() = 0;
68 
69     virtual double GetWidth() = 0;
70 
71     virtual double GetHeight() = 0;
72 };
73 
74 class IMFInputAttributeAdapter {
75 public:
76     IMFInputAttributeAdapter() = default;
77 
78     virtual ~IMFInputAttributeAdapter() = default;
79 
80     virtual int32_t GetInputPattern() = 0;
81 
82     virtual int32_t GetEnterKeyType() = 0;
83 };
84 
85 class IMFSelectionRangeAdapter {
86 public:
87     IMFSelectionRangeAdapter() = default;
88 
89     virtual ~IMFSelectionRangeAdapter() = default;
90 
91     virtual int32_t GetStart() = 0;
92 
93     virtual int32_t GetEnd() = 0;
94 };
95 
96 class IMFTextConfigAdapter {
97 public:
98     IMFTextConfigAdapter() = default;
99 
100     virtual ~IMFTextConfigAdapter() = default;
101 
102     virtual std::shared_ptr<IMFInputAttributeAdapter> GetInputAttribute() = 0;
103 
104     virtual std::shared_ptr<IMFCursorInfoAdapter> GetCursorInfo() = 0;
105 
106     virtual std::shared_ptr<IMFSelectionRangeAdapter> GetSelectionRange() = 0;
107 
108     virtual uint32_t GetWindowId() = 0;
109 
GetPositionY()110     virtual double GetPositionY()
111     {
112         return 0.0;
113     }
114 
GetHeight()115     virtual double GetHeight()
116     {
117         return 0.0;
118     }
119 };
120 
121 enum class IMFAdapterKeyboardStatus : int32_t { NONE = 0, HIDE, SHOW };
122 
123 class IMFAdapterFunctionKeyAdapter {
124 public:
125     IMFAdapterFunctionKeyAdapter() = default;
126 
127     virtual ~IMFAdapterFunctionKeyAdapter() = default;
128 
129     virtual IMFAdapterEnterKeyType GetEnterKeyType() = 0;
130 };
131 
132 class IMFTextListenerAdapter {
133 public:
134     IMFTextListenerAdapter() = default;
135 
136     virtual ~IMFTextListenerAdapter() = default;
137 
138     virtual void InsertText(const std::u16string& text) = 0;
139 
140     virtual void DeleteForward(int32_t length) = 0;
141 
142     virtual void DeleteBackward(int32_t length) = 0;
143 
144     virtual void SendKeyEventFromInputMethod() = 0;
145 
146     virtual void SendKeyboardStatus(const IMFAdapterKeyboardStatus& keyboardStatus) = 0;
147 
148     virtual void SendFunctionKey(std::shared_ptr<IMFAdapterFunctionKeyAdapter> functionKey) = 0;
149 
150     virtual void SetKeyboardStatus(bool status) = 0;
151 
152     virtual void MoveCursor(const IMFAdapterDirection direction) = 0;
153 
154     virtual void HandleSetSelection(int32_t start, int32_t end) = 0;
155 
156     virtual void HandleExtendAction(int32_t action) = 0;
157 
158     virtual void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) = 0;
159 
160     virtual int32_t GetTextIndexAtCursor() = 0;
161 
162     virtual std::u16string GetLeftTextOfCursor(int32_t number) = 0;
163 
164     virtual std::u16string GetRightTextOfCursor(int32_t number) = 0;
165 
166     virtual int32_t SetPreviewText(const std::u16string& text, int32_t start, int32_t end) = 0;
167 
168     virtual void FinishTextPreview() = 0;
169 
170     virtual void SetNeedUnderLine(bool isNeedUnderline) = 0;
171 
AutoFillWithIMFEvent(bool isUsername,bool isOtherAccount,bool isNewPassword,const std::string & content)172     virtual void AutoFillWithIMFEvent(bool isUsername, bool isOtherAccount, bool isNewPassword,
173         const std::string& content) {}
174 
KeyboardUpperRightCornerHide()175     virtual void KeyboardUpperRightCornerHide() {};
176 };
177 
178 class IMFAdapter {
179 public:
180     IMFAdapter() = default;
181 
182     virtual ~IMFAdapter() = default;
183 
184     virtual bool Attach(std::shared_ptr<IMFTextListenerAdapter> listener, bool isShowKeyboard) = 0;
185 
186     virtual bool Attach(std::shared_ptr<IMFTextListenerAdapter> listener, bool isShowKeyboard,
187         const std::shared_ptr<IMFTextConfigAdapter> config, bool isResetListener) = 0;
188 
189     virtual void ShowCurrentInput(const IMFAdapterTextInputType& inputType) = 0;
190 
191     virtual void HideTextInput() = 0;
192 
193     virtual void Close() = 0;
194 
195     virtual void OnCursorUpdate(const std::shared_ptr<IMFCursorInfoAdapter> cursorInfo) = 0;
196 
197     virtual void OnSelectionChange(std::u16string text, int start, int end) = 0;
198 
SendPrivateCommand(const std::string & commandKey,const std::string & commandValue)199     virtual bool SendPrivateCommand(const std::string& commandKey, const std::string& commandValue)
200     {
201         return false;
202     }
203 };
204 
205 } // namespace OHOS::NWeb
206 
207 #endif // IMF_ADAPTER_H
208