[NUI] Add EnableBackNavigation to Page
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Navigation / Page.cs
index 9ee9440..f26a3a4 100755 (executable)
  */
 using System;
 using System.ComponentModel;
+using Tizen.NUI.Binding;
 
 namespace Tizen.NUI.Components
 {
     /// <summary>
-    /// PageAppearingEventArgs is a class to record page appearing event arguments which will be sent to user.
+    /// PageAppearingEventArgs is a class to record <see cref="Page.Appearing"/> event arguments which will be sent to user.
     /// </summary>
-    [EditorBrowsable(EditorBrowsableState.Never)]
+    /// <since_tizen> 9 </since_tizen>
     public class PageAppearingEventArgs : EventArgs
     {
     }
 
     /// <summary>
-    /// PageDisappearingEventArgs is a class to record page disappearing event arguments which will be sent to user.
+    /// PageDisappearingEventArgs is a class to record <see cref="Page.Disappearing"/> event arguments which will be sent to user.
     /// </summary>
-    [EditorBrowsable(EditorBrowsableState.Never)]
+    /// <since_tizen> 9 </since_tizen>
     public class PageDisappearingEventArgs : EventArgs
     {
     }
 
     /// <summary>
-    /// PageAppearedEventArgs is a class to record page appeared event arguments which will be sent to user.
+    /// PageAppearedEventArgs is a class to record <see cref="Page.Appeared"/> event arguments which will be sent to user.
     /// </summary>
-    [EditorBrowsable(EditorBrowsableState.Never)]
+    /// <since_tizen> 9 </since_tizen>
     public class PageAppearedEventArgs : EventArgs
     {
     }
 
     /// <summary>
-    /// PageDisappearedEventArgs is a class to record page disappeared event arguments which will be sent to user.
+    /// PageDisappearedEventArgs is a class to record <see cref="Page.Disappeared"/> event arguments which will be sent to user.
     /// </summary>
-    [EditorBrowsable(EditorBrowsableState.Never)]
+    /// <since_tizen> 9 </since_tizen>
     public class PageDisappearedEventArgs : EventArgs
     {
     }
@@ -57,22 +58,72 @@ namespace Tizen.NUI.Components
     /// <since_tizen> 9 </since_tizen>
     public abstract class Page : Control
     {
-        private const int DefaultTransitionDuration = 500;
+        /// <summary>
+        /// AppearingTransitionProperty
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static readonly BindableProperty AppearingTransitionProperty = BindableProperty.Create(nameof(AppearingTransition), typeof(TransitionBase), typeof(Page), null, propertyChanged: (bindable, oldValue, newValue) =>
+        {
+            var instance = (Page)bindable;
+            if (newValue != null)
+            {
+                instance.InternalAppearingTransition = newValue as TransitionBase;
+            }
+        },
+        defaultValueCreator: (bindable) =>
+        {
+            var instance = (Page)bindable;
+            return instance.InternalAppearingTransition;
+        });
+
+        /// <summary>
+        /// DisappearingTransitionProperty
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static readonly BindableProperty DisappearingTransitionProperty = BindableProperty.Create(nameof(DisappearingTransition), typeof(TransitionBase), typeof(Page), null, propertyChanged: (bindable, oldValue, newValue) =>
+        {
+            var instance = (Page)bindable;
+            if (newValue != null)
+            {
+                instance.InternalDisappearingTransition = newValue as TransitionBase;
+            }
+        },
+        defaultValueCreator: (bindable) =>
+        {
+            var instance = (Page)bindable;
+            return instance.InternalDisappearingTransition;
+        });
+
+        /// <summary>
+        /// EnableBackNavigationProperty
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static readonly BindableProperty EnableBackNavigationProperty = BindableProperty.Create(nameof(EnableBackNavigation), typeof(bool), typeof(Page), default(bool), propertyChanged: (bindable, oldValue, newValue) =>
+        {
+            var instance = (Page)bindable;
+            if (newValue != null)
+            {
+                instance.InternalEnableBackNavigation = (bool)newValue;
+            }
+        },
+        defaultValueCreator: (bindable) =>
+        {
+            var instance = (Page)bindable;
+            return instance.InternalEnableBackNavigation;
+        });
+
+        /// <inheritdoc/>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected internal BaseComponents.View LastFocusedView = null;
 
         private Navigator navigator = null;
 
         // Default transition is Fade.
-        private TransitionBase appearingTransition = new Fade()
-        {
-            TimePeriod = new TimePeriod(DefaultTransitionDuration),
-            AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Default),
-        };
+        private TransitionBase appearingTransition = null;
 
-        private TransitionBase disappearingTransition = new Fade()
-        {
-            TimePeriod = new TimePeriod(DefaultTransitionDuration),
-            AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Default),
-        };
+        private TransitionBase disappearingTransition = null;
+
+        private bool enableBackNavigation = true;
 
         /// <summary>
         /// Creates a new instance of a Page.
@@ -83,6 +134,15 @@ namespace Tizen.NUI.Components
         }
 
         /// <summary>
+        /// Creates a new instance of a Page with style.
+        /// </summary>
+        /// <param name="style">A style applied to the newly created Page.</param>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public Page(ControlStyle style) : base(style)
+        {
+        }
+
+        /// <summary>
         /// Navigator which has pushed the Page into its stack.
         /// If this Page has not been pushed into any Navigator, then Navigator is null.
         /// </summary>
@@ -110,6 +170,18 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public TransitionBase AppearingTransition
         {
+            get
+            {
+                return GetValue(AppearingTransitionProperty) as TransitionBase;
+            }
+            set
+            {
+                SetValue(AppearingTransitionProperty, value);
+                NotifyPropertyChanged();
+            }
+        }
+        private TransitionBase InternalAppearingTransition
+        {
             set
             {
                 appearingTransition = value;
@@ -126,6 +198,18 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         public TransitionBase DisappearingTransition
         {
+            get
+            {
+                return GetValue(DisappearingTransitionProperty) as TransitionBase;
+            }
+            set
+            {
+                SetValue(DisappearingTransitionProperty, value);
+                NotifyPropertyChanged();
+            }
+        }
+        private TransitionBase InternalDisappearingTransition
+        {
             set
             {
                 disappearingTransition = value;
@@ -137,29 +221,58 @@ namespace Tizen.NUI.Components
         }
 
         /// <summary>
-        /// An event for the page appearing signal which can be used to subscribe or unsubscribe the event handler provided by the user.
+        /// Appearing event is invoked right before the page appears.
         /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
+        /// <since_tizen> 9 </since_tizen>
         public event EventHandler<PageAppearingEventArgs> Appearing;
 
         /// <summary>
-        /// An event for the page disappearing signal which can be used to subscribe or unsubscribe the event handler provided by the user.
+        /// Disappearing event is invoked right before the page disappears.
         /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
+        /// <since_tizen> 9 </since_tizen>
         public event EventHandler<PageDisappearingEventArgs> Disappearing;
 
         /// <summary>
-        /// An event for the page appeared signal which can be used to subscribe or unsubscribe the event handler provided by the user.
+        /// Appeared event is invoked right after the page appears.
         /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
+        /// <since_tizen> 9 </since_tizen>
         public event EventHandler<PageAppearedEventArgs> Appeared;
 
         /// <summary>
-        /// An event for the page disappeared signal which can be used to subscribe or unsubscribe the event handler provided by the user.
+        /// Disappeared event is invoked right after the page disappears.
         /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
+        /// <since_tizen> 9 </since_tizen>
         public event EventHandler<PageDisappearedEventArgs> Disappeared;
 
+        /// <summary>
+        /// Gets or sets if this page is popped when back button or back key is pressed and released.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public bool EnableBackNavigation
+        {
+            get
+            {
+                return (bool)GetValue(EnableBackNavigationProperty);
+            }
+            set
+            {
+                SetValue(EnableBackNavigationProperty, value);
+                NotifyPropertyChanged();
+            }
+        }
+
+        private bool InternalEnableBackNavigation
+        {
+            set
+            {
+                enableBackNavigation = value;
+            }
+            get
+            {
+                return enableBackNavigation;
+            }
+        }
+
         internal void InvokeAppearing()
         {
             Appearing?.Invoke(this, new PageAppearingEventArgs());
@@ -179,5 +292,78 @@ namespace Tizen.NUI.Components
         {
             Disappeared?.Invoke(this, new PageDisappearedEventArgs());
         }
+
+        /// <summary>
+        /// works only when DefaultAlgorithm is enabled.
+        /// to save the currently focused View when disappeared.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected internal virtual void SaveKeyFocus()
+        {
+            if (FocusManager.Instance.IsDefaultAlgorithmEnabled())
+            {
+                if (this is DialogPage)
+                {
+                    FocusManager.Instance.ResetFocusFinderRootView();
+                }
+
+                var currentFocusedView = FocusManager.Instance.GetCurrentFocusView();
+                if (currentFocusedView)
+                {
+                    var findChild = FindDescendantByID(currentFocusedView.ID);
+                    if (findChild)
+                    {
+                        LastFocusedView = findChild;
+                        return;
+                    }
+                }
+                LastFocusedView = null;
+            }
+        }
+
+        /// <summary>
+        /// works only when DefaultAlgorithm is enabled.
+        /// to set key focused View when showing.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected internal virtual void RestoreKeyFocus()
+        {
+            if (FocusManager.Instance.IsDefaultAlgorithmEnabled())
+            {
+                if (LastFocusedView)
+                {
+                    FocusManager.Instance.SetCurrentFocusView(LastFocusedView);
+                }
+                else
+                {
+                    var temp = new Tizen.NUI.BaseComponents.View()
+                    {
+                        Size = new Size(0.1f, 0.1f, 0.0f),
+                        Position = new Position(0, 0, 0),
+                        Focusable = true,
+                    };
+                    this.Add(temp);
+                    temp.LowerToBottom();
+                    FocusManager.Instance.SetCurrentFocusView(temp);
+                    var focused = FocusManager.Instance.GetNearestFocusableActor(this, temp, Tizen.NUI.BaseComponents.View.FocusDirection.Down);
+                    if (focused)
+                    {
+                        FocusManager.Instance.SetCurrentFocusView(focused);
+                    }
+                    else
+                    {
+                        FocusManager.Instance.ClearFocus();
+                    }
+                    temp.Unparent();
+                    temp.Dispose();
+                }
+
+                if (this is DialogPage)
+                {
+                    FocusManager.Instance.SetFocusFinderRootView(this);
+                }
+            }
+        }
+
     }
 }