Skip to content

Commit 5cf47de

Browse files
Merge branch 'main' into feature/stephenquan/math-ar
2 parents 2c03b7f + fb6e664 commit 5cf47de

File tree

5 files changed

+50
-17
lines changed

5 files changed

+50
-17
lines changed

samples/CommunityToolkit.Maui.Sample/Pages/Views/CameraView/CameraViewPage.xaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161

6262
<FlexLayout Margin="5" JustifyContent="SpaceBetween" Wrap="Wrap">
6363

64+
<Picker
65+
Title="Cameras"
66+
ItemsSource="{Binding Cameras}"
67+
ItemDisplayBinding="{Binding Name}"
68+
SelectedItem="{Binding SelectedCamera}" />
69+
6470
<Picker
6571
Title="Flash"
6672
IsVisible="{Binding Path=SelectedCamera.IsFlashSupported, FallbackValue=false}"

samples/CommunityToolkit.Maui.Sample/ViewModels/Views/CameraView/CameraViewViewModel.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,36 @@ partial void OnSelectedResolutionChanged(Size value)
6666
UpdateResolutionText();
6767
}
6868

69+
partial void OnSelectedCameraChanged(CameraInfo? oldValue, CameraInfo? newValue)
70+
{
71+
UpdateCameraInfoText();
72+
}
73+
74+
void UpdateCameraInfoText()
75+
{
76+
if (SelectedCamera is null)
77+
{
78+
CameraNameText = string.Empty;
79+
ZoomRangeText = string.Empty;
80+
}
81+
else
82+
{
83+
CameraNameText = $"{SelectedCamera.Name}";
84+
ZoomRangeText = $"Min Zoom: {SelectedCamera.MinimumZoomFactor}, Max Zoom: {SelectedCamera.MaximumZoomFactor}";
85+
UpdateFlashModeText();
86+
}
87+
}
88+
6989
void UpdateFlashModeText()
7090
{
7191
if (SelectedCamera is null)
7292
{
73-
return;
93+
FlashModeText = string.Empty;
94+
}
95+
else
96+
{
97+
FlashModeText = $"{(SelectedCamera.IsFlashSupported ? $"Flash mode: {FlashMode}" : "Flash not supported")}";
7498
}
75-
FlashModeText = $"{(SelectedCamera.IsFlashSupported ? $"Flash mode: {FlashMode}" : "Flash not supported")}";
7699
}
77100

78101
void UpdateCurrentZoomText()

src/CommunityToolkit.Maui.Camera/Providers/CameraProvider.windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public async partial ValueTask RefreshAvailableCameras(CancellationToken token)
1414
var deviceInfoCollection = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture).AsTask(token);
1515
var mediaFrameSourceGroup = await MediaFrameSourceGroup.FindAllAsync().AsTask(token);
1616
var videoCaptureSourceGroup = mediaFrameSourceGroup.Where(sourceGroup => deviceInfoCollection.Any(deviceInfo => deviceInfo.Id == sourceGroup.Id)).ToList();
17-
var mediaCapture = new MediaCapture();
1817

1918
var availableCameras = new List<CameraInfo>();
2019

2120
foreach (var sourceGroup in videoCaptureSourceGroup)
2221
{
22+
using var mediaCapture = new MediaCapture();
2323
await mediaCapture.InitializeCameraForCameraView(sourceGroup.Id, token);
2424

2525
CameraPosition position = CameraPosition.Unknown;

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)