• 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 
27 namespace OHOS::Ace {
28 
29 constexpr uint32_t KEY_NULL = 0;
30 constexpr uint32_t KEY_ALT = 1 << 0;
31 constexpr uint32_t KEY_SHIFT = 1 << 1;
32 constexpr uint32_t KEY_CTRL = 1 << 2;
33 constexpr uint32_t KEY_META = 1 << 3;
34 
35 constexpr int32_t INVALID_VALUE = -1;
36 const std::string PRIVATE_DATA_KEY = "previewTextStyle";
37 
38 enum class CaretMoveIntent {
39     Left,
40     Right,
41     Up,
42     Down,
43     LeftWord,
44     RightWord,
45     ParagraghBegin,
46     ParagraghEnd,
47     LineBegin,
48     LineEnd,
49     Home,
50     End,
51 };
52 
53 struct PreviewRange {
54     int32_t start = INVALID_VALUE;
55     int32_t end = INVALID_VALUE;
56     bool operator==(const PreviewRange &range) const
57     {
58         return start == range.start && end == range.end;
59     }
SetPreviewRange60     void Set(int32_t startValue, int32_t endValue)
61     {
62         start = startValue;
63         end = endValue;
64     }
65 };
66 
67 struct KeyComb final {
68     KeyCode code;
69     int32_t modKeyFlags;
70 
codefinal71     KeyComb(KeyCode code, int32_t modKeyFlags = KEY_NULL) : code(code), modKeyFlags(modKeyFlags) {};
72 
73     bool operator<(const KeyComb& other) const
74     {
75         return code == other.code ? modKeyFlags < other.modKeyFlags : code < other.code;
76     }
77 };
78 
79 class TextInputClient : public virtual AceType {
80     DECLARE_ACE_TYPE(TextInputClient, AceType);
81 
82 public:
83     // Requests that this client update its editing state to the given value.
84     virtual void UpdateEditingValue(
85         const std::shared_ptr<TextEditingValue>& value, bool needFireChangeEvent = true) = 0;
86 
87     // Requests that this client perform the given action.
88     virtual void PerformAction(TextInputAction action, bool forceCloseKeyboard = false) = 0;
89 
90     virtual void InsertValue(const std::string& insertValue, bool isIME = false) {};
DeleteBackward(int32_t length)91     virtual void DeleteBackward(int32_t length) {};
DeleteForward(int32_t length)92     virtual void DeleteForward(int32_t length) {};
SetInputMethodStatus(bool keyboardShown)93     virtual void SetInputMethodStatus(bool keyboardShown) {}
NotifyKeyboardClosedByUser()94     virtual void NotifyKeyboardClosedByUser() {}
NotifyKeyboardClosed()95     virtual void NotifyKeyboardClosed() {}
96     virtual void NotifyKeyboardHeight(uint32_t height);
GetLeftTextOfCursor(int32_t number)97     virtual std::u16string GetLeftTextOfCursor(int32_t number)
98     {
99         return StringUtils::DEFAULT_USTRING;
100     }
101 
GetRightTextOfCursor(int32_t number)102     virtual std::u16string GetRightTextOfCursor(int32_t number)
103     {
104         return StringUtils::DEFAULT_USTRING;
105     }
GetTextIndexAtCursor()106     virtual int32_t GetTextIndexAtCursor()
107     {
108         return -1;
109     }
110 
111     virtual void HandleSetSelection(int32_t start, int32_t end, bool showHandle = true) {}
HandleExtendAction(int32_t action)112     virtual void HandleExtendAction(int32_t action) {}
113 
114 #if defined(IOS_PLATFORM)
GetInputEditingValue()115     virtual const TextEditingValue& GetInputEditingValue() const
116     {
117         static TextEditingValue value;
118         return value;
119     };
120 #endif
ResetTouchAtLeftOffsetFlag()121     virtual void ResetTouchAtLeftOffsetFlag() {}
122 
123     // Requests that this client Y point.
GetEditingBoxY()124     virtual double GetEditingBoxY() const
125     {
126         return 0.0;
127     };
GetEditingBoxTopY()128     virtual double GetEditingBoxTopY() const
129     {
130         return 0.0;
131     };
GetEditingBoxModel()132     virtual bool GetEditingBoxModel() const
133     {
134         return false;
135     };
136 
GetInstanceId()137     virtual int32_t GetInstanceId() const
138     {
139         return instanceId_;
140     }
141 
SetInstanceId(int32_t instanceId)142     void SetInstanceId(int32_t instanceId)
143     {
144         instanceId_ = instanceId;
145     }
146 
147     bool HandleKeyEvent(const KeyEvent& keyEvent);
148 
HandleOnEscape()149     virtual bool HandleOnEscape()
150     {
151         return false;
152     }
153 
HandleOnTab(bool backward)154     virtual bool HandleOnTab(bool backward)
155     {
156         return false;
157     }
158 
CursorMove(CaretMoveIntent direction)159     virtual void CursorMove(CaretMoveIntent direction) {}
160 
HandleSelect(CaretMoveIntent direction)161     virtual void HandleSelect(CaretMoveIntent direction) {}
162 
HandleSelectFontStyle(KeyCode code)163     virtual void HandleSelectFontStyle(KeyCode code) {}
164 
HandleOnSelectAll()165     virtual void HandleOnSelectAll() {}
166 
HandleOnEnter()167     virtual void HandleOnEnter() {}
168 
HandleOnShowMenu()169     virtual void HandleOnShowMenu() {}
170 
171     virtual void HandleOnCopy(bool isUsingExternalKeyboard = false) {}
172 
HandleOnCut()173     virtual void HandleOnCut() {}
174 
HandleOnPaste()175     virtual void HandleOnPaste() {}
176 
HandleOnUndoAction()177     virtual void HandleOnUndoAction() {}
178 
HandleOnRedoAction()179     virtual void HandleOnRedoAction() {}
180 
HandleOnDelete(bool backward)181     virtual void HandleOnDelete(bool backward) {}
182 
HandleOnDeleteComb(bool backward)183     virtual bool HandleOnDeleteComb(bool backward)
184     {
185         return false;
186     }
187 
SetPreviewText(const std::string & previewValue,const PreviewRange range)188     virtual int32_t SetPreviewText(const std::string& previewValue, const PreviewRange range)
189     {
190         return 0;
191     }
192 
FinishTextPreview()193     virtual void FinishTextPreview() {}
ReceivePreviewTextStyle(const std::string & style)194     virtual void ReceivePreviewTextStyle (const std::string& style) {}
195 
CheckPreviewTextValidate(const std::string & previewValue,const PreviewRange range)196     virtual int32_t CheckPreviewTextValidate(const std::string& previewValue, const PreviewRange range)
197     {
198         return 0;
199     }
200 
201     static std::map<KeyComb, std::function<bool(TextInputClient*)>> functionKeys_;
202 
203     static std::map<KeyComb, std::function<void(TextInputClient*)>> keyboardShortCuts_;
204 
205 protected:
206     int32_t instanceId_ = -1;
207 };
208 
209 } // namespace OHOS::Ace
210 
211 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_INPUT_CLIENT_H
212