• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
IsOnHover()87     bool IsOnHover() const
88     {
89         return mouseState_ == MouseState::HOVER;
90     }
GetActiveColor()91     Color GetActiveColor() const
92     {
93         return activeColorInspector_;
94     }
GetPointColor()95     Color GetPointColor()
96     {
97         return pointColorInspector_;
98     }
99     virtual void RequestFocusBorder(const Offset& focusOffset, const Size& focusSize, double borderRadius);
100 
101 protected:
102     void OnTouchTestHit(
103         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
104     void OnStatusChanged(RenderStatus renderStatus) override;
105     void InitSize();
106     void CalculateSize();
UpdateUIStatus()107     virtual void UpdateUIStatus()
108     {
109         uiStatus_ = checked_ ? UIStatus::SELECTED : ((onFocus_ && needFocus_) ? UIStatus::FOCUS : UIStatus::UNSELECTED);
110     }
SetDragPosition(double dragPosition)111     void SetDragPosition(double dragPosition)
112     {
113         dragPosition_ = dragPosition;
114     }
GetDragPosition()115     double GetDragPosition() const
116     {
117         return dragPosition_;
118     }
119     std::string UpdateChangedResult();
120     virtual void OnHandleChangedResult(const std::string& result);
121 
122     double width_ = -1.0;
123     double height_ = -1.0;
124     Dimension hotZoneHorizontalPadding_ = 0.0_px;
125     Dimension hotZoneVerticalPadding_ = 0.0_px;
126     Dimension defaultWidth_ = 1.0_px;
127     Dimension defaultHeight_ = 1.0_px;
128     Dimension shadowWidth_ = 1.0_vp;
129     double aspectRatio_ = 1.0;
130     double dragPosition_ = 0.0;
131     Dimension borderWidth_ = 0.0_px;
132     Dimension borderRadius_ = 0.0_px;
133     Dimension checkStroke_ = 0.0_px;
134     Offset paintPosition_;
135     uint32_t pointColor_ = 0;
136     uint32_t activeColor_ = 0;
137     Color pointColorInspector_;
138     Color activeColorInspector_;
139     uint32_t inactiveColor_ = 0;
140     uint32_t focusColor_ = 0;
141     uint32_t inactivePointColor_ = 0;
142     uint32_t shadowColor_ = 0x0C000000;
143     bool disabled_ = false;
144     bool checked_ = false;
145     bool onFocus_ = false;
146     bool needFocus_ = true;
147     bool backgroundSolid_ = true;
148     UIStatus uiStatus_ = UIStatus::UNSELECTED;
149     RefPtr<DragRecognizer> dragRecognizer_;
150     RefPtr<ClickRecognizer> clickRecognizer_;
151     RefPtr<RawRecognizer> touchRecognizer_;
152     std::function<void(const std::string&)> changeEvent_;
153     std::function<void(const std::string&)> valueChangeEvent_;
154     std::function<void()> clickEvent_;
155     std::function<void(const std::string&)> domChangeEvent_;
156     std::function<void(bool)> onChange_;
157     std::function<void()> onClick_;
158     Size drawSize_;
159     bool isTouch_ = false;
160     bool isHover_ = false;
161     bool isDeclarative_ = false;
162 };
163 
164 } // namespace OHOS::Ace
165 
166 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CHECKABLE_RENDER_CHECKABLE_H
167