-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
WindowExtensions.cs
88 lines (70 loc) · 2.35 KB
/
WindowExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using Microsoft.Maui.Devices;
using System.Threading.Tasks;
using Microsoft.Maui.Media;
using WinRT.Interop;
using Windows.Graphics.Display;
namespace Microsoft.Maui.Platform
{
public static partial class WindowExtensions
{
public static void UpdateTitle(this UI.Xaml.Window platformWindow, IWindow window)
{
platformWindow.Title = window.Title;
var rootManager = window.Handler?.MauiContext?.GetNavigationRootManager();
if (rootManager != null)
{
rootManager.SetWindowTitle(window.Title);
}
}
public static IWindow? GetWindow(this UI.Xaml.Window platformWindow)
{
foreach (var window in MauiWinUIApplication.Current.Application.Windows)
{
if (window?.Handler?.PlatformView is UI.Xaml.Window win && win == platformWindow)
return window;
}
return null;
}
public static IntPtr GetWindowHandle(this UI.Xaml.Window platformWindow)
{
var hwnd = WindowNative.GetWindowHandle(platformWindow);
if (hwnd == IntPtr.Zero)
throw new NullReferenceException("The Window Handle is null.");
return hwnd;
}
public static float GetDisplayDensity(this UI.Xaml.Window platformWindow)
{
var hwnd = platformWindow.GetWindowHandle();
if (hwnd == IntPtr.Zero)
return 1.0f;
return PlatformMethods.GetDpiForWindow(hwnd) / DeviceDisplay.BaseLogicalDpi;
}
public static UI.Windowing.AppWindow? GetAppWindow(this UI.Xaml.Window platformWindow)
{
var hwnd = platformWindow.GetWindowHandle();
if (hwnd == IntPtr.Zero)
return null;
var windowId = UI.Win32Interop.GetWindowIdFromWindow(hwnd);
return UI.Windowing.AppWindow.GetFromWindowId(windowId);
}
internal static DisplayOrientation GetOrientation(this IWindow? window)
{
if (window == null)
return DeviceDisplay.Current.MainDisplayInfo.Orientation;
var appWindow = window.Handler?.MauiContext?.GetPlatformWindow()?.GetAppWindow();
if (appWindow == null)
return DisplayOrientation.Unknown;
DisplayOrientations orientationEnum;
int theScreenWidth = appWindow.Size.Width;
int theScreenHeight = appWindow.Size.Height;
if (theScreenWidth > theScreenHeight)
orientationEnum = DisplayOrientations.Landscape;
else
orientationEnum = DisplayOrientations.Portrait;
return orientationEnum == DisplayOrientations.Landscape
? DisplayOrientation.Landscape
: DisplayOrientation.Portrait;
}
}
}