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 Executes the long press event action 95 * 96 * @param [in] event The long press event, contain press position. 97 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 98 */ 99 bool OnLongPressEvent(const LongPressEvent& event) override; 100 101 /** 102 * @brief Sets the view style. 103 * 104 * @param style Indicates the view style. 105 */ 106 void SetStyle(Style& style) override; 107 108 /** 109 * @brief Sets a style. 110 * 111 * @param key Indicates the key of the style to set. 112 * @param value Indicates the value matching the key. 113 */ 114 void SetStyle(uint8_t key, int64_t value) override; 115 116 /** 117 * @brief Checks whether this view needs to be covered before drawing it. 118 * 119 * @param invalidatedArea Indicates the area to draw. 120 * @return Returns <b>true</b> if this view needs to be covered; returns <b> false</b> otherwise. 121 */ OnPreDraw(Rect & invalidatedArea)122 bool OnPreDraw(Rect& invalidatedArea) const override 123 { 124 return false; 125 } 126 127 bool OnDragEvent(const DragEvent& event) override; 128 129 /** 130 * @brief Draws this view. 131 * 132 * @param invalidatedArea Indicates the area to draw. 133 */ 134 void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override; 135 136 void Focus() override; 137 138 void Blur() override; 139 140 /** 141 * @brief Sets the text content. 142 * 143 * @param text Indicates the pointer to the text content. 144 */ 145 void SetText(const char* text); 146 147 /** 148 * @brief Obtains the input text of this view. 149 * 150 * @return Returns the text. 151 */ 152 const char* GetText(); 153 154 /** 155 * @brief Sets the placeholder content. 156 * 157 * @param placeholder Indicates the pointer to the placeholder content. 158 */ 159 void SetPlaceholder(const char* placeholder); 160 161 /** 162 * @brief Sets the vaule listener for this view. 163 * The listener is triggered to invoke the callback function when the value changed. 164 * 165 * @param listener Indicates the listener to set. For details, see {@link OnChangeListener}. 166 */ 167 const char* GetPlaceholder(); 168 169 /** 170 * @brief Set max length of the input text. 171 * 172 * @param maxLength max length size. 173 */ 174 void SetMaxLength(uint16_t maxLength); 175 176 /** 177 * @brief Get max length of the input text. 178 * 179 * @return return the length size. 180 */ 181 uint16_t GetMaxLength(); 182 183 /** 184 * @brief Set the input type. 185 * 186 * @param type the input type, such as text or password. 187 */ 188 void SetInputType(InputType type); 189 190 /** 191 * @brief Get the input type. 192 * 193 * @param type the input type, such as text or password. 194 */ GetInputType()195 InputType GetInputType() 196 { 197 return inputType_; 198 } 199 200 /** 201 * @brief Sets the color for this text. 202 * 203 * @param color Indicates the text color to set. 204 */ SetTextColor(ColorType color)205 void SetTextColor(ColorType color) 206 { 207 useTextColor_ = true; 208 textColor_ = color; 209 } 210 211 /** 212 * @brief Obtains the color of this text. 213 * 214 * @return Returns the text color. 215 */ GetTextColor()216 ColorType GetTextColor() const 217 { 218 return useTextColor_ ? textColor_ : GetStyleConst().textColor_; 219 } 220 221 /** 222 * @brief Sets the color of the palceholder for this text. 223 * 224 * @param color Indicates the palceholder color to set. 225 */ SetPlaceholderColor(ColorType color)226 void SetPlaceholderColor(ColorType color) 227 { 228 placeholderColor_ = color; 229 } 230 231 /** 232 * @brief Obtains the palceholder color of this text. 233 * 234 * @return Returns the palceholder color. 235 */ GetPlaceholderColor()236 ColorType GetPlaceholderColor() const 237 { 238 return placeholderColor_; 239 } 240 241 /** 242 * @brief Sets the cursor color. 243 * 244 * @param color Indicates the cursor color to set. 245 */ SetCursorColor(ColorType color)246 void SetCursorColor(ColorType color) 247 { 248 cursorColor_ = color; 249 } 250 251 /** 252 * @brief Obtains the cursor color. 253 * 254 * @return Returns the cursor color. 255 */ GetCursorColor()256 ColorType GetCursorColor() const 257 { 258 return cursorColor_; 259 } 260 261 /** 262 * @brief Sets the font ID for this view. 263 * 264 * @param fontId Indicates the font ID composed of font name and size. 265 */ 266 void SetFontId(uint16_t fontId); 267 268 /** 269 * @brief Obtains the font ID composed of font name and size. 270 * 271 * @return Returns the front ID of this view. 272 */ GetFontId()273 uint8_t GetFontId() 274 { 275 InitText(); 276 return inputText_->GetFontId(); 277 } 278 279 /** 280 * @brief Sets the font for this viewview. 281 * 282 * @param name Indicates the pointer to the font name. 283 * @param size Indicates the font size to set. 284 */ 285 void SetFont(const char* name, uint8_t size); 286 287 /** 288 * @brief Obtains the width of this text. 289 * 290 * @return Returns the text width. 291 */ 292 uint16_t GetTextWidth(); 293 294 /** 295 * @brief Obtains the height of this text. 296 * 297 * @return Returns the text height. 298 */ 299 uint16_t GetTextHeight(); 300 301 void ReMeasure() override; 302 303 /** 304 * @brief Insert the text passed from the input method. 305 * 306 * @param text the text input by the user passed form input method. 307 */ 308 virtual void InsertText(std::string text); 309 310 /** 311 * @brief Delete the input text from backward. 312 * 313 * @param length the length of charactor to delete. 314 */ 315 virtual void DeleteBackward(uint32_t length); 316 317 /** 318 * @brief Sets the vaule listener for this view. 319 * The listener is triggered to invoke the callback function when the value changed. 320 * 321 * @param listener Indicates the listener to set. For details, see {@link OnChangeListener}. 322 */ SetOnChangeListener(OnChangeListener * onChangeListener)323 void SetOnChangeListener(OnChangeListener* onChangeListener) 324 { 325 onChangeListener_ = onChangeListener; 326 } 327 328 /** 329 * @brief Obtains the vaule change event listener for the view. 330 * 331 * @return Returns the vaule change event listener. 332 */ GetOnChangeListener()333 OnChangeListener*& GetOnChangeListener() 334 { 335 return onChangeListener_; 336 } 337 338 /** 339 * @brief set the cursor index. 340 */ 341 void SetCursorIndex(uint16_t cursorIndex); 342 343 /** 344 * @brief Is it focused or not. 345 */ GetIsFocus()346 bool GetIsFocus() 347 { 348 return isFocused_; 349 } 350 351 uint16_t GetCursorIndex(); 352 353 protected: 354 virtual void InitText(); 355 virtual void UpdateExtraOffsetX(const uint16_t firstVisibleIndex, 356 const uint16_t lastVisibleIndex); 357 virtual uint16_t GetFirstVisibleIndex(); 358 virtual uint16_t GetLastVisibleIndex(); 359 virtual uint16_t GetTextLength(); 360 virtual void UpdateInsertDeletedOffset(); 361 virtual void UpdateOffsetX(); 362 virtual uint16_t GetTextWidthByCursorIndex(const uint16_t cursorIndex); 363 364 void SetText(std::string text); 365 void InsertTextByCursorIndex(std::string text); 366 void UpdateOffsetBySetCursorIndex(); 367 void UpdateOffsetByInputType(); 368 369 Text* inputText_; 370 Text* placeholderText_; 371 int16_t offsetX_; 372 uint16_t cursorIndex_; 373 uint16_t deleteTextWidth_; 374 uint16_t insertTextWidth_; 375 376 enum UpdateOffsetState : uint8_t { 377 UPDATE_OFFSET_UNKNOW = 0, 378 UPDATE_OFFSET_INTERFACE, 379 UPDATE_OFFSET_PRESS_EVENT, 380 UPDATE_OFFSET_DELETE, 381 UPDATE_OFFSET_INSERT, 382 UPDATE_OFFSET_SET_CURSOR, 383 UPDATE_OFFSET_INPUT_TYPE 384 }; 385 386 UpdateOffsetState offsetState_; 387 private: 388 friend class CursorAnimator; 389 390 void RemeasureForMarquee(int16_t textWidth); 391 void UpdateInnerText(); 392 void CheckValueChange(std::string text); 393 void RefreshText(); 394 void UpdateTextString(std::string text); 395 void CalculatedCursorPos(bool drawPlaceholder); 396 void DealPressEvents(bool longPressEvent, const Event &event); 397 std::string GetInnerText(); 398 std::string GetInnerPassword(); 399 void DrawCursor(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, bool drawPlaceholder); 400 401 bool needRefresh_; 402 bool useTextColor_; 403 bool isFocused_; 404 bool drawCursor_; 405 uint16_t maxLength_; 406 uint16_t placeholderEllipsisIndex_; 407 int16_t cursorPosX_; 408 ColorType textColor_; 409 ColorType placeholderColor_; 410 ColorType cursorColor_; 411 OnChangeListener* onChangeListener_; 412 std::string textStr_; 413 std::string passwordStr_; 414 Animator* cursorAnimator_; 415 InputType inputType_ = InputType::TEXT_TYPE; 416 }; 417 } // namespace OHOS 418 #endif // GRAPHIC_LITE_UI_EDIT_TEXT 419