[NUI] Rebase DevelNUI (#2507)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Navigation / Page.cs
1 /*
2  * Copyright(c) 2020 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.Collections.Generic;
19 using System.ComponentModel;
20 using Tizen.NUI.BaseComponents;
21 using Tizen.NUI.Binding;
22 using System.Windows.Input;
23
24 namespace Tizen.NUI.Components
25 {
26     /// <summary>
27     /// PageAppearingEventArgs is a class to record page appearing event arguments which will be sent to user.
28     /// </summary>
29     [EditorBrowsable(EditorBrowsableState.Never)]
30     public class PageAppearingEventArgs : EventArgs
31     {
32     }
33
34     /// <summary>
35     /// PageDisappearingEventArgs is a class to record page disappearing event arguments which will be sent to user.
36     /// </summary>
37     [EditorBrowsable(EditorBrowsableState.Never)]
38     public class PageDisappearingEventArgs : EventArgs
39     {
40     }
41
42     /// <summary>
43     /// The Page class is a class which is an element of navigation.
44     /// </summary>
45     [EditorBrowsable(EditorBrowsableState.Never)]
46     public class Page : Control
47     {
48         private AppBar _appBar = null;
49         private View _content = null;
50
51         /// <summary>
52         /// Creates a new instance of a Page.
53         /// </summary>
54         /// <param name="content">The content to set to Content of Page.</param>
55         [EditorBrowsable(EditorBrowsableState.Never)]
56         public Page(View content = null) : this(null, content)
57         {
58         }
59
60         /// <summary>
61         /// Creates a new instance of a Page.
62         /// </summary>
63         /// <param name="appBar">The content to set to AppBar of Page.</param>
64         /// <param name="content">The content to set to Content of Page.</param>
65         [EditorBrowsable(EditorBrowsableState.Never)]
66         public Page(AppBar appBar, View content = null) : base()
67         {
68             //AppBar and Content are located verically.
69             var linearLayout = new LinearLayout();
70             linearLayout.LinearOrientation = LinearLayout.Orientation.Vertical;
71             Layout = linearLayout;
72
73             //Page fills to parent by default.
74             WidthResizePolicy = ResizePolicyType.FillToParent;
75             HeightResizePolicy = ResizePolicyType.FillToParent;
76
77             if (appBar)
78             {
79                 AppBar = appBar;
80             }
81
82             if (content)
83             {
84                 Content = content;
85             }
86         }
87
88         /// <summary>
89         /// Dispose Page and all children on it.
90         /// </summary>
91         /// <param name="type">Dispose type.</param>
92         [EditorBrowsable(EditorBrowsableState.Never)]
93         protected override void Dispose(DisposeTypes type)
94         {
95             if (disposed)
96             {
97                 return;
98             }
99
100             if (type == DisposeTypes.Explicit)
101             {
102                 if (_appBar != null)
103                 {
104                     Utility.Dispose(_appBar);
105                 }
106
107                 if (_content != null)
108                 {
109                     Utility.Dispose(_content);
110                 }
111             }
112
113             base.Dispose(type);
114         }
115
116         /// <summary>
117         /// AppBar of Page. AppBar is added to Children automatically.
118         /// </summary>
119         [EditorBrowsable(EditorBrowsableState.Never)]
120         public AppBar AppBar
121         {
122             get
123             {
124                 return _appBar;
125             }
126             set
127             {
128                 if (_appBar == value)
129                 {
130                     return;
131                 }
132
133                 if (_appBar != null)
134                 {
135                     Remove(_appBar);
136                 }
137
138                 _appBar = value;
139                 if (_appBar == null)
140                 {
141                     return;
142                 }
143
144                 _appBar.Weight = 0.0f;
145
146                 ResetContent();
147             }
148         }
149
150         /// <summary>
151         /// Content of Page. Content is added to Children automatically.
152         /// </summary>
153         [EditorBrowsable(EditorBrowsableState.Never)]
154         public View Content
155         {
156             get
157             {
158                 return _content;
159             }
160             set
161             {
162                 if (_content == value)
163                 {
164                     return;
165                 }
166
167                 if (_content != null)
168                 {
169                     Remove(_content);
170                 }
171
172                 _content = value;
173                 if (_content == null)
174                 {
175                     return;
176                 }
177
178                 _content.Weight = 1.0f;
179
180                 ResetContent();
181             }
182         }
183
184         private void ResetContent()
185         {
186             //To keep the order of AppBar and Content, the existing contents are
187             //removed and added again.
188             if ((_appBar != null) && Children.Contains(_appBar))
189             {
190                 Remove(_appBar);
191             }
192
193             if ((_content != null) && Children.Contains(_content))
194             {
195                 Remove(_content);
196             }
197
198             if (_appBar != null)
199             {
200                 Add(_appBar);
201             }
202
203             if (_content != null)
204             {
205                 Add(_content);
206             }
207         }
208
209         /// <summary>
210         /// An event for the page appearing signal which can be used to subscribe or unsubscribe the event handler provided by the user.
211         /// </summary>
212         [EditorBrowsable(EditorBrowsableState.Never)]
213         public event EventHandler<PageAppearingEventArgs> Appearing;
214
215         /// <summary>
216         /// An event for the page disappearing signal which can be used to subscribe or unsubscribe the event handler provided by the user.
217         /// </summary>
218         [EditorBrowsable(EditorBrowsableState.Never)]
219         public event EventHandler<PageDisappearingEventArgs> Disappearing;
220
221         internal void InvokeAppearing()
222         {
223             Appearing?.Invoke(this, new PageAppearingEventArgs());
224         }
225
226         internal void InvokeDisappearing()
227         {
228             Disappearing?.Invoke(this, new PageDisappearingEventArgs());
229         }
230     }
231 }