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 if (value == null || textColorSelector == null)
306 Tizen.Log.Fatal("NUI", "[Exception] Tab.TextColorSelector is null");
307 throw new NullReferenceException("Tab.TextColorSelector is null");
311 textColorSelector.Clone(value);
317 /// Add tab item by item data. The added item will be added to end of all items automatically.
319 /// <param name="itemData">Item data which will apply to tab item view.</param>
320 /// <since_tizen> 6 </since_tizen>
321 public void AddItem(TabItemData itemData)
323 AddItemByIndex(itemData, itemList.Count);
327 /// Insert tab item by item data. The inserted item will be added to the special position by index automatically.
329 /// <param name="itemData">Item data which will apply to tab item view.</param>
330 /// <param name="index">Position index where will be inserted.</param>
331 /// <since_tizen> 6 </since_tizen>
332 public void InsertItem(TabItemData itemData, int index)
334 AddItemByIndex(itemData, index);
338 /// Delete tab item by index.
340 /// <param name="itemIndex">Position index where will be deleted.</param>
341 /// <since_tizen> 6 </since_tizen>
342 public void DeleteItem(int itemIndex)
344 if(itemList == null || itemIndex < 0 || itemIndex >= itemList.Count)
349 if (curIndex > itemIndex || (curIndex == itemIndex && itemIndex == itemList.Count - 1))
354 Remove(itemList[itemIndex]);
355 itemList[itemIndex].Dispose();
356 itemList.RemoveAt(itemIndex);
362 /// Apply style to tab.
364 /// <param name="viewStyle">The style to apply.</param>
365 /// <since_tizen> 8 </since_tizen>
366 public override void ApplyStyle(ViewStyle viewStyle)
368 base.ApplyStyle(viewStyle);
370 TabStyle tabStyle = viewStyle as TabStyle;
372 if (null != tabStyle)
374 if (null == underline)
376 underline = new View()
378 PositionUsesPivotPoint = true,
379 ParentOrigin = Tizen.NUI.ParentOrigin.BottomLeft,
380 PivotPoint = Tizen.NUI.PivotPoint.BottomLeft,
383 CreateUnderLineAnimation();
386 underline.ApplyStyle(Style.UnderLine);
391 /// Dispose Tab and all children on it.
393 /// <param name="type">Dispose type.</param>
394 /// <since_tizen> 6 </since_tizen>
395 protected override void Dispose(DisposeTypes type)
402 if (type == DisposeTypes.Explicit)
404 if(underlineAni != null)
406 if(underlineAni.State == Animation.States.Playing)
410 underlineAni.Dispose();
413 Utility.Dispose(underline);
416 for(int i = 0; i < itemList.Count; i++)
419 itemList[i].Dispose();
433 /// <since_tizen> 6 </since_tizen>
434 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
435 [EditorBrowsable(EditorBrowsableState.Never)]
436 protected override void OnUpdate()
444 /// <returns>The default tab style.</returns>
445 /// <since_tizen> 8 </since_tizen>
446 protected override ViewStyle GetViewStyle()
448 return new TabStyle();
452 /// Theme change callback when theme is changed, this callback will be trigger.
454 /// <param name="sender">The sender</param>
455 /// <param name="e">The event data</param>
456 /// <since_tizen> 8 </since_tizen>
457 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
459 TabStyle tabStyle = StyleManager.Instance.GetViewStyle(style) as TabStyle;
460 if (tabStyle != null)
462 Style.CopyFrom(tabStyle);
467 /// Layout child in Tab and it can be override by user.
469 /// <since_tizen> 6 </since_tizen>
470 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
471 [EditorBrowsable(EditorBrowsableState.Never)]
472 protected virtual void LayoutChild()
474 if (itemList == null)
479 int totalNum = itemList.Count;
485 int preX = (int)Style.ItemPadding.Start;
487 int itemSpace = Style.ItemSpace;
489 if (LayoutDirection == ViewLayoutDirectionType.LTR)
491 if (Style.UseTextNaturalSize == true)
493 for (int i = 0; i < totalNum; i++)
495 preW = (itemList[i].TextItem.NaturalSize2D != null ? itemList[i].TextItem.NaturalSize2D.Width : 0);
496 itemList[i].Position2D.X = preX;
497 itemList[i].Size2D.Width = preW;
498 preX = itemList[i].Position2D.X + preW + itemSpace;
499 itemList[i].Index = i;
504 preW = (Size2D.Width - (int)Style.ItemPadding.Start - (int)Style.ItemPadding.End) / totalNum;
505 for (int i = 0; i < totalNum; i++)
507 itemList[i].Position2D.X = preX;
508 itemList[i].Size2D.Width = preW;
509 preX = itemList[i].Position2D.X + preW + itemSpace;
510 itemList[i].Index = i;
516 preX = (int)Style.ItemPadding.End;
517 if (Style.UseTextNaturalSize == true)
519 int w = Size2D.Width;
520 for (int i = 0; i < totalNum; i++)
522 preW = (itemList[i].NaturalSize2D != null ? itemList[i].NaturalSize2D.Width : 0);
523 itemList[i].Position2D.X = w - preW - preX;
524 itemList[i].Size2D.Width = preW;
525 preX = w - itemList[i].Position2D.X + itemSpace;
526 itemList[i].Index = i;
531 preW = (Size2D.Width - (int)Style.ItemPadding.Start - (int)Style.ItemPadding.End) / totalNum;
532 for (int i = totalNum - 1; i >= 0; i--)
534 itemList[i].Position2D.X = preX;
535 itemList[i].Size2D.Width = preW;
536 preX = itemList[i].Position2D.X + preW + itemSpace;
537 itemList[i].Index = i;
541 UpdateUnderLinePos();
544 private void Initialize()
546 LayoutDirectionChanged += OnLayoutDirectionChanged;
549 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
554 private void AddItemByIndex(TabItemData itemData, int index)
556 if (null == itemData) return;
558 int topSpace = (int)Style.ItemPadding.Top;
559 if (Style.UnderLine != null && Style.UnderLine.Size != null)
561 h = (int)Style.UnderLine.Size.Height;
564 Tab.TabItem item = new TabItem();
565 item.TextItem.ApplyStyle(Style.Text);
567 item.Text = itemData.Text;
568 item.Size2D.Height = Size2D.Height - h - topSpace;
569 item.Position2D.Y = topSpace;
570 item.TouchEvent += ItemTouchEvent;
573 if (index >= itemList.Count)
579 itemList.Insert(index, item);
585 private void UpdateItems()
588 if (itemList != null && curIndex >= 0 && curIndex < itemList.Count)
590 itemList[curIndex].ControlState = ControlStates.Selected;
591 UpdateUnderLinePos();
595 if (underline != null)
602 private void CreateUnderLineAnimation()
604 if (underlineAni == null)
606 underlineAni = new Animation(aniTime);
610 private void UpdateUnderLinePos()
612 if (underline == null || Style.UnderLine == null || Style.UnderLine.Size == null
613 || itemList == null || itemList.Count <= 0)
618 Style.UnderLine.Size.Width = itemList[curIndex].Size2D.Width;
620 underline.Size2D = new Size2D(itemList[curIndex].Size2D.Width, (int)Style.UnderLine.Size.Height);
621 underline.BackgroundColor = Style.UnderLine.BackgroundColor.All;
624 CreateUnderLineAnimation();
625 if (underlineAni.State == Animation.States.Playing)
629 underlineAni.Clear();
630 underlineAni.AnimateTo(underline, "PositionX", itemList[curIndex].Position2D.X);
635 underline.Position2D.X = itemList[curIndex].Position2D.X;
636 isNeedAnimation = true;
642 private void UpdateSelectedItem(TabItem item)
644 if(item == null || curIndex == item.Index)
649 ItemChangedEventArgs e = new ItemChangedEventArgs
651 PreviousIndex = curIndex,
652 CurrentIndex = item.Index
654 ItemChangedEvent?.Invoke(this, e);
656 itemList[curIndex].ControlState = ControlStates.Normal;
657 curIndex = item.Index;
658 itemList[curIndex].ControlState = ControlStates.Selected;
660 UpdateUnderLinePos();
663 private bool ItemTouchEvent(object source, TouchEventArgs e)
665 TabItem item = source as TabItem;
670 PointStateType state = e.Touch.GetState(0);
671 if (state == PointStateType.Up)
673 UpdateSelectedItem(item);
679 internal class TabItem : View
681 public TabItem() : base()
683 TextItem = new TextLabel()
685 ParentOrigin = Tizen.NUI.ParentOrigin.Center,
686 PivotPoint = Tizen.NUI.PivotPoint.Center,
687 PositionUsesPivotPoint = true,
688 WidthResizePolicy = ResizePolicyType.FillToParent,
689 HeightResizePolicy = ResizePolicyType.FillToParent,
690 HorizontalAlignment = HorizontalAlignment.Center,
691 VerticalAlignment = VerticalAlignment.Center
695 EnableControlStatePropagation = true;
708 return TextItem.Text;
712 TextItem.Text = value;
716 internal TextLabel TextItem
724 /// TabItemData is a class to record all data which will be applied to Tab item.
726 /// <since_tizen> 6 </since_tizen>
727 public class TabItemData
730 /// Text string in tab item view.
732 /// <since_tizen> 6 </since_tizen>
741 /// ItemChangedEventArgs is a class to record item change event arguments which will sent to user.
743 /// <since_tizen> 6 </since_tizen>
744 public class ItemChangedEventArgs : EventArgs
746 /// <summary> Previous selected index of Tab </summary>
747 /// <since_tizen> 6 </since_tizen>
748 public int PreviousIndex;
749 /// <summary> Current selected index of Tab </summary>
750 /// <since_tizen> 6 </since_tizen>
751 public int CurrentIndex;