2 * Copyright(c) 2023 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 System.Diagnostics.CodeAnalysis;
20 using Tizen.NUI.BaseComponents;
22 namespace Tizen.NUI.Components
25 /// TabView is a class which contains a TabBar and TabContent.
26 /// TabView adds TabButtons and Views to TabBar and TabContent in TabView by <see cref="TabView.AddTab"/>.
27 /// TabView removes TabButtons and Views from TabBar and TabContent in TabView by <see cref="TabView.RemoveTab"/>.
28 /// TabView selects a view from the TabContent according to the selected TabButton in the TabBar.
32 /// var tabView = new TabView()
34 /// WidthSpecification = LayoutParamPolicies.MatchParent,
35 /// HeightSpecification = LayoutParamPolicies.MatchParent,
38 /// var tabButton = new TabButton()
43 /// var content = new View()
45 /// BackgroundColor = Color.Red,
46 /// WidthSpecification = LayoutParamPolicies.MatchParent,
47 /// HeightSpecification = LayoutParamPolicies.MatchParent,
50 /// tabView.AddTab(tabButton, content);
52 /// var tabButton2 = new TabButton()
57 /// var content2 = new View()
59 /// BackgroundColor = Color.Green,
60 /// WidthSpecification = LayoutParamPolicies.MatchParent,
61 /// HeightSpecification = LayoutParamPolicies.MatchParent,
64 /// tabView.AddTab(tabButton2, content2);
66 /// var tabButton3 = new TabButton()
71 /// var content3 = new View()
73 /// BackgroundColor = Color.Blue,
74 /// WidthSpecification = LayoutParamPolicies.MatchParent,
75 /// HeightSpecification = LayoutParamPolicies.MatchParent,
78 /// tabView.AddTab(tabButton3, content3);
82 /// <since_tizen> 9 </since_tizen>
83 public class TabView : Control
85 private TabBar tabBar = null;
87 private TabContent content = null;
89 private void Initialize()
91 Layout = new LinearLayout() { LinearOrientation = LinearLayout.Orientation.Vertical };
92 WidthSpecification = LayoutParamPolicies.MatchParent;
93 HeightSpecification = LayoutParamPolicies.MatchParent;
98 // To show TabBar's shadow TabBar is raised above Content.
99 TabBar.RaiseAbove(Content);
103 /// Creates a new instance of TabView.
105 /// <since_tizen> 9 </since_tizen>
112 /// Creates a new instance of a TabView with style.
114 /// <param name="style">A style applied to the newly created TabView.</param>
115 [EditorBrowsable(EditorBrowsableState.Never)]
116 public TabView(ControlStyle style) : base(style)
122 [EditorBrowsable(EditorBrowsableState.Never)]
123 public override void OnInitialize()
127 AccessibilityRole = Role.PageTabList;
130 private void InitTabBar()
137 tabBar = new TabBar();
138 tabBar.TabButtonSelected += tabButtonSelectedHandler;
143 private void InitContent()
150 content = new TabContent();
151 content.WidthSpecification = LayoutParamPolicies.MatchParent;
152 content.HeightSpecification = LayoutParamPolicies.MatchParent;
157 private void tabButtonSelectedHandler(object sender, TabButtonSelectedEventArgs args)
159 if ((content != null) && (content.ViewCount > args.Index))
161 content.SelectContentView(args.Index);
166 /// Gets TabBar of TabView.
168 /// <since_tizen> 9 </since_tizen>
176 [EditorBrowsable(EditorBrowsableState.Never)]
181 tabBar.TabButtonSelected -= tabButtonSelectedHandler;
182 Utility.Dispose(tabBar);
191 /// Gets TabContent of TabView.
193 /// <since_tizen> 9 </since_tizen>
194 public TabContent Content
201 [EditorBrowsable(EditorBrowsableState.Never)]
206 Utility.Dispose(content);
215 /// Adds a tab with tab button and content view.
217 /// <param name="tabButton">A tab button to be added.</param>
218 /// <param name="view">A content view to be added.</param>
219 /// <since_tizen> 9 </since_tizen>
220 public void AddTab(TabButton tabButton, View view)
224 TabBar.AddTabButton(tabButton);
229 Content.AddContentView(view);
234 /// Removes a tab at the specified index of TabView.
235 /// The indices of tabs(tab buttons and views) in TabView are basically the order of adding to TabView by <see cref="TabView.AddTab"/>.
236 /// 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.
238 /// <param name="index">The index of a tab(tab button and view) in TabView where the tab will be removed.</param>
239 /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of tabs.</exception>
240 /// <since_tizen> 9 </since_tizen>
241 public void RemoveTab(int index)
243 var tabButton = TabBar.GetTabButton(index);
244 if (tabButton != null)
246 TabBar.RemoveTabButton(tabButton);
249 var view = Content.GetView(index);
252 Content.RemoveContentView(view);
257 /// Adds a tab from the given TabItem.
258 /// TabItem contains Title and IconURL of a new TabButton in TabBar and Content of a new View in TabContent.
259 /// <param name="tabItem">The tab item which contains Title and IconURL of a new TabButton in TabBar and Content of a new View in TabContent.</param>
261 [EditorBrowsable(EditorBrowsableState.Never)]
262 [SuppressMessage("Microsoft.Reliability",
263 "CA2000:DisposeObjectsBeforeLosingScope",
264 Justification = "The tabButton is added to TabBar and disposed when TabBar is disposed.")]
265 public virtual void Add(TabItem tabItem)
267 if (tabItem == null) return;
269 var hasTitle = (String.IsNullOrEmpty(tabItem.Title) == false);
270 var hasIconUrl = (String.IsNullOrEmpty(tabItem.IconUrl) == false);
272 // Adds a new TabButton and Content View only if TabItem has TabButton properties and Content View.
273 if ((hasTitle || hasIconUrl) && (tabItem.Content != null))
275 var tabButton = new TabButton(tabItem.TabButtonStyle);
279 tabButton.Text = tabItem.Title;
284 tabButton.IconURL = tabItem.IconUrl;
287 TabBar.AddTabButton(tabButton);
289 Content.AddContentView(tabItem.Content);
294 [EditorBrowsable(EditorBrowsableState.Never)]
295 protected override void Dispose(DisposeTypes type)
302 if (type == DisposeTypes.Explicit)
306 tabBar.TabButtonSelected -= tabButtonSelectedHandler;
307 Utility.Dispose(tabBar);
312 Utility.Dispose(content);