Skip to content

Commit

Permalink
Support snap layouts on Windows 11 (Kinnara#467)
Browse files Browse the repository at this point in the history
* Support snap layouts on Windows 11

* Don't trigger build when pushing to non-master branches
  • Loading branch information
Kinnara authored Jun 18, 2022
1 parent 4d04086 commit 24789fa
Show file tree
Hide file tree
Showing 19 changed files with 357 additions and 40 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Build

on: [push, pull_request]
on:
push:
branches: [ master ]
pull_request:
branches: [ '**' ]

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<Version>0.9.5</Version>
<Authors>Yimeng Wu</Authors>
<Product>ModernWPF UI Library</Product>
<LangVersion>9.0</LangVersion>
<LangVersion>10.0</LangVersion>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion ModernWpf.SampleApp/app.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitor</dpiAwareness>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
</windowsSettings>
</application>
Expand Down
23 changes: 23 additions & 0 deletions ModernWpf/Common/DpiScale2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Windows;

namespace ModernWpf
{
internal readonly record struct DpiScale2
{
public DpiScale2(double dpiScaleX, double dpiScaleY)
{
DpiScaleX = dpiScaleX;
DpiScaleY = dpiScaleY;
}

#if NET462_OR_NEWER
public DpiScale2(DpiScale dpiScale)
: this(dpiScale.DpiScaleX, dpiScale.DpiScaleY)
{
}
#endif

public double DpiScaleX { get; }
public double DpiScaleY { get; }
}
}
22 changes: 5 additions & 17 deletions ModernWpf/Controls/Primitives/MaximizedWindowFixer.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using MS.Win32;
using Standard;
using System;
using System.Diagnostics;
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
using MS.Win32;
using Standard;
using NativeMethods = Standard.NativeMethods;

namespace ModernWpf.Controls.Primitives
Expand Down Expand Up @@ -216,22 +214,12 @@ private Thickness GetMaximizedWindowBorder()
return new Thickness();
}

double dpiScaleX, dpiScaleY;
#if NET462_OR_NEWER
DpiScale dpi = VisualTreeHelper.GetDpi(_window);
dpiScaleX = dpi.DpiScaleX;
dpiScaleY = dpi.DpiScaleY;
#else
Matrix transformToDevice = _hwndSource.CompositionTarget.TransformToDevice;
dpiScaleX = transformToDevice.M11;
dpiScaleY = transformToDevice.M22;
#endif

var dpi = _window.GetDpi();
int frameWidth = NativeMethods.GetSystemMetrics(SM.CXSIZEFRAME);
int frameHeight = NativeMethods.GetSystemMetrics(SM.CYSIZEFRAME);
int borderPadding = NativeMethods.GetSystemMetrics(SM.CXPADDEDBORDER);
Size borderSize = new Size(frameWidth + borderPadding, frameHeight + borderPadding);
Size borderSizeInDips = DpiHelper.DeviceSizeToLogical(borderSize, dpiScaleX, dpiScaleY);
Size borderSizeInDips = DpiHelper.DeviceSizeToLogical(borderSize, dpi.DpiScaleX, dpi.DpiScaleY);

return new Thickness(borderSizeInDips.Width, borderSizeInDips.Height, borderSizeInDips.Width, borderSizeInDips.Height);
}
Expand Down
27 changes: 27 additions & 0 deletions ModernWpf/Helpers/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;

namespace ModernWpf
Expand Down Expand Up @@ -80,6 +82,31 @@ public static bool HasLocalValue(this DependencyObject d, DependencyProperty dp)
{
return d.ReadLocalValue(dp) != DependencyProperty.UnsetValue;
}

public static DpiScale2 GetDpi(this Window window)
{
if (window is null)
{
throw new ArgumentNullException(nameof(window));
}

#if NET462_OR_NEWER
return new DpiScale2(VisualTreeHelper.GetDpi(window));
#else
var hwnd = new WindowInteropHelper(window).Handle;
var hwndSource = HwndSource.FromHwnd(hwnd);
if (hwndSource != null)
{
Matrix transformToDevice = hwndSource.CompositionTarget.TransformToDevice;
return new DpiScale2(transformToDevice.M11, transformToDevice.M22);
}
else
{
Debug.Fail("Should not reach here");
return new DpiScale2(1, 1);
}
#endif
}
}

internal enum InterestPoint
Expand Down
2 changes: 2 additions & 0 deletions ModernWpf/Helpers/OSVersionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ internal static class OSVersionHelper

internal static bool IsWindows10OrGreater { get; } = IsWindowsNT && _osVersion >= new Version(10, 0);

internal static bool IsWindows11OrGreater { get; } = IsWindowsNT && _osVersion >= new Version(10, 0, 22000);

private static Version GetOSVersion()
{
var osv = new RTL_OSVERSIONINFOEX();
Expand Down
6 changes: 6 additions & 0 deletions ModernWpf/ModernWpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.2.1-beta">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Page Remove="Properties\DesignTimeResources.xaml" />
</ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions ModernWpf/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
WM_NCHITTEST
WM_NCMOUSEMOVE
WM_NCLBUTTONDOWN
WM_NCLBUTTONUP
WM_NCMOUSELEAVE
HTCLIENT
HTMAXBUTTON
19 changes: 19 additions & 0 deletions ModernWpf/Standard/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,29 @@

namespace Standard
{
using System;
using System.Diagnostics.CodeAnalysis;

internal static partial class Utility
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static int GET_X_LPARAM(IntPtr lParam)
{
return LOWORD(lParam.ToInt32());
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static int GET_Y_LPARAM(IntPtr lParam)
{
return HIWORD(lParam.ToInt32());
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static int HIWORD(int i)
{
return (short)(i >> 16);
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static int LOWORD(int i)
{
Expand Down
2 changes: 1 addition & 1 deletion ModernWpf/Styles/Window.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
x:Shared="False"
CaptionHeight="{DynamicResource {x:Static local:TitleBar.HeightKey}}"
CornerRadius="0"
GlassFrameThickness="0,1,0,0"
GlassFrameThickness="{x:Static WindowChrome.GlassFrameCompleteThickness}"
UseAeroCaptionButtons="False" />

<Style x:Key="BaseWindowStyle" TargetType="Window">
Expand Down
Loading

0 comments on commit 24789fa

Please sign in to comment.