Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
0b6b4a0
Switch from using `page.GetParentWindow` to user32.dll `GetForeground…
ne0rrmatrix Feb 10, 2025
760792a
Merge branch 'main' into FixWindowsMultiWindows
ne0rrmatrix Feb 13, 2025
476a790
Merge branch 'main' into FixWindowsMultiWindows
ne0rrmatrix Feb 15, 2025
c99f6ec
Merge branch 'main' into FixWindowsMultiWindows
ne0rrmatrix Mar 7, 2025
5b7b18c
Merge branch 'main' into FixWindowsMultiWindows
ne0rrmatrix Apr 1, 2025
5a1ea68
Merge branch 'main' into FixWindowsMultiWindows
TheCodeTraveler Apr 12, 2025
2a42423
Merge branch 'main' into FixWindowsMultiWindows
ne0rrmatrix May 4, 2025
2dc5943
Merge branch 'main' into FixWindowsMultiWindows
ne0rrmatrix May 7, 2025
8f18056
Merge branch 'main' into FixWindowsMultiWindows
ne0rrmatrix May 29, 2025
4baa00d
Merge branch 'main' into FixWindowsMultiWindows
ne0rrmatrix Jun 3, 2025
518b85c
Merge branch 'main' into FixWindowsMultiWindows
ne0rrmatrix Jun 8, 2025
60598fd
Merge branch 'main' into FixWindowsMultiWindows
ne0rrmatrix Jun 20, 2025
ef53d5d
Merge branch 'main' into FixWindowsMultiWindows
ne0rrmatrix Jun 25, 2025
25c5ca0
Merge branch 'main' into FixWindowsMultiWindows
ne0rrmatrix Jul 3, 2025
0a090cb
Merge branch 'main' into FixWindowsMultiWindows
ne0rrmatrix Jul 8, 2025
f182173
Merge branch 'main' into FixWindowsMultiWindows
ne0rrmatrix Aug 3, 2025
2eeb7ee
Update src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.…
ne0rrmatrix Aug 3, 2025
04b8ba3
Update method for Getting `AppWindow` to use `TryGetForeGroundWindow(…
ne0rrmatrix Aug 3, 2025
3aabbee
Merge branch 'main' into FixWindowsMultiWindows
TheCodeTraveler Oct 15, 2025
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 @@ -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,17 @@ 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();

/// <summary>
/// Safely gets the foreground window handle, returning null if no foreground window exists.
/// </summary>
internal static IntPtr? TryGetForegroundWindow()
{
var hwnd = GetForegroundWindow();
return hwnd == IntPtr.Zero ? null : hwnd;
}
readonly Popup popup = new();
readonly Grid fullScreenGrid = new();
readonly MediaPlayerElement mediaPlayerElement;
Expand Down Expand Up @@ -157,24 +167,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 = TryGetForegroundWindow() ?? throw new InvalidOperationException("No foreground window found.");
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