From 35c5d154221f63fb00a98902697e3f830e996285 Mon Sep 17 00:00:00 2001 From: "sung-su.kim" Date: Tue, 28 Apr 2020 17:22:26 +0900 Subject: [PATCH] Add ContentPopupManager --- .../CircularUI.cs | 1 - .../ContentPopupRenderer.cs | 64 ++++++++--------- .../ContentPopup.cs | 68 ++++--------------- .../ContentPopupManager.cs | 45 ++++++++++++ .../IContentPopupRenderer.cs | 10 ++- .../WearableUIGallery/TC/TCContentPopup.xaml | 4 +- .../TC/TCContentPopup.xaml.cs | 43 +++++++----- 7 files changed, 122 insertions(+), 113 deletions(-) create mode 100644 src/Tizen.Wearable.CircularUI.Forms/ContentPopupManager.cs diff --git a/src/Tizen.Wearable.CircularUI.Forms.Renderer/CircularUI.cs b/src/Tizen.Wearable.CircularUI.Forms.Renderer/CircularUI.cs index 961d9fda..83789a15 100644 --- a/src/Tizen.Wearable.CircularUI.Forms.Renderer/CircularUI.cs +++ b/src/Tizen.Wearable.CircularUI.Forms.Renderer/CircularUI.cs @@ -48,7 +48,6 @@ public static void Init() { if (IsInitialized) return; IsInitialized = true; - ContentPopup.RendererFunc = () => new ContentPopupRenderer(); } public static void Init(string apiKey) diff --git a/src/Tizen.Wearable.CircularUI.Forms.Renderer/ContentPopupRenderer.cs b/src/Tizen.Wearable.CircularUI.Forms.Renderer/ContentPopupRenderer.cs index c4959a36..a96c44f0 100644 --- a/src/Tizen.Wearable.CircularUI.Forms.Renderer/ContentPopupRenderer.cs +++ b/src/Tizen.Wearable.CircularUI.Forms.Renderer/ContentPopupRenderer.cs @@ -16,13 +16,12 @@ using System; using System.ComponentModel; -using Tizen.Wearable.CircularUI.Forms; -using Tizen.Wearable.CircularUI.Forms.Renderer; +using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Platform.Tizen; using XForms = Xamarin.Forms.Forms; -[assembly: ExportRenderer(typeof(ContentPopup), typeof(ContentPopupRenderer))] +[assembly: Dependency(typeof(Tizen.Wearable.CircularUI.Forms.Renderer.ContentPopupRenderer))] namespace Tizen.Wearable.CircularUI.Forms.Renderer { @@ -30,23 +29,12 @@ public class ContentPopupRenderer : IContentPopupRenderer { ElmSharp.Popup _popup; ContentPopup _element; - - public void SetElement(Element element) - { - if (element.Parent == null) - element.Parent = Application.Current; - element.PropertyChanged += OnElementPropertyChanged; - _element = element as ContentPopup; - - UpdateContent(); - UpdateIsShow(); - } + TaskCompletionSource _tcs; public ContentPopupRenderer() { _popup = new ElmSharp.Popup(XForms.NativeParent); _popup.Style = "circle"; - _popup.BackButtonPressed += OnBackButtonPressed; _popup.Dismissed += OnDismissed; } @@ -56,32 +44,37 @@ public ContentPopupRenderer() Dispose(false); } - public void Dispose() + public void SetElement(ContentPopup element) { - Dispose(true); - GC.SuppressFinalize(this); + if (element.Parent == null) + element.Parent = Application.Current; + element.PropertyChanged += OnElementPropertyChanged; + _element = element; + + UpdateContent(); } - public void Dismiss() + public void Dispose() { - _popup?.Hide(); + Dispose(true); + GC.SuppressFinalize(this); } - public void Show() + public Task Open() { - _popup?.Show(); + _popup.Show(); + _element.IsOpen = true; + _tcs = new TaskCompletionSource(); + return _tcs.Task; } protected void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == ContentPopup.ContentProperty.PropertyName) - { UpdateContent(); - } - if (e.PropertyName == ContentPopup.IsShowProperty.PropertyName) - { - UpdateIsShow(); - } + + if (e.PropertyName == ContentPopup.IsOpenProperty.PropertyName) + UpdateIsOpen(); } protected virtual void Dispose(bool disposing) @@ -105,13 +98,14 @@ protected virtual void Dispose(bool disposing) void OnBackButtonPressed(object sender, EventArgs e) { - _element.SendBackButtonPressed(); + if (!_element.SendBackButtonPressed()) + _popup?.Hide(); } void OnDismissed(object sender, EventArgs e) { _element.SendDismissed(); - Dispose(); + _tcs?.SetResult(true); } void UpdateContent() @@ -121,7 +115,7 @@ void UpdateContent() var renderer = Platform.GetOrCreateRenderer(_element.Content); (renderer as LayoutRenderer)?.RegisterOnLayoutUpdated(); var native = renderer.NativeView; - native.MinimumHeight = 360; + native.MinimumHeight = XForms.NativeParent.Geometry.Height; _popup.SetContent(native, false); } else @@ -130,12 +124,10 @@ void UpdateContent() } } - void UpdateIsShow() + void UpdateIsOpen() { - if (_element.IsShow) - Show(); - else - Dismiss(); + if (!_element.IsOpen) + _popup?.Hide(); } } } diff --git a/src/Tizen.Wearable.CircularUI.Forms/ContentPopup.cs b/src/Tizen.Wearable.CircularUI.Forms/ContentPopup.cs index 2ab88e0a..7aa47fce 100644 --- a/src/Tizen.Wearable.CircularUI.Forms/ContentPopup.cs +++ b/src/Tizen.Wearable.CircularUI.Forms/ContentPopup.cs @@ -24,16 +24,8 @@ namespace Tizen.Wearable.CircularUI.Forms /// The ContentPopup is a Popup, which allows you to customize the View to be displayed. /// /// 4 - public class ContentPopup : Element, IDisposable + public class ContentPopup : Element { - IContentPopupRenderer _renderer; - - /// - /// For internal use. - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public static Func RendererFunc { get; set; } = null; - /// /// BindableProperty. Identifies the Content bindable property. /// @@ -44,13 +36,7 @@ public class ContentPopup : Element, IDisposable /// BindableProperty. Identifies the IsShow bindable property. /// /// 4 - public static readonly BindableProperty IsShowProperty = BindableProperty.Create(nameof(IsShow), typeof(bool), typeof(ContentPopup), false, propertyChanged:(b, o, n) => ((ContentPopup)b).UpdateRenderer()); - - /// - /// Occurs when the device's back button is pressed. - /// - /// 4 - public event EventHandler BackButtonPressed; + public static readonly BindableProperty IsOpenProperty = BindableProperty.Create(nameof(IsOpen), typeof(bool), typeof(ContentPopup), false); /// /// Occurs when the popup is dismissed. @@ -69,22 +55,13 @@ public View Content } /// - /// Gets or sets the popup is shown. - /// - /// 4 - public bool IsShow - { - get { return (bool)GetValue(IsShowProperty); } - set { SetValue(IsShowProperty, value); } - } - - /// - /// Shows the popup. + /// Gets or sets the popup is opened. /// /// 4 - public void Show() + public bool IsOpen { - IsShow = true; + get { return (bool)GetValue(IsOpenProperty); } + set { SetValue(IsOpenProperty, value); } } /// @@ -93,7 +70,7 @@ public void Show() /// 4 public void Dismiss() { - IsShow = false; + IsOpen = false; } /// @@ -109,27 +86,18 @@ public void SendDismissed() /// For internal use. /// [EditorBrowsable(EditorBrowsableState.Never)] - public void SendBackButtonPressed() + public bool SendBackButtonPressed() { - BackButtonPressed?.Invoke(this, EventArgs.Empty); + return OnBackButtonPressed(); } /// - /// Dispose the popup. + /// To change the default behavior of the BackButton. Default behavior is dismiss. /// - /// 4 - public void Dispose() - { - Dispose(true); - } - - protected virtual void Dispose(bool disposing) + /// Default is false + protected virtual bool OnBackButtonPressed() { - if (disposing && _renderer != null) - { - _renderer.Dispose(); - _renderer = null; - } + return false; } protected override void OnBindingContextChanged() @@ -144,15 +112,5 @@ void UpdateContent() if (Content != null) OnChildAdded(Content); } - - void UpdateRenderer() - { - if (_renderer == null) - { - _renderer = RendererFunc(); - _renderer.SetElement(this); - } - } - } } diff --git a/src/Tizen.Wearable.CircularUI.Forms/ContentPopupManager.cs b/src/Tizen.Wearable.CircularUI.Forms/ContentPopupManager.cs new file mode 100644 index 00000000..ae81878e --- /dev/null +++ b/src/Tizen.Wearable.CircularUI.Forms/ContentPopupManager.cs @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Flora License, Version 1.1 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * 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.Threading.Tasks; +using Xamarin.Forms; + +namespace Tizen.Wearable.CircularUI.Forms +{ + public static class ContentPopupManager + { + public static async Task ShowPopup(this INavigation navigation, ContentPopup popup) + { + await ShowPopup(popup); + } + + public static async Task ShowPopup(ContentPopup popup) + { + if (popup == null) + await Task.FromResult(false); + + using (var renderer = DependencyService.Get(DependencyFetchTarget.NewInstance)) + { + if (renderer == null) + await Task.FromResult(false); + + renderer.SetElement(popup); + + await renderer.Open(); + } + } + } +} diff --git a/src/Tizen.Wearable.CircularUI.Forms/IContentPopupRenderer.cs b/src/Tizen.Wearable.CircularUI.Forms/IContentPopupRenderer.cs index dab7be9e..5cb62d36 100644 --- a/src/Tizen.Wearable.CircularUI.Forms/IContentPopupRenderer.cs +++ b/src/Tizen.Wearable.CircularUI.Forms/IContentPopupRenderer.cs @@ -15,7 +15,7 @@ */ using System; -using Xamarin.Forms; +using System.Threading.Tasks; namespace Tizen.Wearable.CircularUI.Forms { @@ -29,6 +29,12 @@ public interface IContentPopupRenderer : IDisposable /// Sets the Element associated with this renderer. /// /// New element. - void SetElement(Element element); + void SetElement(ContentPopup element); + + /// + /// Open a popup. + /// + /// Returns a Task with the dismiss result of the popup. + Task Open(); } } diff --git a/test/WearableUIGallery/WearableUIGallery/TC/TCContentPopup.xaml b/test/WearableUIGallery/WearableUIGallery/TC/TCContentPopup.xaml index f9fcdb94..953f1bbe 100644 --- a/test/WearableUIGallery/WearableUIGallery/TC/TCContentPopup.xaml +++ b/test/WearableUIGallery/WearableUIGallery/TC/TCContentPopup.xaml @@ -20,7 +20,7 @@