[NUI] Update NUI samples based on the latest back navigation
authorJaehyun Cho <jae_hyun.cho@samsung.com>
Tue, 29 Nov 2022 07:40:33 +0000 (16:40 +0900)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Tue, 6 Dec 2022 08:08:09 +0000 (17:08 +0900)
NUI samples has been updated to use EnableBackNavigation and
OnBackNavigation of Navigator and Page based on the default action of
the latest back navigation.

test/NUITizenGallery/NUITizenGallery.cs
test/Tizen.NUI.StyleGuide/Examples/NavigatorExample.cs
test/Tizen.NUI.StyleGuide/Tizen.NUI.StyleGuide.cs

index b9ddae8..6b36109 100755 (executable)
@@ -147,6 +147,27 @@ namespace NUITizenGallery
         }
     }
 
+    public class CustomNavigator : Navigator
+    {
+        // Customizes how to handle back navigation.
+        // base.OnBackNavigation() pops the peek page.
+        protected override void OnBackNavigation(BackNavigationEventArgs args)
+        {
+            if (PageCount > 1)
+            {
+                // Deactivates the peek page example before page pop.
+                if (Peek() is IExample currentExample)
+                {
+                    currentExample.Deactivate();
+                }
+            }
+
+            // Pops the peek page if navigator has more than one page.
+            // If navigator has only one page, then the program is exited.
+            base.OnBackNavigation(args);
+        }
+    }
+
     class Program : NUIApplication
     {
         private Window window;
@@ -160,24 +181,6 @@ namespace NUITizenGallery
         private SearchField field;
         private List<Gallery> testSource;
 
-        public void OnKeyEvent(object sender, Window.KeyEventArgs e)
-        {
-            if (e.Key.State == Key.StateType.Up)
-            {
-                if (e.Key.KeyPressedName == "Escape" || e.Key.KeyPressedName == "XF86Back" || e.Key.KeyPressedName == "BackSpace")
-                {
-                    if (null != currentExample)
-                    {
-                        ExitSample();
-                    }
-                    else
-                    {
-                        Exit();
-                    }
-                }
-            }
-        }
-
         public void OnSelectionChanged(object sender, SelectionChangedEventArgs ev)
         {
             Console.WriteLine($"@@@ OnSelectionChanged() {ev.CurrentSelection}");
@@ -229,9 +232,25 @@ namespace NUITizenGallery
         {
             window = GetDefaultWindow();
             window.Title = "NUITizenGallery";
-            window.KeyEvent += OnKeyEvent;
 
-            navigator = window.GetDefaultNavigator();
+            // In this example, GetDefaultNavigator() has not been called before so default navigator has not been set yet.
+            // Therefore, the following codes for unsetting and disposing the previous default navigator are not required in this example.
+            /*
+            var prevDefaultNavigator = window.GetDefaultNavigator();
+            window.Remove(prevDefaultNavigator);
+            prevDefaultNavigator.Dispose();
+            prevDefaultNavigator = null;
+            */
+
+            // Uses customized navigator to customize how to handle back navigation.
+            navigator = new CustomNavigator()
+            {
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+            };
+
+            // Sets the customized navigator as the default navigator of the window.
+            window.SetDefaultNavigator(navigator);
         }
 
         void OnSearchBtnClicked(object sender, ClickedEventArgs e)
index 716d4f0..bc0ad67 100644 (file)
@@ -49,16 +49,92 @@ namespace Tizen.NUI.StyleGuide
             };
 
             navigator = NUIApplication.GetDefaultWindow().GetDefaultNavigator();
-            Content = generatePage();
+            Content = generateContent(this);
+        }
+
+        protected override void OnBackNavigation(PageBackNavigationEventArgs eventArgs)
+        {
+            var noButton = new Button()
+            {
+                Text = "No"
+            };
+            noButton.Clicked += (o, e) =>
+            {
+                // Pops the dialog page.
+                Navigator.Pop();
+            };
+
+            var yesButton = new Button()
+            {
+                Text = "Yes"
+            };
+            yesButton.Clicked += (o, e) =>
+            {
+                // Removes the dialog page.
+                Navigator.RemoveAt(Navigator.PageCount - 1);
+
+                // Pops the content page.
+                Navigator.Pop();
+            };
+
+            DialogPage.ShowAlertDialog("", "Do you really want to pop?", new View[] { noButton, yesButton });
         }
 
         private ContentPage generatePage()
         {
-            var page = new ContentPage();
-            page.AppBar = new AppBar()
+            var page = new ContentPage()
             {
-                Title = "NavigatorTestPage",
+                AppBar = new AppBar()
+                {
+                    Title = "NavigatorTestPage" + navigator.PageCount.ToString()
+                }
             };
+            page.Content = generateContent(page);
+
+            return page;
+        }
+
+        private void updateBackNavigationButtonText(Button button, Navigator navigator)
+        {
+            if (navigator == null)
+            {
+                return;
+            }
+
+            if (navigator.EnableBackNavigation)
+            {
+                button.Text = "Navigator BackNavigation is enabled";
+            }
+            else
+            {
+                button.Text = "Navigator BackNavigation is disabled";
+            }
+        }
+
+        private void updatePageBackNavigationButtonText(Button button, Page page)
+        {
+            if (page == null)
+            {
+                return;
+            }
+
+            if (page.EnableBackNavigation)
+            {
+                button.Text = "Page BackNavigation is enabled";
+            }
+            else
+            {
+                button.Text = "Page BackNavigation is disabled";
+            }
+        }
+
+        private View generateContent(Page page)
+        {
+            if (page == null)
+            {
+                Tizen.Log.Error("NUITEST", "The page should not be null");
+                return null;
+            }
 
             var contentView = new View()
             {
@@ -69,7 +145,6 @@ namespace Tizen.NUI.StyleGuide
                     CellPadding = new Size2D(0, 10),
                 },
             };
-            page.Content = contentView;
 
             var buttonPush = new Button()
             {
@@ -85,7 +160,6 @@ namespace Tizen.NUI.StyleGuide
                     return;
                 }
                 var newPage = generatePage();
-                newPage.AppBar.Title = "NavigatorTestPage" + navigator.PageCount.ToString();
                 navigator.Push(newPage);
             };
             contentView.Add(buttonPush);
@@ -107,6 +181,54 @@ namespace Tizen.NUI.StyleGuide
             };
             contentView.Add(buttonPop);
 
+            var buttonBackNavigation = new Button()
+            {
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+            };
+
+            updateBackNavigationButtonText(buttonBackNavigation, navigator);
+
+            buttonBackNavigation.Clicked += (s, e) =>
+            {
+                if (navigator == null)
+                {
+                    Tizen.Log.Error("NUITEST", "The page should be pushed to a Navigator");
+                    return;
+                }
+
+                navigator.EnableBackNavigation = !navigator.EnableBackNavigation;
+                updateBackNavigationButtonText(buttonBackNavigation, navigator);
+            };
+            contentView.Add(buttonBackNavigation);
+
+            var buttonPageBackNavigation = new Button()
+            {
+                Text = "Page BackNavigation is enabled",
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+            };
+
+            buttonPageBackNavigation.Clicked += (s, e) =>
+            {
+                if (navigator == null)
+                {
+                    Tizen.Log.Error("NUITEST", "The page should be pushed to a Navigator");
+                    return;
+                }
+
+                page.EnableBackNavigation = !page.EnableBackNavigation;
+                updatePageBackNavigationButtonText(buttonPageBackNavigation, page);
+            };
+            contentView.Add(buttonPageBackNavigation);
+
+            // Update back navigation button's text when the page is appearing.
+            page.Appearing += (o, e) =>
+            {
+                updateBackNavigationButtonText(buttonBackNavigation, navigator);
+                updatePageBackNavigationButtonText(buttonPageBackNavigation, page);
+            };
+
             Color backgroundColor;
             switch (navigator.PageCount % numberOfDifferentColor)
             {
@@ -125,7 +247,9 @@ namespace Tizen.NUI.StyleGuide
             };
             buttonPush.BackgroundColor = backgroundColor;
             buttonPop.BackgroundColor = backgroundColor;
-            return page;
+            buttonBackNavigation.BackgroundColor = backgroundColor;
+            buttonPageBackNavigation.BackgroundColor = backgroundColor;
+            return contentView;
         }
     }
 }
index c520827..da74df0 100644 (file)
@@ -165,6 +165,27 @@ namespace Tizen.NUI.StyleGuide
         }
     }
 
+    public class CustomNavigator : Navigator
+    {
+        // Customizes how to handle back navigation.
+        // base.OnBackNavigation() pops the peek page.
+        protected override void OnBackNavigation(BackNavigationEventArgs args)
+        {
+            if (PageCount > 1)
+            {
+                // Deactivates the peek page example before page pop.
+                if (Peek() is IExample currentExample)
+                {
+                    currentExample.Deactivate();
+                }
+            }
+
+            // Pops the peek page if navigator has more than one page.
+            // If navigator has only one page, then the program is exited.
+            base.OnBackNavigation(args);
+        }
+    }
+
     class Program : NUIApplication
     {
         private Window window;
@@ -175,26 +196,6 @@ namespace Tizen.NUI.StyleGuide
         private SearchField field;
         private List<ControlMenu> testSource;
 
-        public void OnKeyEvent(object sender, Window.KeyEventArgs e)
-        {
-            // FIXME:: Navigator should provide Back/Escape event processing.
-            if (e.Key.State == Key.StateType.Up)
-            {
-                Log.Info("StyleGuide", $"[{e.Key.KeyPressedName}] is pressed!\n");
-                if (e.Key.KeyPressedName == "Escape" || e.Key.KeyPressedName == "XF86Back" || e.Key.KeyPressedName == "BackSpace")
-                {
-                    if (navigator == null) return;
-
-                    ExitSample();
-
-                    if (navigator.PageCount == 0)
-                    {
-                        Exit();
-                    }
-                }
-            }
-        }
-
         public void OnSelectionChanged(object sender, SelectionChangedEventArgs ev)
         {
             Console.WriteLine($"OnSelectionChanged() {ev.CurrentSelection}");
@@ -222,13 +223,30 @@ namespace Tizen.NUI.StyleGuide
         {
             window = GetDefaultWindow();
             window.Title = "NUI Style Guide";
-            window.KeyEvent += OnKeyEvent;
 
-            navigator = window.GetDefaultNavigator();
+            // In this example, GetDefaultNavigator() has not been called before so default navigator has not been set yet.
+            // Therefore, the following codes for unsetting and disposing the previous default navigator are not required in this example.
+            /*
+            var prevDefaultNavigator = window.GetDefaultNavigator();
+            window.Remove(prevDefaultNavigator);
+            prevDefaultNavigator.Dispose();
+            prevDefaultNavigator = null;
+            */
+
+            // Uses customized navigator to customize how to handle back navigation.
+            navigator = new CustomNavigator()
+            {
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+            };
+
             navigator.Popped += (object obj, PoppedEventArgs ev) =>
             {
                 Page top = navigator.Peek();
             };
+
+            // Sets the customized navigator as the default navigator of the window.
+            window.SetDefaultNavigator(navigator);
         }
 
         void OnSearchBtnClicked(object sender, ClickedEventArgs e)
@@ -339,17 +357,6 @@ namespace Tizen.NUI.StyleGuide
             }
         }
 
-        private void ExitSample()
-        {
-            if (navigator.Peek() is IExample currentExample)
-            {
-                currentExample.Deactivate();
-            }
-
-            navigator.Pop();
-            // FullGC();
-        }
-
         private void FullGC()
         {
             global::System.GC.Collect();