1 /*
2 * Copyright (c) 2025 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 "ui/view/components/tabs/tabs.h"
17
18 #include "interfaces/inner_api/ace_kit/src/view/frame_node_impl.h"
19 #include "ui/base/ace_type.h"
20 #include "ui/view_stack/view_stack_processor.h"
21
22 #include "core/components_ng/pattern/tabs/tabs_model.h"
23 #include "core/components_ng/pattern/tabs/tabs_model_ng.h"
24 #include "core/components_ng/pattern/tabs/tabs_node.h"
25 #include "core/components_ng/pattern/tabs/tabs_pattern.h"
26
27 namespace OHOS::Ace::Kit {
28
Tabs()29 Tabs::Tabs()
30 {
31 int32_t nodeId = Ace::Kit::ViewStackProcessor::ClaimNodeId();
32 auto aceNode = NG::TabsModelNG::CreateFrameNode(nodeId);
33 node_ = AceType::MakeRefPtr<FrameNodeImpl>(aceNode);
34 }
35
Tabs(RefPtr<FrameNode> & node)36 Tabs::Tabs(RefPtr<FrameNode>& node)
37 {
38 node_ = node;
39 }
40
41 Tabs::~Tabs() = default;
42
Create()43 RefPtr<Tabs> Tabs::Create()
44 {
45 return Referenced::MakeRefPtr<Tabs>();
46 }
47
Create(RefPtr<FrameNode> & node)48 RefPtr<Tabs> Tabs::Create(RefPtr<FrameNode>& node)
49 {
50 return Referenced::MakeRefPtr<Tabs>(node);
51 }
52
GetTabsNode(const RefPtr<FrameNode> & node)53 RefPtr<NG::TabsNode> GetTabsNode(const RefPtr<FrameNode>& node)
54 {
55 auto frameNodeImpl = AceType::DynamicCast<FrameNodeImpl>(node);
56 CHECK_NULL_RETURN(frameNodeImpl, nullptr);
57 auto aceFrameNode = frameNodeImpl->GetAceNode();
58 CHECK_NULL_RETURN(aceFrameNode, nullptr);
59 return AceType::DynamicCast<NG::TabsNode>(aceFrameNode);
60 }
61
UpdateDividerOpacity(const double opacity)62 void Tabs::UpdateDividerOpacity(const double opacity)
63 {
64 auto tabsNode = GetTabsNode(node_);
65 CHECK_NULL_VOID(tabsNode);
66 auto divider = AceType::DynamicCast<NG::FrameNode>(tabsNode->GetDivider());
67 CHECK_NULL_VOID(divider);
68 NG::ViewAbstract::SetOpacity(Referenced::RawPtr(divider), opacity);
69 }
70
UpdateTabBarBrightness(const BrightnessOption & brightnessOption)71 void Tabs::UpdateTabBarBrightness(const BrightnessOption& brightnessOption)
72 {
73 auto tabsNode = GetTabsNode(node_);
74 CHECK_NULL_VOID(tabsNode);
75 auto tabBarNode = AceType::DynamicCast<NG::FrameNode>(tabsNode->GetTabBar());
76 CHECK_NULL_VOID(tabBarNode);
77 NG::ViewAbstract::SetBgDynamicBrightness(Referenced::RawPtr(tabBarNode), brightnessOption);
78 }
79
UpdateEffectNodeBrightness(const BrightnessOption & brightnessOption)80 void Tabs::UpdateEffectNodeBrightness(const BrightnessOption& brightnessOption)
81 {
82 auto tabsNode = GetTabsNode(node_);
83 CHECK_NULL_VOID(tabsNode);
84 auto effectNode = AceType::DynamicCast<NG::FrameNode>(tabsNode->GetEffectNode());
85 CHECK_NULL_VOID(effectNode);
86 NG::ViewAbstract::SetBgDynamicBrightness(Referenced::RawPtr(effectNode), brightnessOption);
87 }
88
GetTabBarFrameRect(const RefPtr<FrameNode> & node)89 auto GetTabBarFrameRect(const RefPtr<FrameNode>& node)
90 {
91 auto tabsNode = GetTabsNode(node);
92 CHECK_NULL_RETURN(tabsNode, NG::RectF {});
93 auto tabBarNode = AceType::DynamicCast<NG::FrameNode>(tabsNode->GetTabBar());
94 CHECK_NULL_RETURN(tabBarNode, NG::RectF {});
95 auto geometryNode = tabBarNode->GetGeometryNode();
96 CHECK_NULL_RETURN(geometryNode, NG::RectF {});
97 return geometryNode->GetFrameRect();
98 }
99
SetBarBackgroundBlurStyle(const BlurStyleOption & styleOption)100 void Tabs::SetBarBackgroundBlurStyle(const BlurStyleOption& styleOption)
101 {
102 auto tabsNode = GetTabsNode(node_);
103 CHECK_NULL_VOID(tabsNode);
104 NG::TabsModelNG::SetBarBackgroundBlurStyle(Referenced::RawPtr(tabsNode), styleOption);
105 }
106
SetBarBackgroundColor(const Color & backgroundColor)107 void Tabs::SetBarBackgroundColor(const Color& backgroundColor)
108 {
109 auto tabsNode = GetTabsNode(node_);
110 CHECK_NULL_VOID(tabsNode);
111 NG::TabsModelNG::SetBarBackgroundColor(Referenced::RawPtr(tabsNode), backgroundColor);
112 }
113
SetBarBackgroundEffect(const EffectOption & effectOption)114 void Tabs::SetBarBackgroundEffect(const EffectOption& effectOption)
115 {
116 auto tabsNode = GetTabsNode(node_);
117 CHECK_NULL_VOID(tabsNode);
118 NG::TabsModelNG::SetBarBackgroundEffect(Referenced::RawPtr(tabsNode), effectOption);
119 }
120
GetTabBarTop()121 double Tabs::GetTabBarTop()
122 {
123 const auto tabBarRect = GetTabBarFrameRect(node_);
124 return tabBarRect.Top();
125 }
126
GetTabBarBottom()127 double Tabs::GetTabBarBottom()
128 {
129 const auto tabBarRect = GetTabBarFrameRect(node_);
130 return tabBarRect.Bottom();
131 }
132
SetTabBarWidth(const Dimension & tabBarWidth)133 void Tabs::SetTabBarWidth(const Dimension& tabBarWidth)
134 {
135 auto tabsNode = GetTabsNode(node_);
136 CHECK_NULL_VOID(tabsNode);
137 NG::TabsModelNG::SetTabBarWidth(Referenced::RawPtr(tabsNode), tabBarWidth);
138 }
139
SetTabBarHeight(const Dimension & tabBarHeight)140 void Tabs::SetTabBarHeight(const Dimension& tabBarHeight)
141 {
142 auto tabsNode = GetTabsNode(node_);
143 CHECK_NULL_VOID(tabsNode);
144 NG::TabsModelNG::SetTabBarHeight(Referenced::RawPtr(tabsNode), tabBarHeight);
145 }
146
SetDivider(const TabsItemDivider & divider)147 void Tabs::SetDivider(const TabsItemDivider& divider)
148 {
149 auto tabsNode = GetTabsNode(node_);
150 CHECK_NULL_VOID(tabsNode);
151
152 Ace::TabsItemDivider aceDivider;
153 aceDivider.strokeWidth = divider.strokeWidth;
154 aceDivider.color = divider.color;
155 aceDivider.startMargin = divider.startMargin;
156 aceDivider.endMargin = divider.endMargin;
157 aceDivider.isNull = divider.isNull;
158 NG::TabsModelNG::SetDivider(Referenced::RawPtr(tabsNode), aceDivider);
159 }
160
SetEffectNodeOption(const TabsEffectNodeOption & option)161 void Tabs::SetEffectNodeOption(const TabsEffectNodeOption& option)
162 {
163 auto tabsNode = GetTabsNode(node_);
164 CHECK_NULL_VOID(tabsNode);
165 NG::TabsModelNG::SetEffectNodeOption(Referenced::RawPtr(tabsNode), option);
166 }
167
SetTabBarMode(const TabBarMode & barMode)168 void Tabs::SetTabBarMode(const TabBarMode& barMode)
169 {
170 auto tabsNode = GetTabsNode(node_);
171 CHECK_NULL_VOID(tabsNode);
172 NG::TabsModelNG::SetTabBarMode(Referenced::RawPtr(tabsNode), barMode);
173 }
174
SetScrollableBarModeOptions(const ScrollableBarModeOptions & option)175 void Tabs::SetScrollableBarModeOptions(const ScrollableBarModeOptions& option)
176 {
177 auto tabsNode = GetTabsNode(node_);
178 CHECK_NULL_VOID(tabsNode);
179 NG::TabsModelNG::SetScrollableBarModeOptions(Referenced::RawPtr(tabsNode), option);
180 }
181
GetTabBar()182 RefPtr<FrameNode> Tabs::GetTabBar()
183 {
184 auto tabsNode = GetTabsNode(node_);
185 CHECK_NULL_RETURN(tabsNode, nullptr);
186 auto tabBarNode = AceType::DynamicCast<NG::FrameNode>(tabsNode->GetTabBar());
187 CHECK_NULL_RETURN(tabBarNode, nullptr);
188 return AceType::MakeRefPtr<FrameNodeImpl>(Referenced::RawPtr(tabBarNode));
189 }
190
GetEffectNode()191 RefPtr<FrameNode> Tabs::GetEffectNode()
192 {
193 auto tabsNode = GetTabsNode(node_);
194 CHECK_NULL_RETURN(tabsNode, nullptr);
195 auto effectNode = AceType::DynamicCast<NG::FrameNode>(tabsNode->GetEffectNode());
196 CHECK_NULL_RETURN(effectNode, nullptr);
197 return AceType::MakeRefPtr<FrameNodeImpl>(Referenced::RawPtr(effectNode));
198 }
199
SetOnChange(OnChangeEvent onChangeEvent)200 void Tabs::SetOnChange(OnChangeEvent onChangeEvent)
201 {
202 auto frameNodeImpl = AceType::DynamicCast<FrameNodeImpl>(node_);
203 CHECK_NULL_VOID(frameNodeImpl);
204 auto aceFrameNode = frameNodeImpl->GetAceNode();
205 CHECK_NULL_VOID(aceFrameNode);
206 auto tabPattern = aceFrameNode->GetPattern<NG::TabsPattern>();
207 CHECK_NULL_VOID(tabPattern);
208 auto onChange = [onChangeEvent](const BaseEventInfo* info) {
209 const auto* tabsInfo = TypeInfoHelper::DynamicCast<TabContentChangeEvent>(info);
210 if (!tabsInfo) {
211 return;
212 }
213 onChangeEvent(tabsInfo->GetIndex());
214 };
215 tabPattern->SetOnChangeEvent(std::move(onChange));
216 }
217
218 } // namespace OHOS::Ace::Kit
219