2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
21 namespace Tizen.NUI.Components
24 /// TabView is a class which contains a TabBar and TabContent.
25 /// TabView adds TabButtons and Views to TabBar and TabContent in TabView by <see cref="TabView.AddTab"/>.
26 /// TabView removes TabButtons and Views from TabBar and TabContent in TabView by <see cref="TabView.RemoveTab"/>.
27 /// TabView selects a view from the TabContent according to the selected TabButton in the TabBar.
31 /// var tabView = new TabView()
33 /// WidthSpecification = LayoutParamPolicies.MatchParent,
34 /// HeightSpecification = LayoutParamPolicies.MatchParent,
37 /// var tabButton = new TabButton()
42 /// var content = new View()
44 /// BackgroundColor = Color.Red,
45 /// WidthSpecification = LayoutParamPolicies.MatchParent,
46 /// HeightSpecification = LayoutParamPolicies.MatchParent,
49 /// tabView.AddTab(tabButton, content);
51 /// var tabButton2 = new TabButton()
56 /// var content2 = new View()
58 /// BackgroundColor = Color.Green,
59 /// WidthSpecification = LayoutParamPolicies.MatchParent,
60 /// HeightSpecification = LayoutParamPolicies.MatchParent,
63 /// tabView.AddTab(tabButton2, content2);
65 /// var tabButton3 = new TabButton()
70 /// var content3 = new View()
72 /// BackgroundColor = Color.Blue,
73 /// WidthSpecification = LayoutParamPolicies.MatchParent,
74 /// HeightSpecification = LayoutParamPolicies.MatchParent,
77 /// tabView.AddTab(tabButton3, content3);
81 /// <since_tizen> 9 </since_tizen>
82 public class TabView : Control
84 private TabBar tabBar = null;
86 private TabContent content = null;
89 /// Creates a new instance of TabView.
91 /// <since_tizen> 9 </since_tizen>
94 // FIXME: Now, WidthSpecification/HeightSpecification are updated internally.
95 // When this is resolved, LinearLayout can be used.
96 Layout = new AbsoluteLayout();
97 WidthSpecification = LayoutParamPolicies.MatchParent;
98 HeightSpecification = LayoutParamPolicies.MatchParent;
106 private void InitTabBar()
113 tabBar = new TabBar();
114 tabBar.TabButtonSelected += tabButtonSelectedHandler;
119 private void InitContent()
126 content = new TabContent();
127 content.WidthSpecification = LayoutParamPolicies.MatchParent;
128 content.HeightSpecification = LayoutParamPolicies.MatchParent;
133 private void tabButtonSelectedHandler(object sender, TabButtonSelectedEventArgs args)
135 if ((content != null) && (content.ViewCount > args.Index))
137 content.Select(args.Index);
142 /// Gets TabBar of TabView.
144 /// <since_tizen> 9 </since_tizen>
154 /// Gets TabContent of TabView.
156 /// <since_tizen> 9 </since_tizen>
157 public TabContent Content
166 /// Adds a tab with tab button and content view.
168 /// <param name="tabButton">A tab button to be added.</param>
169 /// <param name="view">A content view to be added.</param>
170 /// <since_tizen> 9 </since_tizen>
171 public void AddTab(TabButton tabButton, View view)
175 TabBar.AddTabButton(tabButton);
180 Content.AddView(view);
185 /// Removes a tab at the specified index of TabView.
186 /// The indices of tabs(tab buttons and views) in TabView are basically the order of adding to TabView by <see cref="TabView.AddTab"/>.
187 /// So the index of a tab(tab button and view) in TabView can be changed whenever <see cref="TabView.AddTab"/> or <see cref="TabView.RemoveTab"/> is called.
189 /// <param name="index">The index of a tab(tab button and view) in TabView where the tab will be removed.</param>
190 /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of tabs.</exception>
191 /// <since_tizen> 9 </since_tizen>
192 public void RemoveTab(int index)
194 var tabButton = TabBar.GetTabButton(index);
195 if (tabButton != null)
197 TabBar.RemoveTabButton(tabButton);
200 var view = Content.GetView(index);
203 Content.RemoveView(view);
208 [EditorBrowsable(EditorBrowsableState.Never)]
209 public override void OnRelayout(Vector2 size, RelayoutContainer container)
211 base.OnRelayout(size, container);
217 [EditorBrowsable(EditorBrowsableState.Never)]
218 protected override void Dispose(DisposeTypes type)
225 if (type == DisposeTypes.Explicit)
229 tabBar.TabButtonSelected -= tabButtonSelectedHandler;
230 Utility.Dispose(tabBar);
235 Utility.Dispose(content);
242 private void CalculatePosition()
244 // If TabView size has not been set yet, then content size cannot be calculated.
245 if ((Size2D.Width == 0) && (Size2D.Height == 0))
252 // FIXME: Now, WidthSpecification/HeightSpecification are updated internally.
253 // When this is resolved, comparing Specification with Size is removed.
254 if ((tabBar.WidthSpecification == LayoutParamPolicies.MatchParent) || (tabBar.HeightSpecification == LayoutParamPolicies.MatchParent) ||
255 (tabBar.WidthSpecification > Size.Width) || (tabBar.HeightSpecification > Size.Height))
257 int tabBarSizeW = tabBar.Size2D.Width;
258 int tabBarSizeH = tabBar.Size2D.Height;
260 if ((tabBar.WidthSpecification == LayoutParamPolicies.MatchParent) || (tabBar.WidthSpecification > Size.Width))
262 tabBarSizeW = Size2D.Width - Padding.Start - Padding.End - tabBar.Margin.Start - tabBar.Margin.End;
265 if ((tabBar.HeightSpecification == LayoutParamPolicies.MatchParent) || (tabBar.HeightSpecification > Size.Height))
267 tabBarSizeH = Size2D.Height - Padding.Top - Padding.Bottom - tabBar.Margin.Top - tabBar.Margin.Bottom;
270 tabBar.Size2D = new Size2D(tabBarSizeW, tabBarSizeH);
276 int contentPosX = Padding.Start + content.Margin.Start;
277 int contentPosY = Padding.Top + content.Margin.Top;
279 content.Position = new Position(contentPosX, contentPosY);
281 // FIXME: Now, WidthSpecification/HeightSpecification are updated internally.
282 // When this is resolved, comparing Specification with Size is removed.
283 if ((content.WidthSpecification == LayoutParamPolicies.MatchParent) || (content.HeightSpecification == LayoutParamPolicies.MatchParent) ||
284 (content.WidthSpecification > Size.Width) || (content.HeightSpecification > Size.Height))
286 int contentSizeW = content.Size2D.Width;
287 int contentSizeH = content.Size2D.Height;
289 if ((content.WidthSpecification == LayoutParamPolicies.MatchParent) || (content.WidthSpecification > Size.Width))
291 contentSizeW = Size2D.Width - Padding.Start - Padding.End - content.Margin.Start - content.Margin.End;
294 if ((content.HeightSpecification == LayoutParamPolicies.MatchParent) || (content.HeightSpecification > Size.Height))
296 contentSizeH = Size2D.Height - Padding.Top - Padding.Bottom - content.Margin.Top - content.Margin.Bottom - (tabBar?.Size2D.Height ?? 0);
299 content.Size2D = new Size2D(contentSizeW, contentSizeH);
305 int tabBarPosX = Padding.Start + tabBar.Margin.Start;
306 int tabBarPosY = /*Padding.Top +*/ tabBar.Margin.Top + (content?.Size2D.Height ?? 0);
308 tabBar.Position = new Position(tabBarPosX, tabBarPosY);