-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathMauiPopup.tizen.cs
138 lines (120 loc) · 4.07 KB
/
MauiPopup.tizen.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
132
133
134
135
136
137
138
using Microsoft.Maui.Platform;
using Microsoft.Maui.Primitives;
using Tizen.NUI;
using Tizen.UIExtensions.NUI;
using NHorizontalAlignment = Tizen.NUI.HorizontalAlignment;
using NVerticalAlignment = Tizen.NUI.VerticalAlignment;
namespace CommunityToolkit.Maui.Core.Views;
/// <summary>
/// The native implementation of Popup control.
/// </summary>
public class MauiPopup : Popup
{
readonly IMauiContext mauiContext;
/// <summary>
/// Constructor of <see cref="MauiPopup"/>.
/// </summary>
/// <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(IMauiContext mauiContext)
{
this.mauiContext = mauiContext ?? throw new ArgumentNullException(nameof(mauiContext));
OutsideClicked += OnOutsideClicked;
}
/// <summary>
/// An instance of the <see cref="IPopup"/>.
/// </summary>
public IPopup? VirtualView { get; private set; }
/// <inheritdoc/>
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
OutsideClicked -= OnOutsideClicked;
}
base.Dispose(isDisposing);
}
/// <summary>
/// Method to initialize the native implementation.
/// </summary>
/// <param name="element">An instance of <see cref="IPopup"/>.</param>
public void SetElement(IPopup? element)
{
VirtualView = element;
}
/// <summary>
/// Method to show the Popup
/// </summary>
public void ShowPopup()
{
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} cannot be null");
Content = VirtualView.Content?.ToPlatform(mauiContext) ?? throw new InvalidOperationException($"{nameof(VirtualView.Content)} cannot be null");
BackgroundColor = new Tizen.NUI.Color(0.1f, 0.1f, 0.1f, 0.5f);
Content.BackgroundColor = (VirtualView.Color ?? Colors.Transparent).ToNUIColor();
if (VirtualView.Anchor is not null)
{
var anchorView = VirtualView.Anchor.ToPlatform();
var anchorPosition = anchorView.ScreenPosition;
Layout = new AbsoluteLayout();
Content.UpdatePosition(new Tizen.UIExtensions.Common.Point(anchorPosition.X, anchorPosition.Y));
}
else
{
Layout = new LinearLayout
{
LinearOrientation = LinearLayout.Orientation.Vertical,
VerticalAlignment = ToVerticalAlignment(VirtualView.VerticalOptions),
HorizontalAlignment = ToHorizontalAlignment(VirtualView.Content.FlowDirection, VirtualView.HorizontalOptions),
};
Content.UpdatePosition(new Tizen.UIExtensions.Common.Point(0, 0));
}
UpdateContentSize();
Open();
VirtualView.OnOpened();
}
/// <summary>
/// Method to update size of Content
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
public void UpdateContentSize()
{
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} cannot be null");
if (Content is null)
{
return;
}
if (VirtualView.Size.Width > 0 && VirtualView.Size.Height > 0)
{
Content.UpdateSize(VirtualView.Size.ToPixel());
}
else
{
var measured = VirtualView.Content?.Measure(double.PositiveInfinity, double.PositiveInfinity).ToPixel() ?? new Tizen.UIExtensions.Common.Size(0, 0);
Content.UpdateSize(measured);
}
}
static NVerticalAlignment ToVerticalAlignment(LayoutAlignment align) => align switch
{
LayoutAlignment.Start => NVerticalAlignment.Top,
LayoutAlignment.End => NVerticalAlignment.Bottom,
_ => NVerticalAlignment.Center
};
static NHorizontalAlignment ToHorizontalAlignment(FlowDirection direction, LayoutAlignment align) => align switch
{
LayoutAlignment.Start => direction == FlowDirection.RightToLeft ? NHorizontalAlignment.End : NHorizontalAlignment.Begin,
LayoutAlignment.End => direction == FlowDirection.RightToLeft ? NHorizontalAlignment.Begin : NHorizontalAlignment.End,
_ => NHorizontalAlignment.Center
};
void OnOutsideClicked(object? sender, EventArgs e)
{
if (VirtualView?.Handler is null)
{
return;
}
if (VirtualView.CanBeDismissedByTappingOutsideOfPopup)
{
Close();
VirtualView.Handler?.Invoke(nameof(IPopup.OnDismissedByTappingOutsideOfPopup));
}
}
}