9f4ac040a1ee71a5501d91d15f3636084224e6f6
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Navigation / ContentPage.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.ComponentModel;
18 using Tizen.NUI.BaseComponents;
19
20 namespace Tizen.NUI.Components
21 {
22     /// <summary>
23     /// The ContentPage class is a class which is a formatted full screen page.
24     /// ContentPage contains title app bar and content.
25     /// </summary>
26     /// <since_tizen> 9 </since_tizen>
27     public partial class ContentPage : Page
28     {
29         private AppBar appBar = null;
30         private View content = null;
31
32         /// <summary>
33         /// Creates a new instance of a ContentPage.
34         /// </summary>
35         /// <since_tizen> 9 </since_tizen>
36         public ContentPage() : base()
37         {
38             Layout = new AbsoluteLayout();
39
40             // ContentPage fills to parent by default.
41             WidthResizePolicy = ResizePolicyType.FillToParent;
42             HeightResizePolicy = ResizePolicyType.FillToParent;
43         }
44
45         /// <summary>
46         /// Dispose ContentPage and all children on it.
47         /// </summary>
48         /// <param name="type">Dispose type.</param>
49         [EditorBrowsable(EditorBrowsableState.Never)]
50         protected override void Dispose(DisposeTypes type)
51         {
52             if (disposed)
53             {
54                 return;
55             }
56
57             if (type == DisposeTypes.Explicit)
58             {
59                 if (appBar != null)
60                 {
61                     Utility.Dispose(appBar);
62                 }
63
64                 if (content != null)
65                 {
66                     Utility.Dispose(content);
67                 }
68             }
69
70             base.Dispose(type);
71         }
72
73         /// <inheritdoc/>
74         [EditorBrowsable(EditorBrowsableState.Never)]
75         public override void OnInitialize()
76         {
77             base.OnInitialize();
78
79             SetAccessibilityConstructor(Role.PageTab);
80         }
81
82         /// <summary>
83         /// AppBar of ContentPage.
84         /// AppBar is added as a child of ContentPage automatically.
85         /// AppBar is positioned at the top of the Page.
86         /// </summary>
87         /// <since_tizen> 9 </since_tizen>
88         public AppBar AppBar
89         {
90             get
91             {
92                 return GetValue(AppBarProperty) as AppBar;
93             }
94             set
95             {
96                 SetValue(AppBarProperty, value);
97                 NotifyPropertyChanged();
98             }
99         }
100         private AppBar InternalAppBar
101         {
102             get
103             {
104                 return appBar;
105             }
106             set
107             {
108                 if (appBar == value)
109                 {
110                     return;
111                 }
112
113                 if (appBar != null)
114                 {
115                     Remove(appBar);
116                 }
117
118                 appBar = value;
119                 if (appBar == null)
120                 {
121                     return;
122                 }
123
124                 Add(appBar);
125
126                 CalculatePosition();
127             }
128         }
129
130         /// <summary>
131         /// Content of ContentPage.
132         /// Content is added as a child of ContentPage automatically.
133         /// Content is positioned below AppBar.
134         /// Content is resized to fill the full screen except AppBar.
135         /// </summary>
136         /// <since_tizen> 9 </since_tizen>
137         public View Content
138         {
139             get
140             {
141                 return GetValue(ContentProperty) as View;
142             }
143             set
144             {
145                 SetValue(ContentProperty, value);
146                 NotifyPropertyChanged();
147             }
148         }
149         private View InternalContent
150         {
151             get
152             {
153                 return content;
154             }
155             set
156             {
157                 if (content == value)
158                 {
159                     return;
160                 }
161
162                 if (content != null)
163                 {
164                     Remove(content);
165                 }
166
167                 content = value;
168                 if (content == null)
169                 {
170                     return;
171                 }
172
173                 Add(content);
174
175                 CalculatePosition();
176             }
177         }
178
179         /// <inheritdoc/>
180         [EditorBrowsable(EditorBrowsableState.Never)]
181         public override void OnRelayout(Vector2 size, RelayoutContainer container)
182         {
183             base.OnRelayout(size, container);
184
185             CalculatePosition();
186         }
187
188         // Calculate appBar and content's positions.
189         private void CalculatePosition()
190         {
191             // If ContentPage size has not been set yet, then content size cannot be calculated.
192             if ((Size2D.Width == 0) && (Size2D.Height == 0))
193             {
194                 return;
195             }
196
197             if (appBar != null)
198             {
199                 int appBarPosX = Position2D.X + Padding.Start + appBar.Margin.Top;
200                 int appBarPosY = Position2D.Y + Padding.Top + appBar.Margin.Top;
201
202                 appBar.Position2D = new Position2D(appBarPosX, appBarPosY);
203
204                 if ((appBar.WidthSpecification == LayoutParamPolicies.MatchParent) || (appBar.HeightSpecification == LayoutParamPolicies.MatchParent))
205                 {
206                     int appBarSizeW = appBar.Size2D.Width;
207                     int appBarSizeH = appBar.Size2D.Height;
208
209                     if (appBar.WidthSpecification == LayoutParamPolicies.MatchParent)
210                     {
211                         appBarSizeW = Size2D.Width - Padding.Start - Padding.End - appBar.Margin.Start - appBar.Margin.End;
212                     }
213
214                     if (appBar.HeightSpecification == LayoutParamPolicies.MatchParent)
215                     {
216                         appBarSizeH = Size2D.Height - Padding.Top - Padding.Bottom - appBar.Margin.Top - appBar.Margin.Bottom;
217                     }
218
219                     appBar.SetSize(new Size2D(appBarSizeW, appBarSizeH));
220                 }
221             }
222
223             if (content != null)
224             {
225                 int contentPosX = Position2D.X + Padding.Start + content.Margin.Start;
226                 int contentPosY = Position2D.Y + Padding.Top + content.Margin.Top + (appBar?.Size2D.Height ?? 0);
227
228                 content.Position2D = new Position2D(contentPosX, contentPosY);
229
230                 if ((content.WidthSpecification == LayoutParamPolicies.MatchParent) || (content.HeightSpecification == LayoutParamPolicies.MatchParent))
231                 {
232                     int contentSizeW = content.Size2D.Width;
233                     int contentSizeH = content.Size2D.Height;
234
235                     if (content.WidthSpecification == LayoutParamPolicies.MatchParent)
236                     {
237                         contentSizeW = Size2D.Width - Padding.Start - Padding.End - content.Margin.Start - content.Margin.End;
238                     }
239
240                     if (content.HeightSpecification == LayoutParamPolicies.MatchParent)
241                     {
242                         contentSizeH = Size2D.Height - Padding.Top - Padding.Bottom - content.Margin.Top - content.Margin.Bottom - (appBar?.Size2D.Height ?? 0);
243                     }
244
245                     content.SetSize(new Size2D(contentSizeW, contentSizeH));
246                 }
247             }
248         }
249     }
250 }