• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 
176     uint32_t Compare(const RefPtr<Component>& component) const override;
177 
GetStateAttributes()178     RefPtr<StateAttributes<ButtonStateAttribute>> GetStateAttributes()
179     {
180         if (stateAttributeList_ == nullptr) {
181             stateAttributeList_ = MakeRefPtr<StateAttributes<ButtonStateAttribute>>();
182         }
183         return stateAttributeList_;
184     }
185 
HasStateAttributes()186     bool HasStateAttributes()
187     {
188         return stateAttributeList_ != nullptr;
189     }
190 
191 private:
192     RefPtr<ButtonDeclaration> declaration_;
193     ButtonType type_ { ButtonType::NORMAL };
194     bool isInnerBorder_ = false;
195     bool focusable_ = true;
196     bool stateEffect_ = true;
197     bool isDeclarative_ = false;
198     bool isInputButton_ = false;
199     bool isCatchMode_ = true;
200     uint32_t layoutFlag_ = 0;
201     Dimension height_;
202     // for custom button type
203     std::array<Radius, 4> radii_ = { Radius(0.0_vp), Radius(0.0_vp), Radius(0.0_vp), Radius(0.0_vp) };
204     RefPtr<StateAttributes<ButtonStateAttribute>> stateAttributeList_;
205     EventMarker keyEnterId_;
206 };
207 
208 class ButtonBuilder {
209 public:
210     static RefPtr<ButtonComponent> Build(const RefPtr<ThemeManager>& themeManager, const std::string& text);
211     static RefPtr<ButtonComponent> Build(const RefPtr<ThemeManager>& themeManager, const std::string& text,
212         TextStyle& textStyle, const Color& textFocusColor = Color(), bool useTextFocus = false);
213 };
214 
215 } // namespace OHOS::Ace
216 
217 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BUTTON_BUTTON_COMPONENT_H
218