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 #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 buttonComponent->SetHasCustomChild(false);
72 auto buttonTheme = AceType::DynamicCast<ButtonTheme>(themeManager->GetTheme(ButtonTheme::TypeId()));
73 if (!buttonTheme) {
74 return buttonComponent;
75 }
76 if (useTextFocus) {
77 textComponent->SetFocusColor(textFocusColor);
78 } else {
79 textComponent->SetFocusColor(buttonTheme->GetTextFocusColor());
80 }
81 textComponent->SetTextStyle(textStyle);
82 padding->SetPadding(buttonTheme->GetPadding());
83 buttonComponent->SetHeight(buttonTheme->GetHeight());
84 buttonComponent->SetRectRadius(buttonTheme->GetHeight() / 2.0);
85 buttonComponent->SetBackgroundColor(buttonTheme->GetBgColor());
86 buttonComponent->SetClickedColor(buttonTheme->GetClickedColor());
87 buttonComponent->SetFocusColor(buttonTheme->GetBgFocusColor());
88 buttonComponent->SetFocusAnimationColor(buttonTheme->GetBgFocusColor());
89 return buttonComponent;
90 }
91
GetDisabledState() const92 bool ButtonComponent::GetDisabledState() const
93 {
94 return declaration_->GetDisabledState();
95 }
96
GetWaitingState() const97 bool ButtonComponent::GetWaitingState() const
98 {
99 return declaration_->GetWaitingState();
100 }
101
GetAutoFocusState() const102 bool ButtonComponent::GetAutoFocusState() const
103 {
104 return declaration_->GetAutoFocusState();
105 }
106
GetRadiusState() const107 bool ButtonComponent::GetRadiusState() const
108 {
109 return declaration_->GetRadiusState();
110 }
111
GetCatchMode() const112 bool ButtonComponent::GetCatchMode() const
113 {
114 return isCatchMode_;
115 }
116
GetMinWidth() const117 const Dimension& ButtonComponent::GetMinWidth() const
118 {
119 return declaration_->GetMinWidth();
120 }
121
GetRectRadius() const122 const Dimension& ButtonComponent::GetRectRadius() const
123 {
124 return declaration_->GetRectRadius();
125 }
126
GetProgressDiameter() const127 const Dimension& ButtonComponent::GetProgressDiameter() const
128 {
129 return declaration_->GetProgressDiameter();
130 }
131
GetBackgroundColor() const132 const Color& ButtonComponent::GetBackgroundColor() const
133 {
134 return declaration_->GetBackgroundColor();
135 }
136
GetClickedColor() const137 const Color& ButtonComponent::GetClickedColor() const
138 {
139 return declaration_->GetClickedColor();
140 }
141
GetDisabledColor() const142 const Color& ButtonComponent::GetDisabledColor() const
143 {
144 return declaration_->GetDisabledColor();
145 }
146
GetFocusColor() const147 const Color& ButtonComponent::GetFocusColor() const
148 {
149 return declaration_->GetFocusColor();
150 }
151
GetHoverColor() const152 const Color& ButtonComponent::GetHoverColor() const
153 {
154 return declaration_->GetHoverColor();
155 }
156
GetProgressColor() const157 const Color& ButtonComponent::GetProgressColor() const
158 {
159 return declaration_->GetProgressColor();
160 }
161
GetProgressFocusColor() const162 const Color& ButtonComponent::GetProgressFocusColor() const
163 {
164 return declaration_->GetProgressFocusColor();
165 }
166
GetFocusAnimationColor() const167 const Color& ButtonComponent::GetFocusAnimationColor() const
168 {
169 return declaration_->GetFocusAnimationColor();
170 }
171
GetBorderEdge() const172 const BorderEdge& ButtonComponent::GetBorderEdge() const
173 {
174 return declaration_->GetBorderEdge();
175 }
176
GetClickedEventId() const177 const EventMarker& ButtonComponent::GetClickedEventId() const
178 {
179 return declaration_->GetClickedEventId();
180 }
181
GetKeyEnterEventId() const182 const EventMarker& ButtonComponent::GetKeyEnterEventId() const
183 {
184 return keyEnterId_;
185 }
186
GetRemoteMessageEventId() const187 const EventMarker& ButtonComponent::GetRemoteMessageEventId() const
188 {
189 return declaration_->GetRemoteMessageEventId();
190 }
191
GetButtonController() const192 RefPtr<ButtonProgressController> ButtonComponent::GetButtonController() const
193 {
194 return declaration_->GetButtonController();
195 }
196
SetDisabledState(bool state)197 void ButtonComponent::SetDisabledState(bool state)
198 {
199 declaration_->SetDisabledState(state);
200 }
201
SetWaitingState(bool state)202 void ButtonComponent::SetWaitingState(bool state)
203 {
204 declaration_->SetWaitingState(state);
205 }
206
SetAutoFocusState(bool state)207 void ButtonComponent::SetAutoFocusState(bool state)
208 {
209 declaration_->SetAutoFocusState(state);
210 }
211
SetRadiusState(bool state)212 void ButtonComponent::SetRadiusState(bool state)
213 {
214 declaration_->SetRadiusState(state);
215 }
216
SetCatchMode(bool catchMode)217 void ButtonComponent::SetCatchMode(bool catchMode)
218 {
219 isCatchMode_ = catchMode;
220 }
221
SetMinWidth(const Dimension & width)222 void ButtonComponent::SetMinWidth(const Dimension& width)
223 {
224 declaration_->SetMinWidth(width);
225 }
226
SetRectRadius(const Dimension & radius)227 void ButtonComponent::SetRectRadius(const Dimension& radius)
228 {
229 declaration_->SetRectRadius(radius);
230 }
231
SetProgressDiameter(const Dimension & diameter)232 void ButtonComponent::SetProgressDiameter(const Dimension& diameter)
233 {
234 declaration_->SetProgressDiameter(diameter);
235 }
236
SetBackgroundColor(const Color & color)237 void ButtonComponent::SetBackgroundColor(const Color& color)
238 {
239 declaration_->SetBackgroundColor(color);
240 }
241
SetClickedColor(const Color & color)242 void ButtonComponent::SetClickedColor(const Color& color)
243 {
244 declaration_->SetClickedColor(color);
245 }
246
SetDisabledColor(const Color & color)247 void ButtonComponent::SetDisabledColor(const Color& color)
248 {
249 declaration_->SetDisabledColor(color);
250 }
251
SetFocusColor(const Color & color)252 void ButtonComponent::SetFocusColor(const Color& color)
253 {
254 declaration_->SetFocusColor(color);
255 }
256
SetHoverColor(const Color & color)257 void ButtonComponent::SetHoverColor(const Color& color)
258 {
259 declaration_->SetHoverColor(color);
260 }
261
SetProgressColor(const Color & color)262 void ButtonComponent::SetProgressColor(const Color& color)
263 {
264 declaration_->SetProgressColor(color);
265 }
266
SetProgressFocusColor(const Color & color)267 void ButtonComponent::SetProgressFocusColor(const Color& color)
268 {
269 declaration_->SetProgressFocusColor(color);
270 }
271
SetFocusAnimationColor(const Color & color)272 void ButtonComponent::SetFocusAnimationColor(const Color& color)
273 {
274 declaration_->SetFocusAnimationColor(color);
275 }
276
SetBorderEdge(const BorderEdge & borderEdge)277 void ButtonComponent::SetBorderEdge(const BorderEdge& borderEdge)
278 {
279 declaration_->SetBorderEdge(borderEdge);
280 }
281
SetClickedEventId(const EventMarker & eventId)282 void ButtonComponent::SetClickedEventId(const EventMarker& eventId)
283 {
284 declaration_->SetClickedEventId(eventId);
285 }
286
SetKeyEnterEventId(const EventMarker & eventId)287 void ButtonComponent::SetKeyEnterEventId(const EventMarker& eventId)
288 {
289 keyEnterId_ = eventId;
290 }
291
SetRemoteMessageEventId(const EventMarker & eventId)292 void ButtonComponent::SetRemoteMessageEventId(const EventMarker& eventId)
293 {
294 declaration_->SetRemoteMessageEventId(eventId);
295 }
296
SetClickFunction(std::function<void ()> && clickCallback)297 void ButtonComponent::SetClickFunction(std::function<void()>&& clickCallback)
298 {
299 declaration_->SetClickedFunction(std::move(clickCallback));
300 }
301
SetDeclaration(const RefPtr<ButtonDeclaration> & declaration)302 void ButtonComponent::SetDeclaration(const RefPtr<ButtonDeclaration>& declaration)
303 {
304 if (declaration) {
305 declaration_ = declaration;
306 }
307 }
308
ApplyTheme(const RefPtr<ButtonTheme> & theme)309 void ButtonComponent::ApplyTheme(const RefPtr<ButtonTheme>& theme)
310 {
311 height_ = theme->GetHeight();
312 SetLayoutFlag(LAYOUT_FLAG_EXTEND_TO_PARENT);
313 SetBackgroundColor(theme->GetBgColor());
314 SetFocusColor(theme->GetBgColor());
315 SetFocusAnimationColor(theme->GetBgFocusColor());
316 SetHoverColor(theme->GetHoverColor());
317 auto padding = AceType::DynamicCast<PaddingComponent>(GetChildren().front());
318 if (!padding) {
319 return;
320 }
321 padding->SetPadding(theme->GetPadding());
322 auto text = AceType::DynamicCast<TextComponent>(padding->GetChild());
323 if (!text) {
324 return;
325 }
326 auto textStyle = theme->GetTextStyle();
327 textStyle.SetAdaptTextSize(textStyle.GetFontSize(), theme->GetMinFontSize());
328 textStyle.SetTextAlign(TextAlign::CENTER);
329 textStyle.SetMaxLines(theme->GetTextMaxLines());
330 textStyle.SetTextOverflow(TextOverflow::ELLIPSIS);
331 if (SystemProperties::GetDeviceType() == DeviceType::WATCH) {
332 SetBackgroundColor(Color(WATCH_BACKGROUND_COLOR));
333 textStyle.SetTextColor(Color(WATCH_TEXT_COLOR));
334 text->SetFocusColor(Color(WATCH_TEXT_COLOR));
335 }
336 text->SetTextStyle(textStyle);
337 }
338
FitTextHeight(AnimatableDimension & height)339 void ButtonComponent::FitTextHeight(AnimatableDimension& height)
340 {
341 if (isDeclareHeight_) {
342 return;
343 }
344 auto padding = AceType::DynamicCast<PaddingComponent>(GetChildren().front());
345 if (padding == nullptr) {
346 LOGD("Padding component get failed");
347 return;
348 }
349 auto text = AceType::DynamicCast<TextComponent>(padding->GetChild());
350 if (text == nullptr) {
351 LOGW("Text component get failed");
352 return;
353 }
354 auto fontSize = text->GetTextStyle().GetFontSize();
355 if (height.Value() < fontSize.Value()) {
356 height = fontSize;
357 }
358 }
359
Compare(const RefPtr<Component> & component) const360 uint32_t ButtonComponent::Compare(const RefPtr<Component>& component) const
361 {
362 auto button = AceType::DynamicCast<ButtonComponent>(component);
363 if (!button) {
364 return static_cast<uint32_t>(UpdateRenderType::LAYOUT);
365 }
366 uint32_t updateType = static_cast<uint32_t>(UpdateRenderType::NONE);
367 updateType |= static_cast<uint32_t>(button->GetWidth() == width_ ? UpdateRenderType::NONE :
368 UpdateRenderType::LAYOUT);
369 updateType |= static_cast<uint32_t>(button->GetHeight() == height_ ? UpdateRenderType::NONE :
370 UpdateRenderType::LAYOUT);
371 updateType |= static_cast<uint32_t>(button->GetMinWidth() == declaration_->GetMinWidth() ? UpdateRenderType::NONE :
372 UpdateRenderType::LAYOUT);
373 updateType |= static_cast<uint32_t>(button->GetType() == type_ ? UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
374 updateType |= static_cast<uint32_t>(button->GetRadiusState() == declaration_->GetRadiusState() ?
375 UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
376 updateType |= static_cast<uint32_t>(button->GetStateEffect() == stateEffect_ ?
377 UpdateRenderType::NONE : UpdateRenderType::LAYOUT);
378 if (updateType == static_cast<uint32_t>(UpdateRenderType::LAYOUT)) {
379 return updateType;
380 }
381 updateType |= static_cast<uint32_t>(button->GetBackgroundColor() == declaration_->GetBackgroundColor() ?
382 UpdateRenderType::NONE : UpdateRenderType::PAINT);
383 updateType |= static_cast<uint32_t>(button->GetClickedColor() == declaration_->GetClickedColor() ?
384 UpdateRenderType::NONE : UpdateRenderType::PAINT);
385 updateType |= static_cast<uint32_t>(button->GetDisabledColor() == declaration_->GetDisabledColor() ?
386 UpdateRenderType::NONE : UpdateRenderType::PAINT);
387 updateType |= static_cast<uint32_t>(button->GetFocusColor() == declaration_->GetFocusColor() ?
388 UpdateRenderType::NONE : UpdateRenderType::PAINT);
389 updateType |= static_cast<uint32_t>(button->GetHoverColor() == declaration_->GetHoverColor() ?
390 UpdateRenderType::NONE : UpdateRenderType::PAINT);
391 updateType |= static_cast<uint32_t>(button->GetProgressColor() == declaration_->GetProgressColor() ?
392 UpdateRenderType::NONE : UpdateRenderType::PAINT);
393 updateType |= static_cast<uint32_t>(button->GetProgressFocusColor() == declaration_->GetProgressFocusColor() ?
394 UpdateRenderType::NONE : UpdateRenderType::PAINT);
395 updateType |= static_cast<uint32_t>(button->GetFocusAnimationColor() == declaration_->GetFocusAnimationColor() ?
396 UpdateRenderType::NONE : UpdateRenderType::PAINT);
397 return updateType;
398 }
399
400 } // namespace OHOS::Ace
401