forked from lepoco/wpfui
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
950ade6
commit 5bdaf6c
Showing
2 changed files
with
488 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
// This Source Code Form is subject to the terms of the MIT License. | ||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT. | ||
// Copyright (C) Leszek Pomianowski and WPF UI Contributors. | ||
// All Rights Reserved. | ||
|
||
#nullable enable | ||
|
||
using System; | ||
using System.Windows; | ||
using System.Windows.Media; | ||
using System.Windows.Shell; | ||
using Wpf.Ui.Appearance; | ||
using Wpf.Ui.Controls.Interfaces; | ||
using Wpf.Ui.Dpi; | ||
using Size = System.Windows.Size; | ||
|
||
namespace Wpf.Ui.Controls; | ||
|
||
/// <summary> | ||
/// If you use <see cref="WindowChrome"/> to extend the UI elements to the non-client area, you can include this container | ||
/// in the template of <see cref="Window"/> so that the content inside automatically fills the client area. | ||
/// Using this container can let you get rid of various margin adaptations done in | ||
/// Setter/Trigger of the style of <see cref="Window"/> when the window state changes. | ||
/// </summary> | ||
public class ClientAreaBorder : System.Windows.Controls.Border, IThemeControl | ||
{ | ||
private const int SM_CXFRAME = 32; | ||
|
||
private const int SM_CYFRAME = 33; | ||
|
||
private const int SM_CXPADDEDBORDER = 92; | ||
|
||
private Window? _oldWindow; | ||
|
||
private static Thickness? _paddedBorderThickness; | ||
|
||
private static Thickness? _resizeFrameBorderThickness; | ||
|
||
private static Thickness? _windowChromeNonClientFrameThickness; | ||
|
||
public ThemeType Theme { get; set; } = ThemeType.Unknown; | ||
|
||
/// <summary> | ||
/// Get the system <see cref="SM_CXPADDEDBORDER"/> value in WPF units. | ||
/// </summary> | ||
public Thickness PaddedBorderThickness | ||
{ | ||
get | ||
{ | ||
if (_paddedBorderThickness is not null) | ||
return _paddedBorderThickness.Value; | ||
|
||
var paddedBorder = Interop.User32.GetSystemMetrics( | ||
Interop.User32.SM.CXPADDEDBORDER); | ||
|
||
var (factorX, factorY) = GetDpi(); | ||
var frameSize = new Size(paddedBorder, paddedBorder); | ||
var frameSizeInDips = new Size(frameSize.Width / factorX, frameSize.Height / factorY); | ||
|
||
_paddedBorderThickness = new Thickness(frameSizeInDips.Width, frameSizeInDips.Height, | ||
frameSizeInDips.Width, frameSizeInDips.Height); | ||
|
||
return _paddedBorderThickness.Value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get the system <see cref="SM_CXFRAME"/> and <see cref="SM_CYFRAME"/> values in WPF units. | ||
/// </summary> | ||
public Thickness ResizeFrameBorderThickness => _resizeFrameBorderThickness ??= new Thickness( | ||
SystemParameters.ResizeFrameVerticalBorderWidth, | ||
SystemParameters.ResizeFrameHorizontalBorderHeight, | ||
SystemParameters.ResizeFrameVerticalBorderWidth, | ||
SystemParameters.ResizeFrameHorizontalBorderHeight); | ||
|
||
/// <summary> | ||
/// If you use a <see cref="WindowChrome"/> to extend the client area of a window to the non-client area, you need to handle the edge margin issue when the window is maximized. | ||
/// Use this property to get the correct margin value when the window is maximized, so that when the window is maximized, the client area can completely cover the screen client area by no less than a single pixel at any DPI. | ||
/// The<see cref="Interop.User32.GetSystemMetrics"/> method cannot obtain this value directly. | ||
/// </summary> | ||
public Thickness WindowChromeNonClientFrameThickness => _windowChromeNonClientFrameThickness ??= new Thickness( | ||
ResizeFrameBorderThickness.Left + PaddedBorderThickness.Left, | ||
ResizeFrameBorderThickness.Top + PaddedBorderThickness.Top, | ||
ResizeFrameBorderThickness.Right + PaddedBorderThickness.Right, | ||
ResizeFrameBorderThickness.Bottom + PaddedBorderThickness.Bottom); | ||
|
||
public ClientAreaBorder() | ||
{ | ||
Theme = Appearance.Theme.GetAppTheme(); | ||
Appearance.Theme.Changed += OnThemeChanged; | ||
} | ||
|
||
private void OnThemeChanged(ThemeType currentTheme, Color systemAccent) | ||
{ | ||
Theme = currentTheme; | ||
|
||
ApplyDefaultWindowBorder(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void OnVisualParentChanged(DependencyObject oldParent) | ||
{ | ||
base.OnVisualParentChanged(oldParent); | ||
|
||
if (_oldWindow is { } oldWindow) | ||
{ | ||
oldWindow.StateChanged -= OnWindowStateChanged; | ||
} | ||
|
||
var newWindow = (Window?)Window.GetWindow(this); | ||
|
||
if (newWindow is not null) | ||
{ | ||
newWindow.StateChanged -= OnWindowStateChanged; // Unsafe | ||
newWindow.StateChanged += OnWindowStateChanged; | ||
} | ||
|
||
_oldWindow = newWindow; | ||
|
||
ApplyDefaultWindowBorder(); | ||
} | ||
|
||
private void OnWindowStateChanged(object? sender, EventArgs e) | ||
{ | ||
if (sender is not Window window) | ||
return; | ||
|
||
Padding = window.WindowState switch | ||
{ | ||
WindowState.Maximized => WindowChromeNonClientFrameThickness, | ||
_ => default, | ||
}; | ||
} | ||
|
||
private void ApplyDefaultWindowBorder() | ||
{ | ||
if (Win32.Utilities.IsOSWindows11OrNewer || _oldWindow == null) | ||
{ | ||
ResetDefaultWindowBorder(_oldWindow); | ||
return; | ||
} | ||
|
||
ThemeType theme = Theme; | ||
|
||
if (_oldWindow is UiWindow uiWindow) | ||
{ | ||
if (!uiWindow.DefaultBorderEnabled) | ||
{ | ||
ResetDefaultWindowBorder(uiWindow); | ||
return; | ||
} | ||
|
||
if (uiWindow.DefaultBorderThemeOverwrite != ThemeType.Unknown) | ||
theme = uiWindow.DefaultBorderThemeOverwrite; | ||
} | ||
|
||
// SystemParameters.WindowGlassBrush | ||
|
||
Brush brush = new SolidColorBrush(theme == ThemeType.Light ? Color.FromArgb(255, 200, 200, 200) : Color.FromArgb(255, 58, 58, 58)); | ||
|
||
BorderThickness = new Thickness(1); | ||
BorderBrush = brush; | ||
} | ||
|
||
private void ResetDefaultWindowBorder(Window? window) | ||
{ | ||
if (window == null) | ||
return; | ||
|
||
BorderBrush = window.BorderBrush; | ||
BorderThickness = window.BorderThickness; | ||
} | ||
|
||
private (double factorX, double factorY) GetDpi() | ||
{ | ||
if (PresentationSource.FromVisual(this) is { } source) | ||
return (source.CompositionTarget.TransformToDevice.M11, // Possible null reference | ||
source.CompositionTarget.TransformToDevice.M22); | ||
|
||
var systemDPi = DpiHelper.GetSystemDpi(); | ||
|
||
return (systemDPi.DpiScaleX, systemDPi.DpiScaleY); | ||
} | ||
} |
Oops, something went wrong.