From a8a4e48be6d065979fbda78b8c231dd3b88b6187 Mon Sep 17 00:00:00 2001 From: Jaehyun Cho Date: Mon, 16 Nov 2020 17:34:51 +0900 Subject: [PATCH] [NUI] Add Dialog class 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 | 240 +++++++++++++++++++++ .../Controls/Navigation/Navigator.cs | 45 ++++ .../Tizen.NUI.Samples/Samples/DialogSample.cs | 50 +++++ 3 files changed, 335 insertions(+) create mode 100755 src/Tizen.NUI.Components/Controls/Dialog.cs create mode 100755 test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/DialogSample.cs diff --git a/src/Tizen.NUI.Components/Controls/Dialog.cs b/src/Tizen.NUI.Components/Controls/Dialog.cs new file mode 100755 index 0000000..bc2df64 --- /dev/null +++ b/src/Tizen.NUI.Components/Controls/Dialog.cs @@ -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 +{ + /// + /// Dialog class shows a popup content with background scrim. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public class Dialog : Control + { + private View popupContent = null; + private View scrim = null; + private bool enableScrim = true; + + /// + /// Creates a new instance of Dialog. + /// + /// The content to set to Content of Dialog. + [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; + } + } + + /// + /// Popup content of Dialog. Content is added to Children automatically. + /// + [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); + } + + /// + /// Indicates to show scrim behind popup content. + /// + [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(); + } + } + } + } + + /// + /// Indicates to dismiss popup content by touching on scrim. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public bool EnableDismissOnScrim { get; set; } + + /// + /// Scrim covers background behind popup content. + /// + [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); + } + } + } + + /// + /// Dispose Dialog and all children on it. + /// + /// Dispose type. + [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); + } + } +} diff --git a/src/Tizen.NUI.Components/Controls/Navigation/Navigator.cs b/src/Tizen.NUI.Components/Controls/Navigation/Navigator.cs index a75d344..4a2b34a 100755 --- a/src/Tizen.NUI.Components/Controls/Navigation/Navigator.cs +++ b/src/Tizen.NUI.Components/Controls/Navigation/Navigator.cs @@ -335,5 +335,50 @@ namespace Tizen.NUI.Components return defaultNavigator; } + + /// + /// Shows a dialog by pushing a page containing dialog to default navigator. + /// + /// The content of Dialog. + [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 index 0000000..812d556 --- /dev/null +++ b/test/Tizen.NUI.Samples/Tizen.NUI.Samples/Samples/DialogSample.cs @@ -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(); + } + } + } +} -- 2.7.4