Skip to content

Fix Media Element Full Screen in Windows when using Multiple Windows #2506

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<SingleProject>true</SingleProject>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsAotCompatible>true</IsAotCompatible>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>


<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">15.0</SupportedOSPlatformVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Runtime.InteropServices;
using CommunityToolkit.Maui.Extensions;
using CommunityToolkit.Maui.Primitives;
using CommunityToolkit.Maui.Views;
Expand All @@ -8,7 +9,6 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Markup;
using WinRT.Interop;
using Application = Microsoft.Maui.Controls.Application;
using Grid = Microsoft.UI.Xaml.Controls.Grid;
using Page = Microsoft.Maui.Controls.Page;
Expand All @@ -20,7 +20,9 @@ namespace CommunityToolkit.Maui.Core.Views;
/// </summary>
public partial class MauiMediaElement : Grid, IDisposable
{
static readonly AppWindow appWindow = GetAppWindowForCurrentWindow();
[LibraryImport("user32.dll")]
internal static partial IntPtr GetForegroundWindow();

readonly Popup popup = new();
readonly Grid fullScreenGrid = new();
readonly MediaPlayerElement mediaPlayerElement;
Expand Down Expand Up @@ -147,24 +149,16 @@ protected virtual void Dispose(bool disposing)

static AppWindow GetAppWindowForCurrentWindow()
{
// let's cache the CurrentPage here, since the user can navigate or background the app
// while this method is running
var currentPage = CurrentPage;

if (currentPage?.GetParentWindow().Handler.PlatformView is not MauiWinUIWindow window)
{
throw new InvalidOperationException($"{nameof(window)} cannot be null.");
}

var handle = WindowNative.GetWindowHandle(window);
var id = Win32Interop.GetWindowIdFromWindow(handle);

var windowHandle = GetForegroundWindow();
var id = Win32Interop.GetWindowIdFromWindow(windowHandle);
return AppWindow.GetFromWindowId(id);
}

void OnFullScreenButtonClick(object sender, RoutedEventArgs e)
{
var currentPage = CurrentPage;
var appWindow = GetAppWindowForCurrentWindow();

if (appWindow.Presenter.Kind is AppWindowPresenterKind.FullScreen)
{
appWindow.SetPresenter(AppWindowPresenterKind.Default);
Expand Down
Loading