Skip to content

Commit d772b6c

Browse files
ne0rrmatrixTheCodeTravelerCopilot
authored
Fix Media Element Full Screen in Windows when using Multiple Windows (#2506)
* Switch from using `page.GetParentWindow` to user32.dll `GetForegroundWindow` for grabbing correct window in multi-window environment * Update src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.windows.cs Co-authored-by: Copilot <[email protected]> * Update method for Getting `AppWindow` to use `TryGetForeGroundWindow()` and if it is null throw InvalidOperationException if window is null. --------- Co-authored-by: Brandon Minnick <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 3e8586f commit d772b6c

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/CommunityToolkit.Maui.MediaElement/CommunityToolkit.Maui.MediaElement.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<SingleProject>true</SingleProject>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<IsAotCompatible>true</IsAotCompatible>
10+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
11+
1012

1113
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.0</SupportedOSPlatformVersion>
1214
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">15.0</SupportedOSPlatformVersion>

src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.windows.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reflection;
2+
using System.Runtime.InteropServices;
23
using CommunityToolkit.Maui.Extensions;
34
using CommunityToolkit.Maui.Primitives;
45
using CommunityToolkit.Maui.Views;
@@ -8,7 +9,6 @@
89
using Microsoft.UI.Xaml.Controls;
910
using Microsoft.UI.Xaml.Controls.Primitives;
1011
using Microsoft.UI.Xaml.Markup;
11-
using WinRT.Interop;
1212
using Application = Microsoft.Maui.Controls.Application;
1313
using Grid = Microsoft.UI.Xaml.Controls.Grid;
1414
using Page = Microsoft.Maui.Controls.Page;
@@ -20,7 +20,17 @@ namespace CommunityToolkit.Maui.Core.Views;
2020
/// </summary>
2121
public partial class MauiMediaElement : Grid, IDisposable
2222
{
23-
static readonly AppWindow appWindow = GetAppWindowForCurrentWindow();
23+
[LibraryImport("user32.dll")]
24+
internal static partial IntPtr GetForegroundWindow();
25+
26+
/// <summary>
27+
/// Safely gets the foreground window handle, returning null if no foreground window exists.
28+
/// </summary>
29+
internal static IntPtr? TryGetForegroundWindow()
30+
{
31+
var hwnd = GetForegroundWindow();
32+
return hwnd == IntPtr.Zero ? null : hwnd;
33+
}
2434
readonly Popup popup = new();
2535
readonly Grid fullScreenGrid = new();
2636
readonly MediaPlayerElement mediaPlayerElement;
@@ -157,24 +167,16 @@ protected virtual void Dispose(bool disposing)
157167

158168
static AppWindow GetAppWindowForCurrentWindow()
159169
{
160-
// let's cache the CurrentPage here, since the user can navigate or background the app
161-
// while this method is running
162-
var currentPage = CurrentPage;
163-
164-
if (currentPage?.GetParentWindow().Handler.PlatformView is not MauiWinUIWindow window)
165-
{
166-
throw new InvalidOperationException($"{nameof(window)} cannot be null.");
167-
}
168-
169-
var handle = WindowNative.GetWindowHandle(window);
170-
var id = Win32Interop.GetWindowIdFromWindow(handle);
171-
170+
var windowHandle = TryGetForegroundWindow() ?? throw new InvalidOperationException("No foreground window found.");
171+
var id = Win32Interop.GetWindowIdFromWindow(windowHandle);
172172
return AppWindow.GetFromWindowId(id);
173173
}
174174

175175
void OnFullScreenButtonClick(object sender, RoutedEventArgs e)
176176
{
177177
var currentPage = CurrentPage;
178+
var appWindow = GetAppWindowForCurrentWindow();
179+
178180
if (appWindow.Presenter.Kind is AppWindowPresenterKind.FullScreen)
179181
{
180182
appWindow.SetPresenter(AppWindowPresenterKind.Default);

0 commit comments

Comments
 (0)