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;
293 /// Text color selector in Tab.
295 /// <since_tizen> 6 </since_tizen>
296 public ColorSelector TextColorSelector
300 return (ColorSelector)Style?.Text?.TextColor;
304 if (null != value && null != Style?.Text)
306 Style.Text.TextColor = value.Clone() as ColorSelector;
312 /// Add tab item by item data. The added item will be added to end of all items automatically.
314 /// <param name="itemData">Item data which will apply to tab item view.</param>
315 /// <since_tizen> 6 </since_tizen>
316 public void AddItem(TabItemData itemData)
318 AddItemByIndex(itemData, itemList.Count);
322 /// Insert tab item by item data. The inserted item will be added to the special position by index automatically.
324 /// <param name="itemData">Item data which will apply to tab item view.</param>
325 /// <param name="index">Position index where will be inserted.</param>
326 /// <since_tizen> 6 </since_tizen>
327 public void InsertItem(TabItemData itemData, int index)
329 AddItemByIndex(itemData, index);
333 /// Delete tab item by index.
335 /// <param name="itemIndex">Position index where will be deleted.</param>
336 /// <since_tizen> 6 </since_tizen>
337 public void DeleteItem(int itemIndex)
339 if(itemList == null || itemIndex < 0 || itemIndex >= itemList.Count)
344 if (curIndex > itemIndex || (curIndex == itemIndex && itemIndex == itemList.Count - 1))
349 Remove(itemList[itemIndex]);
350 itemList[itemIndex].Dispose();
351 itemList.RemoveAt(itemIndex);
356 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
357 [EditorBrowsable(EditorBrowsableState.Never)]
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();
423 /// Update Tab by attributes.
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()
434 /// Get Tab attribues.
436 /// <since_tizen> 6 </since_tizen>
437 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
438 [EditorBrowsable(EditorBrowsableState.Never)]
439 protected override ViewStyle GetViewStyle()
441 return new TabStyle();
445 /// Theme change callback when theme is changed, this callback will be trigger.
447 /// <since_tizen> 6 </since_tizen>
448 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
449 [EditorBrowsable(EditorBrowsableState.Never)]
450 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
452 TabStyle tempAttributes = StyleManager.Instance.GetAttributes(style) as TabStyle;
453 if (tempAttributes != null)
455 Style.CopyFrom(tempAttributes);
460 /// Layout child in Tab and it can be override by user.
462 /// <since_tizen> 6 </since_tizen>
463 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
464 [EditorBrowsable(EditorBrowsableState.Never)]
465 protected virtual void LayoutChild()
467 if (itemList == null)
472 int totalNum = itemList.Count;
478 int preX = (int)Style.ItemPadding.Start;
480 int itemSpace = Style.ItemSpace;
482 if (LayoutDirection == ViewLayoutDirectionType.LTR)
484 if (Style.UseTextNaturalSize == true)
486 for (int i = 0; i < totalNum; i++)
488 preW = (itemList[i].NaturalSize2D != null ? itemList[i].NaturalSize2D.Width : 0);
489 itemList[i].Position2D.X = preX;
490 itemList[i].Size2D.Width = preW;
491 preX = itemList[i].Position2D.X + preW + itemSpace;
492 itemList[i].Index = i;
497 preW = (Size2D.Width - (int)Style.ItemPadding.Start - (int)Style.ItemPadding.End) / totalNum;
498 for (int i = 0; i < totalNum; i++)
500 itemList[i].Position2D.X = preX;
501 itemList[i].Size2D.Width = preW;
502 preX = itemList[i].Position2D.X + preW + itemSpace;
503 itemList[i].Index = i;
509 preX = (int)Style.ItemPadding.End;
510 if (Style.UseTextNaturalSize == true)
512 int w = Size2D.Width;
513 for (int i = 0; i < totalNum; i++)
515 preW = (itemList[i].NaturalSize2D != null ? itemList[i].NaturalSize2D.Width : 0);
516 itemList[i].Position2D.X = w - preW - preX;
517 itemList[i].Size2D.Width = preW;
518 preX = w - itemList[i].Position2D.X + itemSpace;
519 itemList[i].Index = i;
524 preW = (Size2D.Width - (int)Style.ItemPadding.Start - (int)Style.ItemPadding.End) / totalNum;
525 for (int i = totalNum - 1; i >= 0; i--)
527 itemList[i].Position2D.X = preX;
528 itemList[i].Size2D.Width = preW;
529 preX = itemList[i].Position2D.X + preW + itemSpace;
530 itemList[i].Index = i;
534 UpdateUnderLinePos();
537 private void Initialize()
539 LayoutDirectionChanged += OnLayoutDirectionChanged;
542 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
547 private void AddItemByIndex(TabItemData itemData, int index)
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 CreateUnderLineAttributes()
596 if (Style.UnderLine == null)
598 Style.UnderLine = new ViewStyle()
600 PositionUsesPivotPoint = true,
601 ParentOrigin = Tizen.NUI.ParentOrigin.BottomLeft,
602 PivotPoint = Tizen.NUI.PivotPoint.BottomLeft,
607 private void CreateUnderLineAnimation()
609 if (underlineAni == null)
611 underlineAni = new Animation(aniTime);
615 private void UpdateUnderLinePos()
617 if (underline == null || Style.UnderLine == null || Style.UnderLine.Size == null
618 || itemList == null || itemList.Count <= 0)
623 Style.UnderLine.Size.Width = itemList[curIndex].Size2D.Width;
625 underline.Size2D = new Size2D(itemList[curIndex].Size2D.Width, (int)Style.UnderLine.Size.Height);
626 underline.BackgroundColor = Style.UnderLine.BackgroundColor.All;
629 CreateUnderLineAnimation();
630 if (underlineAni.State == Animation.States.Playing)
634 underlineAni.Clear();
635 underlineAni.AnimateTo(underline, "PositionX", itemList[curIndex].Position2D.X);
640 underline.Position2D.X = itemList[curIndex].Position2D.X;
641 isNeedAnimation = true;
647 private void UpdateSelectedItem(TabItem item)
649 if(item == null || curIndex == item.Index)
654 ItemChangedEventArgs e = new ItemChangedEventArgs
656 PreviousIndex = curIndex,
657 CurrentIndex = item.Index
659 ItemChangedEvent?.Invoke(this, e);
661 itemList[curIndex].ControlState = ControlStates.Normal;
662 curIndex = item.Index;
663 itemList[curIndex].ControlState = ControlStates.Selected;
665 UpdateUnderLinePos();
668 private bool ItemTouchEvent(object source, TouchEventArgs e)
670 TabItem item = source as TabItem;
675 PointStateType state = e.Touch.GetState(0);
676 if (state == PointStateType.Up)
678 UpdateSelectedItem(item);
684 internal class TabItem : View
686 public TabItem() : base()
688 TextItem = new TextLabel()
690 ParentOrigin = Tizen.NUI.ParentOrigin.Center,
691 PivotPoint = Tizen.NUI.PivotPoint.Center,
692 PositionUsesPivotPoint = true,
693 WidthResizePolicy = ResizePolicyType.FillToParent,
694 HeightResizePolicy = ResizePolicyType.FillToParent,
695 HorizontalAlignment = HorizontalAlignment.Center,
696 VerticalAlignment = VerticalAlignment.Center
711 return TextItem.Text;
715 TextItem.Text = value;
719 internal TextLabel TextItem
727 /// TabItemData is a class to record all data which will be applied to Tab item.
729 /// <since_tizen> 6 </since_tizen>
730 public class TabItemData
733 /// Text string in tab item view.
735 /// <since_tizen> 6 </since_tizen>
744 /// ItemChangedEventArgs is a class to record item change event arguments which will sent to user.
746 /// <since_tizen> 6 </since_tizen>
747 public class ItemChangedEventArgs : EventArgs
749 /// <summary> Previous selected index of Tab </summary>
750 /// <since_tizen> 6 </since_tizen>
751 public int PreviousIndex;
752 /// <summary> Current selected index of Tab </summary>
753 /// <since_tizen> 6 </since_tizen>
754 public int CurrentIndex;