• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 /**
17  * @addtogroup UI_Components
18  * @{
19  *
20  * @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
21  *
22  */
23 
24 /**
25  * @file ui_edit_text.h
26  *
27  * @brief Declares a <b>UIEditText</b> class that represents a input edit view.
28  *
29  */
30 
31 #ifndef GRAPHIC_LITE_UI_EDIT_TEXT
32 #define GRAPHIC_LITE_UI_EDIT_TEXT
33 
34 #include "animator/animator.h"
35 #include "common/text.h"
36 #include "components/ui_view.h"
37 
38 namespace OHOS {
39 /**
40  * @brief Defines the functions for presenting a edit text in a specified area, setting the style and background color,
41  *        and setting the display mode such as text and password type.
42  */
43 class UIEditText : public UIView {
44 public:
45     /**
46      * @brief Defines a value change event listener. You need to register this listener with the view to listen to
47      *        value change events.
48      */
49     class OnChangeListener : public HeapBase {
50     public:
51         /**
52          * @brief Called when edit text value changed.
53          *
54          * @param view Indicates the UIEditView.
55          * @param value Indicates the changed value.
56          */
OnChange(UIView & view,const char * value)57         virtual void OnChange(UIView& view, const char* value) {}
58 
59         /**
60          * @brief A destructor used to delete the <b>OnChangeListener</b> instance.
61          */
~OnChangeListener()62         virtual ~OnChangeListener() {}
63     };
64 
65     /**
66      * @brief A constructor used to create a <b>UIEditText</b> instance.
67      */
68     UIEditText();
69 
70     /**
71      * @brief A destructor used to delete the <b>UIEditText</b> instance.
72      */
73     virtual ~UIEditText();
74 
75     /**
76      * @brief Obtains the view type.
77      *
78      * @return Returns <b>UI_EDIT_TEXT</b>, as defined in {@link UIViewType}.
79      */
GetViewType()80     UIViewType GetViewType() const override
81     {
82         return UI_EDIT_TEXT;
83     }
84 
85     /**
86      * @brief   Executes the press event action
87      *
88      * @param   [in] event   The press event, contain press position.
89      * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise.
90      */
91     bool OnPressEvent(const PressEvent& event) override;
92 
93     /**
94      * @brief Sets the view style.
95      *
96      * @param style Indicates the view style.
97      */
98     void SetStyle(Style& style) override;
99 
100     /**
101      * @brief Sets a style.
102      *
103      * @param key Indicates the key of the style to set.
104      * @param value Indicates the value matching the key.
105      */
106     void SetStyle(uint8_t key, int64_t value) override;
107 
108     /**
109      * @brief Checks whether this view needs to be covered before drawing it.
110      *
111      * @param invalidatedArea Indicates the area to draw.
112      * @return Returns <b>true</b> if this view needs to be covered; returns <b> false</b> otherwise.
113      */
OnPreDraw(Rect & invalidatedArea)114     bool OnPreDraw(Rect& invalidatedArea) const override
115     {
116         return false;
117     }
118 
119     /**
120      * @brief Draws this view.
121      *
122      * @param invalidatedArea Indicates the area to draw.
123      */
124     void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
125 
126     void Focus() override;
127 
128     void Blur() override;
129 
130     /**
131      * @brief Sets the text content.
132      *
133      * @param text Indicates the pointer to the text content.
134      */
135     void SetText(const char* text);
136 
137     /**
138      * @brief Obtains the input text of this view.
139      *
140      * @return Returns the text.
141      */
142     const char* GetText();
143 
144     /**
145      * @brief Sets the placeholder content.
146      *
147      * @param placeholder Indicates the pointer to the placeholder content.
148      */
149     void SetPlaceholder(const char* placeholder);
150 
151     /**
152      * @brief Sets the vaule listener for this view.
153      *        The listener is triggered to invoke the callback function when the value changed.
154      *
155      * @param listener Indicates the listener to set. For details, see {@link OnChangeListener}.
156      */
157     const char* GetPlaceholder();
158 
159     /**
160      * @brief Set max length of the input text.
161      *
162      * @param maxLength max length size.
163      */
164     void SetMaxLength(uint16_t maxLength);
165 
166     /**
167      * @brief Get max length of the input text.
168      *
169      * @return return the length size.
170      */
171     uint16_t GetMaxLength();
172 
173     /**
174      * @brief Set the input type.
175      *
176      * @param type the input type, such as text or password.
177      */
178     void SetInputType(InputType type);
179 
180     /**
181      * @brief Get the input type.
182      *
183      * @param type the input type, such as text or password.
184      */
GetInputType()185     InputType GetInputType()
186     {
187         return inputType_;
188     }
189 
190     /**
191      * @brief Sets the color for this text.
192      *
193      * @param color Indicates the text color to set.
194      */
SetTextColor(ColorType color)195     void SetTextColor(ColorType color)
196     {
197         useTextColor_ = true;
198         textColor_ = color;
199     }
200 
201     /**
202      * @brief Obtains the color of this text.
203      *
204      * @return Returns the text color.
205      */
GetTextColor()206     ColorType GetTextColor() const
207     {
208         return useTextColor_ ? textColor_ : GetStyleConst().textColor_;
209     }
210 
211     /**
212      * @brief Sets the color of the palceholder for this text.
213      *
214      * @param color Indicates the palceholder color to set.
215      */
SetPlaceholderColor(ColorType color)216     void SetPlaceholderColor(ColorType color)
217     {
218         placeholderColor_ = color;
219     }
220 
221     /**
222      * @brief Obtains the palceholder color of this text.
223      *
224      * @return Returns the palceholder color.
225      */
GetPlaceholderColor()226     ColorType GetPlaceholderColor() const
227     {
228         return placeholderColor_;
229     }
230 
231     /**
232      * @brief Sets the cursor color.
233      *
234      * @param color Indicates the cursor color to set.
235      */
SetCursorColor(ColorType color)236     void SetCursorColor(ColorType color)
237     {
238         cursorColor_ = color;
239     }
240 
241     /**
242      * @brief Obtains the cursor color.
243      *
244      * @return Returns the cursor color.
245      */
GetCursorColor()246     ColorType GetCursorColor() const
247     {
248         return cursorColor_;
249     }
250 
251     /**
252      * @brief Sets the font ID for this view.
253      *
254      * @param fontId Indicates the font ID composed of font name and size.
255      */
256     void SetFontId(uint16_t fontId);
257 
258     /**
259      * @brief Obtains the font ID composed of font name and size.
260      *
261      * @return Returns the front ID of this view.
262      */
GetFontId()263     uint8_t GetFontId()
264     {
265         InitText();
266         return inputText_->GetFontId();
267     }
268 
269     /**
270      * @brief Sets the font for this viewview.
271      *
272      * @param name Indicates the pointer to the font name.
273      * @param size Indicates the font size to set.
274      */
275     void SetFont(const char* name, uint8_t size);
276 
277     /**
278      * @brief Obtains the width of this text.
279      *
280      * @return Returns the text width.
281      */
282     uint16_t GetTextWidth();
283 
284     /**
285      * @brief Obtains the height of this text.
286      *
287      * @return Returns the text height.
288      */
289     uint16_t GetTextHeight();
290 
291     void ReMeasure() override;
292 
293     /**
294      * @brief Insert the text passed from the input method.
295      *
296      * @param text the text input by the user passed form input method.
297      */
298     void InsertText(std::string text);
299 
300     /**
301      * @brief Delete the input text from backward.
302      *
303      * @param length the length of charactor to delete.
304      */
305     void DeleteBackward(uint32_t length);
306 
307     /**
308      * @brief Sets the vaule listener for this view.
309      *        The listener is triggered to invoke the callback function when the value changed.
310      *
311      * @param listener Indicates the listener to set. For details, see {@link OnChangeListener}.
312      */
SetOnChangeListener(OnChangeListener * onChangeListener)313     void SetOnChangeListener(OnChangeListener* onChangeListener)
314     {
315         onChangeListener_ = onChangeListener;
316     }
317 
318     /**
319      * @brief Obtains the vaule change event listener for the view.
320      *
321      * @return Returns the vaule change event listener.
322      */
GetOnChangeListener()323     OnChangeListener*& GetOnChangeListener()
324     {
325         return onChangeListener_;
326     }
327 
328 protected:
329     Text* inputText_;
330     Text* placeholderText_;
331     void RefreshText();
332     virtual void InitText();
333 
334 private:
335     friend class CursorAnimator;
336 
337     void RemeasureForMarquee(int16_t textWidth);
338     void UpdateInnerText();
339     void CheckValueChange(std::string text);
340     void SetText(std::string text);
341     void UpdateTextString(std::string text);
342     std::string GetInnerText();
343     std::string GetInnerPassword();
344     void UpdateOffsetX();
345     void DrawCursor(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, bool drawPlaceholder);
346 
347     bool needRefresh_;
348     bool useTextColor_;
349     bool isFocused_;
350     bool drawCursor_;
351     uint16_t maxLength_;
352     uint16_t placeholderEllipsisIndex_;
353     int16_t offsetX_;
354     ColorType textColor_;
355     ColorType placeholderColor_;
356     ColorType cursorColor_;
357     OnChangeListener* onChangeListener_;
358     std::string textStr_;
359     std::string passwordStr_;
360     Animator* cursorAnimator_;
361     InputType inputType_ = InputType::TEXT_TYPE;
362 };
363 } // namespace OHOS
364 #endif // GRAPHIC_LITE_UI_EDIT_TEXT
365