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/piece/piece_component.h"
17 
18 #include "core/components/display/render_display.h"
19 #include "core/components/flex/flex_item_component.h"
20 #include "core/components/gesture_listener/gesture_listener_component.h"
21 #include "core/components/image/image_component.h"
22 #include "core/components/padding/padding_component.h"
23 #include "core/components/piece/render_piece.h"
24 
25 namespace OHOS::Ace {
26 
PieceComponent()27 PieceComponent::PieceComponent()
28 {
29     if (!declaration_) {
30         declaration_ = AceType::MakeRefPtr<PieceDeclaration>();
31         declaration_->Init();
32     }
33 }
InitializeStyle(RefPtr<PieceTheme> & theme)34 void PieceComponent::InitializeStyle(RefPtr<PieceTheme>& theme)
35 {
36     declaration_->InitializeStyle(theme);
37 }
38 
CreateElement()39 RefPtr<Element> PieceComponent::CreateElement()
40 {
41     return AceType::MakeRefPtr<PieceElement>();
42 }
43 
CreateRenderNode()44 RefPtr<RenderNode> PieceComponent::CreateRenderNode()
45 {
46     return RenderPiece::Create();
47 }
48 
BuildChild()49 RefPtr<Component> PieceComponent::BuildChild()
50 {
51     if (GetContent().empty()) {
52         return nullptr;
53     }
54     std::list<RefPtr<Component>> rowChildren;
55     auto row = AceType::MakeRefPtr<RowComponent>(FlexAlign::FLEX_START, FlexAlign::CENTER, rowChildren);
56     row->SetMainAxisSize(MainAxisSize::MIN);
57     if (iconPosition_ == IconPosition::Start) {
58         SetImage(row);
59         SetText(row);
60     } else {
61         SetText(row);
62         SetImage(row);
63     }
64     row->SetTextDirection(GetTextDirection());
65     return row;
66 }
67 
GetContent() const68 const std::string& PieceComponent::GetContent() const
69 {
70     return declaration_->GetContent();
71 }
SetContent(const std::string & content)72 void PieceComponent::SetContent(const std::string& content)
73 {
74     declaration_->SetContent(content);
75 }
76 
GetIcon() const77 const std::string& PieceComponent::GetIcon() const
78 {
79     return declaration_->GetIcon();
80 }
SetIcon(const std::string & icon)81 void PieceComponent::SetIcon(const std::string& icon)
82 {
83     declaration_->SetIcon(icon);
84 }
85 
GetTextStyle() const86 const TextStyle& PieceComponent::GetTextStyle() const
87 {
88     return declaration_->GetTextStyle();
89 }
SetTextStyle(const TextStyle & textStyle)90 void PieceComponent::SetTextStyle(const TextStyle& textStyle)
91 {
92     declaration_->SetTextStyle(textStyle);
93 }
94 
GetInterval() const95 const Dimension& PieceComponent::GetInterval() const
96 {
97     return declaration_->GetInterval();
98 }
SetInterval(const Dimension & interval)99 void PieceComponent::SetInterval(const Dimension& interval)
100 {
101     declaration_->SetInterval(interval);
102 }
103 
GetIconResource() const104 InternalResource::ResourceId PieceComponent::GetIconResource() const
105 {
106     return declaration_->GetIconResource();
107 }
SetIconResource(InternalResource::ResourceId iconResource)108 void PieceComponent::SetIconResource(InternalResource::ResourceId iconResource)
109 {
110     declaration_->SetIconResource(iconResource);
111 }
112 
GetIconSize() const113 const Dimension& PieceComponent::GetIconSize() const
114 {
115     return declaration_->GetIconSize();
116 }
SetIconSize(const Dimension & iconSize)117 void PieceComponent::SetIconSize(const Dimension& iconSize)
118 {
119     declaration_->SetIconSize(iconSize);
120 }
121 
GetOnDelete() const122 const EventMarker& PieceComponent::GetOnDelete() const
123 {
124     return declaration_->GetOnDelete();
125 }
SetOnDelete(const EventMarker & onDelete)126 void PieceComponent::SetOnDelete(const EventMarker& onDelete)
127 {
128     declaration_->SetOnDelete(onDelete);
129 }
130 
ShowDelete() const131 bool PieceComponent::ShowDelete() const
132 {
133     return declaration_->ShowDelete();
134 }
SetShowDelete(bool showDelete)135 void PieceComponent::SetShowDelete(bool showDelete)
136 {
137     declaration_->SetShowDelete(showDelete);
138 }
139 
GetMargin() const140 const Edge& PieceComponent::GetMargin() const
141 {
142     return declaration_->GetMargin();
143 }
SetMargin(const Edge & margin)144 void PieceComponent::SetMargin(const Edge& margin)
145 {
146     declaration_->SetMargin(margin);
147 }
148 
GetBorder() const149 const Border& PieceComponent::GetBorder() const
150 {
151     return declaration_->GetBorder();
152 }
SetBorder(const Border & border)153 void PieceComponent::SetBorder(const Border& border)
154 {
155     declaration_->SetBorder(border);
156 }
157 
GetHoverColor() const158 const Color& PieceComponent::GetHoverColor() const
159 {
160     return declaration_->GetHoverColor();
161 }
SetHoverColor(const Color & hoverColor)162 void PieceComponent::SetHoverColor(const Color& hoverColor)
163 {
164     declaration_->SetHoverColor(hoverColor);
165 }
166 
GetBackGroundColor() const167 const Color& PieceComponent::GetBackGroundColor() const
168 {
169     return declaration_->GetBackGroundColor();
170 }
SetBackGroundColor(const Color & backGroundColor)171 void PieceComponent::SetBackGroundColor(const Color& backGroundColor)
172 {
173     declaration_->SetBackGroundColor(backGroundColor);
174 }
175 
SetDeclaration(const RefPtr<PieceDeclaration> & declaration)176 void PieceComponent::SetDeclaration(const RefPtr<PieceDeclaration>& declaration)
177 {
178     if (declaration) {
179         declaration_ = declaration;
180     }
181 }
182 
SetImage(RefPtr<RowComponent> & row)183 void PieceComponent::SetImage(RefPtr<RowComponent>& row)
184 {
185     RefPtr<ImageComponent> image =
186         GetIcon().empty() ? MakeRefPtr<ImageComponent>(GetIconResource()) : MakeRefPtr<ImageComponent>(GetIcon());
187     image->SetWidth(GetIconSize());
188     image->SetHeight(GetIconSize());
189     image->SetImageFill(declaration_->GetImageFill());
190     auto gestureListener = MakeRefPtr<GestureListenerComponent>();
191     gestureListener->SetOnClickId(GetOnDelete());
192     gestureListener->SetChild(image);
193     auto padding = MakeRefPtr<PaddingComponent>();
194     if (GetTextDirection() == TextDirection::RTL || GetIconPosition() == IconPosition::Start) {
195         padding->SetPadding(Edge(0.0_vp, 0.0_vp, GetInterval(), 0.0_vp));
196     } else {
197         padding->SetPadding(Edge(GetInterval(), 0.0_vp, 0.0_vp, 0.0_vp));
198     }
199     padding->SetChild(gestureListener);
200     auto iconFlex = MakeRefPtr<FlexItemComponent>(0, 0, 0.0, padding);
201 
202     auto displayComponent = MakeRefPtr<DisplayComponent>();
203     displayComponent->SetChild(iconFlex);
204     if (ShowDelete()) {
205         displayComponent->SetVisible(VisibleType::VISIBLE);
206     } else {
207         displayComponent->SetVisible(VisibleType::GONE);
208     }
209     row->AppendChild(displayComponent);
210 }
211 
SetText(RefPtr<RowComponent> & row)212 void PieceComponent::SetText(RefPtr<RowComponent>& row)
213 {
214     auto text = MakeRefPtr<TextComponent>(GetContent());
215     text->SetTextStyle(GetTextStyle());
216     text->SetFocusColor(GetTextStyle().GetTextColor());
217     auto textFlex = MakeRefPtr<FlexItemComponent>(0, 1, 0.0, text);
218     row->AppendChild(textFlex);
219 }
220 
221 } // namespace OHOS::Ace