Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@ internal static Page GetCurrentPage(this Page currentPage)
{
return modal;
}

if (currentPage is FlyoutPage flyoutPage)
else if (currentPage is FlyoutPage fp)
{
return GetCurrentPage(flyoutPage.Detail);
return GetCurrentPage(fp.Detail);
}

if (currentPage is Shell { CurrentItem.CurrentItem: IShellSectionController shellSectionController })
else if (currentPage is Shell shell && shell.CurrentItem?.CurrentItem is IShellSectionController ssc)
{
return shellSectionController.PresentedPage;
return ssc.PresentedPage;
}

if (currentPage is IPageContainer<Page> paigeContainer)
else if (currentPage is IPageContainer<Page> pc)
{
return GetCurrentPage(paigeContainer.CurrentPage);
return GetCurrentPage(pc.CurrentPage);
}
else
{
return currentPage;
}

return currentPage;
}

internal record struct ParentWindow
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using AVKit;
using CommunityToolkit.Maui.Extensions;
Expand Down Expand Up @@ -152,36 +152,11 @@ static bool TryGetCurrentPage([NotNullWhen(true)] out Page? currentPage)
// TODO: Add support for MediaElement in an ItemsView in a multi-window application
throw new NotSupportedException("MediaElement is not currently supported in multi-window applications");
}

var window = Application.Current.Windows[0];

// If using Shell, return the current page
if (window.Page is Shell { CurrentPage: not null } shell)
{
currentPage = shell.CurrentPage;
return true;
}

// If not using Shell, use the ModelNavigationStack to check for any pages displayed modally
if (TryGetModalPage(window, out var modalPage))
{
currentPage = modalPage;
return true;
}

// If not using Shell or a Modal Page, return the visible page in the (non-modal) NavigationStack
if (window.Navigation.NavigationStack.LastOrDefault() is Page page)
if (Application.Current.Windows[0].Page is Page page)
{
currentPage = page;
currentPage = PageExtensions.GetCurrentPage(page);
return true;
}

return false;

static bool TryGetModalPage(Window window, [NotNullWhen(true)] out Page? page)
{
page = window.Navigation.ModalStack.LastOrDefault();
return page is not null;
}
}
}
104 changes: 104 additions & 0 deletions src/CommunityToolkit.Maui.UnitTests/Extensions/PageExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using CommunityToolkit.Maui.Extensions;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Extensions;
public class PageExtensionsTests : BaseTest
{
[Fact]
public void GetCurrentPage_ReturnsModalPage_WhenModalStackIsNotEmpty()
{
// Arrange
var modalPage = new ContentPage();
var navigationPage = new NavigationPage(new ContentPage());
navigationPage.Navigation.PushModalAsync(modalPage);

// Act
var result = PageExtensions.GetCurrentPage(navigationPage);

// Assert
Assert.Equal(modalPage, result);
}

[Fact]
public void GetCurrentPage_ReturnsFlyoutDetailPage_WhenCurrentPageIsFlyoutPage()
{
// Arrange
var detailPage = new ContentPage();
var flyoutPage = new FlyoutPage
{
Detail = new NavigationPage(detailPage),
Flyout = new ContentPage()
{
Title = "Flyout",
}
};

// Act
var result = PageExtensions.GetCurrentPage(flyoutPage);

// Assert
Assert.Equal(detailPage, result);
}

[Fact]
public void GetCurrentPage_ReturnsShellPresentedPage_WhenCurrentPageIsShell()
{
// Arrange
var presentedPage = new ContentPage();
var shell = new Shell();
var shellSection = new ShellSection
{
Items = { new ShellContent { Content = presentedPage } }
};
shell.Items.Add(new ShellItem { Items = { shellSection } });
shell.CurrentItem = shell.Items[0];

// Act
var result = PageExtensions.GetCurrentPage(shell);

// Assert
Assert.Equal(presentedPage, result);
}

[Fact]
public void GetCurrentPage_ReturnsContainerCurrentPage_WhenCurrentPageIsPageContainer()
{
var dispatcher = DispatcherProvider.Current.GetForCurrentThread() ?? throw new InvalidOperationException("Dispatcher is not available for the current thread.");
// Arrange
var currentPage = new ContentPage();

TabbedPage? pageContainer = null;
dispatcher.Dispatch(() =>
{
pageContainer = new TabbedPage
{
Children =
{
currentPage,
}
};
});
if (pageContainer is null)
{
throw new InvalidOperationException("PageContainer is null");
}
// Act
var result = PageExtensions.GetCurrentPage(pageContainer);

// Assert
Assert.Equal(currentPage, result);
}

[Fact]
public void GetCurrentPage_ReturnsCurrentPage_WhenNoSpecialCasesApply()
{
// Arrange
var currentPage = new ContentPage();

// Act
var result = PageExtensions.GetCurrentPage(currentPage);

// Assert
Assert.Equal(currentPage, result);
}
}
Loading