2 * Copyright(c) 2019 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 Tizen.NUI.BaseComponents;
20 using System.ComponentModel;
22 namespace Tizen.NUI.Components
25 /// Tab is one kind of common component, it can be used as menu label.
26 /// User can handle Tab by adding/inserting/deleting TabItem.
28 /// <since_tizen> 6 </since_tizen>
29 public class Tab : Control
31 private const int aniTime = 100; // will be defined in const file later
32 private List<TabItem> itemList = new List<TabItem>();
33 private int curIndex = 0;
34 private View underline = null;
35 private Animation underlineAni = null;
36 private bool isNeedAnimation = false;
37 private Extents space;
41 /// Creates a new instance of a Tab.
43 /// <since_tizen> 6 </since_tizen>
50 /// Creates a new instance of a Tab with style.
52 /// <param name="style">Create Tab by special style defined in UX.</param>
53 /// <since_tizen> 8 </since_tizen>
54 public Tab(string style) : base(style)
60 /// Creates a new instance of a Tab with style.
62 /// <param name="tabStyle">Create Tab by style customized by user.</param>
63 /// <since_tizen> 8 </since_tizen>
64 public Tab(TabStyle tabStyle) : base(tabStyle)
70 /// An event for the item changed signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
72 /// <since_tizen> 6 </since_tizen>
73 public event EventHandler<ItemChangedEventArgs> ItemChangedEvent;
78 /// <since_tizen> 8 </since_tizen>
79 public new TabStyle Style => ViewStyle as TabStyle;
82 /// Selected item's index in Tab.
84 /// <since_tizen> 6 </since_tizen>
85 public int SelectedItemIndex
93 if (value < itemList.Count)
95 UpdateSelectedItem(itemList[value]);
101 /// Flag to decide if TabItem is adjusted by text's natural width.
102 /// If true, TabItem's width will be equal as text's natural width, if false, it will be decided by Tab's width and tab item count.
104 /// <since_tizen> 6 </since_tizen>
105 public bool UseTextNaturalSize
109 return Style?.UseTextNaturalSize ?? false;
115 Style.UseTextNaturalSize = value;
122 /// Gap between items.
124 /// <since_tizen> 6 </since_tizen>
129 return Style?.ItemSpace ?? 0;
135 Style.ItemSpace = value;
142 /// Space in Tab. Sequence as Left, Right, Top, Bottom
144 /// <since_tizen> 6 </since_tizen>
158 /// Item paddings in Tab. Sequence as Left, Right, Top, Bottom
160 /// <since_tizen> 6 </since_tizen>
161 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
162 [EditorBrowsable(EditorBrowsableState.Never)]
163 public Extents ItemPadding
171 if(null != value && null != Style?.ItemPadding)
173 Style.ItemPadding.CopyFrom(value);
177 space = new Extents((ushort start, ushort end, ushort top, ushort bottom) =>
179 Style.ItemPadding.Start = start;
180 Style.ItemPadding.End = end;
181 Style.ItemPadding.Top = top;
182 Style.ItemPadding.Bottom = bottom;
184 }, value.Start, value.End, value.Top, value.Bottom);
188 space.CopyFrom(value);
197 /// UnderLine view's size in Tab.
199 /// <since_tizen> 6 </since_tizen>
200 public Size UnderLineSize
204 return Style?.UnderLine?.Size;
208 if (null != Style?.UnderLine)
210 Style.UnderLine.Size = value;
216 /// UnderLine view's background in Tab.
218 /// <since_tizen> 6 </since_tizen>
219 public Color UnderLineBackgroundColor
223 return Style?.UnderLine?.BackgroundColor?.All;
227 if (null != Style?.UnderLine)
229 Style.UnderLine.BackgroundColor = value;
235 /// Text point size in Tab.
237 /// <since_tizen> 6 </since_tizen>
238 public float PointSize
242 return Style?.Text?.PointSize?.All ?? 0;
246 if (null != Style?.Text)
248 Style.Text.PointSize = value;
254 /// Text font family in Tab.
256 /// <since_tizen> 6 </since_tizen>
257 public string FontFamily
261 return Style?.Text?.FontFamily?.All;
265 if (null != Style?.Text)
267 Style.Text.FontFamily = value;
273 /// Text color in Tab.
275 /// <since_tizen> 6 </since_tizen>
276 public Color TextColor
280 return Style?.Text?.TextColor?.All;
284 if (null != Style?.Text)
286 Style.Text.TextColor = value;
291 private ColorSelector textColorSelector = new ColorSelector();
293 /// Text color selector in Tab.
295 /// <since_tizen> 6 </since_tizen>
296 public ColorSelector TextColorSelector
300 return textColorSelector;
304 textColorSelector.Clone(value);
309 /// Add tab item by item data. The added item will be added to end of all items automatically.
311 /// <param name="itemData">Item data which will apply to tab item view.</param>
312 /// <since_tizen> 6 </since_tizen>
313 public void AddItem(TabItemData itemData)
315 AddItemByIndex(itemData, itemList.Count);
319 /// Insert tab item by item data. The inserted item will be added to the special position by index automatically.
321 /// <param name="itemData">Item data which will apply to tab item view.</param>
322 /// <param name="index">Position index where will be inserted.</param>
323 /// <since_tizen> 6 </since_tizen>
324 public void InsertItem(TabItemData itemData, int index)
326 AddItemByIndex(itemData, index);
330 /// Delete tab item by index.
332 /// <param name="itemIndex">Position index where will be deleted.</param>
333 /// <since_tizen> 6 </since_tizen>
334 public void DeleteItem(int itemIndex)
336 if(itemList == null || itemIndex < 0 || itemIndex >= itemList.Count)
341 if (curIndex > itemIndex || (curIndex == itemIndex && itemIndex == itemList.Count - 1))
346 Remove(itemList[itemIndex]);
347 itemList[itemIndex].Dispose();
348 itemList.RemoveAt(itemIndex);
354 /// Apply style to tab.
356 /// <param name="viewStyle">The style to apply.</param>
357 /// <since_tizen> 8 </since_tizen>
358 public override void ApplyStyle(ViewStyle viewStyle)
360 base.ApplyStyle(viewStyle);
362 TabStyle tabStyle = viewStyle as TabStyle;
364 if (null != tabStyle)
366 if (null == underline)
368 underline = new View()
370 PositionUsesPivotPoint = true,
371 ParentOrigin = Tizen.NUI.ParentOrigin.BottomLeft,
372 PivotPoint = Tizen.NUI.PivotPoint.BottomLeft,
375 CreateUnderLineAnimation();
378 underline.ApplyStyle(Style.UnderLine);
383 /// Dispose Tab and all children on it.
385 /// <param name="type">Dispose type.</param>
386 /// <since_tizen> 6 </since_tizen>
387 protected override void Dispose(DisposeTypes type)
394 if (type == DisposeTypes.Explicit)
396 if(underlineAni != null)
398 if(underlineAni.State == Animation.States.Playing)
402 underlineAni.Dispose();
405 Utility.Dispose(underline);
408 for(int i = 0; i < itemList.Count; i++)
411 itemList[i].Dispose();
425 /// <since_tizen> 6 </since_tizen>
426 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
427 [EditorBrowsable(EditorBrowsableState.Never)]
428 protected override void OnUpdate()
436 /// <returns>The default tab style.</returns>
437 /// <since_tizen> 8 </since_tizen>
438 protected override ViewStyle GetViewStyle()
440 return new TabStyle();
444 /// Theme change callback when theme is changed, this callback will be trigger.
446 /// <param name="sender">The sender</param>
447 /// <param name="e">The event data</param>
448 /// <since_tizen> 8 </since_tizen>
449 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
451 TabStyle tabStyle = StyleManager.Instance.GetViewStyle(style) as TabStyle;
452 if (tabStyle != null)
454 Style.CopyFrom(tabStyle);
459 /// Layout child in Tab and it can be override by user.
461 /// <since_tizen> 6 </since_tizen>
462 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
463 [EditorBrowsable(EditorBrowsableState.Never)]
464 protected virtual void LayoutChild()
466 if (itemList == null)
471 int totalNum = itemList.Count;
477 int preX = (int)Style.ItemPadding.Start;
479 int itemSpace = Style.ItemSpace;
481 if (LayoutDirection == ViewLayoutDirectionType.LTR)
483 if (Style.UseTextNaturalSize == true)
485 for (int i = 0; i < totalNum; i++)
487 preW = (itemList[i].TextItem.NaturalSize2D != null ? itemList[i].TextItem.NaturalSize2D.Width : 0);
488 itemList[i].Position2D.X = preX;
489 itemList[i].Size2D.Width = preW;
490 preX = itemList[i].Position2D.X + preW + itemSpace;
491 itemList[i].Index = i;
496 preW = (Size2D.Width - (int)Style.ItemPadding.Start - (int)Style.ItemPadding.End) / totalNum;
497 for (int i = 0; i < totalNum; i++)
499 itemList[i].Position2D.X = preX;
500 itemList[i].Size2D.Width = preW;
501 preX = itemList[i].Position2D.X + preW + itemSpace;
502 itemList[i].Index = i;
508 preX = (int)Style.ItemPadding.End;
509 if (Style.UseTextNaturalSize == true)
511 int w = Size2D.Width;
512 for (int i = 0; i < totalNum; i++)
514 preW = (itemList[i].NaturalSize2D != null ? itemList[i].NaturalSize2D.Width : 0);
515 itemList[i].Position2D.X = w - preW - preX;
516 itemList[i].Size2D.Width = preW;
517 preX = w - itemList[i].Position2D.X + itemSpace;
518 itemList[i].Index = i;
523 preW = (Size2D.Width - (int)Style.ItemPadding.Start - (int)Style.ItemPadding.End) / totalNum;
524 for (int i = totalNum - 1; i >= 0; i--)
526 itemList[i].Position2D.X = preX;
527 itemList[i].Size2D.Width = preW;
528 preX = itemList[i].Position2D.X + preW + itemSpace;
529 itemList[i].Index = i;
533 UpdateUnderLinePos();
536 private void Initialize()
538 LayoutDirectionChanged += OnLayoutDirectionChanged;
541 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
546 private void AddItemByIndex(TabItemData itemData, int index)
548 if (null == itemData) return;
550 int topSpace = (int)Style.ItemPadding.Top;
551 if (Style.UnderLine != null && Style.UnderLine.Size != null)
553 h = (int)Style.UnderLine.Size.Height;
556 Tab.TabItem item = new TabItem();
557 item.TextItem.ApplyStyle(Style.Text);
559 item.Text = itemData.Text;
560 item.Size2D.Height = Size2D.Height - h - topSpace;
561 item.Position2D.Y = topSpace;
562 item.TouchEvent += ItemTouchEvent;
565 if (index >= itemList.Count)
571 itemList.Insert(index, item);
577 private void UpdateItems()
580 if (itemList != null && curIndex >= 0 && curIndex < itemList.Count)
582 itemList[curIndex].ControlState = ControlStates.Selected;
583 UpdateUnderLinePos();
587 if (underline != null)
594 private void CreateUnderLineAnimation()
596 if (underlineAni == null)
598 underlineAni = new Animation(aniTime);
602 private void UpdateUnderLinePos()
604 if (underline == null || Style.UnderLine == null || Style.UnderLine.Size == null
605 || itemList == null || itemList.Count <= 0)
610 Style.UnderLine.Size.Width = itemList[curIndex].Size2D.Width;
612 underline.Size2D = new Size2D(itemList[curIndex].Size2D.Width, (int)Style.UnderLine.Size.Height);
613 underline.BackgroundColor = Style.UnderLine.BackgroundColor.All;
616 CreateUnderLineAnimation();
617 if (underlineAni.State == Animation.States.Playing)
621 underlineAni.Clear();
622 underlineAni.AnimateTo(underline, "PositionX", itemList[curIndex].Position2D.X);
627 underline.Position2D.X = itemList[curIndex].Position2D.X;
628 isNeedAnimation = true;
634 private void UpdateSelectedItem(TabItem item)
636 if(item == null || curIndex == item.Index)
641 ItemChangedEventArgs e = new ItemChangedEventArgs
643 PreviousIndex = curIndex,
644 CurrentIndex = item.Index
646 ItemChangedEvent?.Invoke(this, e);
648 itemList[curIndex].ControlState = ControlStates.Normal;
649 curIndex = item.Index;
650 itemList[curIndex].ControlState = ControlStates.Selected;
652 UpdateUnderLinePos();
655 private bool ItemTouchEvent(object source, TouchEventArgs e)
657 TabItem item = source as TabItem;
662 PointStateType state = e.Touch.GetState(0);
663 if (state == PointStateType.Up)
665 UpdateSelectedItem(item);
671 internal class TabItem : View
673 public TabItem() : base()
675 TextItem = new TextLabel()
677 ParentOrigin = Tizen.NUI.ParentOrigin.Center,
678 PivotPoint = Tizen.NUI.PivotPoint.Center,
679 PositionUsesPivotPoint = true,
680 WidthResizePolicy = ResizePolicyType.FillToParent,
681 HeightResizePolicy = ResizePolicyType.FillToParent,
682 HorizontalAlignment = HorizontalAlignment.Center,
683 VerticalAlignment = VerticalAlignment.Center
698 return TextItem.Text;
702 TextItem.Text = value;
706 internal TextLabel TextItem
714 /// TabItemData is a class to record all data which will be applied to Tab item.
716 /// <since_tizen> 6 </since_tizen>
717 public class TabItemData
720 /// Text string in tab item view.
722 /// <since_tizen> 6 </since_tizen>
731 /// ItemChangedEventArgs is a class to record item change event arguments which will sent to user.
733 /// <since_tizen> 6 </since_tizen>
734 public class ItemChangedEventArgs : EventArgs
736 /// <summary> Previous selected index of Tab </summary>
737 /// <since_tizen> 6 </since_tizen>
738 public int PreviousIndex;
739 /// <summary> Current selected index of Tab </summary>
740 /// <since_tizen> 6 </since_tizen>
741 public int CurrentIndex;