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