Skip to content

Commit b869ec9

Browse files
committed
Optional popover animations on iOS
1 parent 20ef99d commit b869ec9

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

src/Playground/Playground.Original/Views/Pages/PopoverPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
HorizontalOptions="Start"
1818
PopoverAlignment="{Binding Source={x:Reference popoverAlignmentPicker}, Path=SelectedItem}"
1919
UseDefaultStyling="{Binding Source={x:Reference useDefaultStylingSwitch}, Path=IsToggled}"
20-
IsAnimated="{Binding Source={x:Reference useDefaultStylingSwitch}, Path=IsToggled}" />
20+
IsAnimated="{Binding Source={x:Reference isAnimatedSwitch}, Path=IsToggled}" />
2121
<controls:PopoverTest
2222
HorizontalOptions="Center"
2323
PopoverAlignment="{Binding Source={x:Reference popoverAlignmentPicker}, Path=SelectedItem}"

src/SimpleToolkit.Core/Handlers/Popover/PopoverHandler.MaciOS.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,31 @@ public static void MapContent(PopoverHandler handler, IPopover popover)
3131

3232
public static void MapIsAnimated(PopoverHandler handler, IPopover popover)
3333
{
34+
handler.PlatformView.IsAnimated = popover.IsAnimated;
3435
}
3536

3637
public static void MapPermittedArrowDirections(PopoverHandler handler, IPopover popover)
3738
{
3839
handler.PlatformView.PermittedArrowDirections = popover.PermittedArrowDirections.ToUIPopoverArrowDirection();
3940
}
4041

41-
public static void MapShow(PopoverHandler handler, IPopover popover, object parentView)
42+
public static async void MapShow(PopoverHandler handler, IPopover popover, object parentView)
4243
{
4344
if (parentView is not IElement anchor)
4445
return;
4546

46-
handler.PlatformView?.Show(popover, anchor);
47+
try { await handler.PlatformView?.Show(popover, anchor); }
48+
catch { throw; }
4749
}
4850

4951
public static async void MapHide(PopoverHandler handler, IPopover popover, object arg3)
5052
{
5153
var vc = handler.PlatformView.ViewController;
5254
if (vc is not null)
53-
await vc.DismissViewControllerAsync(true);
55+
{
56+
try { await vc.DismissViewControllerAsync(true); }
57+
catch { throw; }
58+
}
5459
//handler.PlatformView.CleanUp();
5560
}
5661
}

src/SimpleToolkit.Core/Platform/MaciOS/PopoverViewController.cs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class PopoverViewController(IMauiContext mauiContext) : UIViewController
1414
{
1515
private readonly IMauiContext mauiContext = mauiContext ?? throw new ArgumentNullException(nameof(mauiContext));
1616
private Grid contentWrapper = null;
17+
private bool isAnimated = true;
1718
private WeakReference<IPopover> virtualViewReference;
1819

1920
internal UIViewController ViewController { get; private set; }
@@ -31,6 +32,17 @@ public virtual UIPopoverArrowDirection PermittedArrowDirections
3132
set => ((UIPopoverPresentationController)PresentationController).PermittedArrowDirections = value;
3233
}
3334

35+
public virtual bool IsAnimated
36+
{
37+
get => isAnimated;
38+
set
39+
{
40+
isAnimated = value;
41+
if (PresentationController.Delegate is PopoverDelegate popoverDelegate)
42+
popoverDelegate.IsAnimated = value;
43+
}
44+
}
45+
3446

3547
public override void ViewWillAppear(bool animated)
3648
{
@@ -45,7 +57,8 @@ public override void ViewWillAppear(bool animated)
4557
View.Superview.Layer.CornerRadius = 0f;
4658
View.Superview.Layer.MasksToBounds = false;
4759

48-
AnimateIn();
60+
if (IsAnimated)
61+
AnimateIn();
4962
}
5063
}
5164

@@ -84,7 +97,7 @@ public void SetElement(IPopover element)
8497
}
8598

8699
[MemberNotNull(nameof(ViewController))]
87-
public void Show(in IPopover virtualView, in IElement anchor)
100+
public async Task Show(IPopover virtualView, IElement anchor)
88101
{
89102
if (IsBeingPresented || IsBeingDismissed)
90103
return;
@@ -95,7 +108,12 @@ public void Show(in IPopover virtualView, in IElement anchor)
95108
_ = ViewController ?? throw new InvalidOperationException($"{nameof(ViewController)} cannot be null");
96109

97110
SetAnchor(virtualView, anchor);
98-
PresentInViewController(ViewController);
111+
await PresentInViewController(ViewController);
112+
}
113+
114+
public async Task Hide()
115+
{
116+
await ViewController.DismissViewControllerAsync(IsAnimated);
99117
}
100118

101119
public void CleanUp()
@@ -187,8 +205,11 @@ private void SetAnchor(IPopover popover, IElement anchor)
187205
private void SetUpPresentationController(IPopover virtualView)
188206
{
189207
var presentationController = (UIPopoverPresentationController)PresentationController;
190-
191-
presentationController.Delegate = new PopoverDelegate();
208+
209+
presentationController.Delegate = new PopoverDelegate
210+
{
211+
IsAnimated = IsAnimated
212+
};
192213
presentationController.PermittedArrowDirections = virtualView.PermittedArrowDirections.ToUIPopoverArrowDirection();
193214

194215
if (!virtualView.UseDefaultStyling)
@@ -198,9 +219,9 @@ private void SetUpPresentationController(IPopover virtualView)
198219
}
199220
}
200221

201-
private void PresentInViewController(UIViewController viewController)
222+
private async Task PresentInViewController(UIViewController viewController)
202223
{
203-
viewController.PresentViewController(this, true, null);
224+
await viewController.PresentViewControllerAsync(this, IsAnimated);
204225
}
205226

206227
private CGPoint GetContentOffset(bool useDefaultStyling)
@@ -233,6 +254,14 @@ private static void RemoveShadow(UIView containerView)
233254

234255
private class PopoverDelegate : UIPopoverPresentationControllerDelegate
235256
{
257+
public bool IsAnimated { get; set; } = true;
258+
259+
public override bool ShouldDismiss(UIPresentationController presentationController)
260+
{
261+
presentationController.PresentingViewController.DismissViewController(IsAnimated, null);
262+
return true;
263+
}
264+
236265
public override UIModalPresentationStyle GetAdaptivePresentationStyle(UIPresentationController forPresentationController) =>
237266
UIModalPresentationStyle.None;
238267

0 commit comments

Comments
 (0)