[NUI] Add Dialog class
authorJaehyun Cho <jae_hyun.cho@samsung.com>
Mon, 16 Nov 2020 08:34:51 +0000 (17:34 +0900)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Mon, 21 Dec 2020 12:05:23 +0000 (21:05 +0900)
Dialog is used to show a popup content with background scrim.

Navigator provides static method ShowDialog which pushes Page containing
Dialog.

src/Tizen.NUI.Components/Controls/Dialog.cs [new file with mode: 0755]
src/Tizen.NUI.Components/Controls/Navigation/Navigator.cs
test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/DialogSample.cs [new file with mode: 0755]

diff --git a/src/Tizen.NUI.Components/Controls/Dialog.cs b/src/Tizen.NUI.Components/Controls/Dialog.cs
new file mode 100755 (executable)
index 0000000..bc2df64
--- /dev/null
@@ -0,0 +1,240 @@
+/*
+ * Copyright(c) 2020 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+using System;
+using System.ComponentModel;
+using Tizen.NUI.BaseComponents;
+
+namespace Tizen.NUI.Components
+{
+    /// <summary>
+    /// Dialog class shows a popup content with background scrim.
+    /// </summary>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    public class Dialog : Control
+    {
+        private View popupContent = null;
+        private View scrim = null;
+        private bool enableScrim = true;
+
+        /// <summary>
+        /// Creates a new instance of Dialog.
+        /// </summary>
+        /// <param name="content">The content to set to Content of Dialog.</param>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public Dialog(View content = null) : base()
+        {
+            EnableScrim = true;
+            EnableDismissOnScrim = true;
+
+            //FIXME: Needs to separate GUI implementation codes to style cs file.
+            var scrim = new VisualView();
+            scrim.BackgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.5f);
+            //FIXME: Needs to set proper size to Scrim.
+            scrim.Size = NUIApplication.GetDefaultWindow().Size;
+            scrim.TouchEvent += (object source, TouchEventArgs e) =>
+            {
+                if ((EnableDismissOnScrim == true) && (e.Touch.GetState(0) == PointStateType.Up))
+                {
+                    //TODO: To show hide animation.
+                    this.Hide();
+                    this.Dispose();
+                }
+                return true;
+            };
+
+            Scrim = scrim;
+
+            if (content != null)
+            {
+                content.RaiseAbove(scrim);
+                Content = content;
+            }
+        }
+
+        /// <summary>
+        /// Popup content of Dialog. Content is added to Children automatically.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public View Content
+        {
+            get
+            {
+                return popupContent;
+            }
+            set
+            {
+                if (popupContent == value)
+                {
+                    return;
+                }
+
+                if (popupContent != null)
+                {
+                    Remove(popupContent);
+                }
+
+                popupContent = value;
+                if (popupContent == null)
+                {
+                    return;
+                }
+
+                popupContent.Relayout += PopupContentRelayout;
+
+                //FIXME: Needs to separate GUI implementation codes to style cs file.
+                CalculateContentPosition();
+
+                Add(popupContent);
+
+                if (Scrim != null)
+                {
+                    popupContent.RaiseAbove(Scrim);
+                }
+            }
+        }
+
+        private void PopupContentRelayout(object sender, EventArgs e)
+        {
+            //FIXME: Needs to separate GUI implementation codes to style cs file.
+            CalculateContentPosition();
+        }
+
+        private void CalculateContentPosition()
+        {
+            var size = popupContent.Size2D;
+            var parent = GetParent();
+            Size2D parentSize;
+
+            if ((parent != null) && (parent is View))
+            {
+                parentSize = ((View)parent).Size;
+            }
+            else
+            {
+                parentSize = NUIApplication.GetDefaultWindow().Size;
+            }
+
+            popupContent.Position2D = new Position2D((parentSize.Width - size.Width) / 2, (parentSize.Height - size.Height) / 2);
+        }
+
+        /// <summary>
+        /// Indicates to show scrim behind popup content.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public bool EnableScrim
+        {
+            get
+            {
+                return enableScrim;
+            }
+            set
+            {
+                if (enableScrim == value)
+                {
+                    return;
+                }
+
+                enableScrim = value;
+
+                if ((Scrim != null) && (enableScrim != Scrim.Visibility))
+                {
+                    if (enableScrim)
+                    {
+                        Scrim.Show();
+                    }
+                    else
+                    {
+                        Scrim.Hide();
+                    }
+                }
+            }
+        }
+
+        /// <summary>
+        /// Indicates to dismiss popup content by touching on scrim.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public bool EnableDismissOnScrim { get; set; }
+
+        /// <summary>
+        /// Scrim covers background behind popup content.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected internal View Scrim
+        {
+            get
+            {
+                return scrim;
+            }
+            set
+            {
+                if (scrim == value)
+                {
+                    return;
+                }
+
+                if (scrim != null)
+                {
+                    Remove(scrim);
+                }
+
+                scrim = value;
+                if (scrim == null)
+                {
+                    return;
+                }
+
+                Add(scrim);
+
+                if (Content != null)
+                {
+                    Content.RaiseAbove(scrim);
+                }
+            }
+        }
+
+        /// <summary>
+        /// Dispose Dialog and all children on it.
+        /// </summary>
+        /// <param name="type">Dispose type.</param>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected override void Dispose(DisposeTypes type)
+        {
+            if (disposed)
+            {
+                return;
+            }
+
+            if (type == DisposeTypes.Explicit)
+            {
+                if (popupContent != null)
+                {
+                    popupContent.Relayout -= PopupContentRelayout;
+                    Utility.Dispose(popupContent);
+                }
+
+                if (scrim != null)
+                {
+                    Utility.Dispose(scrim);
+                }
+            }
+
+            base.Dispose(type);
+        }
+    }
+}
index a75d344..4a2b34a 100755 (executable)
@@ -335,5 +335,50 @@ namespace Tizen.NUI.Components
 
             return defaultNavigator;
         }
+
+        /// <summary>
+        /// Shows a dialog by pushing a page containing dialog to default navigator.
+        /// </summary>
+        /// <param name="content">The content of Dialog.</param>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static void ShowDialog(View content = null)
+        {
+            var window = NUIApplication.GetDefaultWindow();
+            var defaultNavigator = window.GetDefaultNavigator();
+
+            var dialog = new Dialog(content);
+            SetDialogScrim(dialog);
+
+            var dialogPage = new Page(dialog);
+            defaultNavigator.Push(dialogPage);
+        }
+
+        private static void SetDialogScrim(Dialog dialog)
+        {
+            if (dialog == null)
+            {
+                return;
+            }
+
+            var window = NUIApplication.GetDefaultWindow();
+            var defaultNavigator = window.GetDefaultNavigator();
+            var defaultScrim = dialog.Scrim;
+
+            //Copies default scrim's GUI properties.
+            var scrim = new VisualView();
+            scrim.BackgroundColor = defaultScrim.BackgroundColor;
+            scrim.Size = defaultScrim.Size;
+            scrim.TouchEvent += (object source, View.TouchEventArgs e) =>
+            {
+                if (e.Touch.GetState(0) == PointStateType.Up)
+                {
+                    defaultNavigator.Pop();
+                }
+
+                return true;
+            };
+
+            dialog.Scrim = scrim;
+        }
     }
 } //namespace Tizen.NUI
diff --git a/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/DialogSample.cs b/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/DialogSample.cs
new file mode 100755 (executable)
index 0000000..812d556
--- /dev/null
@@ -0,0 +1,50 @@
+using Tizen.NUI.BaseComponents;
+using Tizen.NUI.Components;
+
+namespace Tizen.NUI.Samples
+{
+    public class DialogSample : IExample
+    {
+        private int oldPageCount = 0;
+
+        public void Activate()
+        {
+            var window = NUIApplication.GetDefaultWindow();
+
+            oldPageCount = window.GetDefaultNavigator().NavigationPages.Count;
+
+            var button = new Button()
+            {
+                Text = "Click to show Dialog",
+                WidthResizePolicy = ResizePolicyType.FillToParent,
+                HeightResizePolicy = ResizePolicyType.FillToParent
+            };
+
+            button.Clicked += (object sender, ClickedEventArgs e) =>
+            {
+                var textLabel = new TextLabel("Message")
+                {
+                    BackgroundColor = Color.White,
+                    Size = new Size(180, 180),
+                    HorizontalAlignment = HorizontalAlignment.Center,
+                    VerticalAlignment = VerticalAlignment.Center
+                };
+
+                Navigator.ShowDialog(textLabel);
+            };
+
+            window.GetDefaultNavigator().Push(new Page(button));
+        }
+
+        public void Deactivate()
+        {
+            var window = NUIApplication.GetDefaultWindow();
+            var newPageCount = window.GetDefaultNavigator().NavigationPages.Count;
+
+            for (int i = 0; i < (newPageCount - oldPageCount); i++)
+            {
+                window.GetDefaultNavigator().Pop();
+            }
+        }
+    }
+}