[NUI] Add constructors with string style to apply string style
[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         /// <summary>
98         /// EnableBackNavigationProperty
99         /// </summary>
100         [EditorBrowsable(EditorBrowsableState.Never)]
101         public static readonly BindableProperty EnableBackNavigationProperty = BindableProperty.Create(nameof(EnableBackNavigation), typeof(bool), typeof(Page), default(bool), propertyChanged: (bindable, oldValue, newValue) =>
102         {
103             var instance = (Page)bindable;
104             if (newValue != null)
105             {
106                 instance.InternalEnableBackNavigation = (bool)newValue;
107             }
108         },
109         defaultValueCreator: (bindable) =>
110         {
111             var instance = (Page)bindable;
112             return instance.InternalEnableBackNavigation;
113         });
114
115         /// <inheritdoc/>
116         [EditorBrowsable(EditorBrowsableState.Never)]
117         protected internal BaseComponents.View LastFocusedView = null;
118
119         private Navigator navigator = null;
120
121         // Default transition is Fade.
122         private TransitionBase appearingTransition = null;
123
124         private TransitionBase disappearingTransition = null;
125
126         private bool enableBackNavigation = true;
127
128         /// <summary>
129         /// Creates a new instance of a Page.
130         /// </summary>
131         /// <since_tizen> 9 </since_tizen>
132         public Page() : base()
133         {
134         }
135
136         /// <summary>
137         /// Creates a new instance of Page with style.
138         /// </summary>
139         /// <param name="style">Creates Page by special style defined in UX.</param>
140         [EditorBrowsable(EditorBrowsableState.Never)]
141         public Page(string style) : base(style)
142         {
143         }
144
145         /// <summary>
146         /// Creates a new instance of a Page with style.
147         /// </summary>
148         /// <param name="style">A style applied to the newly created Page.</param>
149         [EditorBrowsable(EditorBrowsableState.Never)]
150         public Page(ControlStyle style) : base(style)
151         {
152         }
153
154         /// <summary>
155         /// Navigator which has pushed the Page into its stack.
156         /// If this Page has not been pushed into any Navigator, then Navigator is null.
157         /// </summary>
158         /// <since_tizen> 9 </since_tizen>
159         public Navigator Navigator
160         {
161             get
162             {
163                 return navigator;
164             }
165             internal set
166             {
167                 if (navigator == value)
168                 {
169                     return;
170                 }
171
172                 navigator = value;
173             }
174         }
175
176         /// <summary>
177         /// Transition properties for the transition of Views in this page during this page is pushed to Navigator.
178         /// </summary>
179         [EditorBrowsable(EditorBrowsableState.Never)]
180         public TransitionBase AppearingTransition
181         {
182             get
183             {
184                 return GetValue(AppearingTransitionProperty) as TransitionBase;
185             }
186             set
187             {
188                 SetValue(AppearingTransitionProperty, value);
189                 NotifyPropertyChanged();
190             }
191         }
192         private TransitionBase InternalAppearingTransition
193         {
194             set
195             {
196                 appearingTransition = value;
197             }
198             get
199             {
200                 return appearingTransition;
201             }
202         }
203
204         /// <summary>
205         /// Transition properties for the transition of Views in this page during this page is popped from Navigator.
206         /// </summary>
207         [EditorBrowsable(EditorBrowsableState.Never)]
208         public TransitionBase DisappearingTransition
209         {
210             get
211             {
212                 return GetValue(DisappearingTransitionProperty) as TransitionBase;
213             }
214             set
215             {
216                 SetValue(DisappearingTransitionProperty, value);
217                 NotifyPropertyChanged();
218             }
219         }
220         private TransitionBase InternalDisappearingTransition
221         {
222             set
223             {
224                 disappearingTransition = value;
225             }
226             get
227             {
228                 return disappearingTransition;
229             }
230         }
231
232         /// <summary>
233         /// Appearing event is invoked right before the page appears.
234         /// </summary>
235         /// <since_tizen> 9 </since_tizen>
236         public event EventHandler<PageAppearingEventArgs> Appearing;
237
238         /// <summary>
239         /// Disappearing event is invoked right before the page disappears.
240         /// </summary>
241         /// <since_tizen> 9 </since_tizen>
242         public event EventHandler<PageDisappearingEventArgs> Disappearing;
243
244         /// <summary>
245         /// Appeared event is invoked right after the page appears.
246         /// </summary>
247         /// <since_tizen> 9 </since_tizen>
248         public event EventHandler<PageAppearedEventArgs> Appeared;
249
250         /// <summary>
251         /// Disappeared event is invoked right after the page disappears.
252         /// </summary>
253         /// <since_tizen> 9 </since_tizen>
254         public event EventHandler<PageDisappearedEventArgs> Disappeared;
255
256         /// <summary>
257         /// Gets or sets if this page proceeds back navigation when back button or back key is pressed and released.
258         /// Back navigation pops the peek page if Navigator has more than one page.
259         /// If Navigator has only one page, then the current program is exited.
260         /// </summary>
261         [EditorBrowsable(EditorBrowsableState.Never)]
262         public bool EnableBackNavigation
263         {
264             get
265             {
266                 return (bool)GetValue(EnableBackNavigationProperty);
267             }
268             set
269             {
270                 SetValue(EnableBackNavigationProperty, value);
271                 NotifyPropertyChanged();
272             }
273         }
274
275         private bool InternalEnableBackNavigation
276         {
277             set
278             {
279                 enableBackNavigation = value;
280             }
281             get
282             {
283                 return enableBackNavigation;
284             }
285         }
286
287         internal void InvokeAppearing()
288         {
289             Appearing?.Invoke(this, new PageAppearingEventArgs());
290         }
291
292         internal void InvokeDisappearing()
293         {
294             Disappearing?.Invoke(this, new PageDisappearingEventArgs());
295         }
296
297         internal void InvokeAppeared()
298         {
299             Appeared?.Invoke(this, new PageAppearedEventArgs());
300         }
301
302         internal void InvokeDisappeared()
303         {
304             Disappeared?.Invoke(this, new PageDisappearedEventArgs());
305         }
306
307         /// <summary>
308         /// works only when DefaultAlgorithm is enabled.
309         /// to save the currently focused View when disappeared.
310         /// </summary>
311         [EditorBrowsable(EditorBrowsableState.Never)]
312         protected internal virtual void SaveKeyFocus()
313         {
314             if (FocusManager.Instance.IsDefaultAlgorithmEnabled())
315             {
316                 if (this is DialogPage)
317                 {
318                     FocusManager.Instance.ResetFocusFinderRootView();
319                 }
320
321                 var currentFocusedView = FocusManager.Instance.GetCurrentFocusView();
322                 if (currentFocusedView)
323                 {
324                     var findChild = FindDescendantByID(currentFocusedView.ID);
325                     if (findChild)
326                     {
327                         LastFocusedView = findChild;
328                         return;
329                     }
330                 }
331                 LastFocusedView = null;
332             }
333         }
334
335         /// <summary>
336         /// works only when DefaultAlgorithm is enabled.
337         /// to set key focused View when showing.
338         /// </summary>
339         [EditorBrowsable(EditorBrowsableState.Never)]
340         protected internal virtual void RestoreKeyFocus()
341         {
342             if (FocusManager.Instance.IsDefaultAlgorithmEnabled())
343             {
344                 if (LastFocusedView)
345                 {
346                     FocusManager.Instance.SetCurrentFocusView(LastFocusedView);
347                 }
348                 else
349                 {
350                     var temp = new Tizen.NUI.BaseComponents.View()
351                     {
352                         Size = new Size(0.1f, 0.1f, 0.0f),
353                         Position = new Position(0, 0, 0),
354                         Focusable = true,
355                     };
356                     this.Add(temp);
357                     temp.LowerToBottom();
358                     FocusManager.Instance.SetCurrentFocusView(temp);
359                     var focused = FocusManager.Instance.GetNearestFocusableActor(this, temp, Tizen.NUI.BaseComponents.View.FocusDirection.Down);
360                     if (focused)
361                     {
362                         FocusManager.Instance.SetCurrentFocusView(focused);
363                     }
364                     else
365                     {
366                         FocusManager.Instance.ClearFocus();
367                     }
368                     temp.Unparent();
369                     temp.Dispose();
370                 }
371
372                 if (this is DialogPage)
373                 {
374                     FocusManager.Instance.SetFocusFinderRootView(this);
375                 }
376             }
377         }
378
379         /// <summary>
380         /// Called when the back navigation is started.
381         /// Back navigation pops the peek page if Navigator has more than one page.
382         /// If Navigator has only one page, then the current program is exited.
383         /// </summary>
384         /// <param name="eventArgs">The back navigation information.</param>
385         [EditorBrowsable(EditorBrowsableState.Never)]
386         protected virtual void OnBackNavigation(PageBackNavigationEventArgs eventArgs)
387         {
388             if (Navigator.PageCount > 1)
389             {
390                 Navigator.Pop();
391             }
392             else
393             {
394                 NUIApplication.Current?.Exit();
395             }
396         }
397
398         /// <summary>
399         /// Called when the back navigation is required outside Navigator.
400         /// </summary>
401         internal void NavigateBack()
402         {
403             OnBackNavigation(new PageBackNavigationEventArgs());
404         }
405     }
406
407     /// <summary>
408     /// PageBackNavigationEventArgs is a class to record back navigation event arguments which will sent to user.
409     /// </summary>
410     [EditorBrowsable(EditorBrowsableState.Never)]
411     public class PageBackNavigationEventArgs : EventArgs
412     {
413     }
414 }