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 System.Diagnostics.CodeAnalysis;
20 using Tizen.NUI.BaseComponents;
22 namespace Tizen.NUI.Components
25 /// TabButton is a class which is used for selecting one content in a TabView.
27 /// <since_tizen> 9 </since_tizen>
28 public class TabButton : SelectButton
30 private bool selectedAgain = false;
32 private TabButtonStyle tabButtonStyle = null;
34 private bool styleApplied = false;
37 /// Creates a new instance of TabButton.
39 /// <since_tizen> 9 </since_tizen>
46 /// Creates a new instance of TabButton.
48 /// <param name="style">Creates TabButton by special style defined in UX.</param>
49 [EditorBrowsable(EditorBrowsableState.Never)]
50 public TabButton(string style) : base(style)
56 /// Creates a new instance of TabButton.
58 /// <param name="tabButtonStyle">Creates TabButton by style customized by user.</param>
59 [EditorBrowsable(EditorBrowsableState.Never)]
60 public TabButton(TabButtonStyle tabButtonStyle) : base(tabButtonStyle)
66 [EditorBrowsable(EditorBrowsableState.Never)]
67 public override void OnInitialize()
71 AccessibilityRole = Role.PageTab;
75 [EditorBrowsable(EditorBrowsableState.Never)]
76 public override void ApplyStyle(ViewStyle viewStyle)
80 base.ApplyStyle(viewStyle);
82 tabButtonStyle = viewStyle as TabButtonStyle;
86 //Calculate children's sizes and positions based on padding sizes.
91 [EditorBrowsable(EditorBrowsableState.Never)]
92 public override bool OnKey(Key key)
94 if ((IsEnabled == false) || (key == null))
99 if (key.State == Key.StateType.Up)
101 if (key.KeyPressedName == "Return")
103 if (IsSelected == true)
105 selectedAgain = true;
110 bool ret = base.OnKey(key);
112 if (selectedAgain == true)
115 selectedAgain = false;
122 [EditorBrowsable(EditorBrowsableState.Never)]
123 public override void OnRelayout(Vector2 size, RelayoutContainer container)
125 base.OnRelayout(size, container);
126 UpdateSizeAndSpacing();
130 [EditorBrowsable(EditorBrowsableState.Never)]
131 protected override void Dispose(DisposeTypes type)
138 if (type == DisposeTypes.Explicit)
140 // Dispose children explicitly
147 /// Gets TabButton style.
149 /// <returns>The default TabButton style.</returns>
150 [EditorBrowsable(EditorBrowsableState.Never)]
151 protected override ViewStyle CreateViewStyle()
153 return new TabButtonStyle();
157 [EditorBrowsable(EditorBrowsableState.Never)]
158 protected override bool HandleControlStateOnTouch(Touch touch)
160 if ((IsEnabled == false) || (touch == null))
165 PointStateType state = touch.GetState(0);
168 case PointStateType.Up:
169 if (IsSelected == true)
171 selectedAgain = true;
178 bool ret = base.HandleControlStateOnTouch(touch);
180 if (selectedAgain == true)
183 selectedAgain = false;
190 [SuppressMessage("Microsoft.Design",
191 "CA1062: Validate arguments of public methods",
192 MessageId = "controlStateChangedInfo",
193 Justification = "OnControlStateChanged is called when controlState is changed so controlStateChangedInfo cannot be null.")]
194 [EditorBrowsable(EditorBrowsableState.Never)]
195 protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
197 if (controlStateChangedInfo.PreviousState.Contains(ControlState.Selected) != controlStateChangedInfo.CurrentState.Contains(ControlState.Selected))
199 // TabButton does not invoke SelectedChanged if button or key is
200 // unpressed while its state is selected.
201 if (selectedAgain == true)
206 base.OnControlStateChanged(controlStateChangedInfo);
211 [EditorBrowsable(EditorBrowsableState.Never)]
212 protected override void LayoutItems()
214 UpdateSizeAndSpacing();
217 private void Initialize()
219 Layout = new LinearLayout()
221 LinearOrientation = LinearLayout.Orientation.Vertical,
222 HorizontalAlignment = HorizontalAlignment.Center,
223 VerticalAlignment = VerticalAlignment.Center,
226 WidthSpecification = LayoutParamPolicies.MatchParent;
228 AccessibilityHighlightable = true;
231 private void UpdateSizeAndSpacing()
233 if (styleApplied == false)
238 bool isEmptyIcon = false;
239 bool isEmptyText = false;
241 if (String.IsNullOrEmpty(Icon.ResourceUrl))
246 if (String.IsNullOrEmpty(TextLabel.Text))
253 if (Children.Contains(Icon))
258 else if (Children.Contains(Icon) == false)
265 if (Children.Contains(TextLabel))
270 else if (Children.Contains(TextLabel) == false)
275 if (tabButtonStyle != null)
278 if (!isEmptyIcon && !isEmptyText)
280 if (tabButtonStyle.Size != null)
282 WidthSpecification = (int)tabButtonStyle.Size.Width;
283 HeightSpecification = (int)tabButtonStyle.Size.Height;
286 Padding = tabButtonStyle.Padding;
289 else if (!isEmptyIcon && isEmptyText)
291 if (tabButtonStyle.SizeWithIconOnly != null)
293 WidthSpecification = (int)tabButtonStyle.SizeWithIconOnly.Width;
294 HeightSpecification = (int)tabButtonStyle.SizeWithIconOnly.Height;
297 Padding = tabButtonStyle.PaddingWithIconOnly;
299 if (tabButtonStyle.IconSizeWithIconOnly != null)
301 Icon.WidthSpecification = (int)tabButtonStyle.IconSizeWithIconOnly.Width;
302 Icon.HeightSpecification = (int)tabButtonStyle.IconSizeWithIconOnly.Height;
306 else if (isEmptyIcon && !isEmptyText)
308 if (tabButtonStyle.SizeWithTextOnly != null)
310 WidthSpecification = (int)tabButtonStyle.SizeWithTextOnly.Width;
311 HeightSpecification = (int)tabButtonStyle.SizeWithTextOnly.Height;
314 Padding = tabButtonStyle.PaddingWithTextOnly;
316 if ((tabButtonStyle.Icon != null) && (tabButtonStyle.Icon.Size != null))
318 Icon.WidthSpecification = (int)tabButtonStyle.Icon.Size.Width;
319 Icon.HeightSpecification = (int)tabButtonStyle.Icon.Size.Height;
325 if (tabButtonStyle.Size != null)
327 WidthSpecification = (int)tabButtonStyle.Size.Width;
328 HeightSpecification = (int)tabButtonStyle.Size.Height;
331 Padding = tabButtonStyle.Padding;