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.
34 /// <param name="index">The selected index.</param>
35 [EditorBrowsable(EditorBrowsableState.Never)]
36 public TabButtonSelectedEventArgs(int index)
42 /// The index of the selected tab button.
44 [EditorBrowsable(EditorBrowsableState.Never)]
45 public int Index { get; }
49 /// TabBar is a class which contains a set of TabButtons and has one of them selected.
51 [EditorBrowsable(EditorBrowsableState.Never)]
52 public class TabBar : Control
54 private IList<TabButton> tabButtons;
56 private TabButtonGroup tabButtonGroup;
59 /// Creates a new instance of TabBar.
61 [EditorBrowsable(EditorBrowsableState.Never)]
64 Layout = new LinearLayout() { LinearOrientation = LinearLayout.Orientation.Horizontal };
66 WidthSpecification = LayoutParamPolicies.MatchParent;
68 tabButtons = new List<TabButton>();
69 tabButtonGroup = new TabButtonGroup();
74 /// An event for the tab button selected signal which can be used to
75 /// subscribe or unsubscribe the event handler provided by a user.
77 [EditorBrowsable(EditorBrowsableState.Never)]
78 public event EventHandler<TabButtonSelectedEventArgs> TabButtonSelected;
81 /// The index of the selected tab button.
83 [EditorBrowsable(EditorBrowsableState.Never)]
84 protected int SelectedIndex { get; set; }
87 /// Gets the count of tab buttons.
89 [EditorBrowsable(EditorBrowsableState.Never)]
90 public int TabButtonCount => tabButtons.Count;
93 /// Adds a tab button to TabBar.
95 /// <param name="tabButton">A tab button to be added to TabBar.</param>
96 /// <exception cref="ArgumentNullException">Thrown when the argument tabButton is null.</exception>
97 [EditorBrowsable(EditorBrowsableState.Never)]
98 protected internal void AddTabButton(TabButton tabButton)
100 if (tabButton == null)
102 throw new ArgumentNullException(nameof(tabButton), "tabButton should not be null.");
105 tabButtons.Add(tabButton);
107 tabButtonGroup.Add(tabButton);
109 tabButton.Clicked += (object sender, ClickedEventArgs e) =>
111 int index = tabButtons.IndexOf(tabButton);
113 if (SelectedIndex == index)
118 SelectedIndex = index;
120 if (TabButtonSelected != null)
122 TabButtonSelectedEventArgs args = new TabButtonSelectedEventArgs(SelectedIndex);
123 TabButtonSelected(this, args);
127 if (SelectedIndex == -1)
129 tabButton.IsSelected = true;
132 if (TabButtonSelected != null)
134 TabButtonSelectedEventArgs args = new TabButtonSelectedEventArgs(SelectedIndex);
135 TabButtonSelected(this, args);
139 //TODO: To support non-unified tab button size.
140 CalculateUnifiedTabButtonSize();
144 /// Removes a tab button from TabBar.
146 /// <param name="tabButton">A tab button to be removed from TabBar.</param>
147 /// <exception cref="ArgumentNullException">Thrown when the argument tabButton is null.</exception>
148 /// <exception cref="ArgumentException">Thrown when the argument tabButton does not exist in TabBar.</exception>
149 [EditorBrowsable(EditorBrowsableState.Never)]
150 protected internal void RemoveTabButton(TabButton tabButton)
152 if (tabButton == null)
154 throw new ArgumentNullException(nameof(tabButton), "tabButton should not be null.");
157 if (tabButtons.Contains(tabButton) == false)
159 throw new ArgumentException("tabButton does not exist in TabBar.", nameof(tabButton));
162 int index = tabButtons.IndexOf(tabButton);
163 TabButton selectedTabButton = tabButtons[SelectedIndex];
165 tabButtons.Remove(tabButton);
167 tabButtonGroup.Remove(tabButton);
169 if ((index < SelectedIndex) || (tabButtons.Count == SelectedIndex))
173 if (TabButtonSelected != null)
175 TabButtonSelectedEventArgs args = new TabButtonSelectedEventArgs(SelectedIndex);
176 TabButtonSelected(this, args);
180 if ((SelectedIndex != -1) && (selectedTabButton != tabButtons[SelectedIndex]))
182 tabButtons[SelectedIndex].IsSelected = true;
185 //TODO: To support non-unified tab button size.
186 CalculateUnifiedTabButtonSize();
190 public override void OnRelayout(Vector2 size, RelayoutContainer container)
192 base.OnRelayout(size, container);
194 //TODO: To support non-unified tab button size.
195 CalculateUnifiedTabButtonSize();
198 private void CalculateUnifiedTabButtonSize()
200 if (tabButtons.Count <= 0)
205 var tabButtonWidth = Size.Width / tabButtons.Count;
207 foreach (TabButton tabButton in tabButtons)
209 if (tabButton.Size.Width != tabButtonWidth)
211 tabButton.Size = new Size(tabButtonWidth, tabButton.Size.Height);
217 /// Gets the tab button at the specified index of TabBar.
219 /// <param name="index">The index of TabBar where the specified tab button exists.</param>
220 /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of tab buttons.</exception>
221 [EditorBrowsable(EditorBrowsableState.Never)]
222 public TabButton GetTabButton(int index)
224 if ((index < 0) || (index >= tabButtons.Count))
226 throw new ArgumentOutOfRangeException(nameof(index), "index should not be greater than or equal to 0, and less than the number of tab buttons.");
229 return tabButtons[index];
233 /// Dispose TabBar and all children on it.
235 /// <param name="type">Dispose type.</param>
236 [EditorBrowsable(EditorBrowsableState.Never)]
237 protected override void Dispose(DisposeTypes type)
244 if (type == DisposeTypes.Explicit)
246 if (tabButtons != null)
248 foreach (TabButton tabButton in tabButtons)
250 Utility.Dispose(tabButton);
256 tabButtonGroup = null;