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/counter/counter_component.h"
17
18 #include "base/log/log.h"
19 #include "base/utils/string_utils.h"
20 #include "core/components/box/box_component.h"
21 #include "core/components/button/button_component.h"
22 #include "core/components/common/properties/color.h"
23 #include "core/components/counter/counter_element.h"
24 #include "core/components/counter/render_counter.h"
25 #include "core/components/padding/padding_component.h"
26 #include "core/pipeline/base/render_node.h"
27
28 namespace OHOS::Ace {
29 namespace {
30
31 constexpr uint32_t CLICKED_BLEND_COLOR = 0x19000000;
32 constexpr char COUNTER_BUTTON_ADD[] = "+";
33 constexpr char COUNTER_BUTTON_LESS[] = "-";
34 constexpr double COUNTER_BORDER_WIDTH = 1.0;
35 constexpr double COUNTER_RADIUS_DELTA = 2.0;
36 constexpr int32_t COUNTER_ITEM_SIZE_DIFF = 2;
37 constexpr double STATE_FAIELD_OPACITY = 0.3;
38
39 } // namespace
40
CreateRenderNode()41 RefPtr<RenderNode> CounterComponent::CreateRenderNode()
42 {
43 return RenderCounter::Create();
44 }
45
CreateElement()46 RefPtr<Element> CounterComponent::CreateElement()
47 {
48 return AceType::MakeRefPtr<CounterElement>();
49 }
50
BuildChild(const RefPtr<ThemeManager> & themeManager)51 RefPtr<Component> CounterComponent::BuildChild(const RefPtr<ThemeManager>& themeManager)
52 {
53 if (!themeManager) {
54 return nullptr;
55 }
56
57 counterTheme_ = AceType::DynamicCast<CounterTheme>(themeManager->GetTheme(CounterTheme::TypeId()));
58 if (!counterTheme_) {
59 return nullptr;
60 }
61
62 // build up counter_component.
63 RefPtr<BoxComponent> counterBox = AceType::MakeRefPtr<BoxComponent>();
64 auto backDecoration = AceType::MakeRefPtr<Decoration>();
65 BorderEdge edge(Color::GRAY, Dimension(COUNTER_BORDER_WIDTH, DimensionUnit::VP), BorderStyle::NONE);
66 Border border(edge);
67 border.SetBorderRadius(Radius(GetControlRadius()));
68 backDecoration->SetBorder(border);
69 counterBox->SetBackDecoration(backDecoration);
70 counterBox->SetHeight(GetHeight());
71 counterBox->SetWidth(GetWidth());
72 std::list<RefPtr<Component>> rowChildren;
73 RefPtr<RowComponent> counterRow =
74 AceType::MakeRefPtr<RowComponent>(FlexAlign::FLEX_START, FlexAlign::CENTER, rowChildren);
75 counterRow->SetMainAxisSize(MainAxisSize::MIN);
76 BuildControl(counterTheme_, counterRow, decreButtonComponent_, std::string(COUNTER_BUTTON_LESS), true);
77 counterRow->AppendChild(BuildDivider());
78 RefPtr<BoxComponent> contentBox = AceType::MakeRefPtr<BoxComponent>();
79 BuildContent(counterTheme_, counterRow, contentBox);
80 counterRow->AppendChild(BuildDivider());
81 BuildControl(counterTheme_, counterRow, increButtonComponent_, std::string(COUNTER_BUTTON_ADD), false);
82 counterBox->SetChild(counterRow);
83
84 displayComponent_ = AceType::MakeRefPtr<DisplayComponent>(counterBox);
85 if (propState_ == false) {
86 displayComponent_->SetOpacity(STATE_FAIELD_OPACITY);
87 }
88
89 return displayComponent_;
90 }
91
BuildDivider()92 RefPtr<DividerComponent> CounterComponent::BuildDivider()
93 {
94 auto divider = AceType::MakeRefPtr<DividerComponent>();
95 divider->SetStrokeWidth(Dimension(COUNTER_BORDER_WIDTH, DimensionUnit::VP));
96 divider->SetVertical(true);
97 divider->SetDividerColor(Color::GRAY);
98 return divider;
99 }
100
BuildControl(const RefPtr<CounterTheme> & counterTheme,const RefPtr<RowComponent> & row,RefPtr<ButtonComponent> & button,std::string content,bool isLeft)101 void CounterComponent::BuildControl(const RefPtr<CounterTheme>& counterTheme, const RefPtr<RowComponent>& row,
102 RefPtr<ButtonComponent>& button, std::string content, bool isLeft)
103 {
104 std::list<RefPtr<Component>> buttonChildren;
105 auto textComponent = AceType::MakeRefPtr<TextComponent>(content);
106 textComponent->SetTextStyle(counterTheme->GetContentTextStyle());
107 buttonChildren.emplace_back(textComponent);
108 button = AceType::MakeRefPtr<ButtonComponent>(buttonChildren);
109 button->SetHeight(Dimension(GetHeight().Value() - COUNTER_RADIUS_DELTA * COUNTER_BORDER_WIDTH, GetHeight().Unit()));
110 button->SetWidth(
111 Dimension(GetControlWidth().Value() - COUNTER_RADIUS_DELTA * COUNTER_BORDER_WIDTH, GetControlWidth().Unit()));
112 button->SetBackgroundColor(propBackgroundColor_);
113 button->SetClickedColor(Color(CLICKED_BLEND_COLOR));
114 button->SetType(ButtonType::CUSTOM);
115
116 // set up radius and click function for button.
117 if (isLeft) {
118 std::array<Radius, 4> radii = {
119 Radius(Dimension(GetControlRadius().Value() - COUNTER_BORDER_WIDTH, GetControlRadius().Unit())),
120 Radius(0.0_vp),
121 Radius(0.0_vp),
122 Radius(Dimension(GetControlRadius().Value() - COUNTER_BORDER_WIDTH, GetControlRadius().Unit()))
123 };
124 button->SetRectRadii(radii);
125 if (propState_) {
126 button->SetClickFunction([weak = AceType::WeakClaim(this)] {
127 auto spThis = weak.Upgrade();
128 if (spThis) {
129 V2::ResumeEventCallback(spThis, &CounterComponent::GetOnDec);
130 }
131 });
132 } else {
133 button->SetBackgroundColor(Color::GRAY);
134 }
135 } else {
136 std::array<Radius, 4> radii = {
137 Radius(0.0_vp),
138 Radius(Dimension(GetControlRadius().Value() - COUNTER_BORDER_WIDTH, GetControlRadius().Unit())),
139 Radius(Dimension(GetControlRadius().Value() - COUNTER_BORDER_WIDTH, GetControlRadius().Unit())),
140 Radius(0.0_vp)
141 };
142 button->SetRectRadii(radii);
143 if (propState_) {
144 button->SetClickFunction([weak = AceType::WeakClaim(this)] {
145 auto spThis = weak.Upgrade();
146 if (spThis) {
147 V2::ResumeEventCallback(spThis, &CounterComponent::GetOnInc);
148 }
149 });
150 } else {
151 button->SetBackgroundColor(Color::GRAY);
152 }
153 }
154
155 row->AppendChild(button);
156 }
157
BuildContent(const RefPtr<CounterTheme> & counterTheme,const RefPtr<RowComponent> & row,RefPtr<BoxComponent> & box)158 void CounterComponent::BuildContent(
159 const RefPtr<CounterTheme>& counterTheme, const RefPtr<RowComponent>& row, RefPtr<BoxComponent>& box)
160 {
161 box->SetAlignment(Alignment::CENTER);
162 if (!GetChildren().empty()) {
163 box->SetChild(GetChildren().front());
164 }
165
166 if (GreatNotEqual(GetHeight().Value() - COUNTER_RADIUS_DELTA * COUNTER_BORDER_WIDTH, 0.0)) {
167 box->SetHeight(
168 Dimension(GetHeight().Value() - COUNTER_RADIUS_DELTA * COUNTER_BORDER_WIDTH, GetHeight().Unit()));
169 } else {
170 box->SetHeight(Dimension(0.0, DimensionUnit::VP));
171 }
172 if (GreatNotEqual(GetWidth().Value() - COUNTER_ITEM_SIZE_DIFF * GetControlWidth().Value(), 0.0)) {
173 box->SetWidth(
174 Dimension(GetWidth().Value() - COUNTER_ITEM_SIZE_DIFF * GetControlWidth().Value(), GetWidth().Unit()));
175 } else {
176 box->SetWidth(Dimension(0.0, DimensionUnit::VP));
177 }
178
179 box->SetOverflow(Overflow::FORCE_CLIP);
180 auto backDecoration = AceType::MakeRefPtr<Decoration>();
181 backDecoration->SetBackgroundColor(propBackgroundColor_);
182 box->SetBackDecoration(backDecoration);
183 row->AppendChild(box);
184 }
185
186 } // namespace OHOS::Ace
187