-
Notifications
You must be signed in to change notification settings - Fork 416
/
Copy pathMauiPopup.android.cs
131 lines (108 loc) · 3.33 KB
/
MauiPopup.android.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System.Diagnostics.CodeAnalysis;
using Android.Content;
using Android.Views;
using Microsoft.Maui.Platform;
using AView = Android.Views.View;
namespace CommunityToolkit.Maui.Core.Views;
/// <summary>
/// The native implementation of Popup control.
/// </summary>
public class MauiPopup : Dialog, IDialogInterfaceOnCancelListener
{
readonly IMauiContext mauiContext;
/// <summary>
/// Constructor of <see cref="MauiPopup"/>.
/// </summary>
/// <param name="context">An instance of <see cref="Context"/>.</param>
/// <param name="mauiContext">An instance of <see cref="IMauiContext"/>.</param>
/// <exception cref="ArgumentNullException">If <paramref name="mauiContext"/> is null an exception will be thrown. </exception>
public MauiPopup(Context context, IMauiContext mauiContext)
: base(context)
{
this.mauiContext = mauiContext ?? throw new ArgumentNullException(nameof(mauiContext));
}
/// <summary>
/// An instance of the <see cref="IPopup"/>.
/// </summary>
public IPopup? VirtualView { get; private set; }
/// <summary>
/// Method to initialize the native implementation.
/// </summary>
/// <param name="element">An instance of <see cref="IPopup"/>.</param>
public AView? SetElement(IPopup? element)
{
ArgumentNullException.ThrowIfNull(element);
VirtualView = element;
if (TryCreateContainer(VirtualView, out var container))
{
SubscribeEvents();
}
return container;
}
/// <summary>
/// Method to show the Popup.
/// </summary>
public override void Show()
{
base.Show();
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} cannot be null");
VirtualView.OnOpened();
}
/// <summary>
/// Method triggered when the Popup is dismissed by tapping outside of the Popup.
/// </summary>
/// <param name="dialog">An instance of the <see cref="IDialogInterface"/>.</param>
public void OnDismissedByTappingOutsideOfPopup(IDialogInterface dialog)
{
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} cannot be null");
_ = VirtualView.Handler ?? throw new InvalidOperationException($"{nameof(VirtualView.Handler)} cannot be null");
VirtualView.Handler?.Invoke(nameof(IPopup.OnDismissedByTappingOutsideOfPopup));
}
/// <summary>
/// Method to clean up the resources of the <see cref="MauiPopup"/>.
/// </summary>
public void CleanUp()
{
VirtualView = null;
}
/// <inheritdoc/>
public override bool OnTouchEvent(MotionEvent e)
{
if (VirtualView is not null)
{
if (VirtualView.CanBeDismissedByTappingOutsideOfPopup &&
e.Action == MotionEventActions.Up)
{
if (Window?.DecorView is AView decorView)
{
float x = e.GetX();
float y = e.GetY();
if (!(x >= 0 && x <= decorView.Width && y >= 0 && y <= decorView.Height))
{
if (IsShowing)
{
OnDismissedByTappingOutsideOfPopup(this);
}
}
}
}
}
return !this.IsDisposed() && base.OnTouchEvent(e);
}
bool TryCreateContainer(in IPopup popup, [NotNullWhen(true)] out AView? container)
{
container = null;
if (popup.Content is null)
{
return false;
}
container = popup.Content.ToPlatform(mauiContext);
SetContentView(container);
return true;
}
void SubscribeEvents()
{
SetOnCancelListener(this);
}
void IDialogInterfaceOnCancelListener.OnCancel(IDialogInterface? dialog) => OnDismissedByTappingOutsideOfPopup(this);
}