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_COMPONENTS_CHECKABLE_RENDER_CHECKABLE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_RENDER_CHECKABLE_H 18 19 #include "base/utils/system_properties.h" 20 #include "core/components/checkable/checkable_component.h" 21 #include "core/components/focus_animation/render_focus_animation.h" 22 #include "core/gestures/click_recognizer.h" 23 #include "core/gestures/drag_recognizer.h" 24 #include "core/pipeline/base/render_node.h" 25 26 namespace OHOS::Ace { 27 28 constexpr Dimension DEFAULT_CHECKABLE_BORDER_WIDTH = 1.0_vp; 29 30 enum class UIStatus { 31 SELECTED = 0, 32 UNSELECTED, 33 FOCUS, 34 ON_TO_OFF, 35 OFF_TO_ON, 36 PART, 37 PART_TO_OFF, 38 OFF_TO_PART, 39 PART_TO_ON, 40 ON_TO_PART, 41 }; 42 43 class RenderCheckable : public RenderNode { 44 DECLARE_ACE_TYPE(RenderCheckable, RenderNode); 45 46 public: 47 void Update(const RefPtr<Component>& component) override; 48 void PerformLayout() override; 49 virtual void OnMouseHoverEnterTest() override; 50 virtual void OnMouseHoverExitTest() override; 51 virtual void HandleClick(); 52 void ApplyAspectRatio(Size& drawSize) const; 53 void InitClickRecognizer(bool catchMode); 54 void AddAccessibilityAction(); GetChecked()55 bool GetChecked() const 56 { 57 return checked_; 58 } 59 SetNeedFocus(bool needFocus)60 void SetNeedFocus(bool needFocus) 61 { 62 needFocus_ = needFocus; 63 } 64 SetOnChange(const std::function<void (bool)> & value)65 void SetOnChange(const std::function<void(bool)>& value) 66 { 67 onChange_ = value; 68 } 69 Isdisable()70 bool Isdisable() 71 { 72 return disabled_; 73 } 74 IsPhone()75 bool IsPhone() const 76 { 77 return SystemProperties::GetDeviceType() == DeviceType::PHONE; 78 } IsOnHover()79 bool IsOnHover() const 80 { 81 return mouseState_ == MouseState::HOVER; 82 } GetActiveColor()83 Color GetActiveColor() const 84 { 85 return activeColorInspector_; 86 } GetPointColor()87 Color GetPointColor() 88 { 89 return pointColorInspector_; 90 } 91 virtual void RequestFocusBorder(const Offset& focusOffset, const Size& focusSize, double borderRadius); 92 93 protected: 94 void OnTouchTestHit( 95 const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override; 96 void OnStatusChanged(RenderStatus renderStatus) override; 97 void InitSize(); 98 void CalculateSize(); UpdateUIStatus()99 virtual void UpdateUIStatus() 100 { 101 uiStatus_ = checked_ ? UIStatus::SELECTED : ((onFocus_ && needFocus_) ? UIStatus::FOCUS : UIStatus::UNSELECTED); 102 } SetDragPosition(double dragPosition)103 void SetDragPosition(double dragPosition) 104 { 105 dragPosition_ = dragPosition; 106 } GetDragPosition()107 double GetDragPosition() const 108 { 109 return dragPosition_; 110 } 111 std::string UpdateChangedResult(); 112 virtual void OnHandleChangedResult(const std::string& result); 113 114 double width_ = -1.0; 115 double height_ = -1.0; 116 Dimension hotZoneHorizontalPadding_ = 0.0_px; 117 Dimension hotZoneVerticalPadding_ = 0.0_px; 118 Dimension defaultWidth_ = 1.0_px; 119 Dimension defaultHeight_ = 1.0_px; 120 Dimension shadowWidth_ = 1.0_vp; 121 double aspectRatio_ = 1.0; 122 double dragPosition_ = 0.0; 123 Dimension borderWidth_ = 0.0_px; 124 Dimension borderRadius_ = 0.0_px; 125 Dimension checkStroke_ = 0.0_px; 126 Offset paintPosition_; 127 uint32_t pointColor_ = 0; 128 uint32_t activeColor_ = 0; 129 Color pointColorInspector_; 130 Color activeColorInspector_; 131 uint32_t inactiveColor_ = 0; 132 uint32_t focusColor_ = 0; 133 uint32_t inactivePointColor_ = 0; 134 uint32_t shadowColor_ = 0x0C000000; 135 bool disabled_ = false; 136 bool checked_ = false; 137 bool onFocus_ = false; 138 bool needFocus_ = true; 139 bool backgroundSolid_ = true; 140 UIStatus uiStatus_ = UIStatus::UNSELECTED; 141 RefPtr<DragRecognizer> dragRecognizer_; 142 RefPtr<ClickRecognizer> clickRecognizer_; 143 std::function<void(const std::string&)> changeEvent_; 144 std::function<void(const std::string&)> valueChangeEvent_; 145 std::function<void()> clickEvent_; 146 std::function<void(const std::string&)> domChangeEvent_; 147 std::function<void(bool)> onChange_; 148 std::function<void()> onClick_; 149 Size drawSize_; 150 }; 151 152 } // namespace OHOS::Ace 153 154 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_RENDER_CHECKABLE_H 155