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/tab_bar/tab_bar_item_component.h"
17
18 #include "core/components/flex/flex_component.h"
19 #include "core/components/padding/padding_component.h"
20 #include "core/components/tab_bar/render_tab_bar_item.h"
21 #include "core/components/tab_bar/tab_bar_item_element.h"
22
23 namespace OHOS::Ace {
24
25 constexpr Dimension DEFAULT_SINGLE_TEXT_FONT_SIZE = 16.0_fp;
26 constexpr Dimension DEFAULT_SMALL_TEXT_FONT_SIZE = 10.0_fp;
27 constexpr Dimension DEFAULT_SMALL_IMAGE_WIDTH = 24.0_vp;
28 constexpr Dimension DEFAULT_SMALL_IMAGE_HEIGHT = 26.0_vp;
29
TabBarItemComponent(const RefPtr<Component> & child)30 TabBarItemComponent::TabBarItemComponent(const RefPtr<Component>& child)
31 {
32 SetChild(child);
33 }
34
TabBarItemComponent(const std::string & text,const RefPtr<Component> & imageComponent)35 TabBarItemComponent::TabBarItemComponent(const std::string& text, const RefPtr<Component>& imageComponent)
36 {
37 std::list<RefPtr<Component>> children;
38 RefPtr<ImageComponent> icon = AceType::DynamicCast<ImageComponent>(imageComponent);
39 if (icon) {
40 icon_ = icon;
41 children.push_back(icon_);
42 }
43 if (!text.empty()) {
44 text_ = AceType::MakeRefPtr<TextComponent>(text);
45 TextStyle textStyle;
46 textStyle.SetTextAlign(TextAlign::CENTER);
47 textStyle.SetMaxLines(1);
48 textStyle.SetTextOverflow(TextOverflow::CLIP);
49 text_->SetTextStyle(textStyle);
50 children.push_back(text_);
51 }
52 if (!children.empty()) {
53 auto flex = AceType::MakeRefPtr<ColumnComponent>(FlexAlign::CENTER, FlexAlign::CENTER, children);
54 SetChild(flex);
55 } else {
56 LOGE("Create Tab err: text == null && icon == null");
57 }
58 }
59
CreateElement()60 RefPtr<Element> TabBarItemComponent::CreateElement()
61 {
62 return AceType::MakeRefPtr<TabBarItemElement>();
63 }
64
CreateRenderNode()65 RefPtr<RenderNode> TabBarItemComponent::CreateRenderNode()
66 {
67 return RenderTabBarItem::Create();
68 }
69
FindChildren(const RefPtr<Component> & component,std::stack<RefPtr<Component>> & allChildren)70 void FindChildren(const RefPtr<Component>& component, std::stack<RefPtr<Component>>& allChildren)
71 {
72 auto singleChildGroup = AceType::DynamicCast<SingleChild>(component);
73 if (singleChildGroup) {
74 allChildren.push(singleChildGroup->GetChild());
75 }
76
77 auto multiChildGroup = AceType::DynamicCast<ComponentGroup>(component);
78 if (multiChildGroup) {
79 for (const auto& item : multiChildGroup->GetChildren()) {
80 allChildren.push(item);
81 }
82 }
83 }
84
UpdateStyle(const TextStyle & textStyle,const Color & color)85 void TabBarItemComponent::UpdateStyle(const TextStyle& textStyle, const Color& color)
86 {
87 std::stack<RefPtr<Component>> allChildren;
88 allChildren.push(GetChild());
89
90 while (!allChildren.empty()) {
91 auto component = allChildren.top();
92 allChildren.pop();
93 auto text = AceType::DynamicCast<TextComponent>(component);
94 auto image = AceType::DynamicCast<ImageComponent>(component);
95 if (text) {
96 text->SetTextStyle(textStyle);
97 } else if (image) {
98 image->SetImageFill(color);
99 } else {
100 FindChildren(component, allChildren);
101 }
102 }
103 }
104
BuildWithTextIcon(const std::string & textStr,const std::string & iconUri)105 RefPtr<Component> TabBarItemComponent::BuildWithTextIcon(const std::string& textStr, const std::string& iconUri)
106 {
107 if (!textStr.empty() && !iconUri.empty()) {
108 auto imageComponent = AceType::MakeRefPtr<ImageComponent>(iconUri);
109 auto box = AceType::MakeRefPtr<BoxComponent>();
110 auto padding = AceType::MakeRefPtr<PaddingComponent>();
111 padding->SetPadding(Edge(0, 0, 0, 2, DimensionUnit::VP));
112 padding->SetChild(imageComponent);
113 box->SetChild(padding);
114 box->SetWidth(DEFAULT_SMALL_IMAGE_WIDTH);
115 box->SetHeight(DEFAULT_SMALL_IMAGE_HEIGHT);
116 auto textComponent = AceType::MakeRefPtr<TextComponent>(textStr);
117 auto textStyle = textComponent->GetTextStyle();
118 textStyle.SetFontSize(DEFAULT_SMALL_TEXT_FONT_SIZE);
119 textStyle.SetMaxLines(1);
120 textStyle.SetTextOverflow(TextOverflow::ELLIPSIS);
121 textComponent->SetTextStyle(textStyle);
122 std::list<RefPtr<Component>> children;
123 children.emplace_back(box);
124 children.emplace_back(textComponent);
125 auto columnComponent = AceType::MakeRefPtr<ColumnComponent>(FlexAlign::FLEX_START, FlexAlign::CENTER, children);
126 columnComponent->SetMainAxisSize(MainAxisSize::MIN);
127 return columnComponent;
128 }
129
130 if (!textStr.empty()) {
131 auto text = AceType::MakeRefPtr<TextComponent>(textStr);
132 auto textStyle = text->GetTextStyle();
133 textStyle.SetFontSize(DEFAULT_SINGLE_TEXT_FONT_SIZE);
134 textStyle.SetMaxLines(1);
135 textStyle.SetTextOverflow(TextOverflow::ELLIPSIS);
136 text->SetTextStyle(textStyle);
137 text->SetAutoMaxLines(false);
138 return text;
139 }
140
141 return nullptr;
142 }
143
144 } // namespace OHOS::Ace
145