WIn_RustTools/RustTools/Helpers/TitleBarHelper.cs

96 lines
3.3 KiB
C#
Raw Normal View History

2024-07-14 11:24:10 +00:00
using System.Runtime.InteropServices;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Windows.UI;
using Windows.UI.ViewManagement;
namespace RustTools.Helpers;
// Helper class to workaround custom title bar bugs.
// DISCLAIMER: The resource key names and color values used below are subject to change. Do not depend on them.
// https://github.com/microsoft/TemplateStudio/issues/4516
internal class TitleBarHelper
{
private const int WAINACTIVE = 0x00;
private const int WAACTIVE = 0x01;
private const int WMACTIVATE = 0x0006;
[DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);
public static void UpdateTitleBar(ElementTheme theme)
{
if (App.MainWindow.ExtendsContentIntoTitleBar)
{
if (theme == ElementTheme.Default)
{
var uiSettings = new UISettings();
var background = uiSettings.GetColorValue(UIColorType.Background);
theme = background == Colors.White ? ElementTheme.Light : ElementTheme.Dark;
}
if (theme == ElementTheme.Default)
{
theme = Application.Current.RequestedTheme == ApplicationTheme.Light ? ElementTheme.Light : ElementTheme.Dark;
}
App.MainWindow.AppWindow.TitleBar.ButtonForegroundColor = theme switch
{
ElementTheme.Dark => Colors.White,
ElementTheme.Light => Colors.Black,
_ => Colors.Transparent
};
App.MainWindow.AppWindow.TitleBar.ButtonHoverForegroundColor = theme switch
{
ElementTheme.Dark => Colors.White,
ElementTheme.Light => Colors.Black,
_ => Colors.Transparent
};
App.MainWindow.AppWindow.TitleBar.ButtonHoverBackgroundColor = theme switch
{
ElementTheme.Dark => Color.FromArgb(0x33, 0xFF, 0xFF, 0xFF),
ElementTheme.Light => Color.FromArgb(0x33, 0x00, 0x00, 0x00),
_ => Colors.Transparent
};
App.MainWindow.AppWindow.TitleBar.ButtonPressedBackgroundColor = theme switch
{
ElementTheme.Dark => Color.FromArgb(0x66, 0xFF, 0xFF, 0xFF),
ElementTheme.Light => Color.FromArgb(0x66, 0x00, 0x00, 0x00),
_ => Colors.Transparent
};
App.MainWindow.AppWindow.TitleBar.BackgroundColor = Colors.Transparent;
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(App.MainWindow);
if (hwnd == GetActiveWindow())
{
SendMessage(hwnd, WMACTIVATE, WAINACTIVE, IntPtr.Zero);
SendMessage(hwnd, WMACTIVATE, WAACTIVE, IntPtr.Zero);
}
else
{
SendMessage(hwnd, WMACTIVATE, WAACTIVE, IntPtr.Zero);
SendMessage(hwnd, WMACTIVATE, WAINACTIVE, IntPtr.Zero);
}
}
}
public static void ApplySystemThemeToCaptionButtons()
{
var frame = App.AppTitlebar as FrameworkElement;
if (frame != null)
{
UpdateTitleBar(frame.ActualTheme);
}
}
}