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 ?? false;
116 Style.UseTextNaturalSize = value;
123 /// Gap between items.
125 /// <since_tizen> 6 </since_tizen>
130 return Style?.ItemSpace ?? 0;
136 Style.ItemSpace = value;
143 /// Space in Tab. Sequence as Left, Right, Top, Bottom
145 /// <since_tizen> 6 </since_tizen>
159 /// Item paddings in Tab. Sequence as Left, Right, Top, Bottom
161 /// <since_tizen> 6 </since_tizen>
162 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
163 [EditorBrowsable(EditorBrowsableState.Never)]
164 public Extents ItemPadding
172 if(null != value && null != Style?.ItemPadding)
174 Style.ItemPadding.CopyFrom(value);
178 space = new Extents((ushort start, ushort end, ushort top, ushort bottom) =>
180 Style.ItemPadding.Start = start;
181 Style.ItemPadding.End = end;
182 Style.ItemPadding.Top = top;
183 Style.ItemPadding.Bottom = bottom;
185 }, value.Start, value.End, value.Top, value.Bottom);
189 space.CopyFrom(value);
198 /// UnderLine view's size in Tab.
200 /// <since_tizen> 6 </since_tizen>
201 public Size UnderLineSize
205 return Style?.UnderLine?.Size;
209 if (null != Style?.UnderLine)
211 Style.UnderLine.Size = value;
217 /// UnderLine view's background in Tab.
219 /// <since_tizen> 6 </since_tizen>
220 public Color UnderLineBackgroundColor
224 return Style?.UnderLine?.BackgroundColor?.All;
228 if (null != Style?.UnderLine)
230 Style.UnderLine.BackgroundColor = value;
236 /// Text point size in Tab.
238 /// <since_tizen> 6 </since_tizen>
239 public float PointSize
243 return Style?.Text?.PointSize?.All ?? 0;
247 if (null != Style?.Text)
249 Style.Text.PointSize = value;
255 /// Text font family in Tab.
257 /// <since_tizen> 6 </since_tizen>
258 public string FontFamily
262 return Style?.Text?.FontFamily?.All;
266 if (null != Style?.Text)
268 Style.Text.FontFamily = value;
274 /// Text color in Tab.
276 /// <since_tizen> 6 </since_tizen>
277 public Color TextColor
281 return Style?.Text?.TextColor?.All;
285 if (null != Style?.Text)
287 Style.Text.TextColor = value;
292 private ColorSelector textColorSelector = new ColorSelector();
294 /// Text color selector in Tab.
296 /// <since_tizen> 6 </since_tizen>
297 public ColorSelector TextColorSelector
301 return textColorSelector;
305 textColorSelector.Clone(value);
310 /// Add tab item by item data. The added item will be added to end of all items automatically.
312 /// <param name="itemData">Item data which will apply to tab item view.</param>
313 /// <since_tizen> 6 </since_tizen>
314 public void AddItem(TabItemData itemData)
316 AddItemByIndex(itemData, itemList.Count);
320 /// Insert tab item by item data. The inserted item will be added to the special position by index automatically.
322 /// <param name="itemData">Item data which will apply to tab item view.</param>
323 /// <param name="index">Position index where will be inserted.</param>
324 /// <since_tizen> 6 </since_tizen>
325 public void InsertItem(TabItemData itemData, int index)
327 AddItemByIndex(itemData, index);
331 /// Delete tab item by index.
333 /// <param name="itemIndex">Position index where will be deleted.</param>
334 /// <since_tizen> 6 </since_tizen>
335 public void DeleteItem(int itemIndex)
337 if(itemList == null || itemIndex < 0 || itemIndex >= itemList.Count)
342 if (curIndex > itemIndex || (curIndex == itemIndex && itemIndex == itemList.Count - 1))
347 Remove(itemList[itemIndex]);
348 itemList[itemIndex].Dispose();
349 itemList.RemoveAt(itemIndex);
354 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
355 [EditorBrowsable(EditorBrowsableState.Never)]
356 public override void ApplyStyle(ViewStyle viewStyle)
358 base.ApplyStyle(viewStyle);
360 TabStyle tabStyle = viewStyle as TabStyle;
362 if (null != tabStyle)
364 if (null == underline)
366 underline = new View()
368 PositionUsesPivotPoint = true,
369 ParentOrigin = Tizen.NUI.ParentOrigin.BottomLeft,
370 PivotPoint = Tizen.NUI.PivotPoint.BottomLeft,
373 CreateUnderLineAnimation();
376 underline.ApplyStyle(Style.UnderLine);
381 /// Dispose Tab and all children on it.
383 /// <param name="type">Dispose type.</param>
384 /// <since_tizen> 6 </since_tizen>
385 protected override void Dispose(DisposeTypes type)
392 if (type == DisposeTypes.Explicit)
394 if(underlineAni != null)
396 if(underlineAni.State == Animation.States.Playing)
400 underlineAni.Dispose();
403 Utility.Dispose(underline);
406 for(int i = 0; i < itemList.Count; i++)
409 itemList[i].Dispose();
421 /// Update Tab by attributes.
423 /// <since_tizen> 6 </since_tizen>
424 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
425 [EditorBrowsable(EditorBrowsableState.Never)]
426 protected override void OnUpdate()
432 /// Get Tab attribues.
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 ViewStyle GetViewStyle()
439 return new TabStyle();
443 /// Theme change callback when theme is changed, this callback will be trigger.
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 void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
450 TabStyle tempAttributes = StyleManager.Instance.GetAttributes(style) as TabStyle;
451 if (tempAttributes != null)
453 Style.CopyFrom(tempAttributes);
458 /// Layout child in Tab and it can be override by user.
460 /// <since_tizen> 6 </since_tizen>
461 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
462 [EditorBrowsable(EditorBrowsableState.Never)]
463 protected virtual void LayoutChild()
465 if (itemList == null)
470 int totalNum = itemList.Count;
476 int preX = (int)Style.ItemPadding.Start;
478 int itemSpace = Style.ItemSpace;
480 if (LayoutDirection == ViewLayoutDirectionType.LTR)
482 if (Style.UseTextNaturalSize == true)
484 for (int i = 0; i < totalNum; i++)
486 preW = (itemList[i].TextItem.NaturalSize2D != null ? itemList[i].TextItem.NaturalSize2D.Width : 0);
487 itemList[i].Position2D.X = preX;
488 itemList[i].Size2D.Width = preW;
489 preX = itemList[i].Position2D.X + preW + itemSpace;
490 itemList[i].Index = i;
495 preW = (Size2D.Width - (int)Style.ItemPadding.Start - (int)Style.ItemPadding.End) / totalNum;
496 for (int i = 0; i < totalNum; i++)
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;
507 preX = (int)Style.ItemPadding.End;
508 if (Style.UseTextNaturalSize == true)
510 int w = Size2D.Width;
511 for (int i = 0; i < totalNum; i++)
513 preW = (itemList[i].NaturalSize2D != null ? itemList[i].NaturalSize2D.Width : 0);
514 itemList[i].Position2D.X = w - preW - preX;
515 itemList[i].Size2D.Width = preW;
516 preX = w - itemList[i].Position2D.X + itemSpace;
517 itemList[i].Index = i;
522 preW = (Size2D.Width - (int)Style.ItemPadding.Start - (int)Style.ItemPadding.End) / totalNum;
523 for (int i = totalNum - 1; i >= 0; i--)
525 itemList[i].Position2D.X = preX;
526 itemList[i].Size2D.Width = preW;
527 preX = itemList[i].Position2D.X + preW + itemSpace;
528 itemList[i].Index = i;
532 UpdateUnderLinePos();
535 private void Initialize()
537 LayoutDirectionChanged += OnLayoutDirectionChanged;
540 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
545 private void AddItemByIndex(TabItemData itemData, int index)
548 int topSpace = (int)Style.ItemPadding.Top;
549 if (Style.UnderLine != null && Style.UnderLine.Size != null)
551 h = (int)Style.UnderLine.Size.Height;
554 Tab.TabItem item = new TabItem();
555 item.TextItem.ApplyStyle(Style.Text);
557 item.Text = itemData.Text;
558 item.Size2D.Height = Size2D.Height - h - topSpace;
559 item.Position2D.Y = topSpace;
560 item.TouchEvent += ItemTouchEvent;
563 if (index >= itemList.Count)
569 itemList.Insert(index, item);
575 private void UpdateItems()
578 if (itemList != null && curIndex >= 0 && curIndex < itemList.Count)
580 itemList[curIndex].ControlState = ControlStates.Selected;
581 UpdateUnderLinePos();
585 if (underline != null)
592 private void CreateUnderLineAttributes()
594 if (Style.UnderLine == null)
596 Style.UnderLine = new ViewStyle()
598 PositionUsesPivotPoint = true,
599 ParentOrigin = Tizen.NUI.ParentOrigin.BottomLeft,
600 PivotPoint = Tizen.NUI.PivotPoint.BottomLeft,
605 private void CreateUnderLineAnimation()
607 if (underlineAni == null)
609 underlineAni = new Animation(aniTime);
613 private void UpdateUnderLinePos()
615 if (underline == null || Style.UnderLine == null || Style.UnderLine.Size == null
616 || itemList == null || itemList.Count <= 0)
621 Style.UnderLine.Size.Width = itemList[curIndex].Size2D.Width;
623 underline.Size2D = new Size2D(itemList[curIndex].Size2D.Width, (int)Style.UnderLine.Size.Height);
624 underline.BackgroundColor = Style.UnderLine.BackgroundColor.All;
627 CreateUnderLineAnimation();
628 if (underlineAni.State == Animation.States.Playing)
632 underlineAni.Clear();
633 underlineAni.AnimateTo(underline, "PositionX", itemList[curIndex].Position2D.X);
638 underline.Position2D.X = itemList[curIndex].Position2D.X;
639 isNeedAnimation = true;
645 private void UpdateSelectedItem(TabItem item)
647 if(item == null || curIndex == item.Index)
652 ItemChangedEventArgs e = new ItemChangedEventArgs
654 PreviousIndex = curIndex,
655 CurrentIndex = item.Index
657 ItemChangedEvent?.Invoke(this, e);
659 itemList[curIndex].ControlState = ControlStates.Normal;
660 curIndex = item.Index;
661 itemList[curIndex].ControlState = ControlStates.Selected;
663 UpdateUnderLinePos();
666 private bool ItemTouchEvent(object source, TouchEventArgs e)
668 TabItem item = source as TabItem;
673 PointStateType state = e.Touch.GetState(0);
674 if (state == PointStateType.Up)
676 UpdateSelectedItem(item);
682 internal class TabItem : View
684 public TabItem() : base()
686 TextItem = new TextLabel()
688 ParentOrigin = Tizen.NUI.ParentOrigin.Center,
689 PivotPoint = Tizen.NUI.PivotPoint.Center,
690 PositionUsesPivotPoint = true,
691 WidthResizePolicy = ResizePolicyType.FillToParent,
692 HeightResizePolicy = ResizePolicyType.FillToParent,
693 HorizontalAlignment = HorizontalAlignment.Center,
694 VerticalAlignment = VerticalAlignment.Center
709 return TextItem.Text;
713 TextItem.Text = value;
717 internal TextLabel TextItem
725 /// TabItemData is a class to record all data which will be applied to Tab item.
727 /// <since_tizen> 6 </since_tizen>
728 public class TabItemData
731 /// Text string in tab item view.
733 /// <since_tizen> 6 </since_tizen>
742 /// ItemChangedEventArgs is a class to record item change event arguments which will sent to user.
744 /// <since_tizen> 6 </since_tizen>
745 public class ItemChangedEventArgs : EventArgs
747 /// <summary> Previous selected index of Tab </summary>
748 /// <since_tizen> 6 </since_tizen>
749 public int PreviousIndex;
750 /// <summary> Current selected index of Tab </summary>
751 /// <since_tizen> 6 </since_tizen>
752 public int CurrentIndex;