ec70f8c02c0b1e5cd84b9b2cfc1d75d98ad1b219
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / TabView.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17 using System;
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20
21 namespace Tizen.NUI.Components
22 {
23     /// <summary>
24     /// TabView is a class which contains a TabBar and TabContent.
25     /// TabView adds TabButtons and Views to TabBar and TabContent in TabView by <see cref="TabView.AddTab"/>.
26     /// TabView removes TabButtons and Views from TabBar and TabContent in TabView by <see cref="TabView.RemoveTab"/>.
27     /// TabView selects a view from the TabContent according to the selected TabButton in the TabBar.
28     ///
29     /// <example>
30     /// <code>
31     /// var tabView = new TabView()
32     /// {
33     ///     WidthSpecification = LayoutParamPolicies.MatchParent,
34     ///     HeightSpecification = LayoutParamPolicies.MatchParent,
35     /// };
36     ///
37     /// var tabButton = new TabButton()
38     /// {
39     ///     Text = "Tab#1"
40     /// };
41     ///
42     /// var content = new View()
43     /// {
44     ///     BackgroundColor = Color.Red,
45     ///     WidthSpecification = LayoutParamPolicies.MatchParent,
46     ///     HeightSpecification = LayoutParamPolicies.MatchParent,
47     /// };
48     ///
49     /// tabView.AddTab(tabButton, content);
50     ///
51     /// var tabButton2 = new TabButton()
52     /// {
53     ///     Text = "Tab#2"
54     /// };
55     ///
56     /// var content2 = new View()
57     /// {
58     ///     BackgroundColor = Color.Green,
59     ///     WidthSpecification = LayoutParamPolicies.MatchParent,
60     ///     HeightSpecification = LayoutParamPolicies.MatchParent,
61     /// };
62     ///
63     /// tabView.AddTab(tabButton2, content2);
64     ///
65     /// var tabButton3 = new TabButton()
66     /// {
67     ///     Text = "Tab#3"
68     /// };
69     ///
70     /// var content3 = new View()
71     /// {
72     ///     BackgroundColor = Color.Blue,
73     ///     WidthSpecification = LayoutParamPolicies.MatchParent,
74     ///     HeightSpecification = LayoutParamPolicies.MatchParent,
75     /// };
76     ///
77     /// tabView.AddTab(tabButton3, content3);
78     /// </code>
79     /// </example>
80     /// </summary>
81     /// <since_tizen> 9 </since_tizen>
82     public class TabView : Control
83     {
84         private TabBar tabBar = null;
85
86         private TabContent content = null;
87
88         /// <summary>
89         /// Creates a new instance of TabView.
90         /// </summary>
91         /// <since_tizen> 9 </since_tizen>
92         public TabView()
93         {
94             Layout = new LinearLayout() { LinearOrientation = LinearLayout.Orientation.Vertical };
95             WidthSpecification = LayoutParamPolicies.MatchParent;
96             HeightSpecification = LayoutParamPolicies.MatchParent;
97
98             InitTabBar();
99             InitContent();
100
101             // To show TabBar's shadow TabBar is raised above Content.
102             TabBar.RaiseAbove(Content);
103         }
104
105         /// <inheritdoc/>
106         [EditorBrowsable(EditorBrowsableState.Never)]
107         public override void OnInitialize()
108         {
109             base.OnInitialize();
110
111             AccessibilityRole = Role.PageTabList;
112         }
113
114         private void InitTabBar()
115         {
116             if (tabBar != null)
117             {
118                 return;
119             }
120
121             tabBar = new TabBar();
122             tabBar.TabButtonSelected += tabButtonSelectedHandler;
123
124             Add(tabBar);
125         }
126
127         private void InitContent()
128         {
129             if (content != null)
130             {
131                 return;
132             }
133
134             content = new TabContent();
135             content.WidthSpecification = LayoutParamPolicies.MatchParent;
136             content.HeightSpecification = LayoutParamPolicies.MatchParent;
137
138             Add(content);
139         }
140
141         private void tabButtonSelectedHandler(object sender, TabButtonSelectedEventArgs args)
142         {
143             if ((content != null) && (content.ViewCount > args.Index))
144             {
145                 content.Select(args.Index);
146             }
147         }
148
149         /// <summary>
150         /// Gets TabBar of TabView.
151         /// </summary>
152         /// <since_tizen> 9 </since_tizen>
153         public TabBar TabBar
154         {
155             get
156             {
157                 return tabBar;
158             }
159         }
160
161         /// <summary>
162         /// Gets TabContent of TabView.
163         /// </summary>
164         /// <since_tizen> 9 </since_tizen>
165         public TabContent Content
166         {
167             get
168             {
169                 return content;
170             }
171         }
172
173         /// <summary>
174         /// Adds a tab with tab button and content view.
175         /// </summary>
176         /// <param name="tabButton">A tab button to be added.</param>
177         /// <param name="view">A content view to be added.</param>
178         /// <since_tizen> 9 </since_tizen>
179         public void AddTab(TabButton tabButton, View view)
180         {
181             if (TabBar != null)
182             {
183                 TabBar.AddTabButton(tabButton);
184             }
185
186             if (Content != null)
187             {
188                 Content.AddView(view);
189             }
190         }
191
192         /// <summary>
193         /// Removes a tab at the specified index of TabView.
194         /// The indices of tabs(tab buttons and views) in TabView are basically the order of adding to TabView by <see cref="TabView.AddTab"/>.
195         /// So the index of a tab(tab button and view) in TabView can be changed whenever <see cref="TabView.AddTab"/> or <see cref="TabView.RemoveTab"/> is called.
196         /// </summary>
197         /// <param name="index">The index of a tab(tab button and view) in TabView where the tab will be removed.</param>
198         /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of tabs.</exception>
199         /// <since_tizen> 9 </since_tizen>
200         public void RemoveTab(int index)
201         {
202             var tabButton = TabBar.GetTabButton(index);
203             if (tabButton != null)
204             {
205                 TabBar.RemoveTabButton(tabButton);
206             }
207
208             var view = Content.GetView(index);
209             if (view != null)
210             {
211                 Content.RemoveView(view);
212             }
213         }
214
215         /// <inheritdoc/>
216         [EditorBrowsable(EditorBrowsableState.Never)]
217         protected override void Dispose(DisposeTypes type)
218         {
219             if (disposed)
220             {
221                 return;
222             }
223
224             if (type == DisposeTypes.Explicit)
225             {
226                 if (tabBar != null)
227                 {
228                     tabBar.TabButtonSelected -= tabButtonSelectedHandler;
229                     Utility.Dispose(tabBar);
230                 }
231
232                 if (content != null)
233                 {
234                     Utility.Dispose(content);
235                 }
236             }
237
238             base.Dispose(type);
239         }
240     }
241 }