2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.Collections.Generic;
20 using System.ComponentModel;
21 using Tizen.NUI.BaseComponents;
23 namespace Tizen.NUI.Components
26 /// The Navigator is a class which navigates pages with stack methods such as Push and Pop.
29 /// With Transition class, Navigator supports smooth transition of View pair between two Pages
30 /// by using <see cref="PushWithTransition(Page)"/> and <see cref="PopWithTransition()"/> methods.
31 /// If current top Page and next top Page have <see cref="View"/>s those have same TransitionTag,
32 /// Navigator creates smooth transition motion for them.
33 /// Navigator.Transition property can be used to set properties of the Transition such as TimePeriod and AlphaFunction.
34 /// When all transitions are finished, Navigator calls a callback methods those connected on the "TransitionFinished" event.
38 /// Navigator navigator = new Navigator()
40 /// TimePeriod = new TimePeriod(500),
41 /// AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseInOutSine)
44 /// View view = new View()
46 /// TransitionOptions = new TransitionOptions()
48 /// /* Set properties for the transition of this View */
52 /// ContentPage newPage = new ContentPage()
57 /// Navigator.PushWithTransition(newPage);
60 /// <since_tizen> 9 </since_tizen>
61 public class Navigator : Control
63 private const int DefaultTransitionDuration = 500;
65 //This will be replaced with view transition class instance.
66 private Animation curAnimation = null;
68 //This will be replaced with view transition class instance.
69 private Animation newAnimation = null;
71 private TransitionSet transitionSet = null;
73 private Transition transition = new Transition()
75 TimePeriod = new TimePeriod(DefaultTransitionDuration),
76 AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Default),
79 private bool transitionFinished = true;
81 //TODO: Needs to consider how to remove disposed window from dictionary.
82 //Two dictionaries are required to remove disposed navigator from dictionary.
83 private static Dictionary<Window, Navigator> windowNavigator = new Dictionary<Window, Navigator>();
84 private static Dictionary<Navigator, Window> navigatorWindow = new Dictionary<Navigator, Window>();
86 private List<Page> navigationPages = new List<Page>();
89 /// Creates a new instance of a Navigator.
91 /// <since_tizen> 9 </since_tizen>
92 public Navigator() : base()
94 Layout = new AbsoluteLayout();
98 /// An event fired when Transition has been finished.
100 /// <since_tizen> 9 </since_tizen>
101 public event EventHandler<EventArgs> TransitionFinished;
104 /// Returns the count of pages in Navigator.
106 /// <since_tizen> 9 </since_tizen>
107 public int PageCount => navigationPages.Count;
110 /// Transition properties for the transition of View pair having same transition tag.
112 /// <since_tizen> 9 </since_tizen>
113 public Transition Transition
126 /// Pushes a page to Navigator.
127 /// If the page is already in Navigator, then it is not pushed.
129 /// <param name="page">The page to push to Navigator.</param>
130 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
131 /// <since_tizen> 9 </since_tizen>
132 public void PushWithTransition(Page page)
134 if (!transitionFinished)
136 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
142 throw new ArgumentNullException(nameof(page), "page should not be null.");
145 //Duplicate page is not pushed.
146 if (navigationPages.Contains(page)) return;
148 var topPage = Peek();
156 navigationPages.Add(page);
158 page.Navigator = this;
161 page.InvokeAppearing();
162 topPage.InvokeDisappearing();
164 transitionSet = CreateTransition(topPage, page, true);
165 transitionSet.Finished += (object sender, EventArgs e) =>
167 topPage.SetVisible(false);
170 page.InvokeAppeared();
171 topPage.InvokeDisappeared();
173 transitionFinished = false;
177 /// Pops the top page from Navigator.
179 /// <returns>The popped page.</returns>
180 /// <exception cref="InvalidOperationException">Thrown when there is no page in Navigator.</exception>
181 /// <since_tizen> 9 </since_tizen>
182 public Page PopWithTransition()
184 if (!transitionFinished)
186 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
190 if (navigationPages.Count == 0)
192 throw new InvalidOperationException("There is no page in Navigator.");
195 var topPage = Peek();
197 if (navigationPages.Count == 1)
202 var newTopPage = navigationPages[navigationPages.Count - 2];
204 // newTopPage.RaiseAbove(topPage);
207 newTopPage.InvokeAppearing();
208 topPage.InvokeDisappearing();
210 transitionSet = CreateTransition(topPage, newTopPage, false);
211 transitionSet.Finished += (object sender, EventArgs e) =>
214 topPage.SetVisible(true);
217 newTopPage.InvokeAppeared();
218 topPage.InvokeDisappeared();
220 transitionFinished = false;
226 /// Pushes a page to Navigator.
227 /// If the page is already in Navigator, then it is not pushed.
229 /// <param name="page">The page to push to Navigator.</param>
230 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
231 /// <since_tizen> 9 </since_tizen>
232 public void Push(Page page)
234 if (!transitionFinished)
236 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
242 throw new ArgumentNullException(nameof(page), "page should not be null.");
245 //Duplicate page is not pushed.
246 if (navigationPages.Contains(page)) return;
256 navigationPages.Add(page);
258 page.Navigator = this;
261 page.InvokeAppearing();
262 curTop.InvokeDisappearing();
264 //TODO: The following transition codes will be replaced with view transition.
265 InitializeAnimation();
267 if (page is DialogPage == false)
269 curAnimation = new Animation(1000);
270 curAnimation.AnimateTo(curTop, "Opacity", 0.0f, 0, 1000);
271 curAnimation.EndAction = Animation.EndActions.StopFinal;
272 curAnimation.Finished += (object sender, EventArgs args) =>
274 curTop.SetVisible(false);
277 curTop.InvokeDisappeared();
282 page.SetVisible(true);
283 newAnimation = new Animation(1000);
284 newAnimation.AnimateTo(page, "Opacity", 1.0f, 0, 1000);
285 newAnimation.EndAction = Animation.EndActions.StopFinal;
286 newAnimation.Finished += (object sender, EventArgs e) =>
289 page.InvokeAppeared();
296 /// Pops the top page from Navigator.
298 /// <returns>The popped page.</returns>
299 /// <exception cref="InvalidOperationException">Thrown when there is no page in Navigator.</exception>
300 /// <since_tizen> 9 </since_tizen>
303 if (!transitionFinished)
305 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
309 if (navigationPages.Count == 0)
311 throw new InvalidOperationException("There is no page in Navigator.");
316 if (navigationPages.Count == 1)
322 var newTop = navigationPages[navigationPages.Count - 2];
325 newTop.InvokeAppearing();
326 curTop.InvokeDisappearing();
328 //TODO: The following transition codes will be replaced with view transition.
329 InitializeAnimation();
331 if (curTop is DialogPage == false)
333 curAnimation = new Animation(1000);
334 curAnimation.AnimateTo(curTop, "Opacity", 0.0f, 0, 1000);
335 curAnimation.EndAction = Animation.EndActions.StopFinal;
336 curAnimation.Finished += (object sender, EventArgs e) =>
338 //Removes the current top page after transition is finished.
340 curTop.Opacity = 1.0f;
343 curTop.InvokeDisappeared();
347 newTop.Opacity = 0.0f;
348 newTop.SetVisible(true);
349 newAnimation = new Animation(1000);
350 newAnimation.AnimateTo(newTop, "Opacity", 1.0f, 0, 1000);
351 newAnimation.EndAction = Animation.EndActions.StopFinal;
352 newAnimation.Finished += (object sender, EventArgs e) =>
355 newTop.InvokeAppeared();
368 /// Returns the page of the given index in Navigator.
369 /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
370 /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
372 /// <param name="index">The index of a page in Navigator.</param>
373 /// <returns>The page of the given index in Navigator.</returns>
374 /// <exception cref="ArgumentOutOfRangeException">Thrown when the argument index is less than 0, or greater than the number of pages.</exception>
375 public Page GetPage(int index)
377 if ((index < 0) || (index > navigationPages.Count))
379 throw new ArgumentOutOfRangeException(nameof(index), "index should be greater than or equal to 0, and less than or equal to the number of pages.");
382 return navigationPages[index];
386 /// Returns the current index of the given page in Navigator.
387 /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
388 /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
390 /// <param name="page">The page in Navigator.</param>
391 /// <returns>The index of the given page in Navigator. If the given page is not in the Navigator, then -1 is returned.</returns>
392 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
393 /// <since_tizen> 9 </since_tizen>
394 public int IndexOf(Page page)
398 throw new ArgumentNullException(nameof(page), "page should not be null.");
401 for (int i = 0; i < navigationPages.Count; i++)
403 if (navigationPages[i] == page)
413 /// Inserts a page at the specified index of Navigator.
414 /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
415 /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
416 /// To find the current index of a page in Navigator, please use IndexOf(page).
417 /// If the page is already in Navigator, then it is not inserted.
419 /// <param name="index">The index of a page in Navigator where the page will be inserted.</param>
420 /// <param name="page">The page to insert to Navigator.</param>
421 /// <exception cref="ArgumentOutOfRangeException">Thrown when the argument index is less than 0, or greater than the number of pages.</exception>
422 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
423 /// <since_tizen> 9 </since_tizen>
424 public void Insert(int index, Page page)
426 if ((index < 0) || (index > navigationPages.Count))
428 throw new ArgumentOutOfRangeException(nameof(index), "index should be greater than or equal to 0, and less than or equal to the number of pages.");
433 throw new ArgumentNullException(nameof(page), "page should not be null.");
436 //Duplicate page is not pushed.
437 if (navigationPages.Contains(page)) return;
439 //TODO: The following transition codes will be replaced with view transition.
440 InitializeAnimation();
442 if (index == PageCount)
445 page.SetVisible(true);
449 page.SetVisible(false);
453 navigationPages.Insert(index, page);
455 page.Navigator = this;
459 /// Inserts a page to Navigator before an existing page.
460 /// If the page is already in Navigator, then it is not inserted.
462 /// <param name="before">The existing page, before which a page will be inserted.</param>
463 /// <param name="page">The page to insert to Navigator.</param>
464 /// <exception cref="ArgumentNullException">Thrown when the argument before is null.</exception>
465 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
466 /// <exception cref="ArgumentException">Thrown when the argument before does not exist in Navigator.</exception>
467 /// <since_tizen> 9 </since_tizen>
468 public void InsertBefore(Page before, Page page)
472 throw new ArgumentNullException(nameof(before), "before should not be null.");
477 throw new ArgumentNullException(nameof(page), "page should not be null.");
480 //Find the index of before page.
481 int beforeIndex = navigationPages.FindIndex(x => x == before);
483 //before does not exist in Navigator.
484 if (beforeIndex == -1)
486 throw new ArgumentException("before does not exist in Navigator.", nameof(before));
489 Insert(beforeIndex, page);
493 /// Removes a page from Navigator.
495 /// <param name="page">The page to remove from Navigator.</param>
496 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
497 /// <since_tizen> 9 </since_tizen>
498 public void Remove(Page page)
502 throw new ArgumentNullException(nameof(page), "page should not be null.");
505 //TODO: The following transition codes will be replaced with view transition.
506 InitializeAnimation();
508 if ((page == Peek()) && (PageCount >= 2))
510 navigationPages[PageCount - 2].Opacity = 1.0f;
511 navigationPages[PageCount - 2].SetVisible(true);
514 page.Navigator = null;
515 navigationPages.Remove(page);
520 /// Removes a page at the specified index of Navigator.
521 /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
522 /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
523 /// To find the current index of a page in Navigator, please use IndexOf(page).
525 /// <param name="index">The index of a page in Navigator where the page will be removed.</param>
526 /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of pages.</exception>
527 /// <since_tizen> 9 </since_tizen>
528 public void RemoveAt(int index)
530 if ((index < 0) || (index >= navigationPages.Count))
532 throw new ArgumentOutOfRangeException(nameof(index), "index should be greater than or equal to 0, and less than the number of pages.");
535 Remove(navigationPages[index]);
539 /// Returns the page at the top of Navigator.
541 /// <returns>The page at the top of Navigator.</returns>
542 /// <since_tizen> 9 </since_tizen>
545 if (navigationPages.Count == 0) return null;
547 return navigationPages[navigationPages.Count - 1];
551 /// Disposes Navigator and all children on it.
553 /// <param name="type">Dispose type.</param>
554 [EditorBrowsable(EditorBrowsableState.Never)]
555 protected override void Dispose(DisposeTypes type)
562 if (type == DisposeTypes.Explicit)
564 foreach (Page page in navigationPages)
566 Utility.Dispose(page);
568 navigationPages.Clear();
572 if (navigatorWindow.TryGetValue(this, out window) == true)
574 navigatorWindow.Remove(this);
575 windowNavigator.Remove(window);
583 /// Returns the default navigator of the given window.
585 /// <returns>The default navigator of the given window.</returns>
586 /// <exception cref="ArgumentNullException">Thrown when the argument window is null.</exception>
587 /// <since_tizen> 9 </since_tizen>
588 public static Navigator GetDefaultNavigator(Window window)
592 throw new ArgumentNullException(nameof(window), "window should not be null.");
595 if (windowNavigator.ContainsKey(window) == true)
597 return windowNavigator[window];
600 var defaultNavigator = new Navigator();
601 defaultNavigator.WidthResizePolicy = ResizePolicyType.FillToParent;
602 defaultNavigator.HeightResizePolicy = ResizePolicyType.FillToParent;
603 window.Add(defaultNavigator);
604 windowNavigator.Add(window, defaultNavigator);
605 navigatorWindow.Add(defaultNavigator, window);
607 return defaultNavigator;
611 /// Create Transition between currentTopPage and newTopPage
613 /// <param name="currentTopPage">The top page of Navigator.</param>
614 /// <param name="newTopPage">The new top page after transition.</param>
615 /// <param name="pushTransition">True if this transition is for push new page</param>
616 private TransitionSet CreateTransition(Page currentTopPage, Page newTopPage, bool pushTransition)
618 currentTopPage.SetVisible(true);
619 newTopPage.SetVisible(true);
621 List<View> taggedViewsInNewTopPage = new List<View>();
622 RetrieveTaggedViews(taggedViewsInNewTopPage, newTopPage);
623 List<View> taggedViewsInCurrentTopPage = new List<View>();
624 RetrieveTaggedViews(taggedViewsInCurrentTopPage, currentTopPage);
626 List<KeyValuePair<View, View>> sameTaggedViewPair = new List<KeyValuePair<View, View>>();
627 foreach(View currentTopPageView in taggedViewsInCurrentTopPage)
629 bool findPair = false;
630 foreach(View newTopPageView in taggedViewsInNewTopPage)
632 if((currentTopPageView.TransitionOptions != null) && (newTopPageView.TransitionOptions != null) &&
633 currentTopPageView.TransitionOptions?.TransitionTag == newTopPageView.TransitionOptions?.TransitionTag)
635 sameTaggedViewPair.Add(new KeyValuePair<View, View>(currentTopPageView, newTopPageView));
642 taggedViewsInNewTopPage.Remove(sameTaggedViewPair[sameTaggedViewPair.Count - 1].Value);
645 foreach(KeyValuePair<View, View> pair in sameTaggedViewPair)
647 taggedViewsInCurrentTopPage.Remove(pair.Key);
650 TransitionSet newTransitionSet = new TransitionSet();
651 foreach(KeyValuePair<View, View> pair in sameTaggedViewPair)
653 TransitionItem pairTransition = transition.CreateTransition(pair.Key, pair.Value);
654 if(pair.Value.TransitionOptions?.TransitionWithChild ?? false)
656 pairTransition.TransitionWithChild = true;
658 newTransitionSet.AddTransition(pairTransition);
661 newTransitionSet.Finished += (object sender, EventArgs e) =>
663 transitionFinished = true;
664 InvokeTransitionFinished();
665 transitionSet.Dispose();
666 currentTopPage.Opacity = 1.0f;
669 // default appearing/disappearing transition - fast fade (half duration compaired with that of view pair transition)
670 int duration = (transition.TimePeriod.DurationMilliseconds + transition.TimePeriod.DelayMilliseconds);
671 float durationSeconds = (float)duration / 1000.0f;
673 TransitionItemBase disappearingTransition = currentTopPage.DisappearingTransition.CreateTransition(currentTopPage, false);
674 TransitionItemBase appearingTransition = newTopPage.AppearingTransition.CreateTransition(newTopPage, true);
675 disappearingTransition.TransitionWithChild = true;
676 appearingTransition.TransitionWithChild = true;
677 newTransitionSet.AddTransition(disappearingTransition);
678 newTransitionSet.AddTransition(appearingTransition);
680 newTransitionSet.Play();
682 return newTransitionSet;
686 /// Retrieve Tagged Views in the view tree.
688 /// <param name="taggedViews">Returned tagged view list..</param>
689 /// <param name="view">Root View to get tagged child View.</param>
690 private void RetrieveTaggedViews(List<View> taggedViews, View view)
692 if (!string.IsNullOrEmpty(view.TransitionOptions?.TransitionTag))
694 taggedViews.Add((view as View));
697 if (view.ChildCount == 0)
702 if (view.TransitionOptions?.TransitionWithChild ?? false)
707 foreach (View child in view.Children)
709 RetrieveTaggedViews(taggedViews, child);
713 internal void InvokeTransitionFinished()
715 TransitionFinished?.Invoke(this, new EventArgs());
718 //TODO: The following transition codes will be replaced with view transition.
719 private void InitializeAnimation()
721 if (curAnimation != null)
724 curAnimation.Clear();
728 if (newAnimation != null)
731 newAnimation.Clear();