[NUI][TEST] key foucs default algorithm test
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Navigation / Navigator.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
18 using System;
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using Tizen.NUI.BaseComponents;
22 using Tizen.NUI.Binding;
23
24 namespace Tizen.NUI.Components
25 {
26     /// <summary>
27     /// PoppedEventArgs is a class to record <see cref="Navigator.Popped"/> event arguments which will be sent to user.
28     /// </summary>
29     /// <since_tizen> 9 </since_tizen>
30     public class PoppedEventArgs : EventArgs
31     {
32         /// <summary>
33         /// Page popped by Navigator.
34         /// </summary>
35         /// <since_tizen> 9 </since_tizen>
36         public Page Page { get; internal set; }
37     }
38
39     /// <summary>
40     /// The Navigator is a class which navigates pages with stack methods such as Push and Pop.
41     /// </summary>
42     /// <remarks>
43     /// With Transition class, Navigator supports smooth transition of View pair between two Pages
44     /// by using <see cref="PushWithTransition(Page)"/> and <see cref="PopWithTransition()"/> methods.
45     /// If current top Page and next top Page have <see cref="View"/>s those have same TransitionTag,
46     /// Navigator creates smooth transition motion for them.
47     /// Navigator.Transition property can be used to set properties of the Transition such as TimePeriod and AlphaFunction.
48     /// When all transitions are finished, Navigator calls a callback methods those connected on the "TransitionFinished" event.
49     /// </remarks>
50     /// <example>
51     /// <code>
52     /// Navigator navigator = new Navigator()
53     /// {
54     ///     TimePeriod = new TimePeriod(500),
55     ///     AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseInOutSine)
56     /// };
57     ///
58     /// View view = new View()
59     /// {
60     ///     TransitionOptions = new TransitionOptions()
61     ///     {
62     ///         /* Set properties for the transition of this View */
63     ///     }
64     /// };
65     ///
66     /// ContentPage newPage = new ContentPage()
67     /// {
68     ///     Content = view,
69     /// };
70     ///
71     /// Navigator.PushWithTransition(newPage);
72     /// </code>
73     /// </example>
74     /// <since_tizen> 9 </since_tizen>
75     public class Navigator : Control
76     {
77         /// <summary>
78         /// TransitionProperty
79         /// </summary>
80         [EditorBrowsable(EditorBrowsableState.Never)]
81         public static readonly BindableProperty TransitionProperty = BindableProperty.Create(nameof(Transition), typeof(Transition), typeof(Navigator), null, propertyChanged: (bindable, oldValue, newValue) =>
82         {
83             var instance = (Navigator)bindable;
84             if (newValue != null)
85             {
86                 instance.InternalTransition = newValue as Transition;
87             }
88         },
89         defaultValueCreator: (bindable) =>
90         {
91             var instance = (Navigator)bindable;
92             return instance.InternalTransition;
93         });
94
95         private const int DefaultTransitionDuration = 500;
96
97         //This will be replaced with view transition class instance.
98         private Animation curAnimation = null;
99
100         //This will be replaced with view transition class instance.
101         private Animation newAnimation = null;
102
103         private TransitionSet transitionSet = null;
104
105         private Transition transition = new Transition()
106         {
107             TimePeriod = new TimePeriod(DefaultTransitionDuration),
108             AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Default),
109         };
110
111         private bool transitionFinished = true;
112
113         //TODO: Needs to consider how to remove disposed window from dictionary.
114         //Two dictionaries are required to remove disposed navigator from dictionary.
115         private static Dictionary<Window, Navigator> windowNavigator = new Dictionary<Window, Navigator>();
116         private static Dictionary<Navigator, Window> navigatorWindow = new Dictionary<Navigator, Window>();
117
118         private List<Page> navigationPages = new List<Page>();
119
120         /// <summary>
121         /// Creates a new instance of a Navigator.
122         /// </summary>
123         /// <since_tizen> 9 </since_tizen>
124         public Navigator() : base()
125         {
126             Layout = new AbsoluteLayout();
127         }
128
129         /// <inheritdoc/>
130         [EditorBrowsable(EditorBrowsableState.Never)]
131         public override void OnInitialize()
132         {
133             base.OnInitialize();
134
135             SetAccessibilityConstructor(Role.PageTabList);
136         }
137
138         /// <summary>
139         /// An event fired when Transition has been finished.
140         /// </summary>
141         /// <since_tizen> 9 </since_tizen>
142         public event EventHandler<EventArgs> TransitionFinished;
143
144         /// <summary>
145         /// An event fired when Pop of a page has been finished.
146         /// </summary>
147         /// <remarks>
148         /// When you free resources in the Popped event handler, please make sure if the popped page is the page you find.
149         /// </remarks>
150         /// <since_tizen> 9 </since_tizen>
151         public event EventHandler<PoppedEventArgs> Popped;
152
153         /// <summary>
154         /// Returns the count of pages in Navigator.
155         /// </summary>
156         /// <since_tizen> 9 </since_tizen>
157         public int PageCount => navigationPages.Count;
158
159         /// <summary>
160         /// Transition properties for the transition of View pair having same transition tag.
161         /// </summary>
162         /// <since_tizen> 9 </since_tizen>
163         public Transition Transition
164         {
165             get
166             {
167                 return GetValue(TransitionProperty) as Transition;
168             }
169             set
170             {
171                 SetValue(TransitionProperty, value);
172                 NotifyPropertyChanged();
173             }
174         }
175         private Transition InternalTransition
176         {
177             set
178             {
179                 transition = value;
180             }
181             get
182             {
183                 return transition;
184             }
185         }
186
187         /// <summary>
188         /// Pushes a page to Navigator.
189         /// If the page is already in Navigator, then it is not pushed.
190         /// </summary>
191         /// <param name="page">The page to push to Navigator.</param>
192         /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
193         /// <since_tizen> 9 </since_tizen>
194         public void PushWithTransition(Page page)
195         {
196             if (!transitionFinished)
197             {
198                 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
199                 return;
200             }
201
202             if (page == null)
203             {
204                 throw new ArgumentNullException(nameof(page), "page should not be null.");
205             }
206
207             //Duplicate page is not pushed.
208             if (navigationPages.Contains(page)) return;
209
210             var topPage = Peek();
211
212             if (!topPage)
213             {
214                 Insert(0, page);
215                 return;
216             }
217
218             navigationPages.Add(page);
219             Add(page);
220             page.Navigator = this;
221
222             //Invoke Page events
223             page.InvokeAppearing();
224             topPage.InvokeDisappearing();
225
226             transitionSet = CreateTransitions(topPage, page, true);
227             transitionSet.Finished += (object sender, EventArgs e) =>
228             {
229                 if (page is DialogPage == false)
230                 {
231                     topPage.SetVisible(false);
232                 }
233
234                 // Need to update Content of the new page
235                 ShowContentOfPage(page);
236
237                 //Invoke Page events
238                 page.InvokeAppeared();
239                 topPage.InvokeDisappeared();
240                 NotifyAccessibilityStatesChangeOfPages(topPage, page);
241             };
242             transitionFinished = false;
243         }
244
245         /// <summary>
246         /// Pops the top page from Navigator.
247         /// </summary>
248         /// <returns>The popped page.</returns>
249         /// <exception cref="InvalidOperationException">Thrown when there is no page in Navigator.</exception>
250         /// <since_tizen> 9 </since_tizen>
251         public Page PopWithTransition()
252         {
253             if (!transitionFinished)
254             {
255                 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
256                 return null;
257             }
258
259             if (navigationPages.Count == 0)
260             {
261                 throw new InvalidOperationException("There is no page in Navigator.");
262             }
263
264             var topPage = Peek();
265
266             if (navigationPages.Count == 1)
267             {
268                 Remove(topPage);
269
270                 //Invoke Popped event
271                 Popped?.Invoke(this, new PoppedEventArgs() { Page = topPage });
272
273                 return topPage;
274             }
275             var newTopPage = navigationPages[navigationPages.Count - 2];
276
277             //Invoke Page events
278             newTopPage.InvokeAppearing();
279             topPage.InvokeDisappearing();
280
281             transitionSet = CreateTransitions(topPage, newTopPage, false);
282             transitionSet.Finished += (object sender, EventArgs e) =>
283             {
284                 Remove(topPage);
285                 topPage.SetVisible(true);
286
287                 // Need to update Content of the new page
288                 ShowContentOfPage(newTopPage);
289
290                 //Invoke Page events
291                 newTopPage.InvokeAppeared();
292                 topPage.InvokeDisappeared();
293
294                 //Invoke Popped event
295                 Popped?.Invoke(this, new PoppedEventArgs() { Page = topPage });
296             };
297             transitionFinished = false;
298
299             return topPage;
300         }
301
302         /// <summary>
303         /// Pushes a page to Navigator.
304         /// If the page is already in Navigator, then it is not pushed.
305         /// </summary>
306         /// <param name="page">The page to push to Navigator.</param>
307         /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
308         /// <since_tizen> 9 </since_tizen>
309         public void Push(Page page)
310         {
311             if (!transitionFinished)
312             {
313                 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
314                 return;
315             }
316
317             if (page == null)
318             {
319                 throw new ArgumentNullException(nameof(page), "page should not be null.");
320             }
321
322             //Duplicate page is not pushed.
323             if (navigationPages.Contains(page)) return;
324
325             var curTop = Peek();
326
327             if (!curTop)
328             {
329                 Insert(0, page);
330                 return;
331             }
332
333             navigationPages.Add(page);
334             Add(page);
335             page.Navigator = this;
336
337             //Invoke Page events
338             page.InvokeAppearing();
339             curTop.InvokeDisappearing();
340
341             curTop.SaveKeyFocus();
342
343             //TODO: The following transition codes will be replaced with view transition.
344             InitializeAnimation();
345
346             if (page is DialogPage == false)
347             {
348                 curAnimation = new Animation(1000);
349                 curAnimation.AnimateTo(curTop, "Opacity", 1.0f, 0, 1000);
350                 curAnimation.EndAction = Animation.EndActions.StopFinal;
351                 curAnimation.Finished += (object sender, EventArgs args) =>
352                 {
353                     curTop.SetVisible(false);
354
355                     //Invoke Page events
356                     curTop.InvokeDisappeared();
357                 };
358                 curAnimation.Play();
359
360                 page.Opacity = 0.0f;
361                 page.SetVisible(true);
362                 newAnimation = new Animation(1000);
363                 newAnimation.AnimateTo(page, "Opacity", 1.0f, 0, 1000);
364                 newAnimation.EndAction = Animation.EndActions.StopFinal;
365                 newAnimation.Finished += (object sender, EventArgs e) =>
366                 {
367                     // Need to update Content of the new page
368                     ShowContentOfPage(page);
369
370                     //Invoke Page events
371                     page.InvokeAppeared();
372                     NotifyAccessibilityStatesChangeOfPages(curTop, page);
373
374                     page.RestoreKeyFocus();
375                 };
376                 newAnimation.Play();
377             }
378             else
379             {
380                 ShowContentOfPage(page);
381                 page.RestoreKeyFocus();
382             }
383         }
384
385         /// <summary>
386         /// Pops the top page from Navigator.
387         /// </summary>
388         /// <returns>The popped page.</returns>
389         /// <exception cref="InvalidOperationException">Thrown when there is no page in Navigator.</exception>
390         /// <since_tizen> 9 </since_tizen>
391         public Page Pop()
392         {
393             if (!transitionFinished)
394             {
395                 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
396                 return null;
397             }
398
399             if (navigationPages.Count == 0)
400             {
401                 throw new InvalidOperationException("There is no page in Navigator.");
402             }
403
404             var curTop = Peek();
405
406             if (navigationPages.Count == 1)
407             {
408                 Remove(curTop);
409
410                 //Invoke Popped event
411                 Popped?.Invoke(this, new PoppedEventArgs() { Page = curTop });
412
413                 return curTop;
414             }
415
416             var newTop = navigationPages[navigationPages.Count - 2];
417
418             //Invoke Page events
419             newTop.InvokeAppearing();
420             curTop.InvokeDisappearing();
421             curTop.SaveKeyFocus();
422
423             //TODO: The following transition codes will be replaced with view transition.
424             InitializeAnimation();
425
426             if (curTop is DialogPage == false)
427             {
428                 curAnimation = new Animation(1000);
429                 curAnimation.AnimateTo(curTop, "Opacity", 0.0f, 0, 1000);
430                 curAnimation.EndAction = Animation.EndActions.StopFinal;
431                 curAnimation.Finished += (object sender, EventArgs e) =>
432                 {
433                     //Removes the current top page after transition is finished.
434                     Remove(curTop);
435                     curTop.Opacity = 1.0f;
436
437                     //Invoke Page events
438                     curTop.InvokeDisappeared();
439
440                     //Invoke Popped event
441                     Popped?.Invoke(this, new PoppedEventArgs() { Page = curTop });
442                 };
443                 curAnimation.Play();
444
445                 newTop.Opacity = 1.0f;
446                 newTop.SetVisible(true);
447                 newAnimation = new Animation(1000);
448                 newAnimation.AnimateTo(newTop, "Opacity", 1.0f, 0, 1000);
449                 newAnimation.EndAction = Animation.EndActions.StopFinal;
450                 newAnimation.Finished += (object sender, EventArgs e) =>
451                 {
452                     // Need to update Content of the new page
453                     ShowContentOfPage(newTop);
454
455                     //Invoke Page events
456                     newTop.InvokeAppeared();
457
458                     newTop.RestoreKeyFocus();
459                 };
460                 newAnimation.Play();
461             }
462             else
463             {
464                 Remove(curTop);
465             }
466
467             return curTop;
468         }
469
470         /// <summary>
471         /// Returns the page of the given index in Navigator.
472         /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
473         /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
474         /// </summary>
475         /// <param name="index">The index of a page in Navigator.</param>
476         /// <returns>The page of the given index in Navigator.</returns>
477         /// <exception cref="ArgumentOutOfRangeException">Thrown when the argument index is less than 0, or greater than the number of pages.</exception>
478         public Page GetPage(int index)
479         {
480             if ((index < 0) || (index > navigationPages.Count))
481             {
482                 throw new ArgumentOutOfRangeException(nameof(index), "index should be greater than or equal to 0, and less than or equal to the number of pages.");
483             }
484
485             return navigationPages[index];
486         }
487
488         /// <summary>
489         /// Returns the current index of the given page in Navigator.
490         /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
491         /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
492         /// </summary>
493         /// <param name="page">The page in Navigator.</param>
494         /// <returns>The index of the given page in Navigator. If the given page is not in the Navigator, then -1 is returned.</returns>
495         /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
496         /// <since_tizen> 9 </since_tizen>
497         public int IndexOf(Page page)
498         {
499             if (page == null)
500             {
501                 throw new ArgumentNullException(nameof(page), "page should not be null.");
502             }
503
504             for (int i = 0; i < navigationPages.Count; i++)
505             {
506                 if (navigationPages[i] == page)
507                 {
508                     return i;
509                 }
510             }
511
512             return -1;
513         }
514
515         /// <summary>
516         /// Inserts a page at the specified index of Navigator.
517         /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
518         /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
519         /// To find the current index of a page in Navigator, please use IndexOf(page).
520         /// If the page is already in Navigator, then it is not inserted.
521         /// </summary>
522         /// <param name="index">The index of a page in Navigator where the page will be inserted.</param>
523         /// <param name="page">The page to insert to Navigator.</param>
524         /// <exception cref="ArgumentOutOfRangeException">Thrown when the argument index is less than 0, or greater than the number of pages.</exception>
525         /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
526         /// <since_tizen> 9 </since_tizen>
527         public void Insert(int index, Page page)
528         {
529             if ((index < 0) || (index > navigationPages.Count))
530             {
531                 throw new ArgumentOutOfRangeException(nameof(index), "index should be greater than or equal to 0, and less than or equal to the number of pages.");
532             }
533
534             if (page == null)
535             {
536                 throw new ArgumentNullException(nameof(page), "page should not be null.");
537             }
538
539             //Duplicate page is not pushed.
540             if (navigationPages.Contains(page)) return;
541
542             //TODO: The following transition codes will be replaced with view transition.
543             InitializeAnimation();
544
545             ShowContentOfPage(page);
546
547             if (index == PageCount)
548             {
549                 page.Opacity = 1.0f;
550                 page.SetVisible(true);
551             }
552             else
553             {
554                 page.SetVisible(false);
555                 page.Opacity = 0.0f;
556             }
557
558             navigationPages.Insert(index, page);
559             Add(page);
560             page.Navigator = this;
561             if (index == PageCount - 1)
562             {
563                 if (PageCount > 1)
564                 {
565                     NotifyAccessibilityStatesChangeOfPages(navigationPages[PageCount - 2], page);
566                 }
567                 else
568                 {
569                     NotifyAccessibilityStatesChangeOfPages(null, page);
570                 }
571             }
572         }
573
574         /// <summary>
575         /// Inserts a page to Navigator before an existing page.
576         /// If the page is already in Navigator, then it is not inserted.
577         /// </summary>
578         /// <param name="before">The existing page, before which a page will be inserted.</param>
579         /// <param name="page">The page to insert to Navigator.</param>
580         /// <exception cref="ArgumentNullException">Thrown when the argument before is null.</exception>
581         /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
582         /// <exception cref="ArgumentException">Thrown when the argument before does not exist in Navigator.</exception>
583         /// <since_tizen> 9 </since_tizen>
584         public void InsertBefore(Page before, Page page)
585         {
586             if (before == null)
587             {
588                 throw new ArgumentNullException(nameof(before), "before should not be null.");
589             }
590
591             if (page == null)
592             {
593                 throw new ArgumentNullException(nameof(page), "page should not be null.");
594             }
595
596             //Find the index of before page.
597             int beforeIndex = navigationPages.FindIndex(x => x == before);
598
599             //before does not exist in Navigator.
600             if (beforeIndex == -1)
601             {
602                 throw new ArgumentException("before does not exist in Navigator.", nameof(before));
603             }
604
605             Insert(beforeIndex, page);
606         }
607
608         /// <summary>
609         /// Removes a page from Navigator.
610         /// </summary>
611         /// <param name="page">The page to remove from Navigator.</param>
612         /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
613         /// <since_tizen> 9 </since_tizen>
614         public void Remove(Page page)
615         {
616             if (page == null)
617             {
618                 throw new ArgumentNullException(nameof(page), "page should not be null.");
619             }
620
621             //TODO: The following transition codes will be replaced with view transition.
622             InitializeAnimation();
623
624             HideContentOfPage(page);
625
626             if (page == Peek())
627             {
628                 if (PageCount >= 2)
629                 {
630                     navigationPages[PageCount - 2].Opacity = 1.0f;
631                     navigationPages[PageCount - 2].SetVisible(true);
632                     NotifyAccessibilityStatesChangeOfPages(page, navigationPages[PageCount - 2]);
633                 }
634                 else if (PageCount == 1)
635                 {
636                     NotifyAccessibilityStatesChangeOfPages(page, null);
637                 }
638             }
639             page.Navigator = null;
640             navigationPages.Remove(page);
641             base.Remove(page);
642         }
643
644         /// <summary>
645         /// Removes a page at the specified index of Navigator.
646         /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
647         /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
648         /// To find the current index of a page in Navigator, please use IndexOf(page).
649         /// </summary>
650         /// <param name="index">The index of a page in Navigator where the page will be removed.</param>
651         /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of pages.</exception>
652         /// <since_tizen> 9 </since_tizen>
653         public void RemoveAt(int index)
654         {
655             if ((index < 0) || (index >= navigationPages.Count))
656             {
657                 throw new ArgumentOutOfRangeException(nameof(index), "index should be greater than or equal to 0, and less than the number of pages.");
658             }
659
660             Remove(navigationPages[index]);
661         }
662
663         /// <summary>
664         /// Returns the page at the top of Navigator.
665         /// </summary>
666         /// <returns>The page at the top of Navigator.</returns>
667         /// <since_tizen> 9 </since_tizen>
668         public Page Peek()
669         {
670             if (navigationPages.Count == 0) return null;
671
672             return navigationPages[navigationPages.Count - 1];
673         }
674
675         /// <summary>
676         /// Disposes Navigator and all children on it.
677         /// </summary>
678         /// <param name="type">Dispose type.</param>
679         [EditorBrowsable(EditorBrowsableState.Never)]
680         protected override void Dispose(DisposeTypes type)
681         {
682             if (disposed)
683             {
684                 return;
685             }
686
687             if (type == DisposeTypes.Explicit)
688             {
689                 foreach (Page page in navigationPages)
690                 {
691                     Utility.Dispose(page);
692                 }
693                 navigationPages.Clear();
694
695                 Window window;
696
697                 if (navigatorWindow.TryGetValue(this, out window) == true)
698                 {
699                     navigatorWindow.Remove(this);
700                     windowNavigator.Remove(window);
701                 }
702             }
703
704             base.Dispose(type);
705         }
706
707         /// <summary>
708         /// Returns the default navigator of the given window.
709         /// </summary>
710         /// <returns>The default navigator of the given window.</returns>
711         /// <exception cref="ArgumentNullException">Thrown when the argument window is null.</exception>
712         /// <since_tizen> 9 </since_tizen>
713         public static Navigator GetDefaultNavigator(Window window)
714         {
715             if (window == null)
716             {
717                 throw new ArgumentNullException(nameof(window), "window should not be null.");
718             }
719
720             if (windowNavigator.ContainsKey(window) == true)
721             {
722                 return windowNavigator[window];
723             }
724
725             var defaultNavigator = new Navigator();
726             defaultNavigator.WidthResizePolicy = ResizePolicyType.FillToParent;
727             defaultNavigator.HeightResizePolicy = ResizePolicyType.FillToParent;
728             window.Add(defaultNavigator);
729             windowNavigator.Add(window, defaultNavigator);
730             navigatorWindow.Add(defaultNavigator, window);
731
732             return defaultNavigator;
733         }
734
735         /// <summary>
736         /// Create Transitions between currentTopPage and newTopPage
737         /// </summary>
738         /// <param name="currentTopPage">The top page of Navigator.</param>
739         /// <param name="newTopPage">The new top page after transition.</param>
740         /// <param name="pushTransition">True if this transition is for push new page</param>
741         private TransitionSet CreateTransitions(Page currentTopPage, Page newTopPage, bool pushTransition)
742         {
743             currentTopPage.SetVisible(true);
744             newTopPage.SetVisible(true);
745
746             List<View> taggedViewsInNewTopPage = new List<View>();
747             RetrieveTaggedViews(taggedViewsInNewTopPage, newTopPage, true);
748             List<View> taggedViewsInCurrentTopPage = new List<View>();
749             RetrieveTaggedViews(taggedViewsInCurrentTopPage, currentTopPage, true);
750
751             List<KeyValuePair<View, View>> sameTaggedViewPair = new List<KeyValuePair<View, View>>();
752             foreach (View currentTopPageView in taggedViewsInCurrentTopPage)
753             {
754                 bool findPair = false;
755                 foreach (View newTopPageView in taggedViewsInNewTopPage)
756                 {
757                     if ((currentTopPageView.TransitionOptions != null) && (newTopPageView.TransitionOptions != null) &&
758                         currentTopPageView.TransitionOptions?.TransitionTag == newTopPageView.TransitionOptions?.TransitionTag)
759                     {
760                         sameTaggedViewPair.Add(new KeyValuePair<View, View>(currentTopPageView, newTopPageView));
761                         findPair = true;
762                         break;
763                     }
764                 }
765                 if (findPair)
766                 {
767                     taggedViewsInNewTopPage.Remove(sameTaggedViewPair[sameTaggedViewPair.Count - 1].Value);
768                 }
769             }
770             foreach (KeyValuePair<View, View> pair in sameTaggedViewPair)
771             {
772                 taggedViewsInCurrentTopPage.Remove(pair.Key);
773             }
774
775             TransitionSet newTransitionSet = new TransitionSet();
776             foreach (KeyValuePair<View, View> pair in sameTaggedViewPair)
777             {
778                 TransitionItem pairTransition = transition.CreateTransition(pair.Key, pair.Value, pushTransition);
779                 if (pair.Value.TransitionOptions?.TransitionWithChild ?? false)
780                 {
781                     pairTransition.TransitionWithChild = true;
782                 }
783                 newTransitionSet.AddTransition(pairTransition);
784             }
785
786             newTransitionSet.Finished += (object sender, EventArgs e) =>
787             {
788                 if (newTopPage.Layout != null)
789                 {
790                     newTopPage.Layout.RequestLayout();
791                 }
792                 if (currentTopPage.Layout != null)
793                 {
794                     currentTopPage.Layout.RequestLayout();
795                 }
796                 transitionFinished = true;
797                 InvokeTransitionFinished();
798                 transitionSet.Dispose();
799                 currentTopPage.Opacity = 1.0f;
800             };
801
802             if (!pushTransition || newTopPage is DialogPage == false)
803             {
804                 View transitionView = (currentTopPage is ContentPage) ? (currentTopPage as ContentPage).Content : (currentTopPage as DialogPage).Content;
805                 if (currentTopPage.DisappearingTransition != null && transitionView != null)
806                 {
807                     TransitionItemBase disappearingTransition = currentTopPage.DisappearingTransition.CreateTransition(transitionView, false);
808                     disappearingTransition.TransitionWithChild = true;
809                     newTransitionSet.AddTransition(disappearingTransition);
810                 }
811                 else
812                 {
813                     currentTopPage.SetVisible(false);
814                 }
815             }
816             if (pushTransition || currentTopPage is DialogPage == false)
817             {
818                 View transitionView = (newTopPage is ContentPage) ? (newTopPage as ContentPage).Content : (newTopPage as DialogPage).Content;
819                 if (newTopPage.AppearingTransition != null && transitionView != null)
820                 {
821                     TransitionItemBase appearingTransition = newTopPage.AppearingTransition.CreateTransition(transitionView, true);
822                     appearingTransition.TransitionWithChild = true;
823                     newTransitionSet.AddTransition(appearingTransition);
824                 }
825             }
826
827             newTransitionSet.Play();
828
829             return newTransitionSet;
830         }
831
832         /// <summary>
833         /// Retrieve Tagged Views in the view tree.
834         /// </summary>
835         /// <param name="taggedViews">Returned tagged view list..</param>
836         /// <param name="view">Root View to get tagged child View.</param>
837         /// <param name="isRoot">Flag to check current View is page or not</param>
838         private void RetrieveTaggedViews(List<View> taggedViews, View view, bool isRoot)
839         {
840             if (!isRoot && view.TransitionOptions != null)
841             {
842                 if (!string.IsNullOrEmpty(view.TransitionOptions?.TransitionTag))
843                 {
844                     taggedViews.Add((view as View));
845                     if (view.TransitionOptions.TransitionWithChild)
846                     {
847                         return;
848                     }
849                 }
850
851             }
852
853             foreach (View child in view.Children)
854             {
855                 RetrieveTaggedViews(taggedViews, child, false);
856             }
857         }
858
859         /// <summary>
860         /// Notify accessibility states change of pages.
861         /// </summary>
862         /// <param name="disappearedPage">Disappeared page</param>
863         /// <param name="appearedPage">Appeared page</param>
864         private void NotifyAccessibilityStatesChangeOfPages(Page disappearedPage, Page appearedPage)
865         {
866             if (disappearedPage != null)
867             {
868                 disappearedPage.UnregisterDefaultLabel();
869                 //We can call disappearedPage.NotifyAccessibilityStatesChange
870                 //To reduce accessibility events, we are using currently highlighted view instead
871                 View curHighlightedView = Accessibility.Accessibility.GetCurrentlyHighlightedView();
872                 if (curHighlightedView != null)
873                 {
874                     curHighlightedView.NotifyAccessibilityStatesChange(new AccessibilityStates(AccessibilityState.Visible, AccessibilityState.Showing), AccessibilityStatesNotifyMode.Single);
875                 }
876             }
877
878             if (appearedPage != null)
879             {
880                 appearedPage.RegisterDefaultLabel();
881                 appearedPage.NotifyAccessibilityStatesChange(new AccessibilityStates(AccessibilityState.Visible, AccessibilityState.Showing), AccessibilityStatesNotifyMode.Single);
882             }
883         }
884
885         internal void InvokeTransitionFinished()
886         {
887             TransitionFinished?.Invoke(this, new EventArgs());
888         }
889
890         //TODO: The following transition codes will be replaced with view transition.
891         private void InitializeAnimation()
892         {
893             if (curAnimation != null)
894             {
895                 curAnimation.Stop();
896                 curAnimation.Clear();
897                 curAnimation = null;
898             }
899
900             if (newAnimation != null)
901             {
902                 newAnimation.Stop();
903                 newAnimation.Clear();
904                 newAnimation = null;
905             }
906         }
907
908         // Show and Register Content of Page to Accessibility bridge
909         private void ShowContentOfPage(Page page)
910         {
911             View content = (page is DialogPage) ? (page as DialogPage)?.Content : (page as ContentPage)?.Content;
912             if (content != null)
913             {
914                 content.Show(); // Calls RegisterDefaultLabel()
915             }
916         }
917
918         // Hide and Remove Content of Page from Accessibility bridge
919         private void HideContentOfPage(Page page)
920         {
921             View content = (page is DialogPage) ? (page as DialogPage)?.Content : (page as ContentPage)?.Content;
922             if (content != null)
923             {
924                 content.Hide(); // Calls UnregisterDefaultLabel()
925             }
926         }
927     }
928 }