Skip to content

Commit

Permalink
LuckyDucko#63 Fix: Issues with setup and use of configurable animations.
Browse files Browse the repository at this point in the history
Using dynamic loading of types and values.
  • Loading branch information
bakerhillpins committed May 17, 2023
1 parent 878d1c6 commit 4a06b8f
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 242 deletions.
215 changes: 107 additions & 108 deletions Mopups/Mopups.Maui/Platforms/Windows/Impl/PopupPlatformWindows.cs
Original file line number Diff line number Diff line change
@@ -1,108 +1,107 @@

using System;
using Microsoft.Maui.Platform;
using Mopups.Interfaces;
using Mopups.Pages;
using Mopups.Platforms.Windows;
using Mopups.Services;

namespace Mopups.Windows.Implementation
{
class PopupPlatformWindows : IPopupPlatform
{
private IPopupNavigation PopupNavigationInstance => MopupService.Instance;

//public event EventHandler OnInitialized
//{
// add => Popup.OnInitialized += value;
// remove => Popup.OnInitialized -= value;
//}

//public bool IsInitialized => Popup.IsInitialized;

public bool IsSystemAnimationEnabled => true;

public PopupPlatformWindows()
{
//SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
}

public static bool SendBackPressed(Action? backPressedHandler = null)
{
var popupNavigationInstance = MopupService.Instance;

if (popupNavigationInstance.PopupStack.Count > 0)
{
var lastPage = popupNavigationInstance.PopupStack[popupNavigationInstance.PopupStack.Count - 1];

var isPreventClose = lastPage.SendBackButtonPressed();

if (!isPreventClose)
{
popupNavigationInstance.PopAsync().SafeFireAndForget();
}

return true;
}

backPressedHandler?.Invoke();

return false;
}

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

var popup = new global::Microsoft.UI.Xaml.Controls.Primitives.Popup();

// Use TOPLATFORM to create your handlers
// I'd recommend wiring up all your services through ConfigureMopups
// builder.Services.AddScoped<IPopupPlatform, PopupPlatform>();
// builder.Services.AddScoped<IPopupNavigation, PopupNavigation>();
// Then you can use contructor resolution instead of singletons
// But I figured we could do that in a later PR and just work on windows here

var renderer = (PopupPageRenderer)page.ToPlatform(Application.Current.MainPage.Handler.MauiContext);

renderer.Prepare(popup);
popup.Child = renderer;


// https://github.com/microsoft/microsoft-ui-xaml/issues/3389
popup.XamlRoot =
Application.Current.MainPage.Handler.MauiContext.Services.GetService<Microsoft.UI.Xaml.Window>().Content.XamlRoot;

popup.IsOpen = true;
page.ForceLayout();

await Task.Delay(5);
}

public async Task RemoveAsync(PopupPage page)
{
if (page == null)
throw new Exception("Popup page is null");

var renderer = (PopupPageRenderer)page.ToPlatform(Application.Current.MainPage.Handler.MauiContext);
var popup = renderer.Container;

if (popup != null)
{
renderer.Destroy();

Cleanup(page);
page.Parent = null;
popup.Child = null;
popup.IsOpen = false;
}

await Task.Delay(5);
}

internal static void Cleanup(VisualElement element)
{
element.Handler?.DisconnectHandler();
}
}
}
using AsyncAwaitBestPractices;
using Microsoft.Maui.Platform;
using Mopups.Interfaces;
using Mopups.Pages;
using Mopups.Platforms.Windows;
using Mopups.Services;

namespace Mopups.Windows.Implementation
{
class PopupPlatformWindows : IPopupPlatform
{
private IPopupNavigation PopupNavigationInstance => MopupService.Instance;

//public event EventHandler OnInitialized
//{
// add => Popup.OnInitialized += value;
// remove => Popup.OnInitialized -= value;
//}

//public bool IsInitialized => Popup.IsInitialized;

public bool IsSystemAnimationEnabled => true;

public PopupPlatformWindows()
{
//SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
}

public static bool SendBackPressed(Action? backPressedHandler = null)
{
var popupNavigationInstance = MopupService.Instance;

if (popupNavigationInstance.PopupStack.Count > 0)
{
var lastPage = popupNavigationInstance.PopupStack[popupNavigationInstance.PopupStack.Count - 1];

var isPreventClose = lastPage.SendBackButtonPressed();

if (!isPreventClose)
{
popupNavigationInstance.PopAsync().SafeFireAndForget();
}

return true;
}

backPressedHandler?.Invoke();

return false;
}

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

var popup = new global::Microsoft.UI.Xaml.Controls.Primitives.Popup();

// Use TOPLATFORM to create your handlers
// I'd recommend wiring up all your services through ConfigureMopups
// builder.Services.AddScoped<IPopupPlatform, PopupPlatform>();
// builder.Services.AddScoped<IPopupNavigation, PopupNavigation>();
// Then you can use contructor resolution instead of singletons
// But I figured we could do that in a later PR and just work on windows here

var renderer = (PopupPageRenderer)page.ToPlatform(Application.Current.MainPage.Handler.MauiContext);

renderer.Prepare(popup);
popup.Child = renderer;


// https://github.com/microsoft/microsoft-ui-xaml/issues/3389
popup.XamlRoot =
Application.Current.MainPage.Handler.MauiContext.Services.GetService<Microsoft.UI.Xaml.Window>().Content.XamlRoot;

popup.IsOpen = true;
page.ForceLayout();

await Task.Delay(5);
}

public async Task RemoveAsync(PopupPage page)
{
if (page == null)
throw new Exception("Popup page is null");

var renderer = (PopupPageRenderer)page.ToPlatform(Application.Current.MainPage.Handler.MauiContext);
var popup = renderer.Container;

if (popup != null)
{
renderer.Destroy();

Cleanup(page);
page.Parent = null;
popup.Child = null;
popup.IsOpen = false;
}

await Task.Delay(5);
}

internal static void Cleanup(VisualElement element)
{
element.Handler?.DisconnectHandler();
}
}
}
Loading

0 comments on commit 4a06b8f

Please sign in to comment.