[NUI] Modify TabContent to help customizing class
[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         private void Initialize()
89         {
90             Layout = new LinearLayout() { LinearOrientation = LinearLayout.Orientation.Vertical };
91             WidthSpecification = LayoutParamPolicies.MatchParent;
92             HeightSpecification = LayoutParamPolicies.MatchParent;
93
94             InitTabBar();
95             InitContent();
96
97             // To show TabBar's shadow TabBar is raised above Content.
98             TabBar.RaiseAbove(Content);
99         }
100
101         /// <summary>
102         /// Creates a new instance of TabView.
103         /// </summary>
104         /// <since_tizen> 9 </since_tizen>
105         public TabView()
106         {
107             Initialize();
108         }
109
110         /// <summary>
111         /// Creates a new instance of a TabView with style.
112         /// </summary>
113         /// <param name="style">A style applied to the newly created TabView.</param>
114         [EditorBrowsable(EditorBrowsableState.Never)]
115         public TabView(ControlStyle style) : base(style)
116         {
117             Initialize();
118         }
119
120         /// <inheritdoc/>
121         [EditorBrowsable(EditorBrowsableState.Never)]
122         public override void OnInitialize()
123         {
124             base.OnInitialize();
125
126             AccessibilityRole = Role.PageTabList;
127         }
128
129         private void InitTabBar()
130         {
131             if (tabBar != null)
132             {
133                 return;
134             }
135
136             tabBar = new TabBar();
137             tabBar.TabButtonSelected += tabButtonSelectedHandler;
138
139             Add(tabBar);
140         }
141
142         private void InitContent()
143         {
144             if (content != null)
145             {
146                 return;
147             }
148
149             content = new TabContent();
150             content.WidthSpecification = LayoutParamPolicies.MatchParent;
151             content.HeightSpecification = LayoutParamPolicies.MatchParent;
152
153             Add(content);
154         }
155
156         private void tabButtonSelectedHandler(object sender, TabButtonSelectedEventArgs args)
157         {
158             if ((content != null) && (content.ViewCount > args.Index))
159             {
160                 content.SelectContentView(args.Index);
161             }
162         }
163
164         /// <summary>
165         /// Gets TabBar of TabView.
166         /// </summary>
167         /// <since_tizen> 9 </since_tizen>
168         public TabBar TabBar
169         {
170             get
171             {
172                 return tabBar;
173             }
174
175             [EditorBrowsable(EditorBrowsableState.Never)]
176             protected set
177             {
178                 if (tabBar != null)
179                 {
180                     tabBar.TabButtonSelected -= tabButtonSelectedHandler;
181                     Utility.Dispose(tabBar);
182                 }
183
184                 tabBar = value;
185                 Add(tabBar);
186             }
187         }
188
189         /// <summary>
190         /// Gets TabContent of TabView.
191         /// </summary>
192         /// <since_tizen> 9 </since_tizen>
193         public TabContent Content
194         {
195             get
196             {
197                 return content;
198             }
199
200             [EditorBrowsable(EditorBrowsableState.Never)]
201             protected set
202             {
203                 if (content != null)
204                 {
205                     Utility.Dispose(content);
206                 }
207
208                 content = value;
209                 Add(content);
210             }
211         }
212
213         /// <summary>
214         /// Adds a tab with tab button and content view.
215         /// </summary>
216         /// <param name="tabButton">A tab button to be added.</param>
217         /// <param name="view">A content view to be added.</param>
218         /// <since_tizen> 9 </since_tizen>
219         public void AddTab(TabButton tabButton, View view)
220         {
221             if (TabBar != null)
222             {
223                 TabBar.AddTabButton(tabButton);
224             }
225
226             if (Content != null)
227             {
228                 Content.AddContentView(view);
229             }
230         }
231
232         /// <summary>
233         /// Removes a tab at the specified index of TabView.
234         /// The indices of tabs(tab buttons and views) in TabView are basically the order of adding to TabView by <see cref="TabView.AddTab"/>.
235         /// 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.
236         /// </summary>
237         /// <param name="index">The index of a tab(tab button and view) in TabView where the tab will be removed.</param>
238         /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of tabs.</exception>
239         /// <since_tizen> 9 </since_tizen>
240         public void RemoveTab(int index)
241         {
242             var tabButton = TabBar.GetTabButton(index);
243             if (tabButton != null)
244             {
245                 TabBar.RemoveTabButton(tabButton);
246             }
247
248             var view = Content.GetView(index);
249             if (view != null)
250             {
251                 Content.RemoveContentView(view);
252             }
253         }
254
255         /// <inheritdoc/>
256         [EditorBrowsable(EditorBrowsableState.Never)]
257         protected override void Dispose(DisposeTypes type)
258         {
259             if (disposed)
260             {
261                 return;
262             }
263
264             if (type == DisposeTypes.Explicit)
265             {
266                 if (tabBar != null)
267                 {
268                     tabBar.TabButtonSelected -= tabButtonSelectedHandler;
269                     Utility.Dispose(tabBar);
270                 }
271
272                 if (content != null)
273                 {
274                     Utility.Dispose(content);
275                 }
276             }
277
278             base.Dispose(type);
279         }
280     }
281 }