3367c99f74d636edb38be7543df893e6557b00be
[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     [EditorBrowsable(EditorBrowsableState.Never)]
27     public 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         [EditorBrowsable(EditorBrowsableState.Never)]
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         /// <summary>
74         /// AppBar of ContentPage. AppBar is added to Children automatically.
75         /// AppBar is positioned at the top of the Page.
76         /// </summary>
77         [EditorBrowsable(EditorBrowsableState.Never)]
78         public AppBar AppBar
79         {
80             get
81             {
82                 return appBar;
83             }
84             set
85             {
86                 if (appBar == value)
87                 {
88                     return;
89                 }
90
91                 if (appBar != null)
92                 {
93                     Remove(appBar);
94                 }
95
96                 appBar = value;
97                 if (appBar == null)
98                 {
99                     return;
100                 }
101
102                 Add(appBar);
103
104                 CalculatePosition();
105             }
106         }
107
108         /// <summary>
109         /// Content of ContentPage. Content is added to Children automatically.
110         /// Content is positioned below AppBar.
111         /// Content is resized to fill the full screen except AppBar.
112         /// </summary>
113         [EditorBrowsable(EditorBrowsableState.Never)]
114         public View Content
115         {
116             get
117             {
118                 return content;
119             }
120             set
121             {
122                 if (content == value)
123                 {
124                     return;
125                 }
126
127                 if (content != null)
128                 {
129                     Remove(content);
130                 }
131
132                 content = value;
133                 if (content == null)
134                 {
135                     return;
136                 }
137
138                 Add(content);
139
140                 CalculatePosition();
141             }
142         }
143
144         /// <inheritdoc/>
145         [EditorBrowsable(EditorBrowsableState.Never)]
146         public override void OnRelayout(Vector2 size, RelayoutContainer container)
147         {
148             base.OnRelayout(size, container);
149
150             CalculatePosition();
151         }
152
153         // Calculate appBar and content's positions.
154         private void CalculatePosition()
155         {
156             // If ContentPage size has not been set yet, then content size cannot be calculated.
157             if ((Size2D.Width == 0) && (Size2D.Height == 0))
158             {
159                 return;
160             }
161
162             if (appBar != null)
163             {
164                 int appBarPosX = Position2D.X + Padding.Start + appBar.Margin.Top;
165                 int appBarPosY = Position2D.Y + Padding.Top + appBar.Margin.Top;
166
167                 appBar.Position2D = new Position2D(appBarPosX, appBarPosY);
168
169                 if ((appBar.WidthSpecification == LayoutParamPolicies.MatchParent) || (appBar.HeightSpecification == LayoutParamPolicies.MatchParent))
170                 {
171                     int appBarSizeW = appBar.Size2D.Width;
172                     int appBarSizeH = appBar.Size2D.Height;
173
174                     if (appBar.WidthSpecification == LayoutParamPolicies.MatchParent)
175                     {
176                         appBarSizeW = Size2D.Width - Padding.Start - Padding.End - appBar.Margin.Start - appBar.Margin.End;
177                     }
178
179                     if (appBar.HeightSpecification == LayoutParamPolicies.MatchParent)
180                     {
181                         appBarSizeH = Size2D.Height - Padding.Top - Padding.Bottom - appBar.Margin.Top - appBar.Margin.Bottom;
182                     }
183
184                     appBar.Size2D = new Size2D(appBarSizeW, appBarSizeH);
185                 }
186             }
187
188             if (content != null)
189             {
190                 int contentPosX = Position2D.X + Padding.Start + content.Margin.Start;
191                 int contentPosY = Position2D.Y + Padding.Top + content.Margin.Top + (appBar?.Size2D.Height ?? 0);
192
193                 content.Position2D = new Position2D(contentPosX, contentPosY);
194
195                 if ((content.WidthSpecification == LayoutParamPolicies.MatchParent) || (content.HeightSpecification == LayoutParamPolicies.MatchParent))
196                 {
197                     int contentSizeW = content.Size2D.Width;
198                     int contentSizeH = content.Size2D.Height;
199
200                     if (content.WidthSpecification == LayoutParamPolicies.MatchParent)
201                     {
202                         contentSizeW = Size2D.Width - Padding.Start - Padding.End - content.Margin.Start - content.Margin.End;
203                     }
204
205                     if (content.HeightSpecification == LayoutParamPolicies.MatchParent)
206                     {
207                         contentSizeH = Size2D.Height - Padding.Top - Padding.Bottom - content.Margin.Top - content.Margin.Bottom - (appBar?.Size2D.Height ?? 0);
208                     }
209
210                     content.Size2D = new Size2D(contentSizeW, contentSizeH);
211                 }
212             }
213         }
214     }
215 }