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_BUTTON_BUTTON_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUTTON_BUTTON_COMPONENT_H
18 
19 #include "base/geometry/dimension.h"
20 #include "base/utils/label_target.h"
21 #include "base/utils/macros.h"
22 #include "core/components/button/button_theme.h"
23 #include "core/components/common/layout/constants.h"
24 #include "core/components/common/properties/border_edge.h"
25 #include "core/components/common/properties/color.h"
26 #include "core/components/common/properties/state_attributes.h"
27 #include "core/pipeline/base/component_group.h"
28 #include "core/pipeline/base/measurable.h"
29 #include "frameworks/core/components/declaration/button/button_declaration.h"
30 
31 namespace OHOS::Ace {
32 
33 // Layout size extend to max which is specified by parent node when rendering.
34 constexpr int32_t LAYOUT_FLAG_EXTEND_TO_PARENT = 1;
35 
36 using ProgressCallback = std::function<void(uint32_t)>;
37 
38 enum class ButtonStateAttribute { COLOR, RADIUS, HEIGHT, WIDTH };
39 
40 class ACE_EXPORT ButtonComponent : public ComponentGroup, public LabelTarget, public Measurable {
41     DECLARE_ACE_TYPE(ButtonComponent, ComponentGroup, LabelTarget, Measurable);
42 
43 public:
44     explicit ButtonComponent(const std::list<RefPtr<Component>>& children);
45     ~ButtonComponent() override = default;
46 
47     RefPtr<RenderNode> CreateRenderNode() override;
48     RefPtr<Element> CreateElement() override;
49 
GetType()50     ButtonType GetType() const
51     {
52         return type_;
53     }
54 
SetType(ButtonType type)55     void SetType(ButtonType type)
56     {
57         type_ = type;
58     }
59 
60     bool GetDisabledState() const;
61     bool GetWaitingState() const;
62     bool GetAutoFocusState() const;
63     bool GetRadiusState() const;
64     bool GetCatchMode() const;
65     const Dimension& GetMinWidth() const;
66     const Dimension& GetRectRadius() const;
67     const Dimension& GetProgressDiameter() const;
68     const Color& GetBackgroundColor() const;
69     const Color& GetClickedColor() const;
70     const Color& GetDisabledColor() const;
71     const Color& GetFocusColor() const;
72     const Color& GetHoverColor() const;
73     const Color& GetProgressColor() const;
74     const Color& GetProgressFocusColor() const;
75     const Color& GetFocusAnimationColor() const;
76     const BorderEdge& GetBorderEdge() const;
77     const EventMarker& GetClickedEventId() const;
78     const EventMarker& GetKeyEnterEventId() const;
79     const EventMarker& GetRemoteMessageEventId() const;
80     RefPtr<ButtonProgressController> GetButtonController() const;
81 
82     void SetDisabledState(bool state);
83     void SetWaitingState(bool state);
84     void SetAutoFocusState(bool state);
85     void SetRadiusState(bool state);
86     void SetMinWidth(const Dimension& width);
87     void SetRectRadius(const Dimension& radius);
88     void SetCatchMode(bool catchMode);
89     void SetProgressDiameter(const Dimension& diameter);
90     void SetBackgroundColor(const Color& color);
91     void SetClickedColor(const Color& color);
92     void SetDisabledColor(const Color& color);
93     void SetFocusColor(const Color& color);
94     void SetHoverColor(const Color& color);
95     void SetProgressColor(const Color& color);
96     void SetProgressFocusColor(const Color& color);
97     void SetFocusAnimationColor(const Color& color);
98     void SetBorderEdge(const BorderEdge& borderEdge);
99     void SetClickedEventId(const EventMarker& eventId);
100     void SetKeyEnterEventId(const EventMarker& eventId);
101     void SetRemoteMessageEventId(const EventMarker& eventId);
102     void SetClickFunction(std::function<void()>&& clickCallback);
103     void SetDeclaration(const RefPtr<ButtonDeclaration>& declaration);
104     void ApplyTheme(const RefPtr<ButtonTheme>& theme);
105 
GetFocusable()106     bool GetFocusable() const
107     {
108         return focusable_;
109     }
110 
SetFocusable(bool focusable)111     void SetFocusable(bool focusable)
112     {
113         focusable_ = focusable;
114     }
115 
GetLayoutFlag()116     uint32_t GetLayoutFlag() const
117     {
118         return layoutFlag_;
119     }
120 
SetLayoutFlag(uint32_t flag)121     void SetLayoutFlag(uint32_t flag)
122     {
123         layoutFlag_ = flag;
124     }
125 
IsInnerBorder()126     bool IsInnerBorder() const
127     {
128         return isInnerBorder_;
129     }
130 
SetIsInnerBorder(bool isInnerBorder)131     void SetIsInnerBorder(bool isInnerBorder)
132     {
133         isInnerBorder_ = isInnerBorder;
134     }
135 
GetStateEffect()136     bool GetStateEffect() const
137     {
138         return stateEffect_;
139     }
140 
SetStateEffect(bool effect)141     void SetStateEffect(bool effect)
142     {
143         stateEffect_ = effect;
144     }
145 
GetDeclarativeFlag()146     bool GetDeclarativeFlag() const
147     {
148         return isDeclarative_;
149     }
150 
SetDeclarativeFlag(bool flag)151     void SetDeclarativeFlag(bool flag)
152     {
153         isDeclarative_ = flag;
154     }
155 
SetRectRadii(const std::array<Radius,4> & radii)156     void SetRectRadii(const std::array<Radius, 4>& radii)
157     {
158         radii_ = radii;
159     }
160 
GetRectRadii()161     const std::array<Radius, 4>& GetRectRadii() const
162     {
163         return radii_;
164     }
165 
SetInputButton(bool inputButton)166     void SetInputButton(bool inputButton)
167     {
168         isInputButton_ = inputButton;
169     }
170 
IsInputButton()171     bool IsInputButton() const
172     {
173         return isInputButton_;
174     }
175 
SetDeclareHeight(bool declareHeight)176     void SetDeclareHeight(bool declareHeight)
177     {
178         isDeclareHeight_ = declareHeight;
179     }
180 
IsDeclareHeight()181     bool IsDeclareHeight()
182     {
183         return isDeclareHeight_;
184     }
185 
IsNeedResetHeight(bool needResetHeight)186     void IsNeedResetHeight(bool needResetHeight)
187     {
188         needResetHeight_ = needResetHeight;
189     }
190 
NeedResetHeight()191     bool NeedResetHeight()
192     {
193         return needResetHeight_;
194     }
195 
196     void FitTextHeight(AnimatableDimension& height);
197 
198     uint32_t Compare(const RefPtr<Component>& component) const override;
199 
GetStateAttributes()200     RefPtr<StateAttributes<ButtonStateAttribute>> GetStateAttributes()
201     {
202         if (stateAttributeList_ == nullptr) {
203             stateAttributeList_ = MakeRefPtr<StateAttributes<ButtonStateAttribute>>();
204         }
205         return stateAttributeList_;
206     }
207 
HasStateAttributes()208     bool HasStateAttributes()
209     {
210         return stateAttributeList_ != nullptr;
211     }
212 
GetMouseAnimationType()213     HoverAnimationType GetMouseAnimationType() const
214     {
215         return animationType_;
216     }
SetMouseAnimationType(HoverAnimationType animationType)217     void SetMouseAnimationType(HoverAnimationType animationType)
218     {
219         animationType_ = animationType;
220     }
221 
222     // hasCustomChild is true for ets include child such as
223     // Button() {
224     //   Text(button label)
225     // }
SetHasCustomChild(bool value)226     void SetHasCustomChild(bool value)
227     {
228         hasCustomChild_ = value;
229     }
GetHasCustomChild()230     bool GetHasCustomChild()
231     {
232         return hasCustomChild_;
233     }
SetAspectRatio(double ratio)234     void SetAspectRatio(double ratio)
235     {
236         aspectRatio_ = ratio;
237     }
238 
GetAspectRatio()239     double GetAspectRatio() const
240     {
241         return aspectRatio_;
242     }
243 
SetIsPopupButton(bool isPopupButton)244     void SetIsPopupButton(bool isPopupButton)
245     {
246         isPopupButton_ = isPopupButton;
247     }
248 
IsPopupButton()249     bool IsPopupButton() const
250     {
251         return isPopupButton_;
252     }
253 
254 private:
255     RefPtr<ButtonDeclaration> declaration_;
256     ButtonType type_ { ButtonType::NORMAL };
257     bool isInnerBorder_ = false;
258     bool focusable_ = true;
259     bool stateEffect_ = true;
260     bool isDeclarative_ = false;
261     bool isInputButton_ = false;
262     bool isCatchMode_ = true;
263     bool isDeclareHeight_ = false;
264     bool needResetHeight_ = true;
265     bool isPopupButton_ = false;
266     uint32_t layoutFlag_ = 0;
267     Dimension height_;
268     bool hasCustomChild_  = false;
269     double aspectRatio_ = 0.0;
270     // for custom button type
271     std::array<Radius, 4> radii_ = { Radius(0.0_vp), Radius(0.0_vp), Radius(0.0_vp), Radius(0.0_vp) };
272     RefPtr<StateAttributes<ButtonStateAttribute>> stateAttributeList_;
273     EventMarker keyEnterId_;
274     HoverAnimationType animationType_ = HoverAnimationType::UNKNOWN;
275 };
276 
277 class ButtonBuilder {
278 public:
279     static RefPtr<ButtonComponent> Build(const RefPtr<ThemeManager>& themeManager, const std::string& text);
280     static RefPtr<ButtonComponent> Build(const RefPtr<ThemeManager>& themeManager, const std::string& text,
281         TextStyle& textStyle, const Color& textFocusColor = Color(), bool useTextFocus = false);
282 };
283 
284 } // namespace OHOS::Ace
285 
286 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUTTON_BUTTON_COMPONENT_H
287