Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#63 demo project issues #64

Merged
merged 5 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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