[NUI][TEST] key foucs default algorithm test
[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         protected internal BaseComponents.View LastFocusedView = null;
98
99         private Navigator navigator = null;
100
101         // Default transition is Fade.
102         private TransitionBase appearingTransition = null;
103
104         private TransitionBase disappearingTransition = null;
105
106         /// <summary>
107         /// Creates a new instance of a Page.
108         /// </summary>
109         /// <since_tizen> 9 </since_tizen>
110         public Page() : base()
111         {
112         }
113
114         /// <summary>
115         /// Navigator which has pushed the Page into its stack.
116         /// If this Page has not been pushed into any Navigator, then Navigator is null.
117         /// </summary>
118         /// <since_tizen> 9 </since_tizen>
119         public Navigator Navigator
120         {
121             get
122             {
123                 return navigator;
124             }
125             internal set
126             {
127                 if (navigator == value)
128                 {
129                     return;
130                 }
131
132                 navigator = value;
133             }
134         }
135
136         /// <summary>
137         /// Transition properties for the transition of Views in this page during this page is pushed to Navigator.
138         /// </summary>
139         [EditorBrowsable(EditorBrowsableState.Never)]
140         public TransitionBase AppearingTransition
141         {
142             get
143             {
144                 return GetValue(AppearingTransitionProperty) as TransitionBase;
145             }
146             set
147             {
148                 SetValue(AppearingTransitionProperty, value);
149                 NotifyPropertyChanged();
150             }
151         }
152         private TransitionBase InternalAppearingTransition
153         {
154             set
155             {
156                 appearingTransition = value;
157             }
158             get
159             {
160                 return appearingTransition;
161             }
162         }
163
164         /// <summary>
165         /// Transition properties for the transition of Views in this page during this page is popped from Navigator.
166         /// </summary>
167         [EditorBrowsable(EditorBrowsableState.Never)]
168         public TransitionBase DisappearingTransition
169         {
170             get
171             {
172                 return GetValue(DisappearingTransitionProperty) as TransitionBase;
173             }
174             set
175             {
176                 SetValue(DisappearingTransitionProperty, value);
177                 NotifyPropertyChanged();
178             }
179         }
180         private TransitionBase InternalDisappearingTransition
181         {
182             set
183             {
184                 disappearingTransition = value;
185             }
186             get
187             {
188                 return disappearingTransition;
189             }
190         }
191
192         /// <summary>
193         /// Appearing event is invoked right before the page appears.
194         /// </summary>
195         /// <since_tizen> 9 </since_tizen>
196         public event EventHandler<PageAppearingEventArgs> Appearing;
197
198         /// <summary>
199         /// Disappearing event is invoked right before the page disappears.
200         /// </summary>
201         /// <since_tizen> 9 </since_tizen>
202         public event EventHandler<PageDisappearingEventArgs> Disappearing;
203
204         /// <summary>
205         /// Appeared event is invoked right after the page appears.
206         /// </summary>
207         /// <since_tizen> 9 </since_tizen>
208         public event EventHandler<PageAppearedEventArgs> Appeared;
209
210         /// <summary>
211         /// Disappeared event is invoked right after the page disappears.
212         /// </summary>
213         /// <since_tizen> 9 </since_tizen>
214         public event EventHandler<PageDisappearedEventArgs> Disappeared;
215
216         internal void InvokeAppearing()
217         {
218             Appearing?.Invoke(this, new PageAppearingEventArgs());
219         }
220
221         internal void InvokeDisappearing()
222         {
223             Disappearing?.Invoke(this, new PageDisappearingEventArgs());
224         }
225
226         internal void InvokeAppeared()
227         {
228             Appeared?.Invoke(this, new PageAppearedEventArgs());
229         }
230
231         internal void InvokeDisappeared()
232         {
233             Disappeared?.Invoke(this, new PageDisappearedEventArgs());
234         }
235
236         /// <summary>
237         /// works only when DefaultAlgorithm is enabled.
238         /// to save the currentl focused View when disappeared.
239         /// </summary>
240         [EditorBrowsable(EditorBrowsableState.Never)]
241         protected internal virtual void SaveKeyFocus()
242         {
243             if (FocusManager.Instance.IsDefaultAlgorithmEnabled())
244             {
245                 var currentFocusedView = FocusManager.Instance.GetCurrentFocusView();
246                 if (currentFocusedView)
247                 {
248                     var findChild = this.FindDescendantByID(currentFocusedView.ID);
249                     if (findChild)
250                     {
251                         this.LastFocusedView = findChild;
252                         return;
253                     }
254                 }
255                 this.LastFocusedView = null;
256             }
257         }
258
259         /// <summary>
260         /// works only when DefaultAlgorithm is enabled.
261         /// to set key focused View when showing.
262         /// </summary>
263         [EditorBrowsable(EditorBrowsableState.Never)]
264         protected internal virtual void RestoreKeyFocus()
265         {
266             if (FocusManager.Instance.IsDefaultAlgorithmEnabled())
267             {
268                 if (this.LastFocusedView)
269                 {
270                     FocusManager.Instance.SetCurrentFocusView(this.LastFocusedView);
271                 }
272                 else
273                 {
274                     var temp = new Tizen.NUI.BaseComponents.View()
275                     {
276                         Size = new Size(0.1f, 0.1f, 0.0f),
277                         Position = new Position(0, 0, 0),
278                         Focusable = true,
279                     };
280                     this.Add(temp);
281                     temp.LowerToBottom();
282                     FocusManager.Instance.SetCurrentFocusView(temp);
283                     var focused = FocusManager.Instance.GetNearestFocusableActor(this, temp, Tizen.NUI.BaseComponents.View.FocusDirection.Down);
284                     if (focused)
285                     {
286                         FocusManager.Instance.SetCurrentFocusView(focused);
287                     }
288                     else
289                     {
290                         FocusManager.Instance.ClearFocus();
291                     }
292                     temp.Unparent();
293                     temp.Dispose();
294                 }
295             }
296
297         }
298
299     }
300 }