• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_INPUT_CLIENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_INPUT_CLIENT_H
18 
19 #include <cstdint>
20 
21 #include "base/memory/ace_type.h"
22 #include "base/utils/string_utils.h"
23 #include "core/common/ime/text_editing_value.h"
24 #include "core/common/ime/text_input_action.h"
25 #include "core/event/key_event.h"
26 #include "core/components_ng/render/paragraph.h"
27 #include "core/components_ng/pattern/text_field/text_field_model.h"
28 
29 namespace OHOS::Ace {
30 
31 constexpr uint32_t KEY_NULL = 0;
32 constexpr uint32_t KEY_ALT = 1 << 0;
33 constexpr uint32_t KEY_SHIFT = 1 << 1;
34 constexpr uint32_t KEY_CTRL = 1 << 2;
35 constexpr uint32_t KEY_META = 1 << 3;
36 
37 constexpr int32_t INVALID_VALUE = -1;
38 const std::string PRIVATE_DATA_KEY = "previewTextStyle";
39 
40 enum class CaretMoveIntent {
41     Left,
42     Right,
43     Up,
44     Down,
45     LeftWord,
46     RightWord,
47     ParagraghBegin,
48     ParagraghEnd,
49     LineBegin,
50     LineEnd,
51     Home,
52     End,
53 };
54 
55 struct PreviewRange {
56     int32_t start = INVALID_VALUE;
57     int32_t end = INVALID_VALUE;
58     bool operator==(const PreviewRange &range) const
59     {
60         return start == range.start && end == range.end;
61     }
SetPreviewRange62     void Set(int32_t startValue, int32_t endValue)
63     {
64         start = startValue;
65         end = endValue;
66     }
67 };
68 
69 struct KeyComb final {
70     KeyCode code;
71     int32_t modKeyFlags;
72 
codefinal73     KeyComb(KeyCode code, int32_t modKeyFlags = KEY_NULL) : code(code), modKeyFlags(modKeyFlags) {};
74 
75     bool operator<(const KeyComb& other) const
76     {
77         return code == other.code ? modKeyFlags < other.modKeyFlags : code < other.code;
78     }
79 
80     bool operator==(const KeyComb& other) const
81     {
82         return code == other.code && modKeyFlags == other.modKeyFlags;
83     }
84 };
85 
86 class TextInputClient : public virtual AceType {
87     DECLARE_ACE_TYPE(TextInputClient, AceType);
88 
89 public:
90     // Requests that this client update its editing state to the given value.
91     virtual void UpdateEditingValue(
92         const std::shared_ptr<TextEditingValue>& value, bool needFireChangeEvent = true) = 0;
93 
94     // Requests that this client perform the given action.
95     virtual void PerformAction(TextInputAction action, bool forceCloseKeyboard = false) = 0;
96 
97     virtual void InsertValue(const std::string& insertValue, bool isIME = false) {};
DeleteBackward(int32_t length)98     virtual void DeleteBackward(int32_t length) {};
DeleteForward(int32_t length)99     virtual void DeleteForward(int32_t length) {};
SetInputMethodStatus(bool keyboardShown)100     virtual void SetInputMethodStatus(bool keyboardShown) {}
NotifyKeyboardClosedByUser()101     virtual void NotifyKeyboardClosedByUser() {}
NotifyKeyboardClosed()102     virtual void NotifyKeyboardClosed() {}
103     virtual void NotifyKeyboardHeight(uint32_t height);
GetLeftTextOfCursor(int32_t number)104     virtual std::u16string GetLeftTextOfCursor(int32_t number)
105     {
106         return StringUtils::DEFAULT_USTRING;
107     }
108 
GetRightTextOfCursor(int32_t number)109     virtual std::u16string GetRightTextOfCursor(int32_t number)
110     {
111         return StringUtils::DEFAULT_USTRING;
112     }
GetTextIndexAtCursor()113     virtual int32_t GetTextIndexAtCursor()
114     {
115         return -1;
116     }
117 
118     virtual void HandleSetSelection(int32_t start, int32_t end, bool showHandle = true) {}
HandleExtendAction(int32_t action)119     virtual void HandleExtendAction(int32_t action) {}
120 
121 #if defined(IOS_PLATFORM)
GetInputEditingValue()122     virtual const TextEditingValue& GetInputEditingValue() const
123     {
124         static TextEditingValue value;
125         return value;
126     };
127 #endif
UpdateInputFilterErrorText(const std::string & errorText)128     virtual void UpdateInputFilterErrorText(const std::string& errorText) {};
ResetTouchAtLeftOffsetFlag()129     virtual void ResetTouchAtLeftOffsetFlag() {}
130 
131     // Requests that this client Y point.
GetEditingBoxY()132     virtual double GetEditingBoxY() const
133     {
134         return 0.0;
135     };
GetEditingBoxTopY()136     virtual double GetEditingBoxTopY() const
137     {
138         return 0.0;
139     };
GetEditingBoxModel()140     virtual bool GetEditingBoxModel() const
141     {
142         return false;
143     };
144 
GetInstanceId()145     virtual int32_t GetInstanceId() const
146     {
147         return instanceId_;
148     }
149 
SetInstanceId(int32_t instanceId)150     void SetInstanceId(int32_t instanceId)
151     {
152         instanceId_ = instanceId;
153     }
154 
155     bool HandleKeyEvent(const KeyEvent& keyEvent);
UpdateShiftFlag(const KeyEvent & keyEvent)156     virtual void UpdateShiftFlag(const KeyEvent& keyEvent) {}
157 
HandleOnEscape()158     virtual bool HandleOnEscape()
159     {
160         return false;
161     }
162 
HandleOnTab(bool backward)163     virtual bool HandleOnTab(bool backward)
164     {
165         return false;
166     }
167 
CursorMove(CaretMoveIntent direction)168     virtual void CursorMove(CaretMoveIntent direction) {}
169 
HandleSelect(CaretMoveIntent direction)170     virtual void HandleSelect(CaretMoveIntent direction) {}
171 
HandleSelectFontStyle(KeyCode code)172     virtual void HandleSelectFontStyle(KeyCode code) {}
173 
HandleOnSelectAll()174     virtual void HandleOnSelectAll() {}
175 
HandleOnEnter()176     virtual void HandleOnEnter() {}
177 
HandleOnShowMenu()178     virtual void HandleOnShowMenu() {}
179 
180     virtual void HandleOnCopy(bool isUsingExternalKeyboard = false) {}
181 
HandleOnCut()182     virtual void HandleOnCut() {}
183 
HandleOnPaste()184     virtual void HandleOnPaste() {}
185 
HandleOnUndoAction()186     virtual void HandleOnUndoAction() {}
187 
HandleOnRedoAction()188     virtual void HandleOnRedoAction() {}
189 
HandleOnDelete(bool backward)190     virtual void HandleOnDelete(bool backward) {}
191 
HandleOnDeleteComb(bool backward)192     virtual bool HandleOnDeleteComb(bool backward)
193     {
194         return false;
195     }
196 
SetPreviewText(const std::string & previewValue,const PreviewRange range)197     virtual int32_t SetPreviewText(const std::string& previewValue, const PreviewRange range)
198     {
199         return 0;
200     }
201 
FinishTextPreview()202     virtual void FinishTextPreview() {}
ReceivePreviewTextStyle(const std::string & style)203     virtual void ReceivePreviewTextStyle (const std::string& style) {}
204 
CheckPreviewTextValidate(const std::string & previewValue,const PreviewRange range)205     virtual int32_t CheckPreviewTextValidate(const std::string& previewValue, const PreviewRange range)
206     {
207         return 0;
208     }
209 
210     static std::map<KeyComb, std::function<bool(TextInputClient*)>> functionKeys_;
211 
212     static std::map<KeyComb, std::function<void(TextInputClient*)>> keyboardShortCuts_;
213 
SetCaretOffset(int32_t caretPosition)214     virtual bool SetCaretOffset(int32_t caretPosition)
215     {
216         return false;
217     }
218 
219     virtual void SetSelection(int32_t start, int32_t end,
220         const std::optional<SelectionOptions>& options = std::nullopt, bool isForward = false) {}
221 
InsertOrDeleteSpace(int32_t index)222     virtual bool InsertOrDeleteSpace(int32_t index)
223     {
224         return false;
225     }
226     virtual void DeleteRange(int32_t start, int32_t end, bool isIME = true) {}
HandleOnPageUp()227     virtual void HandleOnPageUp() {};
HandleOnPageDown()228     virtual void HandleOnPageDown() {};
ResetOriginCaretPosition()229     virtual void ResetOriginCaretPosition() {};
RecordOriginCaretPosition()230     virtual bool RecordOriginCaretPosition() { return false; };
231 protected:
232     int32_t instanceId_ = -1;
233     bool shiftFlag_ = false;
234 };
235 
236 } // namespace OHOS::Ace
237 
238 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_INPUT_CLIENT_H
239