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.Collections.Generic;
19 using System.ComponentModel;
20 using Tizen.NUI.BaseComponents;
22 namespace Tizen.NUI.Components
25 /// TabButtonSelectedEventArgs is a class to record tab button selected event
26 /// arguments which will be sent to a user.
28 [EditorBrowsable(EditorBrowsableState.Never)]
29 public class TabButtonSelectedEventArgs : EventArgs
32 /// Creates a new instance of TabButtonSelectedEventArgs.
33 /// The indices of tab buttons in TabBar are basically the order of adding to TabBar by <see cref="TabView.AddTab"/>.
34 /// So a tab button's index in TabBar can be changed whenever <see cref="TabView.AddTab"/> or <see cref="TabView.RemoveTab"/> is called.
36 /// <param name="index">The index of the selected tab button in TabBar.</param>
37 [EditorBrowsable(EditorBrowsableState.Never)]
38 public TabButtonSelectedEventArgs(int index)
44 /// The index of the selected tab button.
46 [EditorBrowsable(EditorBrowsableState.Never)]
47 public int Index { get; }
51 /// TabBar is a class which contains a set of TabButtons and has one of them selected.
53 /// <since_tizen> 9 </since_tizen>
54 public class TabBar : Control
56 private IList<TabButton> tabButtons;
58 private TabButtonGroup tabButtonGroup;
61 /// Creates a new instance of TabBar.
63 /// <since_tizen> 9 </since_tizen>
66 Layout = new LinearLayout() { LinearOrientation = LinearLayout.Orientation.Horizontal };
68 WidthSpecification = LayoutParamPolicies.MatchParent;
70 tabButtons = new List<TabButton>();
71 tabButtonGroup = new TabButtonGroup();
76 /// An event for the tab button selected signal which can be used to
77 /// subscribe or unsubscribe the event handler provided by a user.
79 [EditorBrowsable(EditorBrowsableState.Never)]
80 public event EventHandler<TabButtonSelectedEventArgs> TabButtonSelected;
83 /// The index of the selected tab button.
84 /// The indices of tab buttons in TabBar are basically the order of adding to TabBar by <see cref="TabView.AddTab"/>.
85 /// So a tab button's index in TabBar can be changed whenever <see cref="TabView.AddTab"/> or <see cref="TabView.RemoveTab"/> is called.
87 [EditorBrowsable(EditorBrowsableState.Never)]
88 protected int SelectedIndex { get; set; }
91 /// Gets the count of tab buttons.
93 /// <since_tizen> 9 </since_tizen>
94 public int TabButtonCount => tabButtons.Count;
97 [EditorBrowsable(EditorBrowsableState.Never)]
98 public override void OnInitialize()
102 AccessibilityRole = Role.PageTabList;
106 /// Adds a tab button to TabBar.
108 /// <param name="tabButton">A tab button to be added to TabBar.</param>
109 /// <exception cref="ArgumentNullException">Thrown when the argument tabButton is null.</exception>
110 [EditorBrowsable(EditorBrowsableState.Never)]
111 protected internal void AddTabButton(TabButton tabButton)
113 if (tabButton == null)
115 throw new ArgumentNullException(nameof(tabButton), "tabButton should not be null.");
118 tabButtons.Add(tabButton);
120 tabButtonGroup.Add(tabButton);
122 tabButton.Clicked += (object sender, ClickedEventArgs e) =>
124 int index = tabButtons.IndexOf(tabButton);
126 if (SelectedIndex == index)
131 SelectedIndex = index;
133 if (TabButtonSelected != null)
135 TabButtonSelectedEventArgs args = new TabButtonSelectedEventArgs(SelectedIndex);
136 TabButtonSelected(this, args);
140 if (SelectedIndex == -1)
142 tabButton.IsSelected = true;
145 if (TabButtonSelected != null)
147 TabButtonSelectedEventArgs args = new TabButtonSelectedEventArgs(SelectedIndex);
148 TabButtonSelected(this, args);
154 /// Removes a tab button from TabBar.
156 /// <param name="tabButton">A tab button to be removed from TabBar.</param>
157 /// <exception cref="ArgumentNullException">Thrown when the argument tabButton is null.</exception>
158 /// <exception cref="ArgumentException">Thrown when the argument tabButton does not exist in TabBar.</exception>
159 [EditorBrowsable(EditorBrowsableState.Never)]
160 protected internal void RemoveTabButton(TabButton tabButton)
162 if (tabButton == null)
164 throw new ArgumentNullException(nameof(tabButton), "tabButton should not be null.");
167 if (tabButtons.Contains(tabButton) == false)
169 throw new ArgumentException("tabButton does not exist in TabBar.", nameof(tabButton));
172 int index = tabButtons.IndexOf(tabButton);
173 TabButton selectedTabButton = tabButtons[SelectedIndex];
175 tabButtons.Remove(tabButton);
177 tabButtonGroup.Remove(tabButton);
179 if ((index < SelectedIndex) || (tabButtons.Count == SelectedIndex))
183 if (TabButtonSelected != null)
185 TabButtonSelectedEventArgs args = new TabButtonSelectedEventArgs(SelectedIndex);
186 TabButtonSelected(this, args);
190 if ((SelectedIndex != -1) && (selectedTabButton != tabButtons[SelectedIndex]))
192 tabButtons[SelectedIndex].IsSelected = true;
197 /// Gets the tab button at the specified index of TabBar.
198 /// The indices of tab buttons in TabBar are basically the order of adding to TabBar by <see cref="TabView.AddTab"/>.
199 /// So a tab button's index in TabBar can be changed whenever <see cref="TabView.AddTab"/> or <see cref="TabView.RemoveTab"/> is called.
201 /// <param name="index">The index of tab button in TabBar where the specified tab button exists.</param>
202 /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of tab buttons.</exception>
203 /// <since_tizen> 9 </since_tizen>
204 public TabButton GetTabButton(int index)
206 if ((index < 0) || (index >= tabButtons.Count))
208 throw new ArgumentOutOfRangeException(nameof(index), "index should not be greater than or equal to 0, and less than the number of tab buttons.");
211 return tabButtons[index];
215 [EditorBrowsable(EditorBrowsableState.Never)]
216 protected override void Dispose(DisposeTypes type)
223 if (type == DisposeTypes.Explicit)
225 if (tabButtons != null)
227 foreach (TabButton tabButton in tabButtons)
229 Utility.Dispose(tabButton);
235 tabButtonGroup = null;