Skip to content

Commit

Permalink
Add closing process when tapping the outer area of ​​Popup (#2081)
Browse files Browse the repository at this point in the history
* Add closing process when tapping the outer area of ​​Popup

* Modification of conditions for inside/outside judgment

* Change return value to default false

* Add using

---------

Co-authored-by: Shaun Lawrence <[email protected]>
  • Loading branch information
cat0363 and bijington authored Aug 28, 2024
1 parent 2e53efe commit 765d3df
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/CommunityToolkit.Maui.Core/Views/Popup/MauiPopup.android.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using Android.Content;
using Android.Views;
using Microsoft.Maui.Platform;
using AView = Android.Views.View;

Expand Down Expand Up @@ -99,5 +100,38 @@ void SubscribeEvents()
SetOnCancelListener(this);
}

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);
}
}
}
}
}

if (this.IsDisposed())
{
return false;
}
else
{
return base.OnTouchEvent(e);
}
}

void IDialogInterfaceOnCancelListener.OnCancel(IDialogInterface? dialog) => OnDismissedByTappingOutsideOfPopup(this);
}

0 comments on commit 765d3df

Please sign in to comment.