diff --git a/README.md b/README.md index 47878dd3f..a74264c1e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Template Studio -Template Studio is a suite of Visual Studio 2022 extensions that accelerate the creation of new WinUI 3, WPF, and UWP apps using a wizard-based experience. +Template Studio is a suite of Visual Studio 2022 extensions that accelerate the creation of new WinUI 3, WPF, and UWP apps using a wizard-based experience. * [Template Studio for WinUI (C#)](https://marketplace.visualstudio.com/items?itemName=TemplateStudio.TemplateStudioForWinUICs) * Template Studio for WinUI (C++) (FUTURE) diff --git a/code/TemplateStudioForWinUICs/TemplateStudioForWinUICs.csproj b/code/TemplateStudioForWinUICs/TemplateStudioForWinUICs.csproj index 7f3d6ecda..e122cea1f 100644 --- a/code/TemplateStudioForWinUICs/TemplateStudioForWinUICs.csproj +++ b/code/TemplateStudioForWinUICs/TemplateStudioForWinUICs.csproj @@ -265,6 +265,9 @@ true + + true + true diff --git a/code/TemplateStudioForWinUICs/Templates/Ft/ThemeSelection/Services/ThemeSelectorService.cs b/code/TemplateStudioForWinUICs/Templates/Ft/ThemeSelection/Services/ThemeSelectorService.cs index c078bb507..f86800154 100644 --- a/code/TemplateStudioForWinUICs/Templates/Ft/ThemeSelection/Services/ThemeSelectorService.cs +++ b/code/TemplateStudioForWinUICs/Templates/Ft/ThemeSelection/Services/ThemeSelectorService.cs @@ -37,6 +37,8 @@ public class ThemeSelectorService : IThemeSelectorService if (App.MainWindow.Content is FrameworkElement rootElement) { rootElement.RequestedTheme = Theme; + + TitleBarHelper.UpdateTitleBar(Theme); } await Task.CompletedTask; diff --git a/code/TemplateStudioForWinUICs/Templates/Proj/Default/Param_ProjectName/App.xaml b/code/TemplateStudioForWinUICs/Templates/Proj/Default/Param_ProjectName/App.xaml index bd6ff0db2..2b95a284a 100644 --- a/code/TemplateStudioForWinUICs/Templates/Proj/Default/Param_ProjectName/App.xaml +++ b/code/TemplateStudioForWinUICs/Templates/Proj/Default/Param_ProjectName/App.xaml @@ -10,8 +10,6 @@ - Transparent - Transparent diff --git a/code/TemplateStudioForWinUICs/Templates/Proj/Default/Param_ProjectName/Helpers/TitleBarHelper.cs b/code/TemplateStudioForWinUICs/Templates/Proj/Default/Param_ProjectName/Helpers/TitleBarHelper.cs new file mode 100644 index 000000000..fe25386b3 --- /dev/null +++ b/code/TemplateStudioForWinUICs/Templates/Proj/Default/Param_ProjectName/Helpers/TitleBarHelper.cs @@ -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); + } + } + } +} diff --git a/code/TemplateStudioForWinUICs/Templates/_comp/MT/Project.MenuBar/Views/ShellPage.xaml.cs b/code/TemplateStudioForWinUICs/Templates/_comp/MT/Project.MenuBar/Views/ShellPage.xaml.cs index 3d16d9374..af3de7001 100644 --- a/code/TemplateStudioForWinUICs/Templates/_comp/MT/Project.MenuBar/Views/ShellPage.xaml.cs +++ b/code/TemplateStudioForWinUICs/Templates/_comp/MT/Project.MenuBar/Views/ShellPage.xaml.cs @@ -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)); } diff --git a/code/TemplateStudioForWinUICs/Templates/_comp/MT/Project.NavView/Views/ShellPage.xaml.cs b/code/TemplateStudioForWinUICs/Templates/_comp/MT/Project.NavView/Views/ShellPage.xaml.cs index 950cb7ce8..1cc3f1351 100644 --- a/code/TemplateStudioForWinUICs/Templates/_comp/MT/Project.NavView/Views/ShellPage.xaml.cs +++ b/code/TemplateStudioForWinUICs/Templates/_comp/MT/Project.NavView/Views/ShellPage.xaml.cs @@ -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)); }