Skip to content
This repository was archived by the owner on Dec 28, 2023. It is now read-only.

Commit 35c5d15

Browse files
committed
Add ContentPopupManager
1 parent 008c361 commit 35c5d15

7 files changed

Lines changed: 122 additions & 113 deletions

File tree

src/Tizen.Wearable.CircularUI.Forms.Renderer/CircularUI.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public static void Init()
4848
{
4949
if (IsInitialized) return;
5050
IsInitialized = true;
51-
ContentPopup.RendererFunc = () => new ContentPopupRenderer();
5251
}
5352

5453
public static void Init(string apiKey)

src/Tizen.Wearable.CircularUI.Forms.Renderer/ContentPopupRenderer.cs

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,25 @@
1616

1717
using System;
1818
using System.ComponentModel;
19-
using Tizen.Wearable.CircularUI.Forms;
20-
using Tizen.Wearable.CircularUI.Forms.Renderer;
19+
using System.Threading.Tasks;
2120
using Xamarin.Forms;
2221
using Xamarin.Forms.Platform.Tizen;
2322
using XForms = Xamarin.Forms.Forms;
2423

25-
[assembly: ExportRenderer(typeof(ContentPopup), typeof(ContentPopupRenderer))]
24+
[assembly: Dependency(typeof(Tizen.Wearable.CircularUI.Forms.Renderer.ContentPopupRenderer))]
2625

2726
namespace Tizen.Wearable.CircularUI.Forms.Renderer
2827
{
2928
public class ContentPopupRenderer : IContentPopupRenderer
3029
{
3130
ElmSharp.Popup _popup;
3231
ContentPopup _element;
33-
34-
public void SetElement(Element element)
35-
{
36-
if (element.Parent == null)
37-
element.Parent = Application.Current;
38-
element.PropertyChanged += OnElementPropertyChanged;
39-
_element = element as ContentPopup;
40-
41-
UpdateContent();
42-
UpdateIsShow();
43-
}
32+
TaskCompletionSource<bool> _tcs;
4433

4534
public ContentPopupRenderer()
4635
{
4736
_popup = new ElmSharp.Popup(XForms.NativeParent);
4837
_popup.Style = "circle";
49-
5038
_popup.BackButtonPressed += OnBackButtonPressed;
5139
_popup.Dismissed += OnDismissed;
5240
}
@@ -56,32 +44,37 @@ public ContentPopupRenderer()
5644
Dispose(false);
5745
}
5846

59-
public void Dispose()
47+
public void SetElement(ContentPopup element)
6048
{
61-
Dispose(true);
62-
GC.SuppressFinalize(this);
49+
if (element.Parent == null)
50+
element.Parent = Application.Current;
51+
element.PropertyChanged += OnElementPropertyChanged;
52+
_element = element;
53+
54+
UpdateContent();
6355
}
6456

65-
public void Dismiss()
57+
public void Dispose()
6658
{
67-
_popup?.Hide();
59+
Dispose(true);
60+
GC.SuppressFinalize(this);
6861
}
6962

70-
public void Show()
63+
public Task Open()
7164
{
72-
_popup?.Show();
65+
_popup.Show();
66+
_element.IsOpen = true;
67+
_tcs = new TaskCompletionSource<bool>();
68+
return _tcs.Task;
7369
}
7470

7571
protected void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
7672
{
7773
if (e.PropertyName == ContentPopup.ContentProperty.PropertyName)
78-
{
7974
UpdateContent();
80-
}
81-
if (e.PropertyName == ContentPopup.IsShowProperty.PropertyName)
82-
{
83-
UpdateIsShow();
84-
}
75+
76+
if (e.PropertyName == ContentPopup.IsOpenProperty.PropertyName)
77+
UpdateIsOpen();
8578
}
8679

8780
protected virtual void Dispose(bool disposing)
@@ -105,13 +98,14 @@ protected virtual void Dispose(bool disposing)
10598

10699
void OnBackButtonPressed(object sender, EventArgs e)
107100
{
108-
_element.SendBackButtonPressed();
101+
if (!_element.SendBackButtonPressed())
102+
_popup?.Hide();
109103
}
110104

111105
void OnDismissed(object sender, EventArgs e)
112106
{
113107
_element.SendDismissed();
114-
Dispose();
108+
_tcs?.SetResult(true);
115109
}
116110

117111
void UpdateContent()
@@ -121,7 +115,7 @@ void UpdateContent()
121115
var renderer = Platform.GetOrCreateRenderer(_element.Content);
122116
(renderer as LayoutRenderer)?.RegisterOnLayoutUpdated();
123117
var native = renderer.NativeView;
124-
native.MinimumHeight = 360;
118+
native.MinimumHeight = XForms.NativeParent.Geometry.Height;
125119
_popup.SetContent(native, false);
126120
}
127121
else
@@ -130,12 +124,10 @@ void UpdateContent()
130124
}
131125
}
132126

133-
void UpdateIsShow()
127+
void UpdateIsOpen()
134128
{
135-
if (_element.IsShow)
136-
Show();
137-
else
138-
Dismiss();
129+
if (!_element.IsOpen)
130+
_popup?.Hide();
139131
}
140132
}
141133
}

src/Tizen.Wearable.CircularUI.Forms/ContentPopup.cs

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,8 @@ namespace Tizen.Wearable.CircularUI.Forms
2424
/// The ContentPopup is a Popup, which allows you to customize the View to be displayed.
2525
/// </summary>
2626
/// <since_tizen> 4 </since_tizen>
27-
public class ContentPopup : Element, IDisposable
27+
public class ContentPopup : Element
2828
{
29-
IContentPopupRenderer _renderer;
30-
31-
/// <summary>
32-
/// For internal use.
33-
/// </summary>
34-
[EditorBrowsable(EditorBrowsableState.Never)]
35-
public static Func<IContentPopupRenderer> RendererFunc { get; set; } = null;
36-
3729
/// <summary>
3830
/// BindableProperty. Identifies the Content bindable property.
3931
/// </summary>
@@ -44,13 +36,7 @@ public class ContentPopup : Element, IDisposable
4436
/// BindableProperty. Identifies the IsShow bindable property.
4537
/// </summary>
4638
/// <since_tizen> 4 </since_tizen>
47-
public static readonly BindableProperty IsShowProperty = BindableProperty.Create(nameof(IsShow), typeof(bool), typeof(ContentPopup), false, propertyChanged:(b, o, n) => ((ContentPopup)b).UpdateRenderer());
48-
49-
/// <summary>
50-
/// Occurs when the device's back button is pressed.
51-
/// </summary>
52-
/// <since_tizen> 4 </since_tizen>
53-
public event EventHandler BackButtonPressed;
39+
public static readonly BindableProperty IsOpenProperty = BindableProperty.Create(nameof(IsOpen), typeof(bool), typeof(ContentPopup), false);
5440

5541
/// <summary>
5642
/// Occurs when the popup is dismissed.
@@ -69,22 +55,13 @@ public View Content
6955
}
7056

7157
/// <summary>
72-
/// Gets or sets the popup is shown.
73-
/// </summary>
74-
/// <since_tizen> 4 </since_tizen>
75-
public bool IsShow
76-
{
77-
get { return (bool)GetValue(IsShowProperty); }
78-
set { SetValue(IsShowProperty, value); }
79-
}
80-
81-
/// <summary>
82-
/// Shows the popup.
58+
/// Gets or sets the popup is opened.
8359
/// </summary>
8460
/// <since_tizen> 4 </since_tizen>
85-
public void Show()
61+
public bool IsOpen
8662
{
87-
IsShow = true;
63+
get { return (bool)GetValue(IsOpenProperty); }
64+
set { SetValue(IsOpenProperty, value); }
8865
}
8966

9067
/// <summary>
@@ -93,7 +70,7 @@ public void Show()
9370
/// <since_tizen> 4 </since_tizen>
9471
public void Dismiss()
9572
{
96-
IsShow = false;
73+
IsOpen = false;
9774
}
9875

9976
/// <summary>
@@ -109,27 +86,18 @@ public void SendDismissed()
10986
/// For internal use.
11087
/// </summary>
11188
[EditorBrowsable(EditorBrowsableState.Never)]
112-
public void SendBackButtonPressed()
89+
public bool SendBackButtonPressed()
11390
{
114-
BackButtonPressed?.Invoke(this, EventArgs.Empty);
91+
return OnBackButtonPressed();
11592
}
11693

11794
/// <summary>
118-
/// Dispose the popup.
95+
/// To change the default behavior of the BackButton. Default behavior is dismiss.
11996
/// </summary>
120-
/// <since_tizen> 4 </since_tizen>
121-
public void Dispose()
122-
{
123-
Dispose(true);
124-
}
125-
126-
protected virtual void Dispose(bool disposing)
97+
/// <returns>Default is false</returns>
98+
protected virtual bool OnBackButtonPressed()
12799
{
128-
if (disposing && _renderer != null)
129-
{
130-
_renderer.Dispose();
131-
_renderer = null;
132-
}
100+
return false;
133101
}
134102

135103
protected override void OnBindingContextChanged()
@@ -144,15 +112,5 @@ void UpdateContent()
144112
if (Content != null)
145113
OnChildAdded(Content);
146114
}
147-
148-
void UpdateRenderer()
149-
{
150-
if (_renderer == null)
151-
{
152-
_renderer = RendererFunc();
153-
_renderer.SetElement(this);
154-
}
155-
}
156-
157115
}
158116
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3+
*
4+
* Licensed under the Flora License, Version 1.1 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://floralicense.org/license/
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using System.Threading.Tasks;
18+
using Xamarin.Forms;
19+
20+
namespace Tizen.Wearable.CircularUI.Forms
21+
{
22+
public static class ContentPopupManager
23+
{
24+
public static async Task ShowPopup(this INavigation navigation, ContentPopup popup)
25+
{
26+
await ShowPopup(popup);
27+
}
28+
29+
public static async Task ShowPopup(ContentPopup popup)
30+
{
31+
if (popup == null)
32+
await Task.FromResult(false);
33+
34+
using (var renderer = DependencyService.Get<IContentPopupRenderer>(DependencyFetchTarget.NewInstance))
35+
{
36+
if (renderer == null)
37+
await Task.FromResult(false);
38+
39+
renderer.SetElement(popup);
40+
41+
await renderer.Open();
42+
}
43+
}
44+
}
45+
}

src/Tizen.Wearable.CircularUI.Forms/IContentPopupRenderer.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
using System;
18-
using Xamarin.Forms;
18+
using System.Threading.Tasks;
1919

2020
namespace Tizen.Wearable.CircularUI.Forms
2121
{
@@ -29,6 +29,12 @@ public interface IContentPopupRenderer : IDisposable
2929
/// Sets the Element associated with this renderer.
3030
/// </summary>
3131
/// <param name="element">New element.</param>
32-
void SetElement(Element element);
32+
void SetElement(ContentPopup element);
33+
34+
/// <summary>
35+
/// Open a popup.
36+
/// </summary>
37+
/// <returns>Returns a Task with the dismiss result of the popup.</returns>
38+
Task Open();
3339
}
3440
}

test/WearableUIGallery/WearableUIGallery/TC/TCContentPopup.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<Button
2121
AutomationId="dismisstest1"
2222
x:Name="button1"
23-
Clicked="OnContentPopupDismissBackKeyClicked"
23+
Clicked="OnContentPopupTest1Clicked"
2424
FontSize="Small"
2525
HeightRequest="50"
2626
HorizontalOptions="Center"
@@ -30,7 +30,7 @@
3030
<Button
3131
AutomationId="dismisstest2"
3232
x:Name="button2"
33-
Clicked="OnContentPopupDismissButtonClicked"
33+
Clicked="OnContentPopupTest2Clicked"
3434
FontSize="Small"
3535
HeightRequest="50"
3636
HorizontalOptions="Center"

0 commit comments

Comments
 (0)