Revert "Removes workaround theming code for WinUI 3 custom titlebar" => solving title bar problem when changing theme (#4638)

Reverts back removal of titlebar workaround because it was causing bugs for some users in Windows 10.
This commit is contained in:
Humberto Molina 2023-05-06 11:06:39 -06:00 коммит произвёл GitHub
Родитель 9a499c4fd6
Коммит ec70cfb66f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 110 добавлений и 3 удалений

Просмотреть файл

@ -265,6 +265,9 @@
<Content Include="Templates\Ft\AppNotifications\Param_ProjectName\Services\AppNotificationService.cs">
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<Content Include="Templates\Proj\Default\Param_ProjectName\Helpers\TitleBarHelper.cs">
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<Content Include="Templates\_comp\MT\Project.MenuBar.AddMenuItem.Settings\Views\ShellPage_postaction.xaml.cs">
<IncludeInVSIX>true</IncludeInVSIX>
</Content>

Просмотреть файл

@ -37,6 +37,8 @@ public class ThemeSelectorService : IThemeSelectorService
if (App.MainWindow.Content is FrameworkElement rootElement)
{
rootElement.RequestedTheme = Theme;
TitleBarHelper.UpdateTitleBar(Theme);
}
await Task.CompletedTask;

Просмотреть файл

@ -10,8 +10,6 @@
<ResourceDictionary Source="/Styles/Thickness.xaml" />
<ResourceDictionary Source="/Styles/TextBlock.xaml" />
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="WindowCaptionBackground">Transparent</SolidColorBrush>
<SolidColorBrush x:Key="WindowCaptionBackgroundDisabled">Transparent</SolidColorBrush>
</ResourceDictionary>
</Application.Resources>
</Application>

Просмотреть файл

@ -0,0 +1,100 @@
using System.Runtime.InteropServices;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using Windows.UI;
using Windows.UI.ViewManagement;
namespace Param_RootNamespace.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;
}
Application.Current.Resources["WindowCaptionForeground"] = theme switch
{
ElementTheme.Dark => new SolidColorBrush(Colors.White),
ElementTheme.Light => new SolidColorBrush(Colors.Black),
_ => new SolidColorBrush(Colors.Transparent)
};
Application.Current.Resources["WindowCaptionForegroundDisabled"] = theme switch
{
ElementTheme.Dark => new SolidColorBrush(Color.FromArgb(0x66, 0xFF, 0xFF, 0xFF)),
ElementTheme.Light => new SolidColorBrush(Color.FromArgb(0x66, 0x00, 0x00, 0x00)),
_ => new SolidColorBrush(Colors.Transparent)
};
Application.Current.Resources["WindowCaptionButtonBackgroundPointerOver"] = theme switch
{
ElementTheme.Dark => new SolidColorBrush(Color.FromArgb(0x33, 0xFF, 0xFF, 0xFF)),
ElementTheme.Light => new SolidColorBrush(Color.FromArgb(0x33, 0x00, 0x00, 0x00)),
_ => new SolidColorBrush(Colors.Transparent)
};
Application.Current.Resources["WindowCaptionButtonBackgroundPressed"] = theme switch
{
ElementTheme.Dark => new SolidColorBrush(Color.FromArgb(0x66, 0xFF, 0xFF, 0xFF)),
ElementTheme.Light => new SolidColorBrush(Color.FromArgb(0x66, 0x00, 0x00, 0x00)),
_ => new SolidColorBrush(Colors.Transparent)
};
Application.Current.Resources["WindowCaptionButtonStrokePointerOver"] = theme switch
{
ElementTheme.Dark => new SolidColorBrush(Colors.White),
ElementTheme.Light => new SolidColorBrush(Colors.Black),
_ => new SolidColorBrush(Colors.Transparent)
};
Application.Current.Resources["WindowCaptionButtonStrokePressed"] = theme switch
{
ElementTheme.Dark => new SolidColorBrush(Colors.White),
ElementTheme.Light => new SolidColorBrush(Colors.Black),
_ => new SolidColorBrush(Colors.Transparent)
};
Application.Current.Resources["WindowCaptionBackground"] = new SolidColorBrush(Colors.Transparent);
Application.Current.Resources["WindowCaptionBackgroundDisabled"] = new SolidColorBrush(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);
}
}
}
}

Просмотреть файл

@ -34,6 +34,8 @@ public sealed partial class ShellPage : Page
private void OnLoaded(object sender, RoutedEventArgs e)
{
TitleBarHelper.UpdateTitleBar(RequestedTheme);
KeyboardAccelerators.Add(BuildKeyboardAccelerator(VirtualKey.Left, VirtualKeyModifiers.Menu));
KeyboardAccelerators.Add(BuildKeyboardAccelerator(VirtualKey.GoBack));
}

Просмотреть файл

@ -36,6 +36,8 @@ public sealed partial class ShellPage : Page
private void OnLoaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
TitleBarHelper.UpdateTitleBar(RequestedTheme);
KeyboardAccelerators.Add(BuildKeyboardAccelerator(VirtualKey.Left, VirtualKeyModifiers.Menu));
KeyboardAccelerators.Add(BuildKeyboardAccelerator(VirtualKey.GoBack));
}