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;
40 /// Creates a new instance of a Tab.
42 /// <since_tizen> 6 </since_tizen>
49 /// Creates a new instance of a Tab with style.
51 /// <param name="style">Create Tab by special style defined in UX.</param>
52 /// <since_tizen> 6 </since_tizen>
53 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
54 [EditorBrowsable(EditorBrowsableState.Never)]
55 public Tab(string style) : base(style)
61 /// Creates a new instance of a Tab with style.
63 /// <param name="style">Create Tab by style customized by user.</param>
64 /// <since_tizen> 6 </since_tizen>
65 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
66 [EditorBrowsable(EditorBrowsableState.Never)]
67 public Tab(TabStyle style) : base(style)
73 /// An event for the item changed signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
75 /// <since_tizen> 6 </since_tizen>
76 public event EventHandler<ItemChangedEventArgs> ItemChangedEvent;
78 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
79 [EditorBrowsable(EditorBrowsableState.Never)]
80 public new TabStyle Style => ViewStyle as TabStyle;
83 /// Selected item's index in Tab.
85 /// <since_tizen> 6 </since_tizen>
86 public int SelectedItemIndex
94 if (value < itemList.Count)
96 UpdateSelectedItem(itemList[value]);
102 /// Flag to decide if TabItem is adjusted by text's natural width.
103 /// 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.
105 /// <since_tizen> 6 </since_tizen>
106 public bool UseTextNaturalSize
110 return Style.UseTextNaturalSize;
114 Style.UseTextNaturalSize = value;
120 /// Gap between items.
122 /// <since_tizen> 6 </since_tizen>
127 return Style.ItemSpace;
131 Style.ItemSpace = value;
137 /// Space in Tab. Sequence as Left, Right, Top, Bottom
139 /// <since_tizen> 6 </since_tizen>
153 /// Item paddings in Tab. Sequence as Left, Right, Top, Bottom
155 /// <since_tizen> 6 </since_tizen>
156 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
157 [EditorBrowsable(EditorBrowsableState.Never)]
158 public Extents ItemPadding
168 Style.ItemPadding.CopyFrom(value);
172 space = new Extents((ushort start, ushort end, ushort top, ushort bottom) =>
174 Style.ItemPadding.Start = start;
175 Style.ItemPadding.End = end;
176 Style.ItemPadding.Top = top;
177 Style.ItemPadding.Bottom = bottom;
179 }, value.Start, value.End, value.Top, value.Bottom);
183 space.CopyFrom(value);
192 /// UnderLine view's size in Tab.
194 /// <since_tizen> 6 </since_tizen>
195 public Size UnderLineSize
199 return Style.UnderLine?.Size;
205 //CreateUnderLineAttributes();
206 Style.UnderLine.Size = value;
213 /// UnderLine view's background in Tab.
215 /// <since_tizen> 6 </since_tizen>
216 public Color UnderLineBackgroundColor
220 return Style.UnderLine?.BackgroundColor?.All;
226 //CreateUnderLineAttributes();
227 if (Style.UnderLine.BackgroundColor == null)
229 Style.UnderLine.BackgroundColor = new ColorSelector();
231 Style.UnderLine.BackgroundColor.All = value;
238 /// Text point size in Tab.
240 /// <since_tizen> 6 </since_tizen>
241 public float PointSize
245 return Style.Text?.PointSize?.All ?? 0;
249 //CreateTextAttributes();
250 if (Style.Text.PointSize == null)
252 Style.Text.PointSize = new FloatSelector();
254 Style.Text.PointSize.All = value;
260 /// Text font family in Tab.
262 /// <since_tizen> 6 </since_tizen>
263 public string FontFamily
267 return Style.Text?.FontFamily.All;
271 //CreateTextAttributes();
272 Style.Text.FontFamily.All = value;
278 /// Text color in Tab.
280 /// <since_tizen> 6 </since_tizen>
281 public Color TextColor
285 return Style.Text?.TextColor?.All;
289 //CreateTextAttributes();
290 if (Style.Text.TextColor == null)
292 Style.Text.TextColor = new ColorSelector();
294 Style.Text.TextColor.All = value;
300 /// Text color selector in Tab.
302 /// <since_tizen> 6 </since_tizen>
303 public ColorSelector TextColorSelector
307 return (ColorSelector)Style.Text.TextColor;
313 //CreateTextAttributes();
314 Style.Text.TextColor = value.Clone() as ColorSelector;
321 /// Add tab item by item data. The added item will be added to end of all items automatically.
323 /// <param name="itemData">Item data which will apply to tab item view.</param>
324 /// <since_tizen> 6 </since_tizen>
325 public void AddItem(TabItemData itemData)
327 AddItemByIndex(itemData, itemList.Count);
331 /// Insert tab item by item data. The inserted item will be added to the special position by index automatically.
333 /// <param name="itemData">Item data which will apply to tab item view.</param>
334 /// <param name="index">Position index where will be inserted.</param>
335 /// <since_tizen> 6 </since_tizen>
336 public void InsertItem(TabItemData itemData, int index)
338 AddItemByIndex(itemData, index);
342 /// Delete tab item by index.
344 /// <param name="itemIndex">Position index where will be deleted.</param>
345 /// <since_tizen> 6 </since_tizen>
346 public void DeleteItem(int itemIndex)
348 if(itemList == null || itemIndex < 0 || itemIndex >= itemList.Count)
353 if (curIndex > itemIndex || (curIndex == itemIndex && itemIndex == itemList.Count - 1))
358 Remove(itemList[itemIndex]);
359 itemList[itemIndex].Dispose();
360 itemList.RemoveAt(itemIndex);
365 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
366 [EditorBrowsable(EditorBrowsableState.Never)]
367 public override void ApplyStyle(ViewStyle viewStyle)
369 base.ApplyStyle(viewStyle);
371 TabStyle tabStyle = viewStyle as TabStyle;
373 if (null != tabStyle)
375 if (null == underline)
377 underline = new View()
379 PositionUsesPivotPoint = true,
380 ParentOrigin = Tizen.NUI.ParentOrigin.BottomLeft,
381 PivotPoint = Tizen.NUI.PivotPoint.BottomLeft,
384 CreateUnderLineAnimation();
387 underline.ApplyStyle(Style.UnderLine);
392 /// Dispose Tab and all children on it.
394 /// <param name="type">Dispose type.</param>
395 /// <since_tizen> 6 </since_tizen>
396 protected override void Dispose(DisposeTypes type)
403 if (type == DisposeTypes.Explicit)
405 if(underlineAni != null)
407 if(underlineAni.State == Animation.States.Playing)
411 underlineAni.Dispose();
414 Utility.Dispose(underline);
417 for(int i = 0; i < itemList.Count; i++)
420 itemList[i].Dispose();
432 /// Update Tab by attributes.
434 /// <since_tizen> 6 </since_tizen>
435 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
436 [EditorBrowsable(EditorBrowsableState.Never)]
437 protected override void OnUpdate()
443 /// Get Tab attribues.
445 /// <since_tizen> 6 </since_tizen>
446 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
447 [EditorBrowsable(EditorBrowsableState.Never)]
448 protected override ViewStyle GetViewStyle()
450 return new TabStyle();
454 /// Theme change callback when theme is changed, this callback will be trigger.
456 /// <since_tizen> 6 </since_tizen>
457 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
458 [EditorBrowsable(EditorBrowsableState.Never)]
459 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
461 TabStyle tempAttributes = StyleManager.Instance.GetAttributes(style) as TabStyle;
462 if (tempAttributes != null)
464 Style.CopyFrom(tempAttributes);
469 /// Layout child in Tab and it can be override by user.
471 /// <since_tizen> 6 </since_tizen>
472 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
473 [EditorBrowsable(EditorBrowsableState.Never)]
474 protected virtual void LayoutChild()
476 if (itemList == null)
481 int totalNum = itemList.Count;
487 int preX = (int)Style.ItemPadding.Start;
489 int itemSpace = Style.ItemSpace;
491 if (LayoutDirection == ViewLayoutDirectionType.LTR)
493 if (Style.UseTextNaturalSize == true)
495 for (int i = 0; i < totalNum; i++)
497 preW = (itemList[i].NaturalSize2D != null ? itemList[i].NaturalSize2D.Width : 0);
498 itemList[i].Position2D.X = preX;
499 itemList[i].Size2D.Width = preW;
500 preX = itemList[i].Position2D.X + preW + itemSpace;
501 itemList[i].Index = i;
506 preW = (Size2D.Width - (int)Style.ItemPadding.Start - (int)Style.ItemPadding.End) / totalNum;
507 for (int i = 0; i < totalNum; i++)
509 itemList[i].Position2D.X = preX;
510 itemList[i].Size2D.Width = preW;
511 preX = itemList[i].Position2D.X + preW + itemSpace;
512 itemList[i].Index = i;
518 preX = (int)Style.ItemPadding.End;
519 if (Style.UseTextNaturalSize == true)
521 int w = Size2D.Width;
522 for (int i = 0; i < totalNum; i++)
524 preW = (itemList[i].NaturalSize2D != null ? itemList[i].NaturalSize2D.Width : 0);
525 itemList[i].Position2D.X = w - preW - preX;
526 itemList[i].Size2D.Width = preW;
527 preX = w - itemList[i].Position2D.X + itemSpace;
528 itemList[i].Index = i;
533 preW = (Size2D.Width - (int)Style.ItemPadding.Start - (int)Style.ItemPadding.End) / totalNum;
534 for (int i = totalNum - 1; i >= 0; i--)
536 itemList[i].Position2D.X = preX;
537 itemList[i].Size2D.Width = preW;
538 preX = itemList[i].Position2D.X + preW + itemSpace;
539 itemList[i].Index = i;
543 UpdateUnderLinePos();
546 private void Initialize()
548 LayoutDirectionChanged += OnLayoutDirectionChanged;
551 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
556 private void AddItemByIndex(TabItemData itemData, int index)
559 int topSpace = (int)Style.ItemPadding.Top;
560 if (Style.UnderLine != null && Style.UnderLine.Size != null)
562 h = (int)Style.UnderLine.Size.Height;
565 Tab.TabItem item = new TabItem();
566 item.TextItem.ApplyStyle(Style.Text);
568 item.Text = itemData.Text;
569 item.Size2D.Height = Size2D.Height - h - topSpace;
570 item.Position2D.Y = topSpace;
571 item.TouchEvent += ItemTouchEvent;
574 if (index >= itemList.Count)
580 itemList.Insert(index, item);
586 private void UpdateItems()
589 if (itemList != null && curIndex >= 0 && curIndex < itemList.Count)
591 itemList[curIndex].ControlState = ControlStates.Selected;
592 UpdateUnderLinePos();
596 if (underline != null)
603 private void CreateUnderLineAttributes()
605 if (Style.UnderLine == null)
607 Style.UnderLine = new ViewStyle()
609 PositionUsesPivotPoint = true,
610 ParentOrigin = Tizen.NUI.ParentOrigin.BottomLeft,
611 PivotPoint = Tizen.NUI.PivotPoint.BottomLeft,
616 private void CreateUnderLineAnimation()
618 if (underlineAni == null)
620 underlineAni = new Animation(aniTime);
624 private void UpdateUnderLinePos()
626 if (underline == null || Style.UnderLine == null || Style.UnderLine.Size == null
627 || itemList == null || itemList.Count <= 0)
632 Style.UnderLine.Size.Width = itemList[curIndex].Size2D.Width;
634 underline.Size2D = new Size2D(itemList[curIndex].Size2D.Width, (int)Style.UnderLine.Size.Height);
635 underline.BackgroundColor = Style.UnderLine.BackgroundColor.All;
638 CreateUnderLineAnimation();
639 if (underlineAni.State == Animation.States.Playing)
643 underlineAni.Clear();
644 underlineAni.AnimateTo(underline, "PositionX", itemList[curIndex].Position2D.X);
649 underline.Position2D.X = itemList[curIndex].Position2D.X;
650 isNeedAnimation = true;
656 private void UpdateSelectedItem(TabItem item)
658 if(item == null || curIndex == item.Index)
663 ItemChangedEventArgs e = new ItemChangedEventArgs
665 PreviousIndex = curIndex,
666 CurrentIndex = item.Index
668 ItemChangedEvent?.Invoke(this, e);
670 itemList[curIndex].ControlState = ControlStates.Normal;
671 curIndex = item.Index;
672 itemList[curIndex].ControlState = ControlStates.Selected;
674 UpdateUnderLinePos();
677 private bool ItemTouchEvent(object source, TouchEventArgs e)
679 TabItem item = source as TabItem;
684 PointStateType state = e.Touch.GetState(0);
685 if (state == PointStateType.Up)
687 UpdateSelectedItem(item);
693 internal class TabItem : View
695 public TabItem() : base()
697 TextItem = new TextLabel()
699 ParentOrigin = Tizen.NUI.ParentOrigin.Center,
700 PivotPoint = Tizen.NUI.PivotPoint.Center,
701 PositionUsesPivotPoint = true,
702 WidthResizePolicy = ResizePolicyType.FillToParent,
703 HeightResizePolicy = ResizePolicyType.FillToParent,
704 HorizontalAlignment = HorizontalAlignment.Center,
705 VerticalAlignment = VerticalAlignment.Center
720 return TextItem.Text;
724 TextItem.Text = value;
728 internal TextLabel TextItem
736 /// TabItemData is a class to record all data which will be applied to Tab item.
738 /// <since_tizen> 6 </since_tizen>
739 public class TabItemData
742 /// Text string in tab item view.
744 /// <since_tizen> 6 </since_tizen>
753 /// ItemChangedEventArgs is a class to record item change event arguments which will sent to user.
755 /// <since_tizen> 6 </since_tizen>
756 public class ItemChangedEventArgs : EventArgs
758 /// <summary> Previous selected index of Tab </summary>
759 /// <since_tizen> 6 </since_tizen>
760 public int PreviousIndex;
761 /// <summary> Current selected index of Tab </summary>
762 /// <since_tizen> 6 </since_tizen>
763 public int CurrentIndex;