[NUI] Add constructors with string style to apply string style
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Navigation / Page.cs
index 9be8a77..136e595 100755 (executable)
  */
 using System;
 using System.ComponentModel;
-using Tizen.NUI.BaseComponents;
+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>
-    /// The Page class is a class which is an element of navigation.
+    /// PageAppearedEventArgs is a class to record <see cref="Page.Appeared"/> event arguments which will be sent to user.
     /// </summary>
-    [EditorBrowsable(EditorBrowsableState.Never)]
-    public class Page : Control
+    /// <since_tizen> 9 </since_tizen>
+    public class PageAppearedEventArgs : EventArgs
     {
-        private AppBar appBar = null;
-        private View content = null;
+    }
 
+    /// <summary>
+    /// PageDisappearedEventArgs is a class to record <see cref="Page.Disappeared"/> event arguments which will be sent to user.
+    /// </summary>
+    /// <since_tizen> 9 </since_tizen>
+    public class PageDisappearedEventArgs : EventArgs
+    {
+    }
+
+    /// <summary>
+    /// The Page class is a class which is an element of navigation.
+    /// </summary>
+    /// <since_tizen> 9 </since_tizen>
+    public abstract class Page : Control
+    {
         /// <summary>
-        /// Creates a new instance of a Page.
+        /// AppearingTransitionProperty
         /// </summary>
-        /// <param name="content">The content to set to Content of Page.</param>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public Page(View content = null) : this(null, content)
+        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>
-        /// Creates a new instance of a Page.
+        /// DisappearingTransitionProperty
         /// </summary>
-        /// <param name="appBar">The content to set to AppBar of Page.</param>
-        /// <param name="content">The content to set to Content of Page.</param>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public Page(AppBar appBar, View content = null) : base()
+        public static readonly BindableProperty DisappearingTransitionProperty = BindableProperty.Create(nameof(DisappearingTransition), typeof(TransitionBase), typeof(Page), null, propertyChanged: (bindable, oldValue, newValue) =>
         {
-            //AppBar and Content are located vertically.
-            var linearLayout = new LinearLayout();
-            linearLayout.LinearOrientation = LinearLayout.Orientation.Vertical;
-            Layout = linearLayout;
-
-            //Page fills to parent by default.
-            WidthResizePolicy = ResizePolicyType.FillToParent;
-            HeightResizePolicy = ResizePolicyType.FillToParent;
-
-            if (appBar)
+            var instance = (Page)bindable;
+            if (newValue != null)
             {
-                AppBar = appBar;
+                instance.InternalDisappearingTransition = newValue as TransitionBase;
             }
+        },
+        defaultValueCreator: (bindable) =>
+        {
+            var instance = (Page)bindable;
+            return instance.InternalDisappearingTransition;
+        });
 
-            if (content)
+        /// <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)
             {
-                Content = content;
+                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 = null;
+
+        private TransitionBase disappearingTransition = null;
+
+        private bool enableBackNavigation = true;
+
+        /// <summary>
+        /// Creates a new instance of a Page.
+        /// </summary>
+        /// <since_tizen> 9 </since_tizen>
+        public Page() : base()
+        {
         }
 
         /// <summary>
-        /// Dispose Page and all children on it.
+        /// Creates a new instance of Page with style.
         /// </summary>
-        /// <param name="type">Dispose type.</param>
+        /// <param name="style">Creates Page by special style defined in UX.</param>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        protected override void Dispose(DisposeTypes type)
+        public Page(string style) : base(style)
         {
-            if (disposed)
+        }
+
+        /// <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>
+        /// <since_tizen> 9 </since_tizen>
+        public Navigator Navigator
+        {
+            get
             {
-                return;
+                return navigator;
             }
-
-            if (type == DisposeTypes.Explicit)
+            internal set
             {
-                if (appBar != null)
+                if (navigator == value)
                 {
-                    Utility.Dispose(appBar);
+                    return;
                 }
 
-                if (content != null)
-                {
-                    Utility.Dispose(content);
-                }
+                navigator = value;
             }
-
-            base.Dispose(type);
         }
 
         /// <summary>
-        /// AppBar of Page. AppBar is added to Children automatically.
+        /// Transition properties for the transition of Views in this page during this page is pushed to Navigator.
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public AppBar AppBar
+        public TransitionBase AppearingTransition
         {
             get
             {
-                return appBar;
+                return GetValue(AppearingTransitionProperty) as TransitionBase;
             }
             set
             {
-                if (appBar == value)
-                {
-                    return;
-                }
-
-                if (appBar != null)
-                {
-                    Remove(appBar);
-                }
-
-                appBar = value;
-                if (appBar == null)
-                {
-                    return;
-                }
-
-                appBar.Weight = 0.0f;
-
-                ResetContent();
+                SetValue(AppearingTransitionProperty, value);
+                NotifyPropertyChanged();
+            }
+        }
+        private TransitionBase InternalAppearingTransition
+        {
+            set
+            {
+                appearingTransition = value;
+            }
+            get
+            {
+                return appearingTransition;
             }
         }
 
         /// <summary>
-        /// Content of Page. Content is added to Children automatically.
+        /// Transition properties for the transition of Views in this page during this page is popped from Navigator.
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public View Content
+        public TransitionBase DisappearingTransition
         {
             get
             {
-                return content;
+                return GetValue(DisappearingTransitionProperty) as TransitionBase;
             }
             set
             {
-                if (content == value)
-                {
-                    return;
-                }
+                SetValue(DisappearingTransitionProperty, value);
+                NotifyPropertyChanged();
+            }
+        }
+        private TransitionBase InternalDisappearingTransition
+        {
+            set
+            {
+                disappearingTransition = value;
+            }
+            get
+            {
+                return disappearingTransition;
+            }
+        }
 
-                if (content != null)
-                {
-                    Remove(content);
-                }
+        /// <summary>
+        /// Appearing event is invoked right before the page appears.
+        /// </summary>
+        /// <since_tizen> 9 </since_tizen>
+        public event EventHandler<PageAppearingEventArgs> Appearing;
 
-                content = value;
-                if (content == null)
-                {
-                    return;
-                }
+        /// <summary>
+        /// Disappearing event is invoked right before the page disappears.
+        /// </summary>
+        /// <since_tizen> 9 </since_tizen>
+        public event EventHandler<PageDisappearingEventArgs> Disappearing;
 
-                content.Weight = 1.0f;
+        /// <summary>
+        /// Appeared event is invoked right after the page appears.
+        /// </summary>
+        /// <since_tizen> 9 </since_tizen>
+        public event EventHandler<PageAppearedEventArgs> Appeared;
 
-                ResetContent();
-            }
-        }
+        /// <summary>
+        /// Disappeared event is invoked right after the page disappears.
+        /// </summary>
+        /// <since_tizen> 9 </since_tizen>
+        public event EventHandler<PageDisappearedEventArgs> Disappeared;
 
-        private void ResetContent()
+        /// <summary>
+        /// Gets or sets if this page proceeds back navigation when back button or back key is pressed and released.
+        /// Back navigation pops the peek page if Navigator has more than one page.
+        /// If Navigator has only one page, then the current program is exited.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public bool EnableBackNavigation
         {
-            //To keep the order of AppBar and Content, the existing contents are
-            //removed and added again.
-            if ((appBar != null) && Children.Contains(appBar))
+            get
             {
-                Remove(appBar);
+                return (bool)GetValue(EnableBackNavigationProperty);
             }
-
-            if ((content != null) && Children.Contains(content))
+            set
             {
-                Remove(content);
+                SetValue(EnableBackNavigationProperty, value);
+                NotifyPropertyChanged();
             }
+        }
 
-            if (appBar != null)
+        private bool InternalEnableBackNavigation
+        {
+            set
             {
-                Add(appBar);
+                enableBackNavigation = value;
             }
-
-            if (content != null)
+            get
             {
-                Add(content);
+                return enableBackNavigation;
             }
         }
 
+        internal void InvokeAppearing()
+        {
+            Appearing?.Invoke(this, new PageAppearingEventArgs());
+        }
+
+        internal void InvokeDisappearing()
+        {
+            Disappearing?.Invoke(this, new PageDisappearingEventArgs());
+        }
+
+        internal void InvokeAppeared()
+        {
+            Appeared?.Invoke(this, new PageAppearedEventArgs());
+        }
+
+        internal void InvokeDisappeared()
+        {
+            Disappeared?.Invoke(this, new PageDisappearedEventArgs());
+        }
+
         /// <summary>
-        /// An event for the page appearing signal which can be used to subscribe or unsubscribe the event handler provided by the user.
+        /// works only when DefaultAlgorithm is enabled.
+        /// to save the currently focused View when disappeared.
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public event EventHandler<PageAppearingEventArgs> Appearing;
+        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>
-        /// An event for the page disappearing signal which can be used to subscribe or unsubscribe the event handler provided by the user.
+        /// works only when DefaultAlgorithm is enabled.
+        /// to set key focused View when showing.
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public event EventHandler<PageDisappearingEventArgs> Disappearing;
+        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();
+                }
 
-        internal void InvokeAppearing()
+                if (this is DialogPage)
+                {
+                    FocusManager.Instance.SetFocusFinderRootView(this);
+                }
+            }
+        }
+
+        /// <summary>
+        /// Called when the back navigation is started.
+        /// Back navigation pops the peek page if Navigator has more than one page.
+        /// If Navigator has only one page, then the current program is exited.
+        /// </summary>
+        /// <param name="eventArgs">The back navigation information.</param>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected virtual void OnBackNavigation(PageBackNavigationEventArgs eventArgs)
         {
-            Appearing?.Invoke(this, new PageAppearingEventArgs());
+            if (Navigator.PageCount > 1)
+            {
+                Navigator.Pop();
+            }
+            else
+            {
+                NUIApplication.Current?.Exit();
+            }
         }
 
-        internal void InvokeDisappearing()
+        /// <summary>
+        /// Called when the back navigation is required outside Navigator.
+        /// </summary>
+        internal void NavigateBack()
         {
-            Disappearing?.Invoke(this, new PageDisappearingEventArgs());
+            OnBackNavigation(new PageBackNavigationEventArgs());
         }
     }
+
+    /// <summary>
+    /// PageBackNavigationEventArgs is a class to record back navigation event arguments which will sent to user.
+    /// </summary>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public class PageBackNavigationEventArgs : EventArgs
+    {
+    }
 }