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 /// PoppedEventArgs is a class to record <see cref="Navigator.Popped"/> event arguments which will be sent to user.
28 /// <since_tizen> 9 </since_tizen>
29 public class PoppedEventArgs : EventArgs
32 /// Page popped by Navigator.
34 /// <since_tizen> 9 </since_tizen>
35 public Page Page { get; internal set; }
39 /// The Navigator is a class which navigates pages with stack methods such as Push and Pop.
42 /// With Transition class, Navigator supports smooth transition of View pair between two Pages
43 /// by using <see cref="PushWithTransition(Page)"/> and <see cref="PopWithTransition()"/> methods.
44 /// If current top Page and next top Page have <see cref="View"/>s those have same TransitionTag,
45 /// Navigator creates smooth transition motion for them.
46 /// Navigator.Transition property can be used to set properties of the Transition such as TimePeriod and AlphaFunction.
47 /// When all transitions are finished, Navigator calls a callback methods those connected on the "TransitionFinished" event.
51 /// Navigator navigator = new Navigator()
53 /// TimePeriod = new TimePeriod(500),
54 /// AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseInOutSine)
57 /// View view = new View()
59 /// TransitionOptions = new TransitionOptions()
61 /// /* Set properties for the transition of this View */
65 /// ContentPage newPage = new ContentPage()
70 /// Navigator.PushWithTransition(newPage);
73 /// <since_tizen> 9 </since_tizen>
74 public class Navigator : Control
76 private const int DefaultTransitionDuration = 500;
78 //This will be replaced with view transition class instance.
79 private Animation curAnimation = null;
81 //This will be replaced with view transition class instance.
82 private Animation newAnimation = null;
84 private TransitionSet transitionSet = null;
86 private Transition transition = new Transition()
88 TimePeriod = new TimePeriod(DefaultTransitionDuration),
89 AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Default),
92 private bool transitionFinished = true;
94 //TODO: Needs to consider how to remove disposed window from dictionary.
95 //Two dictionaries are required to remove disposed navigator from dictionary.
96 private static Dictionary<Window, Navigator> windowNavigator = new Dictionary<Window, Navigator>();
97 private static Dictionary<Navigator, Window> navigatorWindow = new Dictionary<Navigator, Window>();
99 private List<Page> navigationPages = new List<Page>();
102 /// Creates a new instance of a Navigator.
104 /// <since_tizen> 9 </since_tizen>
105 public Navigator() : base()
107 Layout = new AbsoluteLayout();
111 /// An event fired when Transition has been finished.
113 /// <since_tizen> 9 </since_tizen>
114 public event EventHandler<EventArgs> TransitionFinished;
117 /// An event fired when Pop of a page has been finished.
120 /// When you free resources in the Popped event handler, please make sure if the popped page is the page you find.
122 /// <since_tizen> 9 </since_tizen>
123 public event EventHandler<PoppedEventArgs> Popped;
126 /// Returns the count of pages in Navigator.
128 /// <since_tizen> 9 </since_tizen>
129 public int PageCount => navigationPages.Count;
132 /// Transition properties for the transition of View pair having same transition tag.
134 /// <since_tizen> 9 </since_tizen>
135 public Transition Transition
148 /// Pushes a page to Navigator.
149 /// If the page is already in Navigator, then it is not pushed.
151 /// <param name="page">The page to push to Navigator.</param>
152 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
153 /// <since_tizen> 9 </since_tizen>
154 public void PushWithTransition(Page page)
156 if (!transitionFinished)
158 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
164 throw new ArgumentNullException(nameof(page), "page should not be null.");
167 //Duplicate page is not pushed.
168 if (navigationPages.Contains(page)) return;
170 var topPage = Peek();
178 navigationPages.Add(page);
180 page.Navigator = this;
183 page.InvokeAppearing();
184 topPage.InvokeDisappearing();
186 transitionSet = CreateTransitions(topPage, page, true);
187 transitionSet.Finished += (object sender, EventArgs e) =>
189 if (page is DialogPage == false)
191 topPage.SetVisible(false);
195 page.InvokeAppeared();
196 topPage.InvokeDisappeared();
198 transitionFinished = false;
202 /// Pops the top page from Navigator.
204 /// <returns>The popped page.</returns>
205 /// <exception cref="InvalidOperationException">Thrown when there is no page in Navigator.</exception>
206 /// <since_tizen> 9 </since_tizen>
207 public Page PopWithTransition()
209 if (!transitionFinished)
211 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
215 if (navigationPages.Count == 0)
217 throw new InvalidOperationException("There is no page in Navigator.");
220 var topPage = Peek();
222 if (navigationPages.Count == 1)
226 //Invoke Popped event
227 Popped?.Invoke(this, new PoppedEventArgs() { Page = topPage });
231 var newTopPage = navigationPages[navigationPages.Count - 2];
234 newTopPage.InvokeAppearing();
235 topPage.InvokeDisappearing();
237 transitionSet = CreateTransitions(topPage, newTopPage, false);
238 transitionSet.Finished += (object sender, EventArgs e) =>
241 topPage.SetVisible(true);
244 newTopPage.InvokeAppeared();
245 topPage.InvokeDisappeared();
247 //Invoke Popped event
248 Popped?.Invoke(this, new PoppedEventArgs() { Page = topPage });
250 transitionFinished = false;
256 /// Pushes a page to Navigator.
257 /// If the page is already in Navigator, then it is not pushed.
259 /// <param name="page">The page to push to Navigator.</param>
260 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
261 /// <since_tizen> 9 </since_tizen>
262 public void Push(Page page)
264 if (!transitionFinished)
266 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
272 throw new ArgumentNullException(nameof(page), "page should not be null.");
275 //Duplicate page is not pushed.
276 if (navigationPages.Contains(page)) return;
286 navigationPages.Add(page);
288 page.Navigator = this;
291 page.InvokeAppearing();
292 curTop.InvokeDisappearing();
294 //TODO: The following transition codes will be replaced with view transition.
295 InitializeAnimation();
297 if (page is DialogPage == false)
299 curAnimation = new Animation(1000);
300 curAnimation.AnimateTo(curTop, "Opacity", 1.0f, 0, 1000);
301 curAnimation.EndAction = Animation.EndActions.StopFinal;
302 curAnimation.Finished += (object sender, EventArgs args) =>
304 curTop.SetVisible(false);
307 curTop.InvokeDisappeared();
312 page.SetVisible(true);
313 newAnimation = new Animation(1000);
314 newAnimation.AnimateTo(page, "Opacity", 1.0f, 0, 1000);
315 newAnimation.EndAction = Animation.EndActions.StopFinal;
316 newAnimation.Finished += (object sender, EventArgs e) =>
319 page.InvokeAppeared();
326 /// Pops the top page from Navigator.
328 /// <returns>The popped page.</returns>
329 /// <exception cref="InvalidOperationException">Thrown when there is no page in Navigator.</exception>
330 /// <since_tizen> 9 </since_tizen>
333 if (!transitionFinished)
335 Tizen.Log.Error("NUI", "Transition is still not finished.\n");
339 if (navigationPages.Count == 0)
341 throw new InvalidOperationException("There is no page in Navigator.");
346 if (navigationPages.Count == 1)
350 //Invoke Popped event
351 Popped?.Invoke(this, new PoppedEventArgs() { Page = curTop });
356 var newTop = navigationPages[navigationPages.Count - 2];
359 newTop.InvokeAppearing();
360 curTop.InvokeDisappearing();
362 //TODO: The following transition codes will be replaced with view transition.
363 InitializeAnimation();
365 if (curTop is DialogPage == false)
367 curAnimation = new Animation(1000);
368 curAnimation.AnimateTo(curTop, "Opacity", 0.0f, 0, 1000);
369 curAnimation.EndAction = Animation.EndActions.StopFinal;
370 curAnimation.Finished += (object sender, EventArgs e) =>
372 //Removes the current top page after transition is finished.
374 curTop.Opacity = 1.0f;
377 curTop.InvokeDisappeared();
379 //Invoke Popped event
380 Popped?.Invoke(this, new PoppedEventArgs() { Page = curTop });
384 newTop.Opacity = 1.0f;
385 newTop.SetVisible(true);
386 newAnimation = new Animation(1000);
387 newAnimation.AnimateTo(newTop, "Opacity", 1.0f, 0, 1000);
388 newAnimation.EndAction = Animation.EndActions.StopFinal;
389 newAnimation.Finished += (object sender, EventArgs e) =>
392 newTop.InvokeAppeared();
405 /// Returns the page of the given index in Navigator.
406 /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
407 /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
409 /// <param name="index">The index of a page in Navigator.</param>
410 /// <returns>The page of the given index in Navigator.</returns>
411 /// <exception cref="ArgumentOutOfRangeException">Thrown when the argument index is less than 0, or greater than the number of pages.</exception>
412 public Page GetPage(int index)
414 if ((index < 0) || (index > navigationPages.Count))
416 throw new ArgumentOutOfRangeException(nameof(index), "index should be greater than or equal to 0, and less than or equal to the number of pages.");
419 return navigationPages[index];
423 /// Returns the current index of the given page in Navigator.
424 /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
425 /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
427 /// <param name="page">The page in Navigator.</param>
428 /// <returns>The index of the given page in Navigator. If the given page is not in the Navigator, then -1 is returned.</returns>
429 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
430 /// <since_tizen> 9 </since_tizen>
431 public int IndexOf(Page page)
435 throw new ArgumentNullException(nameof(page), "page should not be null.");
438 for (int i = 0; i < navigationPages.Count; i++)
440 if (navigationPages[i] == page)
450 /// Inserts a page at the specified index of Navigator.
451 /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
452 /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
453 /// To find the current index of a page in Navigator, please use IndexOf(page).
454 /// If the page is already in Navigator, then it is not inserted.
456 /// <param name="index">The index of a page in Navigator where the page will be inserted.</param>
457 /// <param name="page">The page to insert to Navigator.</param>
458 /// <exception cref="ArgumentOutOfRangeException">Thrown when the argument index is less than 0, or greater than the number of pages.</exception>
459 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
460 /// <since_tizen> 9 </since_tizen>
461 public void Insert(int index, Page page)
463 if ((index < 0) || (index > navigationPages.Count))
465 throw new ArgumentOutOfRangeException(nameof(index), "index should be greater than or equal to 0, and less than or equal to the number of pages.");
470 throw new ArgumentNullException(nameof(page), "page should not be null.");
473 //Duplicate page is not pushed.
474 if (navigationPages.Contains(page)) return;
476 //TODO: The following transition codes will be replaced with view transition.
477 InitializeAnimation();
479 if (index == PageCount)
482 page.SetVisible(true);
486 page.SetVisible(false);
490 navigationPages.Insert(index, page);
492 page.Navigator = this;
496 /// Inserts a page to Navigator before an existing page.
497 /// If the page is already in Navigator, then it is not inserted.
499 /// <param name="before">The existing page, before which a page will be inserted.</param>
500 /// <param name="page">The page to insert to Navigator.</param>
501 /// <exception cref="ArgumentNullException">Thrown when the argument before is null.</exception>
502 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
503 /// <exception cref="ArgumentException">Thrown when the argument before does not exist in Navigator.</exception>
504 /// <since_tizen> 9 </since_tizen>
505 public void InsertBefore(Page before, Page page)
509 throw new ArgumentNullException(nameof(before), "before should not be null.");
514 throw new ArgumentNullException(nameof(page), "page should not be null.");
517 //Find the index of before page.
518 int beforeIndex = navigationPages.FindIndex(x => x == before);
520 //before does not exist in Navigator.
521 if (beforeIndex == -1)
523 throw new ArgumentException("before does not exist in Navigator.", nameof(before));
526 Insert(beforeIndex, page);
530 /// Removes a page from Navigator.
532 /// <param name="page">The page to remove from Navigator.</param>
533 /// <exception cref="ArgumentNullException">Thrown when the argument page is null.</exception>
534 /// <since_tizen> 9 </since_tizen>
535 public void Remove(Page page)
539 throw new ArgumentNullException(nameof(page), "page should not be null.");
542 //TODO: The following transition codes will be replaced with view transition.
543 InitializeAnimation();
545 if ((page == Peek()) && (PageCount >= 2))
547 navigationPages[PageCount - 2].Opacity = 1.0f;
548 navigationPages[PageCount - 2].SetVisible(true);
551 page.Navigator = null;
552 navigationPages.Remove(page);
557 /// Removes a page at the specified index of Navigator.
558 /// The indices of pages in Navigator are basically the order of pushing or inserting to Navigator.
559 /// So a page's index in Navigator can be changed whenever push/insert or pop/remove occurs.
560 /// To find the current index of a page in Navigator, please use IndexOf(page).
562 /// <param name="index">The index of a page in Navigator where the page will be removed.</param>
563 /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of pages.</exception>
564 /// <since_tizen> 9 </since_tizen>
565 public void RemoveAt(int index)
567 if ((index < 0) || (index >= navigationPages.Count))
569 throw new ArgumentOutOfRangeException(nameof(index), "index should be greater than or equal to 0, and less than the number of pages.");
572 Remove(navigationPages[index]);
576 /// Returns the page at the top of Navigator.
578 /// <returns>The page at the top of Navigator.</returns>
579 /// <since_tizen> 9 </since_tizen>
582 if (navigationPages.Count == 0) return null;
584 return navigationPages[navigationPages.Count - 1];
588 /// Disposes Navigator and all children on it.
590 /// <param name="type">Dispose type.</param>
591 [EditorBrowsable(EditorBrowsableState.Never)]
592 protected override void Dispose(DisposeTypes type)
599 if (type == DisposeTypes.Explicit)
601 foreach (Page page in navigationPages)
603 Utility.Dispose(page);
605 navigationPages.Clear();
609 if (navigatorWindow.TryGetValue(this, out window) == true)
611 navigatorWindow.Remove(this);
612 windowNavigator.Remove(window);
620 /// Returns the default navigator of the given window.
622 /// <returns>The default navigator of the given window.</returns>
623 /// <exception cref="ArgumentNullException">Thrown when the argument window is null.</exception>
624 /// <since_tizen> 9 </since_tizen>
625 public static Navigator GetDefaultNavigator(Window window)
629 throw new ArgumentNullException(nameof(window), "window should not be null.");
632 if (windowNavigator.ContainsKey(window) == true)
634 return windowNavigator[window];
637 var defaultNavigator = new Navigator();
638 defaultNavigator.WidthResizePolicy = ResizePolicyType.FillToParent;
639 defaultNavigator.HeightResizePolicy = ResizePolicyType.FillToParent;
640 window.Add(defaultNavigator);
641 windowNavigator.Add(window, defaultNavigator);
642 navigatorWindow.Add(defaultNavigator, window);
644 return defaultNavigator;
648 /// Create Transitions between currentTopPage and newTopPage
650 /// <param name="currentTopPage">The top page of Navigator.</param>
651 /// <param name="newTopPage">The new top page after transition.</param>
652 /// <param name="pushTransition">True if this transition is for push new page</param>
653 private TransitionSet CreateTransitions(Page currentTopPage, Page newTopPage, bool pushTransition)
655 currentTopPage.SetVisible(true);
656 newTopPage.SetVisible(true);
658 List<View> taggedViewsInNewTopPage = new List<View>();
659 RetrieveTaggedViews(taggedViewsInNewTopPage, newTopPage, true);
660 List<View> taggedViewsInCurrentTopPage = new List<View>();
661 RetrieveTaggedViews(taggedViewsInCurrentTopPage, currentTopPage, true);
663 List<KeyValuePair<View, View>> sameTaggedViewPair = new List<KeyValuePair<View, View>>();
664 foreach(View currentTopPageView in taggedViewsInCurrentTopPage)
666 bool findPair = false;
667 foreach(View newTopPageView in taggedViewsInNewTopPage)
669 if((currentTopPageView.TransitionOptions != null) && (newTopPageView.TransitionOptions != null) &&
670 currentTopPageView.TransitionOptions?.TransitionTag == newTopPageView.TransitionOptions?.TransitionTag)
672 sameTaggedViewPair.Add(new KeyValuePair<View, View>(currentTopPageView, newTopPageView));
679 taggedViewsInNewTopPage.Remove(sameTaggedViewPair[sameTaggedViewPair.Count - 1].Value);
682 foreach(KeyValuePair<View, View> pair in sameTaggedViewPair)
684 taggedViewsInCurrentTopPage.Remove(pair.Key);
687 TransitionSet newTransitionSet = new TransitionSet();
688 foreach(KeyValuePair<View, View> pair in sameTaggedViewPair)
690 TransitionItem pairTransition = transition.CreateTransition(pair.Key, pair.Value);
691 if(pair.Value.TransitionOptions?.TransitionWithChild ?? false)
693 pairTransition.TransitionWithChild = true;
695 newTransitionSet.AddTransition(pairTransition);
698 newTransitionSet.Finished += (object sender, EventArgs e) =>
700 if(newTopPage.Layout != null)
702 newTopPage.Layout.RequestLayout();
704 if(currentTopPage.Layout != null)
706 currentTopPage.Layout.RequestLayout();
708 transitionFinished = true;
709 InvokeTransitionFinished();
710 transitionSet.Dispose();
711 currentTopPage.Opacity = 1.0f;
714 if (!pushTransition || newTopPage is DialogPage == false)
716 View transitionView = (currentTopPage is ContentPage) ? (currentTopPage as ContentPage).Content : (currentTopPage as DialogPage).Content;
717 if (currentTopPage.DisappearingTransition != null && transitionView != null)
719 TransitionItemBase disappearingTransition = currentTopPage.DisappearingTransition.CreateTransition(transitionView, false);
720 disappearingTransition.TransitionWithChild = true;
721 newTransitionSet.AddTransition(disappearingTransition);
725 currentTopPage.SetVisible(false);
728 if (pushTransition || currentTopPage is DialogPage == false)
730 View transitionView = (newTopPage is ContentPage) ? (newTopPage as ContentPage).Content : (newTopPage as DialogPage).Content;
731 if (newTopPage.AppearingTransition != null && transitionView != null)
733 TransitionItemBase appearingTransition = newTopPage.AppearingTransition.CreateTransition(transitionView, true);
734 appearingTransition.TransitionWithChild = true;
735 newTransitionSet.AddTransition(appearingTransition);
739 newTransitionSet.Play();
741 return newTransitionSet;
745 /// Retrieve Tagged Views in the view tree.
747 /// <param name="taggedViews">Returned tagged view list..</param>
748 /// <param name="view">Root View to get tagged child View.</param>
749 /// <param name="isPage">Flag to check current View is page or not</param>
750 private void RetrieveTaggedViews(List<View> taggedViews, View view, bool isPage)
754 if (!string.IsNullOrEmpty(view.TransitionOptions?.TransitionTag))
756 taggedViews.Add((view as View));
759 if (view.ChildCount == 0)
764 if (view.TransitionOptions?.TransitionWithChild ?? false)
769 foreach (View child in view.Children)
771 RetrieveTaggedViews(taggedViews, child, false);
775 internal void InvokeTransitionFinished()
777 TransitionFinished?.Invoke(this, new EventArgs());
780 //TODO: The following transition codes will be replaced with view transition.
781 private void InitializeAnimation()
783 if (curAnimation != null)
786 curAnimation.Clear();
790 if (newAnimation != null)
793 newAnimation.Clear();