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);
152 //TODO: To support non-unified tab button size.
153 CalculateUnifiedTabButtonSize();
157 /// Removes a tab button from TabBar.
159 /// <param name="tabButton">A tab button to be removed from TabBar.</param>
160 /// <exception cref="ArgumentNullException">Thrown when the argument tabButton is null.</exception>
161 /// <exception cref="ArgumentException">Thrown when the argument tabButton does not exist in TabBar.</exception>
162 [EditorBrowsable(EditorBrowsableState.Never)]
163 protected internal void RemoveTabButton(TabButton tabButton)
165 if (tabButton == null)
167 throw new ArgumentNullException(nameof(tabButton), "tabButton should not be null.");
170 if (tabButtons.Contains(tabButton) == false)
172 throw new ArgumentException("tabButton does not exist in TabBar.", nameof(tabButton));
175 int index = tabButtons.IndexOf(tabButton);
176 TabButton selectedTabButton = tabButtons[SelectedIndex];
178 tabButtons.Remove(tabButton);
180 tabButtonGroup.Remove(tabButton);
182 if ((index < SelectedIndex) || (tabButtons.Count == SelectedIndex))
186 if (TabButtonSelected != null)
188 TabButtonSelectedEventArgs args = new TabButtonSelectedEventArgs(SelectedIndex);
189 TabButtonSelected(this, args);
193 if ((SelectedIndex != -1) && (selectedTabButton != tabButtons[SelectedIndex]))
195 tabButtons[SelectedIndex].IsSelected = true;
198 //TODO: To support non-unified tab button size.
199 CalculateUnifiedTabButtonSize();
203 [EditorBrowsable(EditorBrowsableState.Never)]
204 public override void OnRelayout(Vector2 size, RelayoutContainer container)
206 base.OnRelayout(size, container);
208 //TODO: To support non-unified tab button size.
209 CalculateUnifiedTabButtonSize();
212 private void CalculateUnifiedTabButtonSize()
214 if (tabButtons.Count <= 0)
219 var tabButtonWidth = Size.Width / tabButtons.Count;
221 foreach (TabButton tabButton in tabButtons)
223 if (tabButton.Size.Width != tabButtonWidth)
225 tabButton.Size = new Size(tabButtonWidth, tabButton.Size.Height);
231 /// Gets the tab button at the specified index of TabBar.
232 /// The indices of tab buttons in TabBar are basically the order of adding to TabBar by <see cref="TabView.AddTab"/>.
233 /// So a tab button's index in TabBar can be changed whenever <see cref="TabView.AddTab"/> or <see cref="TabView.RemoveTab"/> is called.
235 /// <param name="index">The index of tab button in TabBar where the specified tab button exists.</param>
236 /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of tab buttons.</exception>
237 /// <since_tizen> 9 </since_tizen>
238 public TabButton GetTabButton(int index)
240 if ((index < 0) || (index >= tabButtons.Count))
242 throw new ArgumentOutOfRangeException(nameof(index), "index should not be greater than or equal to 0, and less than the number of tab buttons.");
245 return tabButtons[index];
249 [EditorBrowsable(EditorBrowsableState.Never)]
250 protected override void Dispose(DisposeTypes type)
257 if (type == DisposeTypes.Explicit)
259 if (tabButtons != null)
261 foreach (TabButton tabButton in tabButtons)
263 Utility.Dispose(tabButton);
269 tabButtonGroup = null;