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 #include "core/components/button/button_component.h"
17
18 #include "core/components/button/button_element.h"
19 #include "core/components/button/render_button.h"
20 #include "core/components/padding/padding_component.h"
21 #include "core/components/text/text_component.h"
22 #include "core/components/theme/theme_manager.h"
23
24 namespace OHOS::Ace {
25
26 constexpr uint32_t WATCH_BACKGROUND_COLOR = 0xff007dff;
27 constexpr uint32_t WATCH_TEXT_COLOR = 0xffffffff;
28
ButtonComponent(const std::list<RefPtr<Component>> & children)29 ButtonComponent::ButtonComponent(const std::list<RefPtr<Component>>& children) : ComponentGroup(children)
30 {
31 if (!declaration_) {
32 declaration_ = AceType::MakeRefPtr<ButtonDeclaration>();
33 declaration_->Init();
34 }
35 }
36
CreateRenderNode()37 RefPtr<RenderNode> ButtonComponent::CreateRenderNode()
38 {
39 return RenderButton::Create();
40 }
41
CreateElement()42 RefPtr<Element> ButtonComponent::CreateElement()
43 {
44 return AceType::MakeRefPtr<ButtonElement>();
45 }
46
Build(const RefPtr<ThemeManager> & themeManager,const std::string & text)47 RefPtr<ButtonComponent> ButtonBuilder::Build(const RefPtr<ThemeManager>& themeManager, const std::string& text)
48 {
49 auto buttonTheme = AceType::DynamicCast<ButtonTheme>(themeManager->GetTheme(ButtonTheme::TypeId()));
50 if (!buttonTheme) {
51 TextStyle defaultStyle;
52 return ButtonBuilder::Build(themeManager, text, defaultStyle);
53 }
54 TextStyle textStyle = buttonTheme->GetTextStyle();
55 textStyle.SetAdaptTextSize(buttonTheme->GetMaxFontSize(), buttonTheme->GetMinFontSize());
56 textStyle.SetMaxLines(buttonTheme->GetTextMaxLines());
57 textStyle.SetTextOverflow(TextOverflow::ELLIPSIS);
58 return ButtonBuilder::Build(themeManager, text, textStyle);
59 }
60
Build(const RefPtr<ThemeManager> & themeManager,const std::string & text,TextStyle & textStyle,const Color & textFocusColor,bool useTextFocus)61 RefPtr<ButtonComponent> ButtonBuilder::Build(const RefPtr<ThemeManager>& themeManager, const std::string& text,
62 TextStyle& textStyle, const Color& textFocusColor, bool useTextFocus)
63 {
64 auto textComponent = AceType::MakeRefPtr<TextComponent>(text);
65 auto padding = AceType::MakeRefPtr<PaddingComponent>();
66 padding->SetChild(textComponent);
67 Component::MergeRSNode(padding, textComponent);
68 std::list<RefPtr<Component>> buttonChildren;
69 buttonChildren.emplace_back(padding);
70 auto buttonComponent = AceType::MakeRefPtr<ButtonComponent>(buttonChildren);
71 auto buttonTheme = AceType::DynamicCast<ButtonTheme>(themeManager->GetTheme(ButtonTheme::TypeId()));
72 if (!buttonTheme) {
73 return buttonComponent;
74 }
75 if (useTextFocus) {
76 textComponent->SetFocusColor(textFocusColor);
77 } else {
78 textComponent->SetFocusColor(buttonTheme->GetTextFocusColor());
79 }
80 textComponent->SetTextStyle(textStyle);
81 padding->SetPadding(buttonTheme->GetPadding());
82 buttonComponent->SetHeight(buttonTheme->GetHeight());
83 buttonComponent->SetRectRadius(buttonTheme->GetHeight() / 2.0);
84 buttonComponent->SetBackgroundColor(buttonTheme->GetBgColor());
85 buttonComponent->SetClickedColor(buttonTheme->GetClickedColor());
86 buttonComponent->SetFocusColor(buttonTheme->GetBgFocusColor());
87 buttonComponent->SetFocusAnimationColor(buttonTheme->GetBgFocusColor());
88 return buttonComponent;
89 }
90
GetDisabledState() const91 bool ButtonComponent::GetDisabledState() const
92 {
93 return declaration_->GetDisabledState();
94 }
95
GetWaitingState() const96 bool ButtonComponent::GetWaitingState() const
97 {
98 return declaration_->GetWaitingState();
99 }
100
GetAutoFocusState() const101 bool ButtonComponent::GetAutoFocusState() const
102 {
103 return declaration_->GetAutoFocusState();
104 }
105
GetRadiusState() const106 bool ButtonComponent::GetRadiusState() const
107 {
108 return declaration_->GetRadiusState();
109 }
110
GetCatchMode() const111 bool ButtonComponent::GetCatchMode() const
112 {
113 return isCatchMode_;
114 }
115
GetMinWidth() const116 const Dimension& ButtonComponent::GetMinWidth() const
117 {
118 return declaration_->GetMinWidth();
119 }
120
GetRectRadius() const121 const Dimension& ButtonComponent::GetRectRadius() const
122 {
123 return declaration_->GetRectRadius();
124 }
125
GetProgressDiameter() const126 const Dimension& ButtonComponent::GetProgressDiameter() const
127 {
128 return declaration_->GetProgressDiameter();
129 }
130
GetBackgroundColor() const131 const Color& ButtonComponent::GetBackgroundColor() const
132 {
133 return declaration_->GetBackgroundColor();
134 }
135
GetClickedColor() const136 const Color& ButtonComponent::GetClickedColor() const
137 {
138 return declaration_->GetClickedColor();
139 }
140
GetDisabledColor() const141 const Color& ButtonComponent::GetDisabledColor() const
142 {
143 return declaration_->GetDisabledColor();
144 }
145
GetFocusColor() const146 const Color& ButtonComponent::GetFocusColor() const
147 {
148 return declaration_->GetFocusColor();
149 }
150
GetHoverColor() const151 const Color& ButtonComponent::GetHoverColor() const
152 {
153 return declaration_->GetHoverColor();
154 }
155
GetProgressColor() const156 const Color& ButtonComponent::GetProgressColor() const
157 {
158 return declaration_->GetProgressColor();
159 }
160
GetProgressFocusColor() const161 const Color& ButtonComponent::GetProgressFocusColor() const
162 {
163 return declaration_->GetProgressFocusColor();
164 }
165
GetFocusAnimationColor() const166 const Color& ButtonComponent::GetFocusAnimationColor() const
167 {
168 return declaration_->GetFocusAnimationColor();
169 }
170
GetBorderEdge() const171 const BorderEdge& ButtonComponent::GetBorderEdge() const
172 {
173 return declaration_->GetBorderEdge();
174 }
175
GetClickedEventId() const176 const EventMarker& ButtonComponent::GetClickedEventId() const
177 {
178 return declaration_->GetClickedEventId();
179 }
180
GetKeyEnterEventId() const181 const EventMarker& ButtonComponent::GetKeyEnterEventId() const
182 {
183 return keyEnterId_;
184 }
185
GetRemoteMessageEventId() const186 const EventMarker& ButtonComponent::GetRemoteMessageEventId() const
187 {
188 return declaration_->GetRemoteMessageEventId();
189 }
190
GetButtonController() const191 RefPtr<ButtonProgressController> ButtonComponent::GetButtonController() const
192 {
193 return declaration_->GetButtonController();
194 }
195
ApplyTheme(const RefPtr<ButtonTheme> & theme)196 void ButtonComponent::ApplyTheme(const RefPtr<ButtonTheme>& theme)
197 {
198 height_ = theme->GetHeight();
199 SetLayoutFlag(LAYOUT_FLAG_EXTEND_TO_PARENT);
200 SetBackgroundColor(theme->GetBgColor());
201 SetFocusColor(theme->GetBgColor());
202 SetFocusAnimationColor(theme->GetBgFocusColor());
203 SetHoverColor(theme->GetHoverColor());
204 auto padding = AceType::DynamicCast<PaddingComponent>(GetChildren().front());
205 if (!padding) {
206 return;
207 }
208 padding->SetPadding(theme->GetPadding());
209 auto text = AceType::DynamicCast<TextComponent>(padding->GetChild());
210 if (!text) {
211 return;
212 }
213 auto textStyle = theme->GetTextStyle();
214 textStyle.SetAdaptTextSize(textStyle.GetFontSize(), theme->GetMinFontSize());
215 textStyle.SetTextAlign(TextAlign::CENTER);
216 textStyle.SetMaxLines(theme->GetTextMaxLines());
217 textStyle.SetTextOverflow(TextOverflow::ELLIPSIS);
218 if (SystemProperties::GetDeviceType() == DeviceType::WATCH) {
219 SetBackgroundColor(Color(WATCH_BACKGROUND_COLOR));
220 textStyle.SetTextColor(Color(WATCH_TEXT_COLOR));
221 text->SetFocusColor(Color(WATCH_TEXT_COLOR));
222 }
223 text->SetTextStyle(textStyle);
224 }
225
SetDisabledState(bool state)226 void ButtonComponent::SetDisabledState(bool state)
227 {
228 declaration_->SetDisabledState(state);
229 }
230
SetWaitingState(bool state)231 void ButtonComponent::SetWaitingState(bool state)
232 {
233 declaration_->SetWaitingState(state);
234 }
235
SetAutoFocusState(bool state)236 void ButtonComponent::SetAutoFocusState(bool state)
237 {
238 declaration_->SetAutoFocusState(state);
239 }
240
SetRadiusState(bool state)241 void ButtonComponent::SetRadiusState(bool state)
242 {
243 declaration_->SetRadiusState(state);
244 }
245
SetCatchMode(bool catchMode)246 void ButtonComponent::SetCatchMode(bool catchMode)
247 {
248 isCatchMode_ = catchMode;
249 }
250
SetMinWidth(const Dimension & width)251 void ButtonComponent::SetMinWidth(const Dimension& width)
252 {
253 declaration_->SetMinWidth(width);
254 }
255
SetRectRadius(const Dimension & radius)256 void ButtonComponent::SetRectRadius(const Dimension& radius)
257 {
258 declaration_->SetRectRadius(radius);
259 }
260
SetProgressDiameter(const Dimension & diameter)261 void ButtonComponent::SetProgressDiameter(const Dimension& diameter)
262 {
263 declaration_->SetProgressDiameter(diameter);
264 }
265
SetBackgroundColor(const Color & color)266 void ButtonComponent::SetBackgroundColor(const Color& color)
267 {
268 declaration_->SetBackgroundColor(color);
269
270 }
271
SetClickedColor(const Color & color)272 void ButtonComponent::SetClickedColor(const Color& color)
273 {
274 declaration_->SetClickedColor(color);
275 }
276
SetDisabledColor(const Color & color)277 void ButtonComponent::SetDisabledColor(const Color& color)
278 {
279 declaration_->SetDisabledColor(color);
280 }
281
SetFocusColor(const Color & color)282 void ButtonComponent::SetFocusColor(const Color& color)
283 {
284 declaration_->SetFocusColor(color);
285 }
286
SetHoverColor(const Color & color)287 void ButtonComponent::SetHoverColor(const Color& color)
288 {
289 declaration_->SetHoverColor(color);
290 }
291
SetProgressColor(const Color & color)292 void ButtonComponent::SetProgressColor(const Color& color)
293 {
294 declaration_->SetProgressColor(color);
295 }
296
SetProgressFocusColor(const Color & color)297 void ButtonComponent::SetProgressFocusColor(const Color& color)
298 {
299 declaration_->SetProgressFocusColor(color);
300 }
301
SetFocusAnimationColor(const Color & color)302 void ButtonComponent::SetFocusAnimationColor(const Color& color)
303 {
304 declaration_->SetFocusAnimationColor(color);
305 }
306
SetBorderEdge(const BorderEdge & borderEdge)307 void ButtonComponent::SetBorderEdge(const BorderEdge& borderEdge)
308 {
309 declaration_->SetBorderEdge(borderEdge);
310 }
311
SetClickedEventId(const EventMarker & eventId)312 void ButtonComponent::SetClickedEventId(const EventMarker& eventId)
313 {
314 declaration_->SetClickedEventId(eventId);
315 }
316
SetKeyEnterEventId(const EventMarker & eventId)317 void ButtonComponent::SetKeyEnterEventId(const EventMarker& eventId)
318 {
319 keyEnterId_ = eventId;
320 }
321
SetRemoteMessageEventId(const EventMarker & eventId)322 void ButtonComponent::SetRemoteMessageEventId(const EventMarker& eventId)
323 {
324 declaration_->SetRemoteMessageEventId(eventId);
325 }
326
SetClickFunction(std::function<void ()> && clickCallback)327 void ButtonComponent::SetClickFunction(std::function<void()>&& clickCallback)
328 {
329 declaration_->SetClickedFunction(std::move(clickCallback));
330 }
331
SetDeclaration(const RefPtr<ButtonDeclaration> & declaration)332 void ButtonComponent::SetDeclaration(const RefPtr<ButtonDeclaration>& declaration)
333 {
334 if (declaration) {
335 declaration_ = declaration;
336 }
337 }
338
Compare(const RefPtr<Component> & component) const339 uint32_t ButtonComponent::Compare(const RefPtr<Component>& component) const
340 {
341 auto button = AceType::DynamicCast<ButtonComponent>(component);
342 if (!button) {
343 return static_cast<uint32_t>(UpdateRenderType::LAYOUT);
344 }
345 uint32_t updateType = static_cast<uint32_t>(UpdateRenderType::NONE);
346 updateType |= static_cast<uint32_t>(button->GetWidth() == width_ ? UpdateRenderType::NONE :
347 UpdateRenderType::LAYOUT);
348 updateType |= static_cast<uint32_t>(button->GetHeight() == height_ ? UpdateRenderType::NONE :
349 UpdateRenderType::LAYOUT);
350 updateType |= static_cast<uint32_t>(button->GetMinWidth() == declaration_->GetMinWidth() ? UpdateRenderType::NONE :
351 UpdateRenderType::LAYOUT);
352 updateType |= static_cast<uint32_t>(button->GetType() == type_ ? UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
353 updateType |= static_cast<uint32_t>(button->GetRadiusState() == declaration_->GetRadiusState() ?
354 UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
355 updateType |= static_cast<uint32_t>(button->GetStateEffect() == stateEffect_ ?
356 UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
357 if (updateType == static_cast<uint32_t>(UpdateRenderType::LAYOUT)) {
358 return updateType;
359 }
360 updateType |= static_cast<uint32_t>(button->GetBackgroundColor() == declaration_->GetBackgroundColor() ?
361 UpdateRenderType::NONE : UpdateRenderType::PAINT);
362 updateType |= static_cast<uint32_t>(button->GetClickedColor() == declaration_->GetClickedColor() ?
363 UpdateRenderType::NONE : UpdateRenderType::PAINT);
364 updateType |= static_cast<uint32_t>(button->GetDisabledColor() == declaration_->GetDisabledColor() ?
365 UpdateRenderType::NONE : UpdateRenderType::PAINT);
366 updateType |= static_cast<uint32_t>(button->GetFocusColor() == declaration_->GetFocusColor() ?
367 UpdateRenderType::NONE : UpdateRenderType::PAINT);
368 updateType |= static_cast<uint32_t>(button->GetHoverColor() == declaration_->GetHoverColor() ?
369 UpdateRenderType::NONE : UpdateRenderType::PAINT);
370 updateType |= static_cast<uint32_t>(button->GetProgressColor() == declaration_->GetProgressColor() ?
371 UpdateRenderType::NONE : UpdateRenderType::PAINT);
372 updateType |= static_cast<uint32_t>(button->GetProgressFocusColor() == declaration_->GetProgressFocusColor() ?
373 UpdateRenderType::NONE : UpdateRenderType::PAINT);
374 updateType |= static_cast<uint32_t>(button->GetFocusAnimationColor() == declaration_->GetFocusAnimationColor() ?
375 UpdateRenderType::NONE : UpdateRenderType::PAINT);
376 return updateType;
377 }
378
379 } // namespace OHOS::Ace
380