[NUI] Update NUISettings to guide how to use Navigator in Widget
authorJaehyun Cho <jae_hyun.cho@samsung.com>
Tue, 23 Aug 2022 04:29:07 +0000 (13:29 +0900)
committerSangHyeon Jade Lee <dltkdgus1764@gmail.com>
Tue, 30 Aug 2022 07:41:27 +0000 (16:41 +0900)
To pass data between Widgets, data is passed from Widget to Main app and
Main app passes the data to another Widget.
Since this is difficult to support, NUISettings guides how to use
Navigator in Widget.
By using Widget's own Navigator, Widget knows its Navigator and its
Pages. So Widget can pass data between its Pages easily.

test/NUISettings/NUISettings/NUISettings.cs
test/NUISettings/NUISettingsReset/NUISettingsReset.cs

index a454a19..58610fc 100755 (executable)
@@ -40,6 +40,8 @@ namespace WidgetApplicationTemplate
             int widgetWidth = window.Size.Width;
             int widgetHeight = window.Size.Height;
 
+            Navigator navigator = window.GetDefaultNavigator();
+
             Bundle bundle = new Bundle();
             bundle.AddItem(" ", " ");
             String encodedBundle = bundle.Encode();
@@ -47,23 +49,74 @@ namespace WidgetApplicationTemplate
             // Add Widget of "secondPage@NUISettingsReset" in advance to avoid loading pending.
             AddWidget(out secondPageWidgetView, "secondPage@NUISettingsReset", encodedBundle, widgetWidth, widgetHeight, 0.0f);
 
-            Navigator navigator = window.GetDefaultNavigator();
+            // Add Widget of "thirdPage@NUISettingsReset" in advance to avoid loading pending.
+            AddWidget(out thirdPageWidgetView, "thirdPage@NUISettingsReset", encodedBundle, widgetWidth, widgetHeight, 0.0f);
 
-            var button = new Button()
+            // Add Widget of "fourthPage@NUISettingsReset" in advance to avoid loading pending.
+            AddWidget(out fourthPageWidgetView, "fourthPage@NUISettingsReset", encodedBundle, widgetWidth, widgetHeight, 0.0f);
+
+            var content = new View()
+            {
+                Layout = new LinearLayout()
+                {
+                    LinearOrientation = LinearLayout.Orientation.Vertical,
+                    CellPadding = new Size2D(0, 20),
+                },
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+            };
+
+            var secondPageButton = new Button()
             {
                 Text = "Click to Second Page",
                 WidthSpecification = LayoutParamPolicies.MatchParent,
                 HeightSpecification = LayoutParamPolicies.MatchParent,
             };
-            button.Clicked += (o, e) =>
+            secondPageButton.Clicked += (o, e) =>
             {
-                var page = new ContentPage();
-                page.Content = secondPageWidgetView;
+                var page = new ContentPage()
+                {
+                    Content = secondPageWidgetView,
+                };
                 navigator.Push(page);
             };
+            content.Add(secondPageButton);
+
+            var thirdPageButton = new Button()
+            {
+                Text = "Click to Third Page",
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+            };
+            thirdPageButton.Clicked += (o, e) =>
+            {
+                var page = new ContentPage()
+                {
+                    Content = thirdPageWidgetView,
+                };
+                navigator.Push(page);
+            };
+            content.Add(thirdPageButton);
+
+            var fourthPageButton = new Button()
+            {
+                Text = "Click to Fourth Page",
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+            };
+            fourthPageButton.Clicked += (o, e) =>
+            {
+                var page = new DialogPage()
+                {
+                    Content = fourthPageWidgetView,
+                    EnableScrim = false, // fourthPageWidgetView's DialogPage.Scrim is used, so this DialogPage.Scrim should not be used.
+                };
+                navigator.Push(page);
+            };
+            content.Add(fourthPageButton);
 
             // Push the first page.
-            PushContentPage("First Page", button);
+            PushContentPage("First Page", content);
         }
 
         void AddWidget(out WidgetView widgetView, string widgetID, string contentInfo, int width, int height, float updatePeriod)
@@ -77,9 +130,6 @@ namespace WidgetApplicationTemplate
             Window window = GetDefaultWindow();
             Navigator navigator = window.GetDefaultNavigator();
 
-            window.KeyEvent += OnKeyEvent;
-            window.TouchEvent += OnTouchEvent;
-
             var page = new ContentPage()
             {
                 AppBar = new AppBar()
index 1d4ed8d..89668c2 100755 (executable)
@@ -23,7 +23,38 @@ using Tizen.Applications;
 
 namespace WidgetTemplate
 {
-    class SecondPage : Widget
+    class SecondPage : ContentPage
+    {
+        public SecondPage(Window window) : base()
+        {
+            if (window == null)
+            {
+                throw new ArgumentNullException(nameof(window), "window should not be null.");
+            }
+
+            var appBar = new AppBar()
+            {
+                Title = "Second Page",
+            };
+
+            AppBar = appBar;
+
+            var button = new Button()
+            {
+                Text = "Click to Third Page",
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+            };
+            button.Clicked += (o, e) =>
+            {
+                window.GetDefaultNavigator().Push(new ThirdPage(window));
+            };
+
+            Content = button;
+        }
+    }
+
+    class SecondPageWidget : Widget
     {
         protected override void OnCreate(string contentInfo, Window window)
         {
@@ -31,59 +62,33 @@ namespace WidgetTemplate
 
             window.BackgroundColor = Color.Transparent;
 
-            // Update Widget Content by sending message to add the third page in advance.
-            Bundle nextBundle = new Bundle();
-            nextBundle.AddItem("WIDGET_ID", "thirdPage@NUISettingsReset");
-            nextBundle.AddItem("WIDGET_ACTION", "ADD");
-            nextBundle.AddItem("WIDGET_WIDTH", window.Size.Width.ToString());
-            nextBundle.AddItem("WIDGET_HEIGHT", window.Size.Height.ToString());
-            String encodedBundle = nextBundle.Encode();
-            SetContentInfo(encodedBundle);
+            var secondPage = new SecondPage(window);
 
             var appBar = new AppBar()
             {
-                Title = "Second Page",
+                Title = secondPage.AppBar.Title,
                 AutoNavigationContent = false,
             };
 
+            // Since this is Widget, bundle with "POP" message should be sent to make the main app pops this.
             var appBarStyle = ThemeManager.GetStyle("Tizen.NUI.Components.AppBar");
 
             var navigationContent = new Button(((AppBarStyle)appBarStyle).BackButton);
             navigationContent.Clicked += (o, e) =>
             {
-                // Update Widget Content by sending message to pop the second page.
-                Bundle nextBundle2 = new Bundle();
-                nextBundle2.AddItem("WIDGET_ACTION", "POP");
-                String encodedBundle2 = nextBundle2.Encode();
-                SetContentInfo(encodedBundle2);
+                // Update Widget Content by sending message to pop the current page.
+                Bundle nextBundle = new Bundle();
+                nextBundle.AddItem("WIDGET_ACTION", "POP");
+                String encodedBundle = nextBundle.Encode();
+                SetContentInfo(encodedBundle);
             };
 
+            appBar.AutoNavigationContent = false;
             appBar.NavigationContent = navigationContent;
 
-            var page = new ContentPage()
-            {
-                AppBar = appBar,
-            };
-
-            var button = new Button()
-            {
-                Text = "Click to Third Page",
-                WidthSpecification = LayoutParamPolicies.MatchParent,
-                HeightSpecification = LayoutParamPolicies.MatchParent,
-            };
-            button.Clicked += (o, e) =>
-            {
-                // Update Widget Content by sending message to push the third page.
-                Bundle nextBundle2 = new Bundle();
-                nextBundle2.AddItem("WIDGET_ID", "thirdPage@NUISettingsReset");
-                nextBundle2.AddItem("WIDGET_PAGE", "CONTENT_PAGE");
-                nextBundle2.AddItem("WIDGET_ACTION", "PUSH");
-                String encodedBundle2 = nextBundle2.Encode();
-                SetContentInfo(encodedBundle2);
-            };
+            secondPage.AppBar = appBar;
 
-            page.Content = button;
-            window.Add(page);
+            window.GetDefaultNavigator().Push(secondPage);
         }
 
         protected override void OnPause()
@@ -112,7 +117,37 @@ namespace WidgetTemplate
         }
     }
 
-    class ThirdPage : Widget
+    class ThirdPage : ContentPage
+    {
+        public ThirdPage(Window window) : base()
+        {
+            if (window == null)
+            {
+                throw new ArgumentNullException(nameof(window), "window should not be null.");
+            }
+
+            var appBar = new AppBar()
+            {
+                Title = "Third Page",
+            };
+            AppBar = appBar;
+
+            var button = new Button()
+            {
+                Text = "Click to Fourth Page",
+                WidthSpecification = LayoutParamPolicies.MatchParent,
+                HeightSpecification = LayoutParamPolicies.MatchParent,
+            };
+            button.Clicked += (o, e) =>
+            {
+                window.GetDefaultNavigator().Push(new FourthPage(window));
+            };
+
+            Content = button;
+        }
+    }
+
+    class ThirdPageWidget : Widget
     {
         protected override void OnCreate(string contentInfo, Window window)
         {
@@ -120,59 +155,33 @@ namespace WidgetTemplate
 
             window.BackgroundColor = Color.Transparent;
 
-            // Update Widget Content by sending message to add the fourth page in advance.
-            Bundle nextBundle = new Bundle();
-            nextBundle.AddItem("WIDGET_ID", "fourthPage@NUISettingsReset");
-            nextBundle.AddItem("WIDGET_WIDTH", window.Size.Width.ToString());
-            nextBundle.AddItem("WIDGET_HEIGHT", window.Size.Height.ToString());
-            nextBundle.AddItem("WIDGET_ACTION", "ADD");
-            String encodedBundle = nextBundle.Encode();
-            SetContentInfo(encodedBundle);
+            var thirdPage = new ThirdPage(window);
 
             var appBar = new AppBar()
             {
-                Title = "Third Page",
+                Title = thirdPage.AppBar.Title,
                 AutoNavigationContent = false,
             };
 
+            // Since this is Widget, bundle with "POP" message should be sent to make the main app pops this.
             var appBarStyle = ThemeManager.GetStyle("Tizen.NUI.Components.AppBar");
 
             var navigationContent = new Button(((AppBarStyle)appBarStyle).BackButton);
             navigationContent.Clicked += (o, e) =>
             {
-                // Update Widget Content by sending message to pop the third page.
-                Bundle nextBundle2 = new Bundle();
-                nextBundle2.AddItem("WIDGET_ACTION", "POP");
-                String encodedBundle2 = nextBundle2.Encode();
-                SetContentInfo(encodedBundle2);
+                // Update Widget Content by sending message to pop the current page.
+                Bundle nextBundle = new Bundle();
+                nextBundle.AddItem("WIDGET_ACTION", "POP");
+                String encodedBundle = nextBundle.Encode();
+                SetContentInfo(encodedBundle);
             };
 
+            appBar.AutoNavigationContent = false;
             appBar.NavigationContent = navigationContent;
 
-            var page = new ContentPage()
-            {
-                AppBar = appBar,
-            };
+            thirdPage.AppBar = appBar;
 
-            var button = new Button()
-            {
-                Text = "Click to Fourth Page",
-                WidthSpecification = LayoutParamPolicies.MatchParent,
-                HeightSpecification = LayoutParamPolicies.MatchParent,
-            };
-            button.Clicked += (o, e) =>
-            {
-                // Update Widget Content by sending message to push the fourth page.
-                Bundle nextBundle2 = new Bundle();
-                nextBundle2.AddItem("WIDGET_ID", "fourthPage@NUISettingsReset");
-                nextBundle2.AddItem("WIDGET_PAGE", "DIALOG_PAGE");
-                nextBundle2.AddItem("WIDGET_ACTION", "PUSH");
-                String encodedBundle2 = nextBundle2.Encode();
-                SetContentInfo(encodedBundle2);
-            };
-
-            page.Content = button;
-            window.Add(page);
+            window.GetDefaultNavigator().Push(thirdPage);
         }
 
         protected override void OnPause()
@@ -201,7 +210,77 @@ namespace WidgetTemplate
         }
     }
 
-    class FourthPage : Widget
+    class FourthPage : DialogPage
+    {
+        public FourthPage(Window window) : base()
+        {
+            if (window == null)
+            {
+                throw new ArgumentNullException(nameof(window), "window should not be null.");
+            }
+
+            var button = new Button()
+            {
+                Text = "OK",
+            };
+            button.Clicked += (o, e) =>
+            {
+                window.GetDefaultNavigator().Pop();
+            };
+
+            var dialog = new AlertDialog()
+            {
+                Title = "Fourth Page",
+                Message = "Message",
+                Actions = new View[] { button },
+            };
+
+            Content = dialog;
+        }
+    }
+
+    class WidgetDialogPage : DialogPage
+    {
+        public WidgetDialogPage(Window window) : base()
+        {
+            if (window == null)
+            {
+                throw new ArgumentNullException(nameof(window), "window should not be null.");
+            }
+
+            // Default Scrim calls navigator.Pop() when the Scrim is touched.
+            // To pop widget page when the Scrim is touched, bundle with "POP" message should be sent to make the main app pops this.
+            Scrim = CreateScrim(window);
+        }
+
+        public EventHandler<TouchEventArgs> ScrimTouchEvent;
+
+        private View CreateScrim(Window window)
+        {
+            if (window == null)
+            {
+                throw new ArgumentNullException(nameof(window), "window should not be null.");
+            }
+
+            var scrimStyle = ThemeManager.GetStyle("Tizen.NUI.Components.DialogPage.Scrim");
+            var scrim = new VisualView(scrimStyle);
+            scrim.Size = window.Size;
+            scrim.TouchEvent += (object source, TouchEventArgs e) =>
+            {
+                if ((EnableDismissOnScrim == true) && (e.Touch.GetState(0) == PointStateType.Up))
+                {
+                    // Default Scrim calls navigator.Pop() when the Scrim is touched.
+                    // To pop widget page when the Scrim is touched, bundle with "POP" message should be sent to make the main app pops this.
+                    ScrimTouchEvent?.Invoke(source, e);
+                }
+                return true;
+            };
+
+            return scrim;
+        }
+    }
+
+    class FourthPageWidget : Widget
     {
         protected override void OnCreate(string contentInfo, Window window)
         {
@@ -209,17 +288,19 @@ namespace WidgetTemplate
 
             window.BackgroundColor = Color.Transparent;
 
+            var fourthPage = new WidgetDialogPage(window);
+
             var button = new Button()
             {
                 Text = "OK",
             };
             button.Clicked += (o, e) =>
             {
-                // Update Widget Content by sending message to pop the fourth page.
-                Bundle nextBundle2 = new Bundle();
-                nextBundle2.AddItem("WIDGET_ACTION", "POP");
-                String encodedBundle2 = nextBundle2.Encode();
-                SetContentInfo(encodedBundle2);
+                // Update Widget Content by sending message to pop the current page.
+                Bundle nextBundle = new Bundle();
+                nextBundle.AddItem("WIDGET_ACTION", "POP");
+                String encodedBundle = nextBundle.Encode();
+                SetContentInfo(encodedBundle);
             };
 
             var dialog = new AlertDialog()
@@ -229,7 +310,20 @@ namespace WidgetTemplate
                 Actions = new View[] { button },
             };
 
-            window.Add(dialog);
+            fourthPage.Content = dialog;
+
+            // Default Scrim calls navigator.Pop() when the Scrim is touched.
+            // To pop widget page when the Scrim is touched, bundle with "POP" message should be sent to make the main app pops this.
+            fourthPage.ScrimTouchEvent += (o, e) =>
+            {
+                // Update Widget Content by sending message to pop the current page.
+                Bundle nextBundle = new Bundle();
+                nextBundle.AddItem("WIDGET_ACTION", "POP");
+                String encodedBundle = nextBundle.Encode();
+                SetContentInfo(encodedBundle);
+            };
+
+            window.GetDefaultNavigator().Push(fourthPage);
         }
 
         protected override void OnPause()
@@ -286,9 +380,9 @@ namespace WidgetTemplate
         static void Main(string[] args)
         {
             Dictionary<System.Type, string> widgetSet = new Dictionary<Type, string>();
-            widgetSet.Add(typeof(SecondPage), "secondPage@NUISettingsReset");
-            widgetSet.Add(typeof(ThirdPage), "thirdPage@NUISettingsReset");
-            widgetSet.Add(typeof(FourthPage), "fourthPage@NUISettingsReset");
+            widgetSet.Add(typeof(SecondPageWidget), "secondPage@NUISettingsReset");
+            widgetSet.Add(typeof(ThirdPageWidget), "thirdPage@NUISettingsReset");
+            widgetSet.Add(typeof(FourthPageWidget), "fourthPage@NUISettingsReset");
             var app = new Program(widgetSet);
             app.Run(args);
         }