1 /* 2 * Copyright (c) 2020-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 /** 17 * @addtogroup UI_Components 18 * @{ 19 * 20 * @brief Defines UI components such as buttons, texts, images, lists, and progress bars. 21 * 22 * @since 1.0 23 * @version 1.0 24 */ 25 26 /** 27 * @file ui_view.h 28 * 29 * @brief Declares the base class of a view, providing basic view attributes and operations. All views are derived 30 * from this class. 31 * 32 * @since 1.0 33 * @version 1.0 34 */ 35 36 #ifndef GRAPHIC_LITE_UI_VIEW_H 37 #define GRAPHIC_LITE_UI_VIEW_H 38 39 #include "events/cancel_event.h" 40 #include "events/click_event.h" 41 #include "events/drag_event.h" 42 #include "events/event.h" 43 #include "events/long_press_event.h" 44 #include "events/press_event.h" 45 #include "events/release_event.h" 46 #if ENABLE_ROTATE_INPUT 47 #include "events/rotate_event.h" 48 #endif 49 #include "gfx_utils/color.h" 50 #include "gfx_utils/geometry2d.h" 51 #include "gfx_utils/graphic_buffer.h" 52 #include "gfx_utils/graphic_log.h" 53 #include "gfx_utils/heap_base.h" 54 #include "gfx_utils/image_info.h" 55 #include "gfx_utils/style.h" 56 #include "gfx_utils/transform.h" 57 58 namespace OHOS { 59 /* Enumerates view types. */ 60 enum UIViewType : uint8_t { 61 UI_ROOT_VIEW = 0, 62 UI_VIEW_GROUP, 63 UI_LABEL, 64 UI_ARC_LABEL, 65 UI_LABEL_BUTTON, 66 UI_CHECK_BOX, 67 UI_TOGGLE_BUTTON, 68 UI_RADIO_BUTTON, 69 UI_IMAGE_VIEW, 70 UI_BOX_PROGRESS, 71 UI_SLIDER, 72 UI_CIRCLE_PROGRESS, 73 UI_SCROLL_VIEW, 74 UI_LIST, 75 UI_DIGITAL_CLOCK, 76 UI_ANALOG_CLOCK, 77 UI_PICKER, 78 UI_SWIPE_VIEW, 79 UI_TIME_PICKER, 80 UI_ABSTRACT_CLOCK, 81 UI_ABSTRACT_PROGRESS, 82 UI_ABSTRACT_SCROLL, 83 UI_AXIS, 84 UI_BUTTON, 85 UI_CANVAS, 86 UI_CHART, 87 UI_IMAGE_ANIMATOR_VIEW, 88 UI_REPEAT_BUTTON, 89 UI_TEXTURE_MAPPER, 90 UI_DIALOG, 91 UI_QRCODE, 92 UI_NUMBER_MAX 93 }; 94 95 #if ENABLE_DEBUG 96 const char* const VIEW_TYPE_STRING[UI_NUMBER_MAX] = { 97 "RootView", "UIViewGroup", "UILabel", 98 "UIArcLabel", "UILabelButton", "UICheckBox", 99 "UIToggleButton", "UIRadioButton", "UIImageView", 100 "UIBoxProgress", "UISlider", "UICircleProgress", 101 "UIScrollView", "UIList", "UIDigitalClock", 102 "UIAnalogClock", "UIPicker", "UISwipeView", 103 "UITimePicker", "UIAbstractClock", "UIAbstractProgress", 104 "UIAbstractScroll", "UIAxis", "UIButton", 105 "UICanvas", "UIChart", "UIImageAnimatorView", 106 "UIRepeatButton", "UITextureMapper", "UIDialog", 107 "UIQrcode", 108 }; 109 #endif // ENABLE_DEBUG 110 111 /** 112 * @brief Defines the base class of a view, providing basic view attributes and operations. All views are derived 113 * from this class. 114 * 115 * @since 1.0 116 * @version 1.0 117 */ 118 class UIView : public HeapBase { 119 public: 120 /** 121 * @brief Defines a click event listener. You need to register this listener with the view to listen to 122 * click events. 123 * 124 * @since 1.0 125 * @version 1.0 126 */ 127 class OnClickListener : public HeapBase { 128 public: 129 /** 130 * @brief Called when a view is clicked. 131 * @param view Indicates the view clicked. 132 * @param event Indicates the click event. 133 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 134 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 135 * it is transferred to the parent view after local processing is complete.) 136 * @since 1.0 137 * @version 1.0 138 */ OnClick(UIView & view,const ClickEvent & event)139 virtual bool OnClick(UIView& view, const ClickEvent& event) 140 { 141 return false; 142 } 143 144 /** 145 * @brief A destructor used to delete the <b>OnClickListener</b> instance. 146 * @since 1.0 147 * @version 1.0 148 */ ~OnClickListener()149 virtual ~OnClickListener() {} 150 }; 151 152 /** 153 * @brief Defines a long-press event listener. You need to register this listener with the view to listen to 154 * long-press events. 155 * 156 * @since 1.0 157 * @version 1.0 158 */ 159 class OnLongPressListener : public HeapBase { 160 public: 161 /** 162 * @brief Called when a view is long pressed. 163 * @param view Indicates the view long pressed. 164 * @param event Indicates the long-press event. 165 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 166 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 167 * it is transferred to the parent view after local processing is complete.) 168 * @since 1.0 169 * @version 1.0 170 */ OnLongPress(UIView & view,const LongPressEvent & event)171 virtual bool OnLongPress(UIView& view, const LongPressEvent& event) 172 { 173 return false; 174 } 175 176 /** 177 * @brief A destructor used to delete the <b>OnLongPressListener</b> instance. 178 * @since 1.0 179 * @version 1.0 180 */ ~OnLongPressListener()181 virtual ~OnLongPressListener() {} 182 }; 183 184 /** 185 * @brief Defines a drag event listener. You need to register this listener with the view to listen to drag events. 186 * 187 * @since 1.0 188 * @version 1.0 189 */ 190 class OnDragListener : public HeapBase { 191 public: 192 /** 193 * @brief Called when a view starts to drag. 194 * @param view Indicates the view dragged. 195 * @param event Indicates the drag event. 196 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 197 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 198 * it is transferred to the parent view after local processing is complete.) 199 * @since 1.0 200 * @version 1.0 201 */ OnDragStart(UIView & view,const DragEvent & event)202 virtual bool OnDragStart(UIView& view, const DragEvent& event) 203 { 204 return false; 205 } 206 207 /** 208 * @brief Called when a view is being dragged. 209 * @param view Indicates the view dragged. 210 * @param event Indicates the drag event. 211 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 212 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 213 * it is transferred to the parent view after local processing is complete.) 214 * @since 1.0 215 * @version 1.0 216 */ OnDrag(UIView & view,const DragEvent & event)217 virtual bool OnDrag(UIView& view, const DragEvent& event) 218 { 219 return false; 220 } 221 222 /** 223 * @brief Called when a view stops dragging. 224 * @param view Indicates the view dragged. 225 * @param event Indicates the drag event. 226 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 227 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 228 * it is transferred to the parent view after local processing is complete.) 229 * @since 1.0 230 * @version 1.0 231 */ OnDragEnd(UIView & view,const DragEvent & event)232 virtual bool OnDragEnd(UIView& view, const DragEvent& event) 233 { 234 return false; 235 } 236 237 /** 238 * @brief A destructor used to delete the <b>OnDragListener</b> instance. 239 * @since 1.0 240 * @version 1.0 241 */ ~OnDragListener()242 virtual ~OnDragListener() {} 243 }; 244 245 /** 246 * @brief Defines a touch event listener. You need to register this listener with the view to listen to touch 247 * events. 248 * 249 * @since 1.0 250 * @version 1.0 251 */ 252 class OnTouchListener : public HeapBase { 253 public: 254 /** 255 * @brief Called when a view is pressed. 256 * @param view Indicates the view pressed. 257 * @param event Indicates the press event. 258 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 259 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 260 * it is transferred to the parent view after local processing is complete.) 261 * @since 1.0 262 * @version 1.0 263 */ OnPress(UIView & view,const PressEvent & event)264 virtual bool OnPress(UIView& view, const PressEvent& event) 265 { 266 return false; 267 } 268 269 /** 270 * @brief Called when a view is released. 271 * @param view Indicates the view released. 272 * @param event Indicates the release event. 273 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 274 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 275 * it is transferred to the parent view after local processing is complete.) 276 * @since 1.0 277 * @version 1.0 278 */ OnRelease(UIView & view,const ReleaseEvent & event)279 virtual bool OnRelease(UIView& view, const ReleaseEvent& event) 280 { 281 return false; 282 } 283 284 /** 285 * @brief Called when a click event on a view is canceled. 286 * @param view Indicates the view on which a click event is canceled. 287 * @param event Indicates the cancel event. 288 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 289 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 290 * it is transferred to the parent view after local processing is complete.) 291 * @since 1.0 292 * @version 1.0 293 */ OnCancel(UIView & view,const CancelEvent & event)294 virtual bool OnCancel(UIView& view, const CancelEvent& event) 295 { 296 return false; 297 } 298 299 /** 300 * @brief A destructor used to delete the <b>OnTouchListener</b> instance. 301 * @since 1.0 302 * @version 1.0 303 */ ~OnTouchListener()304 virtual ~OnTouchListener() {} 305 }; 306 307 #if ENABLE_ROTATE_INPUT 308 /** 309 * @brief Defines a rotation event listener. 310 * You need to register this listener with the view to listen for rotation events. 311 * @since 5.0 312 * @version 3.0 313 */ 314 class OnRotateListener : public HeapBase { 315 public: 316 /** 317 * @brief Called when the view starts to rotate. 318 * @param view Indicates the view that responds to the rotation event. 319 * @param event Indicates the rotation event. 320 * @return Returns <b>true</b> if the rotation event is consumed; returns <b>false</b> otherwise. 321 * @since 6 322 */ OnRotateStart(UIView & view,const RotateEvent & event)323 virtual bool OnRotateStart(UIView& view, const RotateEvent& event) 324 { 325 return false; 326 } 327 328 /** 329 * @brief Called when a rotation event occurs on a view. 330 * @param view Indicates the view that responds to the rotation event. 331 * @param event Indicates the rotation event. 332 * @return Returns <b>true</b> if the rotation event is consumed; returns <b>false</b> otherwise. 333 * @since 5.0 334 * @version 3.0 335 */ OnRotate(UIView & view,const RotateEvent & event)336 virtual bool OnRotate(UIView& view, const RotateEvent& event) 337 { 338 return false; 339 } 340 341 /** 342 * @brief Called when the view stops rotating. 343 * @param view Indicates the view that responds to the rotation event. 344 * @param event Indicates the rotation event. 345 * @return Returns <b>true</b> if the rotation event is consumed; returns <b>false</b> otherwise. 346 * @since 6 347 */ OnRotateEnd(UIView & view,const RotateEvent & event)348 virtual bool OnRotateEnd(UIView& view, const RotateEvent& event) 349 { 350 return false; 351 } 352 353 /** 354 * @brief A destructor used to delete an <b>OnRotateListener</b> instance. 355 * @since 5.0 356 * @version 3.0 357 */ ~OnRotateListener()358 virtual ~OnRotateListener() {} 359 }; 360 361 /** 362 * @brief Called when the view starts to rotate. 363 * @param event Indicates the rotation event. 364 * @since 6 365 */ 366 virtual bool OnRotateStartEvent(const RotateEvent& event); 367 368 /** 369 * @brief Called when a rotation event occurs on the view. 370 * @param event Indicates the rotation event. 371 * @since 5.0 372 * @version 3.0 373 */ 374 virtual bool OnRotateEvent(const RotateEvent& event); 375 376 /** 377 * @brief Called when the view stops rotating. 378 * @param event Indicates the rotation event. 379 * @since 6 380 */ 381 virtual bool OnRotateEndEvent(const RotateEvent& event); 382 383 /** 384 * @brief Sets a rotation event listener for the view. 385 * @param onRotateListener Indicates the pointer to the rotation event listener to set. 386 * @since 5.0 387 * @version 3.0 388 */ SetOnRotateListener(OnRotateListener * onRotateListener)389 void SetOnRotateListener(OnRotateListener* onRotateListener) 390 { 391 onRotateListener_ = onRotateListener; 392 } 393 394 /** 395 * @brief Obtains the rotation event listener for the view. 396 * @return Returns the rotation event listener. 397 * @since 5.0 398 * @version 3.0 399 */ GetOnRotateListener()400 OnRotateListener*& GetOnRotateListener() 401 { 402 return onRotateListener_; 403 } 404 #endif 405 406 /** 407 * @brief Stores extra information about a <b>UIView</b> instance. 408 * @param elementPtr Indicates the void pointer to the extra information about the <b>UIView</b> instance. 409 * @since 5.0 410 * @version 3.0 411 */ 412 struct ViewExtraMsg { 413 void* elementPtr; 414 }; 415 416 /** 417 * @brief A default constructor used to create an <b>UIView</b> instance. 418 * @since 1.0 419 * @version 1.0 420 */ 421 UIView(); 422 423 /** 424 * @brief A constructor used to create an <b>UIView</b> instance. 425 * @param id Indicates the pointer to the view ID. 426 * @since 1.0 427 * @version 1.0 428 */ UIView(const char * id)429 UIView(const char* id) : UIView() 430 { 431 id_ = id; 432 } 433 434 /** 435 * @brief A destructor used to delete the <b>UIView</b> instance. 436 * @since 1.0 437 * @version 1.0 438 */ 439 virtual ~UIView(); 440 441 /** 442 * @brief Called before a view is drawn. This function is used to check whether the invalidated area 443 * can be fully cover by this view so as to optimize the drawing process. 444 * @param invalidatedArea Indicates the area to judge and returns the covered area when partly coverd. 445 * @return Returns <b>true</b> if the invalidated area is fully covered; returns <b>false</b> otherwise. 446 * @since 1.0 447 * @version 1.0 448 */ 449 virtual bool OnPreDraw(Rect& invalidatedArea) const; 450 451 /** 452 * @brief Called when a view is drawn. 453 * @param invalidatedArea Indicates the area to draw. 454 * @since 1.0 455 * @version 1.0 456 */ 457 virtual void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea); 458 459 /** 460 * @brief Called after a view is drawn. 461 * @param invalidatedArea Indicates the area in which the view is drawn. 462 * @since 1.0 463 * @version 1.0 464 */ 465 virtual void OnPostDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea); 466 467 /** 468 * @brief Remeasures the view size. 469 * @since 1.0 470 * @version 1.0 471 */ ReMeasure()472 virtual void ReMeasure() {} 473 474 /** 475 * @brief Refreshes the invalidated area of the view. 476 * @since 1.0 477 * @version 1.0 478 */ 479 void Invalidate(); 480 481 /** 482 * @brief Refreshes a view in a specified invalidated area. 483 * @param invalidatedArea Indicates the area to refresh. 484 * @since 1.0 485 * @version 1.0 486 */ 487 void InvalidateRect(const Rect& invalidatedArea); 488 489 /** 490 * @brief Called when the view is long pressed. 491 * @param event Indicates the long-press event. 492 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 493 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 494 * it is transferred to the parent view after local processing is complete.) 495 * @since 1.0 496 * @version 1.0 497 */ 498 virtual bool OnLongPressEvent(const LongPressEvent& event); 499 500 /** 501 * @brief Called when the view starts to drag. 502 * @param event Indicates the drag event. 503 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 504 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 505 * it is transferred to the parent view after local processing is complete.) 506 * @since 1.0 507 * @version 1.0 508 */ 509 virtual bool OnDragStartEvent(const DragEvent& event); 510 511 /** 512 * @brief Called when the view is being dragged. 513 * @param event Indicates the drag event. 514 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 515 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 516 * it is transferred to the parent view after local processing is complete.) 517 * @since 1.0 518 * @version 1.0 519 */ 520 virtual bool OnDragEvent(const DragEvent& event); 521 522 /** 523 * @brief Called when the view stops dragging. 524 * @param event Indicates the drag event. 525 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 526 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 527 * it is transferred to the parent view after local processing is complete.) 528 * @since 1.0 529 * @version 1.0 530 */ 531 virtual bool OnDragEndEvent(const DragEvent& event); 532 533 /** 534 * @brief Called when the view is clicked. 535 * @param event Indicates the click event. 536 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 537 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 538 * it is transferred to the parent view after local processing is complete.) 539 * @since 1.0 540 * @version 1.0 541 */ 542 virtual bool OnClickEvent(const ClickEvent& event); 543 544 /** 545 * @brief Called when the view is pressed. 546 * @param event Indicates the press event. 547 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 548 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 549 * it is transferred to the parent view after local processing is complete.) 550 * @since 1.0 551 * @version 1.0 552 */ 553 virtual bool OnPressEvent(const PressEvent& event); 554 555 /** 556 * @brief Called when the view is released. 557 * @param event Indicates the release event. 558 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 559 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 560 * it is transferred to the parent view after local processing is complete.) 561 * @since 1.0 562 * @version 1.0 563 */ 564 virtual bool OnReleaseEvent(const ReleaseEvent& event); 565 566 /** 567 * @brief Called when a click event on the view is canceled. 568 * @param event Indicates the cancel event. 569 * @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise. 570 * (If an event is consumed, it is not transferred to the parent view. If an event is not consumed, 571 * it is transferred to the parent view after local processing is complete.) 572 * @since 1.0 573 * @version 1.0 574 */ 575 virtual bool OnCancelEvent(const CancelEvent& event); 576 577 /** 578 * @brief Sets a drag event listener for the view. 579 * @param onDragListener Indicates the pointer to the drag event listener to set. 580 * @since 1.0 581 * @version 1.0 582 */ SetOnDragListener(OnDragListener * onDragListener)583 void SetOnDragListener(OnDragListener* onDragListener) 584 { 585 onDragListener_ = onDragListener; 586 } 587 588 /** 589 * @brief Obtains the drag event listener for the view. 590 * @return Returns the drag event listener. 591 * @since 1.0 592 * @version 1.0 593 */ GetOnDragListener()594 OnDragListener*& GetOnDragListener() 595 { 596 return onDragListener_; 597 } 598 599 /** 600 * @brief Sets a click event listener for the view. 601 * @param onClickListener Indicates the pointer to the click event listener to set. 602 * @since 1.0 603 * @version 1.0 604 */ SetOnClickListener(OnClickListener * onClickListener)605 void SetOnClickListener(OnClickListener* onClickListener) 606 { 607 onClickListener_ = onClickListener; 608 } 609 610 /** 611 * @brief Obtains the click event listener for the view. 612 * @return Returns the click event listener. 613 * @since 1.0 614 * @version 1.0 615 */ GetOnClickListener()616 OnClickListener*& GetOnClickListener() 617 { 618 return onClickListener_; 619 } 620 621 /** 622 * @brief Sets a long-press event listener for the view. 623 * @param onLongPressListener Indicates the pointer to the long-press event listener to set. 624 * @since 1.0 625 * @version 1.0 626 */ SetOnLongPressListener(OnLongPressListener * onLongPressListener)627 void SetOnLongPressListener(OnLongPressListener* onLongPressListener) 628 { 629 onLongPressListener_ = onLongPressListener; 630 } 631 632 /** 633 * @brief Obtains the long-press event listener for the view. 634 * @return Returns the long-press event listener. 635 * @since 1.0 636 * @version 1.0 637 */ GetOnLongPressListener()638 OnLongPressListener*& GetOnLongPressListener() 639 { 640 return onLongPressListener_; 641 } 642 643 /** 644 * @brief Sets a touch event listener for the view. 645 * @param onTouchListener Indicates the pointer to the touch event listener to set. 646 * @since 1.0 647 * @version 1.0 648 */ SetOnTouchListener(OnTouchListener * onTouchListener)649 void SetOnTouchListener(OnTouchListener* onTouchListener) 650 { 651 onTouchListener_ = onTouchListener; 652 } 653 654 /** 655 * @brief Obtains the touch event listener for the view. 656 * @return Returns the touch event listener. 657 * @since 1.0 658 * @version 1.0 659 */ GetTouchListener()660 OnTouchListener*& GetTouchListener() 661 { 662 return onTouchListener_; 663 } 664 665 /** 666 * @brief Obtains the top-level view based on specified coordinates. 667 * @param point Indicates the coordinates to specify. 668 * @param last Indicates the double pointer to the view that contains the specified coordinates. 669 * @since 1.0 670 * @version 1.0 671 */ 672 virtual void GetTargetView(const Point& point, UIView** last); 673 674 /** 675 * @brief Obtains the current view and target view based on specified coordinates. The obtained current view must 676 * include the specified coordinates and is a visible top view that can respond to touch events, and the 677 * obtained target view must be the top view that includes the specified coordinates. 678 * @param point Indicates the specified coordinates. 679 * @param current Indicates the double pointer to the current view to obtain. 680 * <b>nullptr</b> indicates that the target view fails to be obtained. 681 * @param target Indicates the double pointer to the target view to obtain. 682 * <b>nullptr</b> indicates that the target view fails to be obtained. 683 * @since 5.0 684 * @version 3.0 685 */ 686 virtual void GetTargetView(const Point& point, UIView** current, UIView** target); 687 688 /** 689 * @brief Sets the parent view for the view. 690 * @param parent Indicates the pointer to the parent view to set. 691 * @since 1.0 692 * @version 1.0 693 */ SetParent(UIView * parent)694 void SetParent(UIView* parent) 695 { 696 parent_ = parent; 697 } 698 699 /** 700 * @brief Obtains the parent view of the view. 701 * @return Returns the pointer to the parent view. 702 * @since 1.0 703 * @version 1.0 704 */ GetParent()705 UIView* GetParent() const 706 { 707 return parent_; 708 } 709 710 /** 711 * @brief Sets the next sibling view for the view. 712 * @param sibling Indicates the pointer to the next sibling view to set. 713 * @since 1.0 714 * @version 1.0 715 */ SetNextSibling(UIView * sibling)716 void SetNextSibling(UIView* sibling) 717 { 718 nextSibling_ = sibling; 719 } 720 721 /** 722 * @brief Obtains the next sibling view of the view. 723 * @return Returns the pointer to the next sibling view. 724 * @since 1.0 725 * @version 1.0 726 */ GetNextSibling()727 UIView* GetNextSibling() const 728 { 729 return nextSibling_; 730 } 731 732 /** 733 * @brief Sets whether the view is visible. 734 * @param visible Specifies whether to set the view visible. Value <b>true</b> means to set the view visible, 735 * and <b>false</b> means the opposite. 736 * @since 1.0 737 * @version 1.0 738 */ SetVisible(bool visible)739 virtual void SetVisible(bool visible) 740 { 741 if (visible_ != visible) { 742 visible_ = visible; 743 needRedraw_ = true; 744 Invalidate(); 745 } 746 } 747 748 /** 749 * @brief Checks whether the view is visible. 750 * @return Returns <b>true</b> if the view is visible; returns <b>false</b> otherwise. 751 * @since 1.0 752 * @version 1.0 753 */ IsVisible()754 bool IsVisible() const 755 { 756 return visible_; 757 } 758 759 /** 760 * @brief Sets whether the view is touchable. 761 * @param touchable Specifies whether to set the view touchable. Value <b>true</b> means to set the view touchable, 762 * and <b>false</b> means the opposite. 763 * @since 1.0 764 * @version 1.0 765 */ SetTouchable(bool touch)766 void SetTouchable(bool touch) 767 { 768 touchable_ = touch; 769 } 770 771 /** 772 * @brief Checks whether the view is touchable. 773 * @return Returns <b>true</b> if the view is touchable; returns <b>false</b> otherwise. 774 * @since 1.0 775 * @version 1.0 776 */ IsTouchable()777 bool IsTouchable() const 778 { 779 return touchable_; 780 } 781 782 /** 783 * @brief Sets whether the view is draggable. 784 * @param draggable Specifies whether to set the view draggable. Value <b>true</b> means to set the view draggable, 785 * and <b>false</b> means the opposite. 786 * @since 1.0 787 * @version 1.0 788 */ SetDraggable(bool draggable)789 void SetDraggable(bool draggable) 790 { 791 draggable_ = draggable; 792 dragParentInstead_ = !draggable; 793 } 794 795 /** 796 * @brief Checks whether the view is draggable. 797 * @return Returns <b>true</b> if the view is draggable; returns <b>false</b> otherwise. 798 * @since 1.0 799 * @version 1.0 800 */ IsDraggable()801 bool IsDraggable() const 802 { 803 return draggable_; 804 } 805 806 /** 807 * @brief Sets whether to transfer the drag event to the parent view for processing when the view is being dragged. 808 * @param dragParentInstead Specifies whether to transfer the event to the parent view for processing. 809 * Value <b>true</b> means to transfer the event to the parent view for processing, 810 * and <b>false</b> means the opposite. 811 * @since 1.0 812 * @version 1.0 813 */ SetDragParentInstead(bool dragParentInstead)814 void SetDragParentInstead(bool dragParentInstead) 815 { 816 dragParentInstead_ = dragParentInstead; 817 } 818 819 /** 820 * @brief Obtains whether the view transfers a drag event to the parent view for processing. 821 * @return Returns <b>true</b> if the view transfers the event to the parent view for processing; 822 * returns <b>false</b> otherwise. 823 * @since 1.0 824 * @version 1.0 825 */ IsDragParentInstead()826 bool IsDragParentInstead() const 827 { 828 return dragParentInstead_; 829 } 830 831 /** 832 * @brief Obtains the absolute rectangle area of the view. When the view has deformation such as rotation, 833 * the rectangle area is the intersection set of the absolute rectangle area and deformation matrix. 834 * @return Returns the absolute rectangle area. 835 * @since 1.0 836 * @version 1.0 837 */ 838 Rect GetRect() const; 839 840 /** 841 * @brief Obtains the visible absolute rectangle area of the view. 842 * @return Returns the visible absolute rectangle area. 843 * @since 1.0 844 * @version 1.0 845 */ 846 Rect GetVisibleRect() const; 847 848 /** 849 * @brief Obtains the valid absolute rectangle area of the view. The valid area refers to the area where the view 850 * can be displayed. Generally, the valid area is the same as the visible view area, but they may be 851 * different in the grid layout. 852 * @return Returns the valid absolute rectangle area. 853 * @since 1.0 854 * @version 1.0 855 */ 856 Rect GetMaskedRect() const; 857 858 /** 859 * @brief Obtains the absolute rectangle area of the view. 860 * @return Returns the absolute rectangle area. 861 * @since 1.0 862 * @version 1.0 863 */ 864 Rect GetOrigRect() const; 865 866 /** 867 * @brief Obtains the content of the absolute rectangle area of the view. This area excludes padding. 868 * @return Returns the content of the absolute rectangle area. 869 * @since 1.0 870 * @version 1.0 871 */ 872 virtual Rect GetContentRect(); 873 874 /** 875 * @brief Obtains the rectangular area of the view relative to the parent view, that is, the rectangular area 876 * relative to the coordinates of the parent view. 877 * @return Returns the rectangle area relative to the parent view. 878 * @since 1.0 879 * @version 1.0 880 */ GetRelativeRect()881 Rect GetRelativeRect() const 882 { 883 return rect_; 884 } 885 886 /** 887 * @brief Adjusts the size of the visible area. This operation may affect the final display size. 888 * @param x Indicates the new x-coordinate. 889 * @param y Indicates the new y-coordinate. 890 * @param width Indicates the new width. 891 * @param height Indicates the new height. 892 * @since 1.0 893 * @version 1.0 894 */ ResizeVisibleArea(int16_t x,int16_t y,int16_t width,int16_t height)895 void ResizeVisibleArea(int16_t x, int16_t y, int16_t width, int16_t height) 896 { 897 if (visibleRect_ == nullptr) { 898 visibleRect_ = new Rect(); 899 if (visibleRect_ == nullptr) { 900 GRAPHIC_LOGE("new Rect fail"); 901 return; 902 } 903 } 904 visibleRect_->SetWidth(width); 905 visibleRect_->SetHeight(height); 906 visibleRect_->SetPosition(x, y); 907 } 908 909 /** 910 * @brief Sets the width for the view. 911 * @param width Indicates the width to set. 912 * @since 1.0 913 * @version 1.0 914 */ SetWidth(int16_t width)915 virtual void SetWidth(int16_t width) 916 { 917 if (GetWidth() != width) { 918 int16_t newWidth = width + style_->paddingLeft_ + style_->paddingRight_ + 919 (style_->borderWidth_ * 2); /* 2: left and right border */ 920 rect_.SetWidth(newWidth); 921 } 922 } 923 924 /** 925 * @brief Sets a percentage that represents the proportion of the view's width to the parent view's width. 926 * @param widthPercent Indicates the percentage to set, the decimal form of which ranges from 0 to 1. 927 * @since 5.0 928 * @version 3.0 929 */ 930 virtual void SetWidthPercent(float widthPercent); 931 932 /** 933 * @brief Obtains the width for the view. 934 * @return Returns the view width. 935 * @since 1.0 936 * @version 1.0 937 */ GetWidth()938 virtual int16_t GetWidth() 939 { 940 return rect_.GetWidth() - (style_->paddingLeft_ + style_->paddingRight_) - 941 (style_->borderWidth_ * 2); /* 2: left and right border */ 942 } 943 944 /** 945 * @brief Sets the height for the view. 946 * @param height Indicates the height to set. 947 * @since 1.0 948 * @version 1.0 949 */ SetHeight(int16_t height)950 virtual void SetHeight(int16_t height) 951 { 952 if (GetHeight() != height) { 953 int16_t newHeight = height + style_->paddingTop_ + style_->paddingBottom_ + 954 (style_->borderWidth_ * 2); /* 2: top and bottom border */ 955 rect_.SetHeight(newHeight); 956 } 957 } 958 959 /** 960 * @brief Sets a percentage that represents the proportion of the view's hieght to the parent view's hieght. 961 * @param widthPercent Indicates the percentage to set, the decimal form of which ranges from 0 to 1. 962 * @since 5.0 963 * @version 3.0 964 */ 965 virtual void SetHeightPercent(float heightPercent); 966 967 /** 968 * @brief Obtains the height for the view. 969 * @return Returns the view height. 970 * @since 1.0 971 * @version 1.0 972 */ GetHeight()973 virtual int16_t GetHeight() 974 { 975 return rect_.GetHeight() - (style_->paddingTop_ + style_->paddingBottom_) - 976 (style_->borderWidth_ * 2); /* 2: top and bottom border */ 977 } 978 979 /** 980 * @brief Adjusts the size of the view. 981 * @param width Indicates the new width. 982 * @param height Indicates the new height. 983 * @since 1.0 984 * @version 1.0 985 */ Resize(int16_t width,int16_t height)986 virtual void Resize(int16_t width, int16_t height) 987 { 988 SetWidth(width); 989 SetHeight(height); 990 } 991 992 /** 993 * @brief Adjusts the size of the view based on specified percentages. 994 * @param widthPercent Indicates the percentage that represents the proportion of the view's width 995 * to the parent view's width to set, the decimal form of which ranges from 0 to 1. 996 * @param heightPercent Indicates the percentage that represents the proportion of the view's height 997 * to the parent view's height to set, the decimal form of which ranges from 0 to 1. 998 * @since 5.0 999 * @version 3.0 1000 */ 1001 virtual void ResizePercent(float widthPercent, float heightPercent); 1002 1003 /** 1004 * @brief Sets the x-coordinate for the view. 1005 * @param x Indicates the x-coordinate to set. 1006 * @since 1.0 1007 * @version 1.0 1008 */ SetX(int16_t x)1009 virtual void SetX(int16_t x) 1010 { 1011 if (GetX() != x) { 1012 rect_.SetX(x + GetStyle(STYLE_MARGIN_LEFT)); 1013 } 1014 } 1015 1016 /** 1017 * @brief Sets a percentage that represents the proportion of the view's x-coordinate 1018 * to the parent view's x-coordinate. 1019 * @param xPercent Indicates the percentage to set, the decimal form of which ranges from 0 to 1. 1020 * @since 5.0 1021 * @version 3.0 1022 */ 1023 virtual void SetXPercent(float xPercent); 1024 1025 /** 1026 * @brief Obtains the x-coordinate for the view. 1027 * @return Returns the x-coordinate. 1028 * @since 1.0 1029 * @version 1.0 1030 */ GetX()1031 int16_t GetX() const 1032 { 1033 return rect_.GetX() - GetStyle(STYLE_MARGIN_LEFT); 1034 } 1035 1036 /** 1037 * @brief Sets the y-coordinate for the view. 1038 * @param y Indicates the y-coordinate to set. 1039 * @since 1.0 1040 * @version 1.0 1041 */ SetY(int16_t y)1042 virtual void SetY(int16_t y) 1043 { 1044 if (GetY() != y) { 1045 rect_.SetY(y + GetStyle(STYLE_MARGIN_TOP)); 1046 } 1047 } 1048 1049 /** 1050 * @brief Sets a percentage that represents the proportion of the view's y-coordinate 1051 * to the parent view's y-coordinate. 1052 * @param yPercent Indicates the percentage to set, the decimal form of which ranges from 0 to 1. 1053 * @since 5.0 1054 * @version 3.0 1055 */ 1056 virtual void SetYPercent(float yPercent); 1057 1058 /** 1059 * @brief Obtains the y-coordinate for the view. 1060 * @return Returns the y-coordinate. 1061 * @since 1.0 1062 * @version 1.0 1063 */ GetY()1064 int16_t GetY() const 1065 { 1066 return rect_.GetY() - GetStyle(STYLE_MARGIN_TOP); 1067 } 1068 1069 /** 1070 * @brief 获取组件设置margin属性后margin的宽度,包括组件宽度加marginLeft 加 marginRight. 1071 * @return margin的宽度 1072 * @since 6 1073 */ GetWidthWithMargin()1074 int16_t GetWidthWithMargin() 1075 { 1076 return GetRelativeRect().GetWidth() + GetStyle(STYLE_MARGIN_LEFT) + GetStyle(STYLE_MARGIN_RIGHT); 1077 } 1078 1079 /** 1080 * @brief 获取组件设置margin属性后margin的高度度,包括组件宽度加marginTop 加 marginBottom. 1081 * @return margin的高度 1082 * @since 6 1083 */ GetHeightWithMargin()1084 int16_t GetHeightWithMargin() 1085 { 1086 return GetRelativeRect().GetHeight() + GetStyle(STYLE_MARGIN_TOP) + GetStyle(STYLE_MARGIN_BOTTOM); 1087 } 1088 1089 /** 1090 * @brief Sets the position for the view. 1091 * @param x Indicates the x-coordinate to set. 1092 * @param y Indicates the y-coordinate to set. 1093 * @since 1.0 1094 * @version 1.0 1095 */ SetPosition(int16_t x,int16_t y)1096 virtual void SetPosition(int16_t x, int16_t y) 1097 { 1098 SetX(x); 1099 SetY(y); 1100 } 1101 1102 /** 1103 * @brief Sets the position percentages for the view. 1104 * @param xPercent Indicates the percentage that represents the proportion of the view's x-coordinate 1105 * to the parent view's x-coordinate to set, the decimal form of which ranges from 0 to 1. 1106 * @param yPercent Indicates the percentage that represents the proportion of the view's y-coordinate 1107 * to the parent view's y-coordinate to set, the decimal form of which ranges from 0 to 1. 1108 * @since 5.0 1109 * @version 3.0 1110 */ 1111 virtual void SetPositionPercent(float xPercent, float yPercent); 1112 1113 /** 1114 * @brief Adjusts the position and size of the view. 1115 * @param x Indicates the new x-coordinate. 1116 * @param y Indicates the new y-coordinate. 1117 * @param width Indicates the new width. 1118 * @param height Indicates the new height. 1119 * @since 1.0 1120 * @version 1.0 1121 */ SetPosition(int16_t x,int16_t y,int16_t width,int16_t height)1122 virtual void SetPosition(int16_t x, int16_t y, int16_t width, int16_t height) 1123 { 1124 SetPosition(x, y); 1125 SetWidth(width); 1126 SetHeight(height); 1127 } 1128 1129 /** 1130 * @brief Sets the position and size percentages for the view. 1131 * @param xPercent Indicates the percentage that represents the proportion of the view's x-coordinate 1132 * to the parent view's x-coordinate to set, the decimal form of which ranges from 0 to 1. 1133 * @param yPercent Indicates the percentage that represents the proportion of the view's y-coordinate 1134 * to the parent view's y-coordinate, the decimal form of which ranges from 0 to 1. 1135 * @param widthPercent Indicates the percentage that represents the proportion of the view's width 1136 * to the parent view's width, the decimal form of which ranges from 0 to 1. 1137 * @param heightPercent Indicates the percentage that represents the proportion of the view's height 1138 * to the parent view's height, the decimal form of which ranges from 0 to 1. 1139 * @since 5.0 1140 * @version 3.0 1141 */ 1142 virtual void SetPositionPercent(float xPercent, float yPercent, float widthPercent, float heightPercent); 1143 1144 /** 1145 * @brief Checks whether the view is a container view. 1146 * @return Returns <b>true</b> if the view is a container view; returns <b>false</b> otherwise. 1147 * @since 1.0 1148 * @version 1.0 1149 */ IsViewGroup()1150 bool IsViewGroup() const 1151 { 1152 return isViewGroup_; 1153 } 1154 1155 /** 1156 * @brief Sets whether to intercept the input event. If intercepted, the view does not transfer the input event to 1157 * the parent view after local processing. 1158 * @param isIntercept Specifies whether to intercept the input event. Value <b>true</b> means to intercept the input 1159 * event, and <b>false</b> means the opposite. 1160 * @since 1.0 1161 * @version 1.0 1162 */ SetIntercept(bool isIntercept)1163 void SetIntercept(bool isIntercept) 1164 { 1165 isIntercept_ = isIntercept; 1166 } 1167 1168 /** 1169 * @brief Gets whether to intercept the input event. If intercepted, the view does not transfer the input event to 1170 * the parent view after local processing. 1171 * @return Returns <b>true</b> if intercept the input event, and <b>false</b> means the opposite. 1172 * @since 1.0 1173 * @version 1.0 1174 */ IsIntercept()1175 bool IsIntercept() 1176 { 1177 return isIntercept_; 1178 } 1179 1180 /** 1181 * @brief Sets the affine transformation matrix. 1182 * @param transMap Indicates the transformation matrix. 1183 * @since 1.0 1184 * @version 1.0 1185 */ 1186 void SetTransformMap(const TransformMap& transMap); 1187 1188 /** 1189 * @brief Obtains an affine transformation matrix. 1190 * @return Returns the transform matrix. 1191 * @since 1.0 1192 * @version 1.0 1193 */ GetTransformMap()1194 TransformMap& GetTransformMap() 1195 { 1196 if (transMap_ == nullptr) { 1197 transMap_ = new TransformMap(); 1198 } 1199 return *transMap_; 1200 } 1201 1202 /** 1203 * @brief Obtains the child view of a specified ID. 1204 * @return Returns the pointer to the child view. 1205 * @since 1.0 1206 * @version 1.0 1207 */ GetChildById(const char * id)1208 virtual UIView* GetChildById(const char* id) const 1209 { 1210 return nullptr; 1211 } 1212 1213 /** 1214 * @brief Sets the view ID. 1215 * @param id Indicates the pointer to the view ID. 1216 * @since 1.0 1217 * @version 1.0 1218 */ SetViewId(const char * id)1219 void SetViewId(const char* id) 1220 { 1221 id_ = id; 1222 } 1223 1224 /** 1225 * @brief Obtains the view ID. 1226 * @return Returns the pointer to the view ID. 1227 * @since 1.0 1228 * @version 1.0 1229 */ GetViewId()1230 const char* GetViewId() const 1231 { 1232 return id_; 1233 } 1234 1235 /** 1236 * @brief Sets the view index. 1237 * @param index Indicates the view index to set. 1238 * @since 1.0 1239 * @version 1.0 1240 */ SetViewIndex(int16_t index)1241 void SetViewIndex(int16_t index) 1242 { 1243 index_ = index; 1244 } 1245 1246 /** 1247 * @brief Obtains the view index. 1248 * @return Returns the view index. 1249 * @since 1.0 1250 * @version 1.0 1251 */ GetViewIndex()1252 int16_t GetViewIndex() const 1253 { 1254 return index_; 1255 } 1256 1257 /** 1258 * @brief Obtains the view type. 1259 * @return Returns the view type. 1260 * @since 1.0 1261 * @version 1.0 1262 */ GetViewType()1263 virtual UIViewType GetViewType() const 1264 { 1265 return UI_NUMBER_MAX; 1266 } 1267 1268 /** 1269 * @brief Lays out all child views according to the preset arrangement mode. 1270 * @param needInvalidate Specifies whether to refresh the invalidated area after the layout is complete. 1271 * Value <b>true</b> means to refresh the invalidated area after the layout is complete, 1272 * and <b>false</b> means the opposite. 1273 * @since 1.0 1274 * @version 1.0 1275 */ 1276 virtual void LayoutChildren(bool neeInvalidate = false) {} 1277 1278 /** 1279 * @brief Lays out the view in the center of the parent view. 1280 * @param xOffset Indicates the offset added to the x-axis after the view is placed. A positive number indicates 1281 * the offset to the right, and a negative number indicates the offset to the left. 1282 * @param yOffset Indicates the offset added to the y-axis after the view is placed. A positive number indicates 1283 * the offset to the bottom, and a negative number indicates the offset to the top. 1284 * @since 1.0 1285 * @version 1.0 1286 */ 1287 void LayoutCenterOfParent(int16_t xOffSet = 0, int16_t yOffset = 0); 1288 1289 /** 1290 * @brief Lays out the view on the left of the parent view. 1291 * @param offset Indicates the offset added to the x-axis after the view is placed. A positive number indicates 1292 * the offset to the right, and a negative number indicates the offset to the left. 1293 * @since 1.0 1294 * @version 1.0 1295 */ 1296 void LayoutLeftOfParent(int16_t offset = 0); 1297 1298 /** 1299 * @brief Lays out the view on the right of the parent view. 1300 * @param offset Indicates the offset added to the x-axis after the view is placed. A positive number indicates 1301 * the offset to the left, and a negative number indicates the offset to the right. 1302 * @since 1.0 1303 * @version 1.0 1304 */ 1305 void LayoutRightOfParent(int16_t offset = 0); 1306 1307 /** 1308 * @brief Lays out the view on the top of the parent view. 1309 * @param offset Indicates the offset added to the y-axis after the view is placed. A positive number indicates 1310 * the offset to the bottom, and a negative number indicates the offset to the top. 1311 * @since 1.0 1312 * @version 1.0 1313 */ 1314 void LayoutTopOfParent(int16_t offset = 0); 1315 1316 /** 1317 * @brief Lays out the view on the bottom of the parent view. 1318 * @param offset Indicates the offset added to the y-axis after the view is placed. A positive number indicates 1319 * the offset to the top, and a negative number indicates the offset to the bottom. 1320 * @since 1.0 1321 * @version 1.0 1322 */ 1323 void LayoutBottomOfParent(int16_t offset = 0); 1324 1325 /** 1326 * @brief Aligns the view with the left of a sibling view. 1327 * @param id Indicates the pointer to the ID of the sibling view. 1328 * @param offset Indicates the offset added to the x-axis after the view is placed. A positive number indicates 1329 * the offset to the right, and a negative number indicates the offset to the left. 1330 * @since 1.0 1331 * @version 1.0 1332 */ 1333 void AlignLeftToSibling(const char* id, int16_t offset = 0); 1334 1335 /** 1336 * @brief Aligns the view with the right of a sibling view. 1337 * @param id Indicates the pointer to the ID of the sibling view. 1338 * @param offset Indicates the offset added to the x-axis after the view is placed. A positive number indicates 1339 * the offset to the left, and a negative number indicates the offset to the right. 1340 * @since 1.0 1341 * @version 1.0 1342 */ 1343 void AlignRightToSibling(const char* id, int16_t offset = 0); 1344 1345 /** 1346 * @brief Aligns the view with the top of a sibling view. 1347 * @param id Indicates the pointer to the ID of the sibling view. 1348 * @param offset Indicates the offset added to the y-axis after the view is placed. A positive number indicates 1349 * the offset to the bottom, and a negative number indicates the offset to the top. 1350 * @since 1.0 1351 * @version 1.0 1352 */ 1353 void AlignTopToSibling(const char* id, int16_t offset = 0); 1354 1355 /** 1356 * @brief Aligns the view with the bottom of a sibling view. 1357 * @param id Indicates the pointer to the ID of the sibling view. 1358 * @param offset Indicates the offset added to the y-axis after the view is placed. A positive number indicates 1359 * the offset to the top, and a negative number indicates the offset to the bottom. 1360 * @since 1.0 1361 * @version 1.0 1362 */ 1363 void AlignBottomToSibling(const char* id, int16_t offset = 0); 1364 1365 /** 1366 * @brief Aligns the view with the center of a sibling view in the x-axis. 1367 * @param id Indicates the pointer to the ID of the sibling view. 1368 * @param offset Indicates the offset added to the x-axis after the view is placed. A positive number indicates 1369 * the offset to the right, and a negative number indicates the offset to the left. 1370 * @since 1.0 1371 * @version 1.0 1372 */ 1373 void AlignHorCenterToSibling(const char* id, int16_t offset = 0); 1374 1375 /** 1376 * @brief Aligns the view with the center of a sibling view in the y-axis. 1377 * @param id Indicates the pointer to the ID of the sibling view. 1378 * @param offset Indicates the offset added to the y-axis after the view is placed. A positive number indicates 1379 * the offset to the bottom, and a negative number indicates the offset to the top. 1380 * @since 1.0 1381 * @version 1.0 1382 */ 1383 void AlignVerCenterToSibling(const char* id, int16_t offset = 0); 1384 1385 /** 1386 * @brief Lays out the view on the left of a sibling view. 1387 * @param id Indicates the pointer to the ID of the sibling view. 1388 * @param offset Indicates the offset added to the x-axis after the view is placed. A positive number indicates 1389 * the offset to the left, and a negative number indicates the offset to the right. 1390 * @since 1.0 1391 * @version 1.0 1392 */ 1393 void LayoutLeftToSibling(const char* id, int16_t offset = 0); 1394 1395 /** 1396 * @brief Lays out the view on the right of a sibling view. 1397 * @param id Indicates the pointer to the ID of the sibling view. 1398 * @param offset Indicates the offset added to the x-axis after the view is placed. A positive number indicates 1399 * the offset to the right, and a negative number indicates the offset to the left. 1400 * @since 1.0 1401 * @version 1.0 1402 */ 1403 void LayoutRightToSibling(const char* id, int16_t offset = 0); 1404 1405 /** 1406 * @brief Lays out the view on the above of a sibling view. 1407 * @param id Indicates the pointer to the ID of the sibling view. 1408 * @param offset Indicates the offset added to the y-axis after the view is placed. A positive number indicates 1409 * the offset to the top, and a negative number indicates the offset to the bottom. 1410 * @since 1.0 1411 * @version 1.0 1412 */ 1413 void LayoutTopToSibling(const char* id, int16_t offset = 0); 1414 1415 /** 1416 * @brief Lays out the view on the below of a sibling view. 1417 * @param id Indicates the pointer to the ID of the sibling view. 1418 * @param offset Indicates the offset added to the y-axis after the view is placed. A positive number indicates 1419 * the offset to the bottom, and a negative number indicates the offset to the top. 1420 * @since 1.0 1421 * @version 1.0 1422 */ 1423 void LayoutBottomToSibling(const char* id, int16_t offset = 0); 1424 1425 /** 1426 * @brief Sets the view style. 1427 * @param style Indicates the view style. 1428 * @since 1.0 1429 * @version 1.0 1430 */ 1431 virtual void SetStyle(Style& style); 1432 1433 /** 1434 * @brief Sets a style. 1435 * 1436 * @param key Indicates the key of the style to set. 1437 * @param value Indicates the value matching the key. 1438 * @since 1.0 1439 * @version 1.0 1440 */ 1441 virtual void SetStyle(uint8_t key, int64_t value); 1442 1443 /** 1444 * @brief Obtains the value of a style. 1445 * 1446 * @param key Indicates the key of the style. 1447 * @return Returns the value of the style. 1448 * @since 1.0 1449 * @version 1.0 1450 */ GetStyle(uint8_t key)1451 virtual int64_t GetStyle(uint8_t key) const 1452 { 1453 return style_->GetStyle(key); 1454 } 1455 1456 /** 1457 * @brief Obtains the view style. This function applies to scenarios where the style does not need to be modified, 1458 * which saves memory. 1459 * @return Returns the view style. 1460 * @since 1.0 1461 * @version 1.0 1462 */ GetStyleConst()1463 const Style& GetStyleConst() const 1464 { 1465 return *style_; 1466 } 1467 1468 /** 1469 * @brief Sets the opacity for the view. 1470 * 1471 * @param opaScale Indicates the opacity to set. 1472 * @since 1.0 1473 * @version 1.0 1474 */ SetOpaScale(uint8_t opaScale)1475 void SetOpaScale(uint8_t opaScale) 1476 { 1477 opaScale_ = opaScale; 1478 } 1479 1480 /** 1481 * @brief Obtains the view opacity. 1482 * 1483 * @return Returns the view opacity. 1484 * @since 1.0 1485 * @version 1.0 1486 */ GetOpaScale()1487 uint8_t GetOpaScale() const 1488 { 1489 return opaScale_; 1490 } 1491 1492 /** 1493 * @brief Obtains the extra message about a <b>UIView</b> instance. This field is ignored by the graphics 1494 * framework and can be anything you set. 1495 * 1496 * @return Returns the pointer to the extra message about the <b>UIView</b> instance. 1497 * @since 5.0 1498 * @version 3.0 1499 */ GetExtraMsg()1500 ViewExtraMsg* GetExtraMsg() 1501 { 1502 return viewExtraMsg_; 1503 } 1504 1505 /** 1506 * @brief Sets the extra message about a <b>UIView</b> instance. This field is ignored by the graphics 1507 * framework and can be anything you set. 1508 * 1509 * @param extraMsg Indicates the extra message to set. 1510 * @since 5.0 1511 * @version 3.0 1512 */ SetExtraMsg(ViewExtraMsg * extraMsg)1513 void SetExtraMsg(ViewExtraMsg* extraMsg) 1514 { 1515 viewExtraMsg_ = extraMsg; 1516 } 1517 1518 /** 1519 * @brief Rotates the view in 2d. 1520 * @param angle Indicates the rotation angle. 1521 * @param pivot Indicates the coordinates of the rotation pivot. 1522 * @since 5.0 1523 * @version 3.0 1524 */ 1525 void Rotate(int16_t angle, const Vector2<float>& pivot); 1526 1527 /** 1528 * @brief Rotates the view in 3d. 1529 * @param angle Indicates the rotation angle. 1530 * @param pivotStart Indicates the coordinates of the rotation start pivot. 1531 * @param pivotEnd Indicates the coordinates of the rotation end pivot. 1532 * @since 5.0 1533 * @version 3.0 1534 */ 1535 void Rotate(int16_t angle, const Vector3<float>& pivotStart, const Vector3<float>& pivotEnd); 1536 1537 /** 1538 * @brief Scales the view in 2d. 1539 * 1540 * @param scale Indicates the scale factor on x- and y- axes. 1541 * @param pivot Indicates the scaling pivot. 1542 * @since 5.0 1543 * @version 3.0 1544 */ 1545 void Scale(const Vector2<float>& scale, const Vector2<float>& pivot); 1546 1547 /** 1548 * @brief Scales the view in 3d. 1549 * 1550 * @param scale Indicates the scale factor on x- and y- axes. 1551 * @param pivot Indicates the scaling pivot. 1552 * @since 5.0 1553 * @version 3.0 1554 */ 1555 void Scale(const Vector3<float>& scale, const Vector3<float>& pivot); 1556 1557 /** 1558 * @brief Shears the view in 3d. 1559 * 1560 * @param shearX Indicates the shear parameters around x- axes, 1561 * which means many it shears in y and z direction(current invalid). 1562 * @param shearY Indicates the shear parameters around y- axes, 1563 * which means many it shears in x and z direction(current invalid). 1564 * @param shaerZ Indicates the shear parameters around z- axes, 1565 * which means many it shears in x and y. 1566 * @since 5.0 1567 * @version 3.0 1568 */ 1569 void Shear(const Vector2<float>& shearX, const Vector2<float>& shearY, const Vector2<float>& shearZ); 1570 1571 void Translate(const Vector2<int16_t>& trans); 1572 1573 void Translate(const Vector3<int16_t>& trans); 1574 1575 bool IsTransInvalid(); 1576 1577 void SetCameraDistance(int16_t distance); 1578 1579 void SetCameraPosition(const Vector2<float>& position); 1580 1581 void ResetTransParameter(); 1582 1583 #if ENABLE_ROTATE_INPUT 1584 /** 1585 * @brief Requests the focus on the view. 1586 * 1587 * @since 5.0 1588 * @version 3.0 1589 */ 1590 virtual void RequestFocus(); 1591 1592 /** 1593 * @brief Clears the focus on the view. 1594 * 1595 * @since 5.0 1596 * @version 3.0 1597 */ 1598 virtual void ClearFocus(); 1599 #endif 1600 #if ENABLE_FOCUS_MANAGER 1601 /** 1602 * @brief 设置视图是否可获焦. 1603 * 1604 * @param focusable 是否可获焦. 1605 * @since 5.0 1606 * @version 3.0 1607 */ SetFocusable(bool focusable)1608 void SetFocusable(bool focusable) 1609 { 1610 focusable_ = focusable; 1611 } 1612 1613 /** 1614 * @brief 获取视图是否可获焦. 1615 * 1616 * @return 是否可获焦. 1617 * @since 5.0 1618 * @version 3.0 1619 */ IsFocusable()1620 bool IsFocusable() const 1621 { 1622 return focusable_; 1623 } 1624 1625 /** 1626 * @brief 组件获焦响应 1627 * 1628 * @since 5.0 1629 * @version 3.0 1630 */ 1631 void Focus(); 1632 1633 /** 1634 * @brief 组件失焦响应 1635 * 1636 * @since 5.0 1637 * @version 3.0 1638 */ 1639 void Blur(); 1640 1641 /** 1642 * @brief 焦点改变事件监听类,开发者需要向视图组件注册该类实现事件的监听. 1643 * 1644 * @since 5.0 1645 * @version 3.0 1646 */ 1647 class OnFocusListener : public HeapBase { 1648 public: 1649 /** 1650 * @brief 回调函数,视图获焦时触发. 1651 * @param view 获焦的视图 1652 * @since 5.0 1653 * @version 3.0 1654 */ OnFocus(UIView & view)1655 virtual bool OnFocus(UIView& view) 1656 { 1657 return false; 1658 } 1659 1660 /** 1661 * @brief 回调函数,视图失焦时触发. 1662 * @param view 失焦的视图 1663 * @since 5.0 1664 * @version 3.0 1665 */ OnBlur(UIView & view)1666 virtual bool OnBlur(UIView& view) 1667 { 1668 return false; 1669 } 1670 1671 /** 1672 * @brief 析构函数. 1673 * @since 5.0 1674 * @version 3.0 1675 */ ~OnFocusListener()1676 virtual ~OnFocusListener() {} 1677 }; 1678 1679 /** 1680 * @brief 设置当前视图焦点改变事件监听者. 1681 * @param onFocusListener 焦点改变事件监听者. 1682 * @since 5.0 1683 * @version 3.0 1684 */ SetOnFocusListener(OnFocusListener * onFocusListener)1685 void SetOnFocusListener(OnFocusListener* onFocusListener) 1686 { 1687 onFocusListener_ = onFocusListener; 1688 } 1689 1690 /** 1691 * @brief 获取当前视图焦点改变事件监听者. 1692 * @return 焦点改变事件监听者. 1693 * @since 5.0 1694 * @version 3.0 1695 */ GetOnFocusListener()1696 OnFocusListener* GetOnFocusListener() const 1697 { 1698 return onFocusListener_; 1699 } 1700 #endif 1701 1702 /** 1703 * @brief 获取当前视图的bitmap截图.请注意该接口会申请内存,请在需要释放时使用{@link ImageCacheFree()}接口. 1704 * @param info bitmap存储对象,获取的截图将被存到该引用中. 1705 * @return bitmap是否获取成功. 1706 * @since 5.0 1707 * @version 3.0 1708 */ 1709 bool GetBitmap(ImageInfo& bitmap); 1710 1711 bool IsOnViewTree(); 1712 1713 protected: 1714 bool touchable_ : 1; 1715 bool visible_ : 1; 1716 bool draggable_ : 1; 1717 bool dragParentInstead_ : 1; 1718 bool isViewGroup_ : 1; 1719 bool needRedraw_ : 1; 1720 bool styleAllocFlag_ : 1; 1721 bool isIntercept_ : 1; 1722 #if ENABLE_FOCUS_MANAGER 1723 bool focusable_ : 1; 1724 #endif 1725 uint8_t opaScale_; 1726 int16_t index_; 1727 const char* id_; 1728 UIView* parent_; 1729 UIView* nextSibling_; 1730 Style* style_; 1731 TransformMap* transMap_; 1732 OnClickListener* onClickListener_; 1733 OnLongPressListener* onLongPressListener_; 1734 OnDragListener* onDragListener_; 1735 OnTouchListener* onTouchListener_; 1736 #if ENABLE_FOCUS_MANAGER 1737 OnFocusListener* onFocusListener_; 1738 #endif 1739 #if ENABLE_ROTATE_INPUT 1740 OnRotateListener* onRotateListener_; 1741 #endif 1742 ViewExtraMsg* viewExtraMsg_; 1743 1744 uint8_t GetMixOpaScale() const; 1745 bool IsInvalid(float percent); 1746 void DrawViewBounds(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea); 1747 void UpdateRectInfo(uint8_t key, const Rect& rect); 1748 1749 private: 1750 Rect rect_; 1751 Rect* visibleRect_; 1752 void SetupThemeStyles(); 1753 }; 1754 } // namespace OHOS 1755 #endif // GRAPHIC_LITE_UI_VIEW_H 1756