Skip to content

Commit

Permalink
Merge branch 'LuckyDucko:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
eichbaum authored Feb 21, 2024
2 parents e09708f + 6031022 commit c497b65
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 67 deletions.
29 changes: 24 additions & 5 deletions Mopups/Mopups.Maui/Mopups.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>

<TargetFrameworks>net7.0;net7.0-ios;net7.0-maccatalyst;;net7.0-android33.0</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
<TargetFrameworks>net8.0;net8.0-maccatalyst;net8.0-ios;net8.0-android34.0</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>

<!-- iOS, Android, MacCatalyst -->
<UseMaui>true</UseMaui>
Expand All @@ -27,12 +27,14 @@
<Authors>Tyson Hooker,Maksym Koshovyi,Aswin P G, Kirill Lyubimov, Martijn Van Dijk, Shane Neuville</Authors>
<Company>$(CompanyName)</Company>
<PackageTags>Mopups;Popups;Maui;rg;Xamarin;ios;android;Windows;MacOS;Mopups;Dialogs;macos;windows</PackageTags>
<Version>1.2.0</Version>
<Version>1.3.0</Version>
<Title>Mopups (Maui Popups)</Title>
<PackageReleaseNotes>Finally did a release that should support Windows
Added in previous work for awaitable popups directly into this repo under prebaked.</PackageReleaseNotes>
<PackageReleaseNotes>Added in several fixes from the community (THANKYOU!)

Upgraded to .NET 8</PackageReleaseNotes>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PublishAot>True</PublishAot>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-android|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -80,6 +82,23 @@ Added in previous work for awaitable popups directly into this repo under prebak
<PlatformTarget>AnyCPU</PlatformTarget>
<Deterministic>False</Deterministic>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-ios|AnyCPU'">
<EnableSGenConc>True</EnableSGenConc>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-ios|AnyCPU'">
<EnableSGenConc>True</EnableSGenConc>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net8.0-android34.0|AnyCPU'">
<AndroidLinkTool>r8</AndroidLinkTool>
<RunAOTCompilation>True</RunAOTCompilation>
<EnableLLVM>True</EnableLLVM>
<AndroidEnableProfiledAot>True</AndroidEnableProfiledAot>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-android34.0|AnyCPU'">
<RunAOTCompilation>True</RunAOTCompilation>
<EnableLLVM>True</EnableLLVM>
<AndroidEnableProfiledAot>True</AndroidEnableProfiledAot>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AsyncAwaitBestPractices" Version="6.0.4" />
</ItemGroup>
Expand Down
20 changes: 10 additions & 10 deletions Mopups/Mopups.Maui/Pages/PopupPage.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System.Windows.Input;

using AsyncAwaitBestPractices;

using AsyncAwaitBestPractices;
using Mopups.Animations;
using Mopups.Animations.Base;
using Mopups.Enums;
using Mopups.Services;
using System.Windows.Input;

namespace Mopups.Pages;

Expand Down Expand Up @@ -108,17 +106,17 @@ public object BackgroundClickedCommandParameter
set => SetValue(BackgroundClickedCommandParameterProperty, value);
}

public static readonly BindableProperty AndroidTalkbackAccessibilityWorkaroundProperty = BindableProperty.Create(nameof(AndroidTalkbackAccessibilityWorkaround), typeof(bool), typeof(PopupPage), false);
public static readonly BindableProperty DisableAndroidAccessibilityHandlingProperty = BindableProperty.Create(nameof(DisableAndroidAccessibilityHandling), typeof(bool), typeof(PopupPage), false);

public bool AndroidTalkbackAccessibilityWorkaround
public bool DisableAndroidAccessibilityHandling
{
get => (bool)GetValue(AndroidTalkbackAccessibilityWorkaroundProperty);
set => SetValue(AndroidTalkbackAccessibilityWorkaroundProperty, value);
get => (bool)GetValue(DisableAndroidAccessibilityHandlingProperty);
set => SetValue(DisableAndroidAccessibilityHandlingProperty, value);
}

public PopupPage()
{
//BackgroundColor = Color.FromArgb("#80000000");
BackgroundColor = Colors.Transparent;
}

protected override bool OnBackButtonPressed()
Expand Down Expand Up @@ -286,7 +284,7 @@ protected virtual bool OnBackgroundClicked()
return CloseWhenBackgroundIsClicked;
}

internal void SendBackgroundClick()
internal bool SendBackgroundClick()
{
BackgroundClicked?.Invoke(this, EventArgs.Empty);
if (BackgroundClickedCommand?.CanExecute(BackgroundClickedCommandParameter) == true)
Expand All @@ -296,6 +294,8 @@ internal void SendBackgroundClick()
if (OnBackgroundClicked())
{
MopupService.Instance.RemovePageAsync(this).SafeFireAndForget();
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ public class PopupPageHandler : PageHandler

public PopupPageHandler()
{
this.SetMauiContext(MauiApplication.Current.Application.Windows[0].Handler.MauiContext);
SetMauiContext(MauiApplication.Current.Application.Windows[0].Handler.MauiContext);
}

public PopupPageHandler(IMauiContext context)
{
SetMauiContext(context);
}

protected override void ConnectHandler(ContentViewGroup platformView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ protected override void OnLayout(bool changed, int left, int top, int right, int
systemPadding = new Thickness();
}

(PopupHandler.VirtualView as PopupPage).SetValue(PopupPage.SystemPaddingProperty, systemPadding);
(PopupHandler.VirtualView as PopupPage).SetValue(PopupPage.KeyboardOffsetProperty, keyboardOffset);
(PopupHandler.VirtualView as PopupPage)?.SetValue(PopupPage.SystemPaddingProperty, systemPadding);
(PopupHandler.VirtualView as PopupPage)?.SetValue(PopupPage.KeyboardOffsetProperty, keyboardOffset);

if (changed)
(PopupHandler.VirtualView as PopupPage).Layout(new Rect(Context.FromPixels(left), Context.FromPixels(top), Context.FromPixels(right), Context.FromPixels(bottom)));
(PopupHandler.VirtualView as PopupPage)?.Layout(new Rect(Context.FromPixels(left), Context.FromPixels(top), Context.FromPixels(right), Context.FromPixels(bottom)));
else
(PopupHandler.VirtualView as PopupPage).ForceLayout();
(PopupHandler.VirtualView as PopupPage)?.ForceLayout();
base.OnLayout(changed, left, top, right, bottom);
//base.OnLayout(changed, 20, 500, 1080, 2000);
//base.OnLayout(changed, visibleRect.Left, visibleRect.Top, visibleRect.Right, visibleRect.Bottom);
Expand Down
68 changes: 41 additions & 27 deletions Mopups/Mopups.Maui/Platforms/Android/Impl/AndroidMopups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,19 @@ public static bool SendBackPressed(Action? backPressedHandler = null)

public Task AddAsync(PopupPage page)
{
try
{
HandleAccessibility(true);
HandleAccessibility(true, page.DisableAndroidAccessibilityHandling, page.Parent as Page);

page.Parent = MauiApplication.Current.Application.Windows[0].Content as Element;
var AndroidNativeView = IPopupPlatform.GetOrCreateHandler<PopupPageHandler>(page).PlatformView as Android.Views.View;
DecoreView?.AddView(AndroidNativeView);
page.Parent = MauiApplication.Current.Application.Windows[0].Content as Element;
page.Parent ??= MauiApplication.Current.Application.Windows[0].Content as Element;

return PostAsync(AndroidNativeView);
}
catch (Exception)
{
throw;
}
var handler = page.Handler ??= new PopupPageHandler(page.Parent.Handler.MauiContext);

var androidNativeView = handler.PlatformView as Android.Views.View;
var decoreView = Platform.CurrentActivity?.Window?.DecorView as FrameLayout;

decoreView?.AddView(androidNativeView);

return PostAsync(androidNativeView);
}

public Task RemoveAsync(PopupPage page)
Expand All @@ -58,7 +57,7 @@ public Task RemoveAsync(PopupPage page)

if (renderer != null)
{
HandleAccessibility(false);
HandleAccessibility(false, page.DisableAndroidAccessibilityHandling, page.Parent as Page);

DecoreView?.RemoveView(renderer.PlatformView as Android.Views.View);
renderer.DisconnectHandler(); //?? no clue if works
Expand All @@ -70,35 +69,50 @@ public Task RemoveAsync(PopupPage page)
return Task.CompletedTask;
}

static void HandleAccessibility(bool showPopup)
//! important keeps reference to pages that accessibility has applied to. This is so accessibility can be removed properly when popup is removed. #https://github.com/LuckyDucko/Mopups/issues/93
readonly List<Android.Views.View?> views = new();
void HandleAccessibility(bool showPopup, bool disableAccessibilityHandling, Page? mainPage = null)
{
Page? mainPage = Application.Current?.MainPage;

if (mainPage is null)
if (disableAccessibilityHandling)
{
return;
}

int navCount = mainPage.Navigation.NavigationStack.Count;
int modalCount = mainPage.Navigation.ModalStack.Count;
if (showPopup)
{
mainPage ??= Application.Current?.MainPage;

ProcessView(showPopup, mainPage.Handler?.PlatformView as Android.Views.View);
if (mainPage is null)
{
return;
}

if (navCount > 0)
{
ProcessView(showPopup, mainPage.Navigation?.NavigationStack[navCount - 1]?.Handler?.PlatformView as Android.Views.View);
views.Add(mainPage.Handler?.PlatformView as Android.Views.View);

int navCount = mainPage.Navigation.NavigationStack.Count;
int modalCount = mainPage.Navigation.ModalStack.Count;

if (navCount > 0)
{
views.Add(mainPage.Navigation?.NavigationStack[navCount - 1]?.Handler?.PlatformView as Android.Views.View);
}

if (modalCount > 0)
{
views.Add(mainPage.Navigation?.ModalStack[modalCount - 1]?.Handler?.PlatformView as Android.Views.View);
}
}

if (modalCount > 0)
foreach (var view in views)
{
ProcessView(showPopup, mainPage.Navigation?.ModalStack[modalCount - 1]?.Handler?.PlatformView as Android.Views.View);
ProcessView(showPopup, view);
}

static void ProcessView(bool showPopup, Android.Views.View? view)
{
if (view is null)
{
return;
return;
}

// Screen reader
Expand All @@ -110,7 +124,7 @@ static void ProcessView(bool showPopup, Android.Views.View? view)
}
}

Task<bool> PostAsync(Android.Views.View? nativeView)
static Task<bool> PostAsync(Android.Views.View? nativeView)
{
if (nativeView == null)
{
Expand Down
6 changes: 5 additions & 1 deletion Mopups/Mopups.Maui/Platforms/iOS/Handler/PopupPageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ public class PopupPageHandler : PageHandler
{
public PopupPageHandler()
{
this.SetMauiContext(MauiUIApplicationDelegate.Current.Application.Windows[0].Handler.MauiContext); //Still a hack?
SetMauiContext(MauiUIApplicationDelegate.Current.Application.Windows[0].Handler.MauiContext); //Still a hack?
}

public PopupPageHandler(IMauiContext context)
{
SetMauiContext(context); //Still a hack?
}
protected override Microsoft.Maui.Platform.ContentView CreatePlatformView()
{
return base.CreatePlatformView();
Expand Down
13 changes: 13 additions & 0 deletions Mopups/Mopups.Maui/Platforms/iOS/PopupWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Mopups.Platforms.iOS
{
internal class PopupWindow : UIWindow
{

private bool _stop = false;
public PopupWindow(IntPtr handle) : base(handle)
{
}
Expand All @@ -24,6 +26,12 @@ public PopupWindow(UIWindowScene uiWindowScene) : base(uiWindowScene)

public override UIView HitTest(CGPoint point, UIEvent? uievent)
{

if (_stop)
{
return base.HitTest(point, uievent);
}

var platformHandler = (PopupPageRenderer?)RootViewController;
var renderer = platformHandler?.Handler;
var hitTestResult = base.HitTest(point, uievent);
Expand All @@ -36,6 +44,11 @@ public override UIView HitTest(CGPoint point, UIEvent? uievent)

if ((formsElement.BackgroundInputTransparent || formsElement.CloseWhenBackgroundIsClicked ) && renderer?.PlatformView == hitTestResult)
{
if (formsElement.CloseWhenBackgroundIsClicked)
{
_stop = true;
}

formsElement.SendBackgroundClick();
if (formsElement.BackgroundInputTransparent)
{
Expand Down
28 changes: 13 additions & 15 deletions Mopups/Mopups.Maui/Platforms/iOS/iOSMopups.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
using CoreGraphics;


using Mopups.Interfaces;
using Mopups.Interfaces;
using Mopups.Pages;
using Mopups.Platforms.iOS;

using UIKit;
using UIKit;
namespace Mopups.iOS.Implementation;

internal class iOSMopups : IPopupPlatform
{
// It's necessary because GC in Xamarin.iOS 13 removes all UIWindow if there are not any references to them. See #459
private readonly List<UIWindow> _windows = new List<UIWindow>();

private static bool IsiOS9OrNewer => UIDevice.CurrentDevice.CheckSystemVersion(9, 0);

private static bool IsiOS13OrNewer => UIDevice.CurrentDevice.CheckSystemVersion(13, 0);

public bool IsSystemAnimationEnabled => true;

public Task AddAsync(PopupPage page)
{
page.Parent = Application.Current.MainPage;
page.Parent ??= Application.Current?.MainPage;

page.DescendantRemoved += HandleChildRemoved;

var keyWindow = GetKeyWindow(UIApplication.SharedApplication);
if (keyWindow?.WindowLevel == UIWindowLevel.Normal)
keyWindow.WindowLevel = -1;

var handler = (PopupPageHandler)IPopupPlatform.GetOrCreateHandler<PopupPageHandler>(page);
var handler = (page.Handler ??= new PopupPageHandler(page.Parent.Handler.MauiContext)) as PopupPageHandler;

PopupWindow window;

Expand Down Expand Up @@ -57,7 +52,7 @@ public Task AddAsync(PopupPage page)

handler.ViewController.ModalPresentationStyle = UIModalPresentationStyle.OverCurrentContext;
handler.ViewController.ModalTransitionStyle = UIModalTransitionStyle.CoverVertical;


return window.RootViewController.PresentViewControllerAsync(handler.ViewController, false);

Expand Down Expand Up @@ -124,12 +119,15 @@ public async Task RemoveAsync(PopupPage page)

private static void DisposeModelAndChildrenHandlers(VisualElement view)
{
foreach (Element child in view.GetVisualTreeDescendants())
foreach (var descendant in view.GetVisualTreeDescendants())
{
IElementHandler handler = child.Handler;
child?.Handler?.DisconnectHandler();
(handler?.PlatformView as UIView)?.RemoveFromSuperview();
(handler?.PlatformView as UIView)?.Dispose();
if (descendant is IElement child)
{
IElementHandler handler = child.Handler;
child?.Handler?.DisconnectHandler();
(handler?.PlatformView as UIView)?.RemoveFromSuperview();
(handler?.PlatformView as UIView)?.Dispose();
}
}

view?.Handler?.DisconnectHandler();
Expand Down
3 changes: 1 addition & 2 deletions SampleMaui/Platforms/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true" android:theme="@style/AppTheme"></application>
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:supportsRtl="true" android:theme="@style/AppTheme"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
4 changes: 2 additions & 2 deletions SampleMaui/SampleMopups.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>

<TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
<TargetFrameworks>net8.0-maccatalyst;net8.0-ios;net8.0-android</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>

<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net7.0-tizen</TargetFrameworks> -->
Expand Down

0 comments on commit c497b65

Please sign in to comment.