[NUI] Add constructor with style instance
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Navigation / Page.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.Binding;
20
21 namespace Tizen.NUI.Components
22 {
23     /// <summary>
24     /// PageAppearingEventArgs is a class to record <see cref="Page.Appearing"/> event arguments which will be sent to user.
25     /// </summary>
26     /// <since_tizen> 9 </since_tizen>
27     public class PageAppearingEventArgs : EventArgs
28     {
29     }
30
31     /// <summary>
32     /// PageDisappearingEventArgs is a class to record <see cref="Page.Disappearing"/> event arguments which will be sent to user.
33     /// </summary>
34     /// <since_tizen> 9 </since_tizen>
35     public class PageDisappearingEventArgs : EventArgs
36     {
37     }
38
39     /// <summary>
40     /// PageAppearedEventArgs is a class to record <see cref="Page.Appeared"/> event arguments which will be sent to user.
41     /// </summary>
42     /// <since_tizen> 9 </since_tizen>
43     public class PageAppearedEventArgs : EventArgs
44     {
45     }
46
47     /// <summary>
48     /// PageDisappearedEventArgs is a class to record <see cref="Page.Disappeared"/> event arguments which will be sent to user.
49     /// </summary>
50     /// <since_tizen> 9 </since_tizen>
51     public class PageDisappearedEventArgs : EventArgs
52     {
53     }
54
55     /// <summary>
56     /// The Page class is a class which is an element of navigation.
57     /// </summary>
58     /// <since_tizen> 9 </since_tizen>
59     public abstract class Page : Control
60     {
61         /// <summary>
62         /// AppearingTransitionProperty
63         /// </summary>
64         [EditorBrowsable(EditorBrowsableState.Never)]
65         public static readonly BindableProperty AppearingTransitionProperty = BindableProperty.Create(nameof(AppearingTransition), typeof(TransitionBase), typeof(Page), null, propertyChanged: (bindable, oldValue, newValue) =>
66         {
67             var instance = (Page)bindable;
68             if (newValue != null)
69             {
70                 instance.InternalAppearingTransition = newValue as TransitionBase;
71             }
72         },
73         defaultValueCreator: (bindable) =>
74         {
75             var instance = (Page)bindable;
76             return instance.InternalAppearingTransition;
77         });
78
79         /// <summary>
80         /// DisappearingTransitionProperty
81         /// </summary>
82         [EditorBrowsable(EditorBrowsableState.Never)]
83         public static readonly BindableProperty DisappearingTransitionProperty = BindableProperty.Create(nameof(DisappearingTransition), typeof(TransitionBase), typeof(Page), null, propertyChanged: (bindable, oldValue, newValue) =>
84         {
85             var instance = (Page)bindable;
86             if (newValue != null)
87             {
88                 instance.InternalDisappearingTransition = newValue as TransitionBase;
89             }
90         },
91         defaultValueCreator: (bindable) =>
92         {
93             var instance = (Page)bindable;
94             return instance.InternalDisappearingTransition;
95         });
96
97         /// <inheritdoc/>
98         [EditorBrowsable(EditorBrowsableState.Never)]
99         protected internal BaseComponents.View LastFocusedView = null;
100
101         private Navigator navigator = null;
102
103         // Default transition is Fade.
104         private TransitionBase appearingTransition = null;
105
106         private TransitionBase disappearingTransition = null;
107
108         /// <summary>
109         /// Creates a new instance of a Page.
110         /// </summary>
111         /// <since_tizen> 9 </since_tizen>
112         public Page() : base()
113         {
114         }
115
116         /// <summary>
117         /// Creates a new instance of a Page with style.
118         /// </summary>
119         /// <param name="style">A style applied to the newly created Page.</param>
120         [EditorBrowsable(EditorBrowsableState.Never)]
121         public Page(ControlStyle style) : base(style)
122         {
123         }
124
125         /// <summary>
126         /// Navigator which has pushed the Page into its stack.
127         /// If this Page has not been pushed into any Navigator, then Navigator is null.
128         /// </summary>
129         /// <since_tizen> 9 </since_tizen>
130         public Navigator Navigator
131         {
132             get
133             {
134                 return navigator;
135             }
136             internal set
137             {
138                 if (navigator == value)
139                 {
140                     return;
141                 }
142
143                 navigator = value;
144             }
145         }
146
147         /// <summary>
148         /// Transition properties for the transition of Views in this page during this page is pushed to Navigator.
149         /// </summary>
150         [EditorBrowsable(EditorBrowsableState.Never)]
151         public TransitionBase AppearingTransition
152         {
153             get
154             {
155                 return GetValue(AppearingTransitionProperty) as TransitionBase;
156             }
157             set
158             {
159                 SetValue(AppearingTransitionProperty, value);
160                 NotifyPropertyChanged();
161             }
162         }
163         private TransitionBase InternalAppearingTransition
164         {
165             set
166             {
167                 appearingTransition = value;
168             }
169             get
170             {
171                 return appearingTransition;
172             }
173         }
174
175         /// <summary>
176         /// Transition properties for the transition of Views in this page during this page is popped from Navigator.
177         /// </summary>
178         [EditorBrowsable(EditorBrowsableState.Never)]
179         public TransitionBase DisappearingTransition
180         {
181             get
182             {
183                 return GetValue(DisappearingTransitionProperty) as TransitionBase;
184             }
185             set
186             {
187                 SetValue(DisappearingTransitionProperty, value);
188                 NotifyPropertyChanged();
189             }
190         }
191         private TransitionBase InternalDisappearingTransition
192         {
193             set
194             {
195                 disappearingTransition = value;
196             }
197             get
198             {
199                 return disappearingTransition;
200             }
201         }
202
203         /// <summary>
204         /// Appearing event is invoked right before the page appears.
205         /// </summary>
206         /// <since_tizen> 9 </since_tizen>
207         public event EventHandler<PageAppearingEventArgs> Appearing;
208
209         /// <summary>
210         /// Disappearing event is invoked right before the page disappears.
211         /// </summary>
212         /// <since_tizen> 9 </since_tizen>
213         public event EventHandler<PageDisappearingEventArgs> Disappearing;
214
215         /// <summary>
216         /// Appeared event is invoked right after the page appears.
217         /// </summary>
218         /// <since_tizen> 9 </since_tizen>
219         public event EventHandler<PageAppearedEventArgs> Appeared;
220
221         /// <summary>
222         /// Disappeared event is invoked right after the page disappears.
223         /// </summary>
224         /// <since_tizen> 9 </since_tizen>
225         public event EventHandler<PageDisappearedEventArgs> Disappeared;
226
227         internal void InvokeAppearing()
228         {
229             Appearing?.Invoke(this, new PageAppearingEventArgs());
230         }
231
232         internal void InvokeDisappearing()
233         {
234             Disappearing?.Invoke(this, new PageDisappearingEventArgs());
235         }
236
237         internal void InvokeAppeared()
238         {
239             Appeared?.Invoke(this, new PageAppearedEventArgs());
240         }
241
242         internal void InvokeDisappeared()
243         {
244             Disappeared?.Invoke(this, new PageDisappearedEventArgs());
245         }
246
247         /// <summary>
248         /// works only when DefaultAlgorithm is enabled.
249         /// to save the currently focused View when disappeared.
250         /// </summary>
251         [EditorBrowsable(EditorBrowsableState.Never)]
252         protected internal virtual void SaveKeyFocus()
253         {
254             if (FocusManager.Instance.IsDefaultAlgorithmEnabled())
255             {
256                 if (this is DialogPage)
257                 {
258                     FocusManager.Instance.ResetFocusFinderRootView();
259                 }
260
261                 var currentFocusedView = FocusManager.Instance.GetCurrentFocusView();
262                 if (currentFocusedView)
263                 {
264                     var findChild = FindDescendantByID(currentFocusedView.ID);
265                     if (findChild)
266                     {
267                         LastFocusedView = findChild;
268                         return;
269                     }
270                 }
271                 LastFocusedView = null;
272             }
273         }
274
275         /// <summary>
276         /// works only when DefaultAlgorithm is enabled.
277         /// to set key focused View when showing.
278         /// </summary>
279         [EditorBrowsable(EditorBrowsableState.Never)]
280         protected internal virtual void RestoreKeyFocus()
281         {
282             if (FocusManager.Instance.IsDefaultAlgorithmEnabled())
283             {
284                 if (LastFocusedView)
285                 {
286                     FocusManager.Instance.SetCurrentFocusView(LastFocusedView);
287                 }
288                 else
289                 {
290                     var temp = new Tizen.NUI.BaseComponents.View()
291                     {
292                         Size = new Size(0.1f, 0.1f, 0.0f),
293                         Position = new Position(0, 0, 0),
294                         Focusable = true,
295                     };
296                     this.Add(temp);
297                     temp.LowerToBottom();
298                     FocusManager.Instance.SetCurrentFocusView(temp);
299                     var focused = FocusManager.Instance.GetNearestFocusableActor(this, temp, Tizen.NUI.BaseComponents.View.FocusDirection.Down);
300                     if (focused)
301                     {
302                         FocusManager.Instance.SetCurrentFocusView(focused);
303                     }
304                     else
305                     {
306                         FocusManager.Instance.ClearFocus();
307                     }
308                     temp.Unparent();
309                     temp.Dispose();
310                 }
311
312                 if (this is DialogPage)
313                 {
314                     FocusManager.Instance.SetFocusFinderRootView(this);
315                 }
316             }
317
318         }
319
320     }
321 }