From 15113cce752083081333a1358c93498b3d2de8e3 Mon Sep 17 00:00:00 2001 From: Mgg sk Date: Thu, 12 Dec 2024 20:22:24 +0100 Subject: [PATCH 01/22] Improved the Backdrop sample --- WinUIGallery/Common/Win32.cs | 21 +- .../ControlPages/SystemBackdropsPage.xaml | 51 +++-- .../ControlPages/SystemBackdropsPage.xaml.cs | 46 +++-- .../SystemBackdropsSample1.txt | 61 +++--- .../SystemBackdropsSample2.txt | 61 +++--- .../SystemBackdropsSample3.txt | 11 +- .../SystemBackdropsSample4.txt | 9 +- WinUIGallery/DataModel/ControlInfoData.json | 21 +- WinUIGallery/Helper/WindowHelper.cs | 27 ++- .../SampleBuiltInSystemBackdropsWindow.xaml | 22 +-- ...SampleBuiltInSystemBackdropsWindow.xaml.cs | 70 ++++--- .../SampleSystemBackdropsWindow.xaml | 22 +-- .../SampleSystemBackdropsWindow.xaml.cs | 180 ++++++++++-------- 13 files changed, 355 insertions(+), 247 deletions(-) diff --git a/WinUIGallery/Common/Win32.cs b/WinUIGallery/Common/Win32.cs index 526ab63a5..81e406ba4 100644 --- a/WinUIGallery/Common/Win32.cs +++ b/WinUIGallery/Common/Win32.cs @@ -39,6 +39,9 @@ internal static class Win32 [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam); + [DllImport("DwmApi.dll")] + public static extern int DwmSetWindowAttribute(nint hWnd, DwmWindowAttribute attribute, ref int value, int size); + public static int SetWindowKeyHook(HookProc hookProc) { @@ -57,11 +60,12 @@ public static bool IsKeyDownHook(IntPtr lWord) public const int WA_INACTIVE = 0x00; public const int WH_KEYBOARD = 2; public const int WM_KEYDOWN = 0x0104; - public const int WM_SETICON = 0x0080; + public const int ICON_SMALL = 0; public const int ICON_BIG = 1; + internal delegate IntPtr WinProc(IntPtr hWnd, WindowMessage Msg, IntPtr wParam, IntPtr lParam); public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam); @@ -75,5 +79,20 @@ internal enum WindowMessage : int { WM_GETMINMAXINFO = 0x0024, } + + internal enum DwmWindowAttribute : int + { + DWMWA_USE_IMMERSIVE_DARK_MODE = 20, //Dark mode + DWMWA_SYSTEMBACKDROP_TYPE = 38 //Title bar backdrop + } + + internal enum DwmSystemBackdropType : int + { + DWMSBT_AUTO = 0, //Default + DWMSBT_NONE = 1, //None + DWMSBT_MAINWINDOW = 2, //Mica + DWMSBT_TRANSIENTWINDOW = 3, //Acrylic thin + DWMSBT_TABBEDWINDOW = 4, //Mica Alt + } } } diff --git a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml index d95b7bc01..6119526c8 100644 --- a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml +++ b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml @@ -19,25 +19,40 @@ mc:Ignorable="d"> - Backdrop types: + -// Option 1 - implement Mica with Xaml. +// Option 1 - Implement Mica with Xaml. <Window.SystemBackdrop> <MicaBackdrop /> </Window.SystemBackdrop> + +//Option 1.5 Implement Mica Alt with Xaml. +<Window.SystemBackdrop> + <MicaBackdrop Kind="BaseAlt" /> +</Window.SystemBackdrop> - - - - - + + + Inheritance: Object -> DependencyObject -> SystemBackdrop + + Mica is an opaque, dynamic material that incorporates theme and desktop wallpaper to paint the background of windows. + You can apply Mica as your application backdrop to create a visual hierarchy, aiding productivity, by increasing clarity about which window is in focus. + Mica is specifically designed for app performance as it only samples the desktop wallpaper once to create its visualization. + Mica is available for Windows 11 build 22000 or later. + + Mica Alt is a variant of Mica, with stronger tinting of the user's desktop background color. + You can apply Mica Alt as your app's backdrop to provide a deeper visual hierarchy than Mica, especially when creating an app with a tabbed title bar. + Mica Alt is available for Windows 11 build 22000 or later. + + + - @@ -47,7 +62,21 @@ </Window.SystemBackdrop> - + + + Inheritance: Object -> DependencyObject -> SystemBackdrop + + Acrylic is a semi-transparent material that replicates the effect of frosted glass. + It's used only for transient, light-dismiss surfaces such as flyouts and context menus. + + + + + + Backdrop controllers: + + Create Window with customizable Desktop Acrylic - diff --git a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs index 4c66fe210..440417e17 100644 --- a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs +++ b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs @@ -7,13 +7,9 @@ // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //********************************************************* -using WinUIGallery.Helper; -using System; -using Microsoft.UI; -using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; -using Microsoft.UI.Xaml.Media; +using WinUIGallery.SamplePages; namespace WinUIGallery.ControlPages { @@ -21,34 +17,46 @@ public sealed partial class SystemBackdropsPage : Page { public SystemBackdropsPage() { - this.InitializeComponent(); + InitializeComponent(); } private void createBuiltInMicaWindow_Click(object sender, RoutedEventArgs e) { - var newWindow = new WinUIGallery.SamplePages.SampleBuiltInSystemBackdropsWindow(); - newWindow.Activate(); + var micaWindow = new SampleBuiltInSystemBackdropsWindow(); + micaWindow.AllowedBackdrops = [SampleBuiltInSystemBackdropsWindow.BackdropType.None, + SampleBuiltInSystemBackdropsWindow.BackdropType.Mica, + SampleBuiltInSystemBackdropsWindow.BackdropType.MicaAlt]; + micaWindow.SetBackdrop(SampleBuiltInSystemBackdropsWindow.BackdropType.Mica); + micaWindow.Activate(); } - private void createCustomMicaWindow_Click(object sender, RoutedEventArgs e) + private void createBuiltInAcrylicWindow_Click(object sender, RoutedEventArgs e) { - var newWindow = new WinUIGallery.SamplePages.SampleSystemBackdropsWindow(); - newWindow.SetBackdrop(WinUIGallery.SamplePages.SampleSystemBackdropsWindow.BackdropType.Mica); - newWindow.Activate(); + var acrylicWindow = new SampleBuiltInSystemBackdropsWindow(); + acrylicWindow.AllowedBackdrops = [SampleBuiltInSystemBackdropsWindow.BackdropType.None, + SampleBuiltInSystemBackdropsWindow.BackdropType.Acrylic]; + acrylicWindow.SetBackdrop(SampleBuiltInSystemBackdropsWindow.BackdropType.Acrylic); + acrylicWindow.Activate(); } - private void createBuiltInAcrylicWindow_Click(object sender, RoutedEventArgs e) + private void createCustomMicaWindow_Click(object sender, RoutedEventArgs e) { - var newWindow = new WinUIGallery.SamplePages.SampleSystemBackdropsWindow(); - newWindow.SetBackdrop(WinUIGallery.SamplePages.SampleSystemBackdropsWindow.BackdropType.DesktopAcrylicBase); - newWindow.Activate(); + var micaWindow = new SampleSystemBackdropsWindow(); + micaWindow.AllowedBackdrops = [SampleSystemBackdropsWindow.BackdropType.None, + SampleSystemBackdropsWindow.BackdropType.Mica, + SampleSystemBackdropsWindow.BackdropType.MicaAlt]; + micaWindow.SetBackdrop(SampleSystemBackdropsWindow.BackdropType.Mica); + micaWindow.Activate(); } private void createCustomDesktopAcrylicWindow_Click(object sender, RoutedEventArgs e) { - var newWindow = new WinUIGallery.SamplePages.SampleSystemBackdropsWindow(); - newWindow.SetBackdrop(WinUIGallery.SamplePages.SampleSystemBackdropsWindow.BackdropType.DesktopAcrylicBase); - newWindow.Activate(); + var acrylicWindow = new SampleSystemBackdropsWindow(); + acrylicWindow.AllowedBackdrops = [SampleSystemBackdropsWindow.BackdropType.None, + SampleSystemBackdropsWindow.BackdropType.Acrylic, + SampleSystemBackdropsWindow.BackdropType.AcrylicThin]; + acrylicWindow.SetBackdrop(SampleSystemBackdropsWindow.BackdropType.Acrylic); + acrylicWindow.Activate(); } } } diff --git a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample1.txt b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample1.txt index 5606bd3b5..f9f4aa60c 100644 --- a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample1.txt +++ b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample1.txt @@ -1,35 +1,35 @@ -using System.Runtime.InteropServices; // For DllImport -using WinRT; // required to support Window.As() +using System.Runtime.InteropServices; +using WinRT; +using Microsoft.UI.Composition; +using Microsoft.UI.Composition.SystemBackdrops; -WindowsSystemDispatcherQueueHelper m_wsdqHelper; // See separate sample below for implementation -Microsoft.UI.Composition.SystemBackdrops.MicaController m_micaController; -Microsoft.UI.Composition.SystemBackdrops.SystemBackdropConfiguration m_configurationSource; +WindowsSystemDispatcherQueueHelper wsdqHelper; // See the helper class sample for the implementation +MicaController micaController; +SystemBackdropConfiguration configurationSource; bool TrySetMicaBackdrop(bool useMicaAlt) { - if (Microsoft.UI.Composition.SystemBackdrops.MicaController.IsSupported()) + if (MicaController.IsSupported()) { - m_wsdqHelper = new WindowsSystemDispatcherQueueHelper(); - m_wsdqHelper.EnsureWindowsSystemDispatcherQueueController(); + wsdqHelper = new WindowsSystemDispatcherQueueHelper(); + wsdqHelper.EnsureWindowsSystemDispatcherQueueController(); // Hooking up the policy object - m_configurationSource = new Microsoft.UI.Composition.SystemBackdrops.SystemBackdropConfiguration(); - this.Activated += Window_Activated; - this.Closed += Window_Closed; - ((FrameworkElement)this.Content).ActualThemeChanged += Window_ThemeChanged; + configurationSource = new SystemBackdropConfiguration(); + Activated += Window_Activated; + Closed += Window_Closed; + ((FrameworkElement)Content).ActualThemeChanged += Window_ThemeChanged; // Initial configuration state. - m_configurationSource.IsInputActive = true; + configurationSource.IsInputActive = true; SetConfigurationSourceTheme(); - m_micaController = new Microsoft.UI.Composition.SystemBackdrops.MicaController(); - - m_micaController.Kind = useMicaAlt ? Microsoft.UI.Composition.SystemBackdrops.MicaKind.BaseAlt : Microsoft.UI.Composition.SystemBackdrops.MicaKind.Base; + micaController = new MicaController(); + micaController.Kind = useMicaAlt ? MicaKind.BaseAlt : MicaKind.Base; // Enable the system backdrop. - // Note: Be sure to have "using WinRT;" to support the Window.As<...>() call. - m_micaController.AddSystemBackdropTarget(this.As()); - m_micaController.SetSystemBackdropConfiguration(m_configurationSource); + micaController.AddSystemBackdropTarget(this.As()); + micaController.SetSystemBackdropConfiguration(configurationSource); return true; // Succeeded. } @@ -38,25 +38,24 @@ bool TrySetMicaBackdrop(bool useMicaAlt) private void Window_Activated(object sender, WindowActivatedEventArgs args) { - m_configurationSource.IsInputActive = args.WindowActivationState != WindowActivationState.Deactivated; + configurationSource.IsInputActive = args.WindowActivationState != WindowActivationState.Deactivated; } private void Window_Closed(object sender, WindowEventArgs args) { - // Make sure any Mica/Acrylic controller is disposed so it doesn't try to - // use this closed window. - if (m_micaController != null) + // Make sure any Mica/Acrylic controller is disposed + if (micaController != null) { - m_micaController.Dispose(); - m_micaController = null; + micaController.Dispose(); + micaController = null; } this.Activated -= Window_Activated; - m_configurationSource = null; + configurationSource = null; } private void Window_ThemeChanged(FrameworkElement sender, object args) { - if (m_configurationSource != null) + if (configurationSource != null) { SetConfigurationSourceTheme(); } @@ -64,10 +63,10 @@ private void Window_ThemeChanged(FrameworkElement sender, object args) private void SetConfigurationSourceTheme() { - switch (((FrameworkElement)this.Content).ActualTheme) + switch (((FrameworkElement)Content).ActualTheme) { - case ElementTheme.Dark: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Dark; break; - case ElementTheme.Light: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Light; break; - case ElementTheme.Default: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Default; break; + case ElementTheme.Dark: configurationSource.Theme = SystemBackdropTheme.Dark; break; + case ElementTheme.Light: configurationSource.Theme = SystemBackdropTheme.Light; break; + case ElementTheme.Default: configurationSource.Theme = SystemBackdropTheme.Default; break; } } diff --git a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample2.txt b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample2.txt index f47c55ac2..34622844e 100644 --- a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample2.txt +++ b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample2.txt @@ -1,35 +1,35 @@ -using System.Runtime.InteropServices; // For DllImport -using WinRT; // required to support Window.As() +using System.Runtime.InteropServices; +using WinRT; +using Microsoft.UI.Composition; +using Microsoft.UI.Composition.SystemBackdrops; -WindowsSystemDispatcherQueueHelper m_wsdqHelper; // See separate sample below for implementation -Microsoft.UI.Composition.SystemBackdrops.DesktopAcrylicController m_acrylicController; -Microsoft.UI.Composition.SystemBackdrops.SystemBackdropConfiguration m_configurationSource; +WindowsSystemDispatcherQueueHelper wsdqHelper; // See the helper class sample for the implementation +SystemBackdrops.DesktopAcrylicController acrylicController; +SystemBackdrops.SystemBackdropConfiguration configurationSource; bool TrySetAcrylicBackdrop(bool useAcrylicThin) { - if (Microsoft.UI.Composition.SystemBackdrops.DesktopAcrylicController.IsSupported()) + if (DesktopAcrylicController.IsSupported()) { - m_wsdqHelper = new WindowsSystemDispatcherQueueHelper(); - m_wsdqHelper.EnsureWindowsSystemDispatcherQueueController(); + wsdqHelper = new WindowsSystemDispatcherQueueHelper(); + wsdqHelper.EnsureWindowsSystemDispatcherQueueController(); // Hooking up the policy object - m_configurationSource = new Microsoft.UI.Composition.SystemBackdrops.SystemBackdropConfiguration(); - this.Activated += Window_Activated; - this.Closed += Window_Closed; - ((FrameworkElement)this.Content).ActualThemeChanged += Window_ThemeChanged; + configurationSource = new SystemBackdropConfiguration(); + Activated += Window_Activated; + Closed += Window_Closed; + ((FrameworkElement)Content).ActualThemeChanged += Window_ThemeChanged; // Initial configuration state. - m_configurationSource.IsInputActive = true; + configurationSource.IsInputActive = true; SetConfigurationSourceTheme(); - m_acrylicController = new Microsoft.UI.Composition.SystemBackdrops.DesktopAcrylicController(); - - m_acrylicController.Kind = useAcrylicThin ? Microsoft.UI.Composition.SystemBackdrops.DesktopAcrylicKind.Thin : Microsoft.UI.Composition.SystemBackdrops.DesktopAcrylicKind.Base; + acrylicController = new DesktopAcrylicController(); + acrylicController.Kind = useAcrylicThin ? DesktopAcrylicKind.Thin : DesktopAcrylicKind.Base; // Enable the system backdrop. - // Note: Be sure to have "using WinRT;" to support the Window.As<...>() call. - m_acrylicController.AddSystemBackdropTarget(this.As()); - m_acrylicController.SetSystemBackdropConfiguration(m_configurationSource); + acrylicController.AddSystemBackdropTarget(As()); + acrylicController.SetSystemBackdropConfiguration(configurationSource); return true; // Succeeded. } @@ -38,25 +38,24 @@ bool TrySetAcrylicBackdrop(bool useAcrylicThin) private void Window_Activated(object sender, WindowActivatedEventArgs args) { - m_configurationSource.IsInputActive = args.WindowActivationState != WindowActivationState.Deactivated; + configurationSource.IsInputActive = args.WindowActivationState != WindowActivationState.Deactivated; } private void Window_Closed(object sender, WindowEventArgs args) { - // Make sure any Mica/Acrylic controller is disposed so it doesn't try to - // use this closed window. - if (m_acrylicController != null) + // Make sure any Mica/Acrylic controller is disposed + if (acrylicController != null) { - m_acrylicController.Dispose(); - m_acrylicController = null; + acrylicController.Dispose(); + acrylicController = null; } - this.Activated -= Window_Activated; - m_configurationSource = null; + Activated -= Window_Activated; + configurationSource = null; } private void Window_ThemeChanged(FrameworkElement sender, object args) { - if (m_configurationSource != null) + if (configurationSource != null) { SetConfigurationSourceTheme(); } @@ -66,8 +65,8 @@ private void SetConfigurationSourceTheme() { switch (((FrameworkElement)this.Content).ActualTheme) { - case ElementTheme.Dark: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Dark; break; - case ElementTheme.Light: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Light; break; - case ElementTheme.Default: m_configurationSource.Theme = Microsoft.UI.Composition.SystemBackdrops.SystemBackdropTheme.Default; break; + case ElementTheme.Dark: configurationSource.Theme = SystemBackdropTheme.Dark; break; + case ElementTheme.Light: configurationSource.Theme = SystemBackdropTheme.Light; break; + case ElementTheme.Default: configurationSource.Theme = SystemBackdropTheme.Default; break; } } diff --git a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample3.txt b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample3.txt index ccfa98a90..3df5c0539 100644 --- a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample3.txt +++ b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample3.txt @@ -1,12 +1,11 @@ -// Option 2 - Implement Mica with codebehind. -// Allows for toggling backdrops as shown in sample. +// Option 2 - Implement Mica with c#. bool TrySetMicaBackdrop(bool useMicaAlt) { - if (Microsoft.UI.Composition.SystemBackdrops.MicaController.IsSupported()) + if (SystemBackdrops.MicaController.IsSupported()) { - Microsoft.UI.Xaml.Media.MicaBackdrop micaBackdrop = new Microsoft.UI.Xaml.Media.MicaBackdrop(); - micaBackdrop.Kind = useMicaAlt ? Microsoft.UI.Composition.SystemBackdrops.MicaKind.BaseAlt : Microsoft.UI.Composition.SystemBackdrops.MicaKind.Base; - this.SystemBackdrop = micaBackdrop; + MicaBackdrop micaBackdrop = new MicaBackdrop(); + micaBackdrop.Kind = useMicaAlt ? MicaKind.BaseAlt : MicaKind.Base; + SystemBackdrop = micaBackdrop; return true; // Succeeded. } diff --git a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample4.txt b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample4.txt index 2b1d40d9b..a27ac9ced 100644 --- a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample4.txt +++ b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample4.txt @@ -1,11 +1,10 @@ -// Option 2 - Implement Acrylic with codebehind. -// Allows for toggling backdrops as shown in sample. +// Option 2 - Implement Acrylic with c#. bool TrySetDesktopAcrylicBackdrop() { - if (Microsoft.UI.Composition.SystemBackdrops.DesktopAcrylicController.IsSupported()) + if (DesktopAcrylicController.IsSupported()) { - Microsoft.UI.Xaml.Media.DesktopAcrylicBackdrop DesktopAcrylicBackdrop = new Microsoft.UI.Xaml.Media.DesktopAcrylicBackdrop(); - this.SystemBackdrop = DesktopAcrylicBackdrop; + DesktopAcrylicBackdrop DesktopAcrylicBackdrop = new DesktopAcrylicBackdrop(); + SystemBackdrop = DesktopAcrylicBackdrop; return true; // Succeeded. } diff --git a/WinUIGallery/DataModel/ControlInfoData.json b/WinUIGallery/DataModel/ControlInfoData.json index 0e347fcd3..b9abb6ad9 100644 --- a/WinUIGallery/DataModel/ControlInfoData.json +++ b/WinUIGallery/DataModel/ControlInfoData.json @@ -2900,14 +2900,31 @@ }, { "UniqueId": "SystemBackdrops", - "Title": "System Backdrops (Mica/Acrylic)", + "Title": "SystemBackdrop", "ApiNamespace": "Microsoft.UI.Composition.SystemBackdrops", "Subtitle": "System backdrops, like Mica and Acrylic, for the window.", "ImagePath": "ms-appx:///Assets/ControlImages/Acrylic.png", - "Description": "System backdrops are used to provide a different background for a Window other than the default white or black (depending on Light or Dark theme). Mica and Desktop Acrylic are the current supported backdrops. There are two options for applying a backdrop: first, to use built-in Mica/Acrylic types, which have no customization and are simple to apply. The second is to create a customizable backdrop, which requires more code.", + "Description": "System backdrops provide a transparency effect for the window background. Mica and Acrylic are the current supported backdrops. There are two options for applying a backdrop: the backdrop types (little customization, but easy to use) and the backdrop controllers (more customization, but harder to use)", "Content": "

Look at the SampleSystemBackdropsWindow.xaml.cs file in Visual Studio to see the full code for applying a backdrop.

", "SourcePath": "/Materials", + "IsUpdated": true, + "BaseClasses": [ + "Object", + "DependencyObject" + ], "Docs": [ + { + "Title": "SystemBackdrop - API", + "Uri": "https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.systembackdrop?view=windows-app-sdk-1.6" + }, + { + "Title": "MicaBackdrop - API", + "Uri": "https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.micabackdrop?view=windows-app-sdk-1.6" + }, + { + "Title": "DesktopAcrylicBackdrop - API", + "Uri": "https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.desktopacrylicbackdrop?view=windows-app-sdk-1.6" + }, { "Title": "MicaController - API", "Uri": "https://learn.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.composition.systembackdrops.micacontroller" diff --git a/WinUIGallery/Helper/WindowHelper.cs b/WinUIGallery/Helper/WindowHelper.cs index 7b1a51ff1..46ec8a6a2 100644 --- a/WinUIGallery/Helper/WindowHelper.cs +++ b/WinUIGallery/Helper/WindowHelper.cs @@ -9,6 +9,7 @@ //********************************************************* using Microsoft.UI; +using Microsoft.UI.Composition.SystemBackdrops; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Media; @@ -16,7 +17,9 @@ using System.Collections.Generic; using System.Threading.Tasks; using Windows.Storage; +using WinRT; using WinRT.Interop; +using static WinUIGallery.Win32; namespace WinUIGallery.Helper { @@ -25,7 +28,7 @@ namespace WinUIGallery.Helper // of all active Windows. The app code must call WindowHelper.CreateWindow // rather than "new Window" so we can keep track of all the relevant // windows. In the future, we would like to support this in platform APIs. - public class WindowHelper + public static class WindowHelper { static public Window CreateWindow() { @@ -99,5 +102,27 @@ static public StorageFolder GetAppLocalFolder() } return localFolder; } + + public static void SetTitleBarBackdrop(this Window window, SystemBackdrop backdrop) + { + var backdropType = DwmSystemBackdropType.DWMSBT_AUTO; + + if (backdrop is MicaBackdrop micaBackdrop) + backdropType = micaBackdrop.Kind == MicaKind.Base ? DwmSystemBackdropType.DWMSBT_MAINWINDOW : DwmSystemBackdropType.DWMSBT_TABBEDWINDOW; + else if (backdrop is DesktopAcrylicBackdrop) + backdropType = DwmSystemBackdropType.DWMSBT_TRANSIENTWINDOW; + else if (backdrop is null) + backdropType = DwmSystemBackdropType.DWMSBT_NONE; + + var backdropTypeInt = ((int)backdropType); + int hResult = DwmSetWindowAttribute(WindowNative.GetWindowHandle(window), DwmWindowAttribute.DWMWA_SYSTEMBACKDROP_TYPE, ref backdropTypeInt, sizeof(DwmSystemBackdropType)); + ExceptionHelpers.ThrowExceptionForHR(hResult); + } + + public static void SetTitleBarTheme(this Window window) + { + int darkMode = ((FrameworkElement)window.Content).ActualTheme == ElementTheme.Dark ? 1 : 0; + DwmSetWindowAttribute(WindowNative.GetWindowHandle(window), DwmWindowAttribute.DWMWA_USE_IMMERSIVE_DARK_MODE, ref darkMode, sizeof(int)); + } } } diff --git a/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml b/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml index 78817cdf2..9e0d8a265 100644 --- a/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml +++ b/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml @@ -4,29 +4,23 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - mc:Ignorable="d"> + mc:Ignorable="d" + Title="SystemBackdrop sample window"> - - - - - - - - + - - @@ -84,9 +86,16 @@
- Helpers: + + + + + + - +
diff --git a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsTitleBarBackdrops.txt b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsTitleBarBackdrops.txt new file mode 100644 index 000000000..d85ccbbcd --- /dev/null +++ b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsTitleBarBackdrops.txt @@ -0,0 +1,35 @@ +internal enum DwmSystemBackdropType : int +{ + DWMSBT_AUTO = 0, //Default + DWMSBT_NONE = 1, //None + DWMSBT_MAINWINDOW = 2, //Mica + DWMSBT_TRANSIENTWINDOW = 3, //Acrylic thin + DWMSBT_TABBEDWINDOW = 4, //Mica Alt +} + + +internal enum DwmWindowAttribute : int +{ + DWMWA_SYSTEMBACKDROP_TYPE = 38 //Title bar backdrop +} + + +[DllImport("DwmApi.dll")] +public static extern int DwmSetWindowAttribute(nint hWnd, DwmWindowAttribute attribute, ref int value, int size); + + +public static void SetTitleBarBackdrop(this Window window, SystemBackdrop backdrop) +{ + var backdropType = DwmSystemBackdropType.DWMSBT_AUTO; + + if (backdrop is MicaBackdrop micaBackdrop) + backdropType = micaBackdrop.Kind == MicaKind.Base ? DwmSystemBackdropType.DWMSBT_MAINWINDOW : DwmSystemBackdropType.DWMSBT_TABBEDWINDOW; + else if (backdrop is DesktopAcrylicBackdrop) + backdropType = DwmSystemBackdropType.DWMSBT_TRANSIENTWINDOW; + else if (backdrop is null) + backdropType = DwmSystemBackdropType.DWMSBT_NONE; + + var backdropTypeInt = ((int)backdropType); + int hResult = DwmSetWindowAttribute(WindowNative.GetWindowHandle(window), DwmWindowAttribute.DWMWA_SYSTEMBACKDROP_TYPE, ref backdropTypeInt, sizeof(DwmSystemBackdropType)); + ExceptionHelpers.ThrowExceptionForHR(hResult); +} \ No newline at end of file diff --git a/WinUIGallery/Helper/Win32WindowHelper.cs b/WinUIGallery/Helper/Win32WindowHelper.cs index 79e5d2778..03b6f6cb4 100644 --- a/WinUIGallery/Helper/Win32WindowHelper.cs +++ b/WinUIGallery/Helper/Win32WindowHelper.cs @@ -1,6 +1,10 @@ using System; using System.Runtime.InteropServices; +using Microsoft.UI.Composition.SystemBackdrops; using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Media; +using WinRT.Interop; +using WinRT; using static WinUIGallery.Win32; namespace WinUIGallery.Helper @@ -85,4 +89,30 @@ private struct MINMAXINFO public POINT ptMaxTrackSize; } } + + public static class Win32HelperExtensions + { + public static void SetTitleBarBackdrop(this Window window, SystemBackdrop backdrop) + { + var backdropType = DwmSystemBackdropType.DWMSBT_AUTO; + + if (backdrop is MicaBackdrop micaBackdrop) + backdropType = micaBackdrop.Kind == MicaKind.Base ? DwmSystemBackdropType.DWMSBT_MAINWINDOW : DwmSystemBackdropType.DWMSBT_TABBEDWINDOW; + else if (backdrop is DesktopAcrylicBackdrop) + backdropType = DwmSystemBackdropType.DWMSBT_TRANSIENTWINDOW; + else if (backdrop is null) + backdropType = DwmSystemBackdropType.DWMSBT_NONE; + + var backdropTypeInt = (int)backdropType; + int hResult = DwmSetWindowAttribute(WindowNative.GetWindowHandle(window), DwmWindowAttribute.DWMWA_SYSTEMBACKDROP_TYPE, ref backdropTypeInt, sizeof(DwmSystemBackdropType)); + ExceptionHelpers.ThrowExceptionForHR(hResult); + } + + public static void SetTitleBarTheme(this Window window, ElementTheme theme = ElementTheme.Default) + { + int darkMode = (theme == ElementTheme.Default ? ((FrameworkElement)window.Content).ActualTheme : theme) == ElementTheme.Dark ? 1 : 0; + int hResult = DwmSetWindowAttribute(WindowNative.GetWindowHandle(window), DwmWindowAttribute.DWMWA_USE_IMMERSIVE_DARK_MODE, ref darkMode, sizeof(int)); + ExceptionHelpers.ThrowExceptionForHR(hResult); + } + } } diff --git a/WinUIGallery/Helper/WindowHelper.cs b/WinUIGallery/Helper/WindowHelper.cs index 46ec8a6a2..b7c2d6aa2 100644 --- a/WinUIGallery/Helper/WindowHelper.cs +++ b/WinUIGallery/Helper/WindowHelper.cs @@ -19,7 +19,6 @@ using Windows.Storage; using WinRT; using WinRT.Interop; -using static WinUIGallery.Win32; namespace WinUIGallery.Helper { @@ -102,27 +101,5 @@ static public StorageFolder GetAppLocalFolder() } return localFolder; } - - public static void SetTitleBarBackdrop(this Window window, SystemBackdrop backdrop) - { - var backdropType = DwmSystemBackdropType.DWMSBT_AUTO; - - if (backdrop is MicaBackdrop micaBackdrop) - backdropType = micaBackdrop.Kind == MicaKind.Base ? DwmSystemBackdropType.DWMSBT_MAINWINDOW : DwmSystemBackdropType.DWMSBT_TABBEDWINDOW; - else if (backdrop is DesktopAcrylicBackdrop) - backdropType = DwmSystemBackdropType.DWMSBT_TRANSIENTWINDOW; - else if (backdrop is null) - backdropType = DwmSystemBackdropType.DWMSBT_NONE; - - var backdropTypeInt = ((int)backdropType); - int hResult = DwmSetWindowAttribute(WindowNative.GetWindowHandle(window), DwmWindowAttribute.DWMWA_SYSTEMBACKDROP_TYPE, ref backdropTypeInt, sizeof(DwmSystemBackdropType)); - ExceptionHelpers.ThrowExceptionForHR(hResult); - } - - public static void SetTitleBarTheme(this Window window) - { - int darkMode = ((FrameworkElement)window.Content).ActualTheme == ElementTheme.Dark ? 1 : 0; - DwmSetWindowAttribute(WindowNative.GetWindowHandle(window), DwmWindowAttribute.DWMWA_USE_IMMERSIVE_DARK_MODE, ref darkMode, sizeof(int)); - } } } diff --git a/WinUIGallery/Helper/WindowsSystemDispatcherQueueHelper.cs b/WinUIGallery/Helper/WindowsSystemDispatcherQueueHelper.cs new file mode 100644 index 000000000..081f8e8cc --- /dev/null +++ b/WinUIGallery/Helper/WindowsSystemDispatcherQueueHelper.cs @@ -0,0 +1,45 @@ +using System; +using System.Runtime.InteropServices; +using Windows.System; + +namespace WinUIGallery.Helper +{ + class WindowsSystemDispatcherQueueHelper + { + [StructLayout(LayoutKind.Sequential)] + struct DispatcherQueueOptions + { + internal int dwSize; + internal int threadType; + internal int apartmentType; + } + + [DllImport("CoreMessaging.dll")] + private static unsafe extern int CreateDispatcherQueueController(DispatcherQueueOptions options, IntPtr* instance); + + IntPtr m_dispatcherQueueController = IntPtr.Zero; + public void EnsureWindowsSystemDispatcherQueueController() + { + if (DispatcherQueue.GetForCurrentThread() != null) + { + // one already exists, so we'll just use it. + return; + } + + if (m_dispatcherQueueController == IntPtr.Zero) + { + DispatcherQueueOptions options; + options.dwSize = Marshal.SizeOf(typeof(DispatcherQueueOptions)); + options.threadType = 2; // DQTYPE_THREAD_CURRENT + options.apartmentType = 2; // DQTAT_COM_STA + + unsafe + { + IntPtr dispatcherQueueController; + CreateDispatcherQueueController(options, &dispatcherQueueController); + m_dispatcherQueueController = dispatcherQueueController; + } + } + } + } +} diff --git a/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml b/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml index 9e0d8a265..02ee18727 100644 --- a/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml +++ b/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml @@ -22,5 +22,13 @@ Click="ChangeBackdropButton_Click" Content="Change Backdrop" /> + + + + + diff --git a/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml.cs b/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml.cs index 7ffb02bf4..3d6fe6931 100644 --- a/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml.cs +++ b/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml.cs @@ -1,6 +1,7 @@ using System.Linq; using Microsoft.UI.Composition.SystemBackdrops; using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; using WinUIGallery.Helper; @@ -18,6 +19,7 @@ public SampleBuiltInSystemBackdropsWindow() ((FrameworkElement)Content).RequestedTheme = ThemeHelper.RootTheme; this.SetTitleBarTheme(); SetBackdrop(BackdropType.Mica); + ThemeComboBox.SelectedIndex = 0; } public enum BackdropType @@ -133,5 +135,16 @@ void ChangeBackdropButton_Click(object sender, RoutedEventArgs e) SetBackdrop(newType); } + + private void ThemeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + ((FrameworkElement)Content).RequestedTheme = ThemeComboBox.SelectedIndex switch + { + 1 => ElementTheme.Light, + 2 => ElementTheme.Dark, + _ => ElementTheme.Default + }; + this.SetTitleBarTheme(); + } } } diff --git a/WinUIGallery/SamplePages/SampleSystemBackdropsWindow.xaml b/WinUIGallery/SamplePages/SampleSystemBackdropsWindow.xaml index 8626f288d..b4d14328a 100644 --- a/WinUIGallery/SamplePages/SampleSystemBackdropsWindow.xaml +++ b/WinUIGallery/SamplePages/SampleSystemBackdropsWindow.xaml @@ -7,20 +7,32 @@ mc:Ignorable="d" Title="SystemBackdrop sample window"> - + - + - - + + + Inheritance: Object + + Manages rendering and system policy for the mica material. + + + - - + + + Inheritance: Object + + Manages rendering and system policy for the background acrylic material. + + + Helpers: - + Date: Sat, 21 Dec 2024 00:07:16 +0100 Subject: [PATCH 04/22] custom title bar setting and more descriptions --- .../ControlPages/SystemBackdropsPage.xaml | 18 +++++---- .../SampleBuiltInSystemBackdropsWindow.xaml | 25 ++++++------ ...SampleBuiltInSystemBackdropsWindow.xaml.cs | 31 +++++++++++++++ .../SampleSystemBackdropsWindow.xaml | 1 + .../SampleSystemBackdropsWindow.xaml.cs | 38 +++++++++++++++++++ 5 files changed, 95 insertions(+), 18 deletions(-) diff --git a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml index 704f63259..7828eea20 100644 --- a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml +++ b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml @@ -64,11 +64,13 @@ - + Inheritance: Object -> DependencyObject -> SystemBackdrop - Acrylic is a semi-transparent material that replicates the effect of frosted glass. - It's used only for transient, light-dismiss surfaces such as flyouts and context menus. + Acrylic is a semi-transparent material that replicates the effect of frosted glass. It's used only for transient, light-dismiss surfaces such as flyouts and context menus. + Acrylic's most noticeable characteristic is its transparency. There are two acrylic blend types that change what's visible through the material: + Background acrylic reveals the desktop wallpaper and other windows that are behind the currently active app, adding depth between application windows while celebrating the user's personalization preferences. + In-app acrylic adds a sense of depth within the app frame, providing both focus and hierarchy. (Inplemented with a AcrilicBrush in Xaml) @@ -79,10 +81,11 @@ - + Inheritance: Object - Manages rendering and system policy for the mica material. + Manages rendering and system policy for the mica material. The MicaController class provides a very customizable way to apply the Mica material to a app. + This is a list of properties that can be modified: FallbackColor, Kind (Can also be set using the MicaBackdrop class), LuminosityOpacity, State, TintColor and the TintOpacity. @@ -91,10 +94,11 @@ - + Inheritance: Object - Manages rendering and system policy for the background acrylic material. + Manages rendering and system policy for the background acrylic material. Acrylic has the same level of customization as Mica, but the type (Base / Thin) can't be changed using the DesktopAcrylicBackdrop class. + If you wan't to use Acrylic Thin in your app you have to use the DesktopAcrylicController class. There are 2 types of Acrylic: Base and Thin. The DesktopAcrylicBackdrop class uses a version of the Base type with less dimming. diff --git a/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml b/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml index 02ee18727..e6fee42cb 100644 --- a/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml +++ b/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml @@ -7,28 +7,31 @@ mc:Ignorable="d" Title="SystemBackdrop sample window"> - + - + - - - - - - - - - Inheritance: Object > DependencyObject > SystemBackdrop + Mica Alt is available for Windows 11 build 22000 or later. Acrylic is a semi-transparent material that replicates the effect of frosted glass. It's used only for transient, light-dismiss surfaces such as flyouts and context menus. Acrylic's most noticeable characteristic is its transparency. There are two acrylic blend types that change what's visible through the material: Background acrylic reveals the desktop wallpaper and other windows that are behind the currently active app, adding depth between application windows while celebrating the user's personalization preferences. In-app acrylic adds a sense of depth within the app frame, providing both focus and hierarchy. (Inplemented with a AcrilicBrush in Xaml) - + + CSharpSource="SystemBackdrops\SystemBackdropsSample2.txt"> - - Inheritance: Object - + Manages rendering and system policy for the mica material. The MicaController class provides a very customizable way to apply the Mica material to a app. This is a list of properties that can be modified: FallbackColor, Kind (Can also be set using the MicaBackdrop class), LuminosityOpacity, State, TintColor and the TintOpacity. @@ -69,11 +54,9 @@ + CSharpSource="SystemBackdrops\SystemBackdropsSample3.txt"> - - Inheritance: Object - + Manages rendering and system policy for the background acrylic material. Acrylic has the same level of customization as Mica, but the type (Base / Thin) can't be changed using the DesktopAcrylicBackdrop class. If you wan't to use Acrylic Thin in your app you have to use the DesktopAcrylicController class. There are 2 types of Acrylic: Base and Thin. The DesktopAcrylicBackdrop class uses a version of the Base type with less dimming. diff --git a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs index 440417e17..b7a2fb37b 100644 --- a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs +++ b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs @@ -20,42 +20,32 @@ public SystemBackdropsPage() InitializeComponent(); } - private void createBuiltInMicaWindow_Click(object sender, RoutedEventArgs e) + private void createBuiltInWindow_Click(object sender, RoutedEventArgs e) { - var micaWindow = new SampleBuiltInSystemBackdropsWindow(); - micaWindow.AllowedBackdrops = [SampleBuiltInSystemBackdropsWindow.BackdropType.None, - SampleBuiltInSystemBackdropsWindow.BackdropType.Mica, - SampleBuiltInSystemBackdropsWindow.BackdropType.MicaAlt]; - micaWindow.SetBackdrop(SampleBuiltInSystemBackdropsWindow.BackdropType.Mica); - micaWindow.Activate(); - } - - private void createBuiltInAcrylicWindow_Click(object sender, RoutedEventArgs e) - { - var acrylicWindow = new SampleBuiltInSystemBackdropsWindow(); - acrylicWindow.AllowedBackdrops = [SampleBuiltInSystemBackdropsWindow.BackdropType.None, - SampleBuiltInSystemBackdropsWindow.BackdropType.Acrylic]; - acrylicWindow.SetBackdrop(SampleBuiltInSystemBackdropsWindow.BackdropType.Acrylic); - acrylicWindow.Activate(); + var buildInBackdropsWindow = new SampleBuiltInSystemBackdropsWindow(); + buildInBackdropsWindow.SetBackdrop(SampleBuiltInSystemBackdropsWindow.BackdropType.Mica); + buildInBackdropsWindow.Activate(); } private void createCustomMicaWindow_Click(object sender, RoutedEventArgs e) { var micaWindow = new SampleSystemBackdropsWindow(); - micaWindow.AllowedBackdrops = [SampleSystemBackdropsWindow.BackdropType.None, + micaWindow.AllowedBackdrops = [ SampleSystemBackdropsWindow.BackdropType.Mica, - SampleSystemBackdropsWindow.BackdropType.MicaAlt]; - micaWindow.SetBackdrop(SampleSystemBackdropsWindow.BackdropType.Mica); + SampleSystemBackdropsWindow.BackdropType.MicaAlt, + SampleSystemBackdropsWindow.BackdropType.None + ]; micaWindow.Activate(); } private void createCustomDesktopAcrylicWindow_Click(object sender, RoutedEventArgs e) { var acrylicWindow = new SampleSystemBackdropsWindow(); - acrylicWindow.AllowedBackdrops = [SampleSystemBackdropsWindow.BackdropType.None, + acrylicWindow.AllowedBackdrops = [ SampleSystemBackdropsWindow.BackdropType.Acrylic, - SampleSystemBackdropsWindow.BackdropType.AcrylicThin]; - acrylicWindow.SetBackdrop(SampleSystemBackdropsWindow.BackdropType.Acrylic); + SampleSystemBackdropsWindow.BackdropType.AcrylicThin, + SampleSystemBackdropsWindow.BackdropType.None + ]; acrylicWindow.Activate(); } } diff --git a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample1_cs.txt b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample1_cs.txt index 3df5c0539..f5fe1b8ce 100644 --- a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample1_cs.txt +++ b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample1_cs.txt @@ -1,4 +1,3 @@ -// Option 2 - Implement Mica with c#. bool TrySetMicaBackdrop(bool useMicaAlt) { if (SystemBackdrops.MicaController.IsSupported()) @@ -11,4 +10,17 @@ bool TrySetMicaBackdrop(bool useMicaAlt) } return false; // Mica is not supported on this system. +} + +bool TrySetDesktopAcrylicBackdrop() +{ + if (DesktopAcrylicController.IsSupported()) + { + DesktopAcrylicBackdrop DesktopAcrylicBackdrop = new DesktopAcrylicBackdrop(); + SystemBackdrop = DesktopAcrylicBackdrop; + + return true; // Succeeded. + } + + return false; // DesktopAcrylic is not supported on this system. } \ No newline at end of file diff --git a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample1_xaml.txt b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample1_xaml.txt index 7171b0d07..98ac5ce23 100644 --- a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample1_xaml.txt +++ b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample1_xaml.txt @@ -1,9 +1,14 @@ - + - + - \ No newline at end of file + + + + + + \ No newline at end of file diff --git a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample4.txt b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample2.txt similarity index 67% rename from WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample4.txt rename to WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample2.txt index 34622844e..f9f4aa60c 100644 --- a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample4.txt +++ b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample2.txt @@ -4,12 +4,12 @@ using Microsoft.UI.Composition; using Microsoft.UI.Composition.SystemBackdrops; WindowsSystemDispatcherQueueHelper wsdqHelper; // See the helper class sample for the implementation -SystemBackdrops.DesktopAcrylicController acrylicController; -SystemBackdrops.SystemBackdropConfiguration configurationSource; +MicaController micaController; +SystemBackdropConfiguration configurationSource; -bool TrySetAcrylicBackdrop(bool useAcrylicThin) +bool TrySetMicaBackdrop(bool useMicaAlt) { - if (DesktopAcrylicController.IsSupported()) + if (MicaController.IsSupported()) { wsdqHelper = new WindowsSystemDispatcherQueueHelper(); wsdqHelper.EnsureWindowsSystemDispatcherQueueController(); @@ -24,16 +24,16 @@ bool TrySetAcrylicBackdrop(bool useAcrylicThin) configurationSource.IsInputActive = true; SetConfigurationSourceTheme(); - acrylicController = new DesktopAcrylicController(); - acrylicController.Kind = useAcrylicThin ? DesktopAcrylicKind.Thin : DesktopAcrylicKind.Base; + micaController = new MicaController(); + micaController.Kind = useMicaAlt ? MicaKind.BaseAlt : MicaKind.Base; // Enable the system backdrop. - acrylicController.AddSystemBackdropTarget(As()); - acrylicController.SetSystemBackdropConfiguration(configurationSource); + micaController.AddSystemBackdropTarget(this.As()); + micaController.SetSystemBackdropConfiguration(configurationSource); return true; // Succeeded. } - return false; // Acrylic is not supported on this system. + return false; // Mica is not supported on this system. } private void Window_Activated(object sender, WindowActivatedEventArgs args) @@ -44,12 +44,12 @@ private void Window_Activated(object sender, WindowActivatedEventArgs args) private void Window_Closed(object sender, WindowEventArgs args) { // Make sure any Mica/Acrylic controller is disposed - if (acrylicController != null) + if (micaController != null) { - acrylicController.Dispose(); - acrylicController = null; + micaController.Dispose(); + micaController = null; } - Activated -= Window_Activated; + this.Activated -= Window_Activated; configurationSource = null; } @@ -63,7 +63,7 @@ private void Window_ThemeChanged(FrameworkElement sender, object args) private void SetConfigurationSourceTheme() { - switch (((FrameworkElement)this.Content).ActualTheme) + switch (((FrameworkElement)Content).ActualTheme) { case ElementTheme.Dark: configurationSource.Theme = SystemBackdropTheme.Dark; break; case ElementTheme.Light: configurationSource.Theme = SystemBackdropTheme.Light; break; diff --git a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample2_cs.txt b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample2_cs.txt deleted file mode 100644 index a27ac9ced..000000000 --- a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample2_cs.txt +++ /dev/null @@ -1,13 +0,0 @@ -// Option 2 - Implement Acrylic with c#. -bool TrySetDesktopAcrylicBackdrop() -{ - if (DesktopAcrylicController.IsSupported()) - { - DesktopAcrylicBackdrop DesktopAcrylicBackdrop = new DesktopAcrylicBackdrop(); - SystemBackdrop = DesktopAcrylicBackdrop; - - return true; // Succeeded. - } - - return false; // DesktopAcrylic is not supported on this system. -} \ No newline at end of file diff --git a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample2_xaml.txt b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample2_xaml.txt deleted file mode 100644 index d1a4e289b..000000000 --- a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample2_xaml.txt +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample3.txt b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample3.txt index f9f4aa60c..34622844e 100644 --- a/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample3.txt +++ b/WinUIGallery/ControlPagesSampleCode/SystemBackdrops/SystemBackdropsSample3.txt @@ -4,12 +4,12 @@ using Microsoft.UI.Composition; using Microsoft.UI.Composition.SystemBackdrops; WindowsSystemDispatcherQueueHelper wsdqHelper; // See the helper class sample for the implementation -MicaController micaController; -SystemBackdropConfiguration configurationSource; +SystemBackdrops.DesktopAcrylicController acrylicController; +SystemBackdrops.SystemBackdropConfiguration configurationSource; -bool TrySetMicaBackdrop(bool useMicaAlt) +bool TrySetAcrylicBackdrop(bool useAcrylicThin) { - if (MicaController.IsSupported()) + if (DesktopAcrylicController.IsSupported()) { wsdqHelper = new WindowsSystemDispatcherQueueHelper(); wsdqHelper.EnsureWindowsSystemDispatcherQueueController(); @@ -24,16 +24,16 @@ bool TrySetMicaBackdrop(bool useMicaAlt) configurationSource.IsInputActive = true; SetConfigurationSourceTheme(); - micaController = new MicaController(); - micaController.Kind = useMicaAlt ? MicaKind.BaseAlt : MicaKind.Base; + acrylicController = new DesktopAcrylicController(); + acrylicController.Kind = useAcrylicThin ? DesktopAcrylicKind.Thin : DesktopAcrylicKind.Base; // Enable the system backdrop. - micaController.AddSystemBackdropTarget(this.As()); - micaController.SetSystemBackdropConfiguration(configurationSource); + acrylicController.AddSystemBackdropTarget(As()); + acrylicController.SetSystemBackdropConfiguration(configurationSource); return true; // Succeeded. } - return false; // Mica is not supported on this system. + return false; // Acrylic is not supported on this system. } private void Window_Activated(object sender, WindowActivatedEventArgs args) @@ -44,12 +44,12 @@ private void Window_Activated(object sender, WindowActivatedEventArgs args) private void Window_Closed(object sender, WindowEventArgs args) { // Make sure any Mica/Acrylic controller is disposed - if (micaController != null) + if (acrylicController != null) { - micaController.Dispose(); - micaController = null; + acrylicController.Dispose(); + acrylicController = null; } - this.Activated -= Window_Activated; + Activated -= Window_Activated; configurationSource = null; } @@ -63,7 +63,7 @@ private void Window_ThemeChanged(FrameworkElement sender, object args) private void SetConfigurationSourceTheme() { - switch (((FrameworkElement)Content).ActualTheme) + switch (((FrameworkElement)this.Content).ActualTheme) { case ElementTheme.Dark: configurationSource.Theme = SystemBackdropTheme.Dark; break; case ElementTheme.Light: configurationSource.Theme = SystemBackdropTheme.Light; break; diff --git a/WinUIGallery/DataModel/ControlInfoData.json b/WinUIGallery/DataModel/ControlInfoData.json index b2a1d7734..23fba96fe 100644 --- a/WinUIGallery/DataModel/ControlInfoData.json +++ b/WinUIGallery/DataModel/ControlInfoData.json @@ -2900,11 +2900,11 @@ }, { "UniqueId": "SystemBackdrops", - "Title": "SystemBackdrop", + "Title": "System Backdrops (Mica/Acrylic)", "ApiNamespace": "Microsoft.UI.Composition.SystemBackdrops", "Subtitle": "System backdrops, like Mica and Acrylic, for the window.", "ImagePath": "ms-appx:///Assets/ControlImages/Acrylic.png", - "Description": "System backdrops provide a transparency effect for the window background. Mica and Acrylic are the current supported backdrops. There are two options for applying a backdrop: the backdrop types (little customization, but easy to use) and the backdrop controllers (more customization, but harder to use)", + "Description": "System backdrops provide a transparency effect for the window background. Mica and Acrylic are the current supported backdrops. There are two options for applying a backdrop: first, to use built-in Mica/Acrylic types, which have no customization and are simple to apply. The second is to create a customizable backdrop, which requires more code.", "Content": "

Look at the SampleSystemBackdropsWindow.xaml.cs file in Visual Studio to see the full code for applying a backdrop.

", "SourcePath": "/Materials", "IsUpdated": true, @@ -2915,15 +2915,15 @@ "Docs": [ { "Title": "SystemBackdrop - API", - "Uri": "https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.systembackdrop?view=windows-app-sdk-1.6" + "Uri": "https://learn.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.systembackdrop" }, { "Title": "MicaBackdrop - API", - "Uri": "https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.micabackdrop?view=windows-app-sdk-1.6" + "Uri": "https://learn.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.micabackdrop" }, { "Title": "DesktopAcrylicBackdrop - API", - "Uri": "https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.desktopacrylicbackdrop?view=windows-app-sdk-1.6" + "Uri": "https://learn.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.desktopacrylicbackdrop" }, { "Title": "MicaController - API", diff --git a/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml b/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml index f5a0b03aa..27b87fc9f 100644 --- a/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml +++ b/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml @@ -43,12 +43,17 @@
- From e6ed9e14d20dd976a9e54c0b1fa4c8180a9878df Mon Sep 17 00:00:00 2001 From: Mgg Sk Date: Wed, 22 Jan 2025 23:46:26 +0100 Subject: [PATCH 14/22] Update WinUIGallery/ControlPages/SystemBackdropsPage.xaml Co-authored-by: Marcel W. --- WinUIGallery/ControlPages/SystemBackdropsPage.xaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml index 77d95c22b..ba2c14ff7 100644 --- a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml +++ b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml @@ -36,7 +36,7 @@ Acrylic is a semi-transparent material that replicates the effect of frosted glass. It's used only for transient, light-dismiss surfaces such as flyouts and context menus. Acrylic's most noticeable characteristic is its transparency. There are two acrylic blend types that change what's visible through the material: Background acrylic reveals the desktop wallpaper and other windows that are behind the currently active app, adding depth between application windows while celebrating the user's personalization preferences. - In-app acrylic adds a sense of depth within the app frame, providing both focus and hierarchy. (Inplemented with a AcrilicBrush in Xaml) + In-app acrylic adds a sense of depth within the app frame, providing both focus and hierarchy. (Inplemented with a AcrilycBrush in XAML)
From e360ddf49fdb577e0775606429ccbdac4e6552f0 Mon Sep 17 00:00:00 2001 From: Mgg Sk Date: Wed, 22 Jan 2025 23:47:22 +0100 Subject: [PATCH 15/22] Update WinUIGallery/ControlPages/SystemBackdropsPage.xaml Co-authored-by: Marcel W. --- WinUIGallery/ControlPages/SystemBackdropsPage.xaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml index ba2c14ff7..47578d538 100644 --- a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml +++ b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml @@ -66,7 +66,7 @@ - +
From e918a641616f9162ba4a62cf142e1c411f615674 Mon Sep 17 00:00:00 2001 From: Mgg sk Date: Thu, 23 Jan 2025 00:07:59 +0100 Subject: [PATCH 16/22] Fix backdrop type sample text --- .../ControlPages/SystemBackdropsPage.xaml | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml index 47578d538..e1db90dcb 100644 --- a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml +++ b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml @@ -23,17 +23,23 @@ CSharpSource="SystemBackdrops\SystemBackdropsSampleBackdropTypes_cs.txt" XamlSource="SystemBackdrops\SystemBackdropsSampleBackdropTypes_xaml.txt"> - + + There are three backdrop types: + 1. SystemBackdrop (The base class of every backdrop type) + 2. MicaBackdrop (A backdrop that uses the Mica material) + 3. DesktopAcrylicBackdrop (A backdrop that uses the Acrylic material) + Mica is an opaque, dynamic material that incorporates theme and desktop wallpaper to paint the background of windows. - You can apply Mica as your application backdrop to create a visual hierarchy, aiding productivity, by increasing clarity about which window is in focus. Mica is specifically designed for app performance as it only samples the desktop wallpaper once to create its visualization. - Mica is available for Windows 11 build 22000 or later. - - Mica Alt is a variant of Mica, with stronger tinting of the user's desktop background color. - You can apply Mica Alt as your app's backdrop to provide a deeper visual hierarchy than Mica, especially when creating an app with a tabbed title bar. - Mica Alt is available for Windows 11 build 22000 or later. + Mica Alt is a variant of Mica, with stronger tinting of the user's desktop background. Recommended when creating an app with a tabbed title bar. + All variants of Mica are available for Windows 11 build 22000 or later. Acrylic is a semi-transparent material that replicates the effect of frosted glass. It's used only for transient, light-dismiss surfaces such as flyouts and context menus. + There are two acrylic blend types that change what's visible through the material: + + Background acrylic reveals the desktop wallpaper and other windows that are behind the currently active app, adding depth between application windows while celebrating the user's personalization preferences. + In-app acrylic adds a sense of depth within the app frame, providing both focus and hierarchy. (Inplemented with a AcrylicBrush in Xaml) + Acrylic's most noticeable characteristic is its transparency. There are two acrylic blend types that change what's visible through the material: Background acrylic reveals the desktop wallpaper and other windows that are behind the currently active app, adding depth between application windows while celebrating the user's personalization preferences. In-app acrylic adds a sense of depth within the app frame, providing both focus and hierarchy. (Inplemented with a AcrilycBrush in XAML) From 1760e4604b926660f9425f50414756a10381fab2 Mon Sep 17 00:00:00 2001 From: Mgg sk Date: Thu, 23 Jan 2025 00:10:13 +0100 Subject: [PATCH 17/22] Fix --- WinUIGallery/ControlPages/SystemBackdropsPage.xaml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml index e1db90dcb..990466946 100644 --- a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml +++ b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml @@ -39,10 +39,6 @@ Background acrylic reveals the desktop wallpaper and other windows that are behind the currently active app, adding depth between application windows while celebrating the user's personalization preferences. In-app acrylic adds a sense of depth within the app frame, providing both focus and hierarchy. (Inplemented with a AcrylicBrush in Xaml) - - Acrylic's most noticeable characteristic is its transparency. There are two acrylic blend types that change what's visible through the material: - Background acrylic reveals the desktop wallpaper and other windows that are behind the currently active app, adding depth between application windows while celebrating the user's personalization preferences. - In-app acrylic adds a sense of depth within the app frame, providing both focus and hierarchy. (Inplemented with a AcrilycBrush in XAML) From de1ce068d55a585f776cf268a988f8ac19c153cf Mon Sep 17 00:00:00 2001 From: Mgg sk Date: Thu, 23 Jan 2025 00:17:30 +0100 Subject: [PATCH 18/22] Fix --- WinUIGallery/ControlPages/SystemBackdropsPage.xaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml index 990466946..001b393f6 100644 --- a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml +++ b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml @@ -60,7 +60,7 @@ Manages rendering and system policy for the background acrylic material. Acrylic has the same level of customization as Mica, but the type (Base / Thin) can't be changed using the DesktopAcrylicBackdrop class. - If you wan't to use Acrylic Thin in your app you have to use the DesktopAcrylicController class. There are 2 types of Acrylic: Base and Thin. The DesktopAcrylicBackdrop class uses a version of the Base type with less dimming. + If you wan't to use Acrylic Thin in your app you have to use the DesktopAcrylicController class. There are 2 types of Acrylic: Base (Acrylic thin but with dimming) and Thin (No dimming). The DesktopAcrylicBackdrop class uses a version of the Base type with less dimming. From e998fbb761f2948188cd5aa35e946270baca38d0 Mon Sep 17 00:00:00 2001 From: Mgg sk Date: Thu, 23 Jan 2025 00:21:25 +0100 Subject: [PATCH 19/22] Fixes --- WinUIGallery/ControlPages/SystemBackdropsPage.xaml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml index 001b393f6..2234840a1 100644 --- a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml +++ b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml @@ -38,7 +38,7 @@ There are two acrylic blend types that change what's visible through the material: Background acrylic reveals the desktop wallpaper and other windows that are behind the currently active app, adding depth between application windows while celebrating the user's personalization preferences. - In-app acrylic adds a sense of depth within the app frame, providing both focus and hierarchy. (Inplemented with a AcrylicBrush in Xaml) + In-app acrylic adds a sense of depth within the app frame, providing both focus and hierarchy. (Inplemented with a AcrylicBrush in XAML)
@@ -59,8 +59,11 @@ CSharpSource="SystemBackdrops\SystemBackdropsSampleDesktopAcrylicController.txt"> - Manages rendering and system policy for the background acrylic material. Acrylic has the same level of customization as Mica, but the type (Base / Thin) can't be changed using the DesktopAcrylicBackdrop class. - If you wan't to use Acrylic Thin in your app you have to use the DesktopAcrylicController class. There are 2 types of Acrylic: Base (Acrylic thin but with dimming) and Thin (No dimming). The DesktopAcrylicBackdrop class uses a version of the Base type with less dimming. + Manages rendering and system policy for the background acrylic material. Acrylic has the same level of customization as Mica, but the type can't be changed using the DesktopAcrylicBackdrop class. + There are 2 types of Acrylic: + Base (Darker) + Thin (Lighter) + If you wan't to use Acrylic Thin in your app you have to use the DesktopAcrylicController class. The DesktopAcrylicBackdrop class uses the Base type. From 32a1a2dbecd5c62b0ab466dbeb8b40e141a746fc Mon Sep 17 00:00:00 2001 From: Mgg sk Date: Thu, 23 Jan 2025 00:21:41 +0100 Subject: [PATCH 20/22] Fix --- WinUIGallery/ControlPages/SystemBackdropsPage.xaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml index 2234840a1..cb2c442c2 100644 --- a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml +++ b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml @@ -61,8 +61,8 @@ Manages rendering and system policy for the background acrylic material. Acrylic has the same level of customization as Mica, but the type can't be changed using the DesktopAcrylicBackdrop class. There are 2 types of Acrylic: - Base (Darker) - Thin (Lighter) + 1. Base (Darker) + 2. Thin (Lighter) If you wan't to use Acrylic Thin in your app you have to use the DesktopAcrylicController class. The DesktopAcrylicBackdrop class uses the Base type. From 4f017a1e38e8334020819a395a835f2acbdc1103 Mon Sep 17 00:00:00 2001 From: Mgg sk Date: Mon, 27 Jan 2025 18:23:57 +0100 Subject: [PATCH 21/22] Fix conflicts --- .../ControlPages/SystemBackdropsPage.xaml.cs | 63 ++- ...SampleBuiltInSystemBackdropsWindow.xaml.cs | 229 +++++----- .../SampleSystemBackdropsWindow.xaml.cs | 391 +++++++++--------- 3 files changed, 340 insertions(+), 343 deletions(-) diff --git a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs index b7a2fb37b..e093605b0 100644 --- a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs +++ b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs @@ -11,42 +11,41 @@ using Microsoft.UI.Xaml.Controls; using WinUIGallery.SamplePages; -namespace WinUIGallery.ControlPages +namespace WinUIGallery.ControlPages; + +public sealed partial class SystemBackdropsPage : Page { - public sealed partial class SystemBackdropsPage : Page + public SystemBackdropsPage() { - public SystemBackdropsPage() - { - InitializeComponent(); - } + InitializeComponent(); + } - private void createBuiltInWindow_Click(object sender, RoutedEventArgs e) - { - var buildInBackdropsWindow = new SampleBuiltInSystemBackdropsWindow(); - buildInBackdropsWindow.SetBackdrop(SampleBuiltInSystemBackdropsWindow.BackdropType.Mica); - buildInBackdropsWindow.Activate(); - } + private void createBuiltInWindow_Click(object sender, RoutedEventArgs e) + { + var buildInBackdropsWindow = new SampleBuiltInSystemBackdropsWindow(); + buildInBackdropsWindow.SetBackdrop(SampleBuiltInSystemBackdropsWindow.BackdropType.Mica); + buildInBackdropsWindow.Activate(); + } - private void createCustomMicaWindow_Click(object sender, RoutedEventArgs e) - { - var micaWindow = new SampleSystemBackdropsWindow(); - micaWindow.AllowedBackdrops = [ - SampleSystemBackdropsWindow.BackdropType.Mica, - SampleSystemBackdropsWindow.BackdropType.MicaAlt, - SampleSystemBackdropsWindow.BackdropType.None - ]; - micaWindow.Activate(); - } + private void createCustomMicaWindow_Click(object sender, RoutedEventArgs e) + { + var micaWindow = new SampleSystemBackdropsWindow(); + micaWindow.AllowedBackdrops = [ + SampleSystemBackdropsWindow.BackdropType.Mica, + SampleSystemBackdropsWindow.BackdropType.MicaAlt, + SampleSystemBackdropsWindow.BackdropType.None + ]; + micaWindow.Activate(); + } - private void createCustomDesktopAcrylicWindow_Click(object sender, RoutedEventArgs e) - { - var acrylicWindow = new SampleSystemBackdropsWindow(); - acrylicWindow.AllowedBackdrops = [ - SampleSystemBackdropsWindow.BackdropType.Acrylic, - SampleSystemBackdropsWindow.BackdropType.AcrylicThin, - SampleSystemBackdropsWindow.BackdropType.None - ]; - acrylicWindow.Activate(); - } + private void createCustomDesktopAcrylicWindow_Click(object sender, RoutedEventArgs e) + { + var acrylicWindow = new SampleSystemBackdropsWindow(); + acrylicWindow.AllowedBackdrops = [ + SampleSystemBackdropsWindow.BackdropType.Acrylic, + SampleSystemBackdropsWindow.BackdropType.AcrylicThin, + SampleSystemBackdropsWindow.BackdropType.None + ]; + acrylicWindow.Activate(); } } diff --git a/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml.cs b/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml.cs index 908b839c9..3e4faf027 100644 --- a/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml.cs +++ b/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml.cs @@ -7,146 +7,145 @@ using WinUIGallery.DesktopWap.Helper; using WinUIGallery.Helper; -namespace WinUIGallery.SamplePages +namespace WinUIGallery.SamplePages; + +public sealed partial class SampleBuiltInSystemBackdropsWindow : Window { - public sealed partial class SampleBuiltInSystemBackdropsWindow : Window + BackdropType currentBackdrop; + public SampleBuiltInSystemBackdropsWindow() { - BackdropType currentBackdrop; - public SampleBuiltInSystemBackdropsWindow() - { - InitializeComponent(); - AppWindow.SetIcon(@"Assets\Tiles\GalleryIcon.ico"); - ExtendsContentIntoTitleBar = true; - backdropComboBox.SelectedIndex = 0; - themeComboBox.SelectedIndex = 0; + InitializeComponent(); + AppWindow.SetIcon(@"Assets\Tiles\GalleryIcon.ico"); + ExtendsContentIntoTitleBar = true; + backdropComboBox.SelectedIndex = 0; + themeComboBox.SelectedIndex = 0; - ((FrameworkElement)Content).RequestedTheme = ThemeHelper.RootTheme; - } + ((FrameworkElement)Content).RequestedTheme = ThemeHelper.RootTheme; + } - public enum BackdropType - { - None, - Mica, - MicaAlt, - Acrylic - } + public enum BackdropType + { + None, + Mica, + MicaAlt, + Acrylic + } + + public void SetBackdrop(BackdropType type) + { + // Reset to default color. If the requested type is supported, we'll update to that. + // Note: This sample completely removes any previous controller to reset to the default + // state. This is done so this sample can show what is expected to be the most + // common pattern of an app simply choosing one controller type which it sets at + // startup. If an app wants to toggle between Mica and Acrylic it could simply + // call RemoveSystemBackdropTarget() on the old controller and then setup the new + // controller, reusing any existing configurationSource and Activated/Closed + // event handlers. - public void SetBackdrop(BackdropType type) + //Reset the backdrop + currentBackdrop = BackdropType.None; + tbChangeStatus.Text = ""; + SystemBackdrop = null; + + //Set the backdrop + if (type == BackdropType.Mica) { - // Reset to default color. If the requested type is supported, we'll update to that. - // Note: This sample completely removes any previous controller to reset to the default - // state. This is done so this sample can show what is expected to be the most - // common pattern of an app simply choosing one controller type which it sets at - // startup. If an app wants to toggle between Mica and Acrylic it could simply - // call RemoveSystemBackdropTarget() on the old controller and then setup the new - // controller, reusing any existing configurationSource and Activated/Closed - // event handlers. - - //Reset the backdrop - currentBackdrop = BackdropType.None; - tbChangeStatus.Text = ""; - SystemBackdrop = null; - - //Set the backdrop - if (type == BackdropType.Mica) - { - if (TrySetMicaBackdrop(false)) - { - currentBackdrop = type; - } - else - { - // Mica isn't supported. Try Acrylic. - type = BackdropType.Acrylic; - tbChangeStatus.Text += " Mica isn't supported. Trying Acrylic."; - } - } - if (type == BackdropType.MicaAlt) + if (TrySetMicaBackdrop(false)) { - if (TrySetMicaBackdrop(true)) - { - currentBackdrop = type; - } - else - { - // MicaAlt isn't supported. Try Acrylic. - type = BackdropType.Acrylic; - tbChangeStatus.Text += " MicaAlt isn't supported. Trying Acrylic."; - } + currentBackdrop = type; } - if (type == BackdropType.Acrylic) + else { - if (TrySetAcrylicBackdrop()) - { - currentBackdrop = type; - } - else - { - // Acrylic isn't supported, so take the next option, which is DefaultColor, which is already set. - tbChangeStatus.Text += " Acrylic isn't supported. Switching to default color."; - } + // Mica isn't supported. Try Acrylic. + type = BackdropType.Acrylic; + tbChangeStatus.Text += " Mica isn't supported. Trying Acrylic."; } - - //Fix the none backdrop - SetNoneBackdropBackground(); - - // Announce visual change to automation. - UIHelper.AnnounceActionForAccessibility(backdropComboBox, $"Background changed to {currentBackdrop}", "BackgroundChangedNotificationActivityId"); } - - bool TrySetMicaBackdrop(bool useMicaAlt) + if (type == BackdropType.MicaAlt) { - if (MicaController.IsSupported()) + if (TrySetMicaBackdrop(true)) { - SystemBackdrop = new MicaBackdrop { Kind = useMicaAlt ? MicaKind.BaseAlt : MicaKind.Base }; ; - return true; + currentBackdrop = type; } - - return false; // Mica is not supported on this system - } - - bool TrySetAcrylicBackdrop() - { - if (DesktopAcrylicController.IsSupported()) + else { - SystemBackdrop = new DesktopAcrylicBackdrop(); - return true; + // MicaAlt isn't supported. Try Acrylic. + type = BackdropType.Acrylic; + tbChangeStatus.Text += " MicaAlt isn't supported. Trying Acrylic."; } - - return false; // Acrylic is not supported on this system } - - private void BackdropComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) + if (type == BackdropType.Acrylic) { - SetBackdrop(backdropComboBox.SelectedIndex switch + if (TrySetAcrylicBackdrop()) { - 1 => BackdropType.MicaAlt, - 2 => BackdropType.Acrylic, - 3 => BackdropType.None, - _ => BackdropType.Mica - }); + currentBackdrop = type; + } + else + { + // Acrylic isn't supported, so take the next option, which is DefaultColor, which is already set. + tbChangeStatus.Text += " Acrylic isn't supported. Switching to default color."; + } } - private void ThemeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) - { - ((FrameworkElement)Content).RequestedTheme = themeComboBox.SelectedIndex switch - { - 1 => ElementTheme.Light, - 2 => ElementTheme.Dark, - _ => ElementTheme.Default - }; + //Fix the none backdrop + SetNoneBackdropBackground(); + + // Announce visual change to automation. + UIHelper.AnnounceActionForAccessibility(backdropComboBox, $"Background changed to {currentBackdrop}", "BackgroundChangedNotificationActivityId"); + } - TitleBarHelper.SetCaptionButtonColors(this, ((FrameworkElement)Content).ActualTheme == ElementTheme.Dark ? Colors.White : Colors.Black); - SetNoneBackdropBackground(); + bool TrySetMicaBackdrop(bool useMicaAlt) + { + if (MicaController.IsSupported()) + { + SystemBackdrop = new MicaBackdrop { Kind = useMicaAlt ? MicaKind.BaseAlt : MicaKind.Base }; ; + return true; } - //Fixes the background color not changing when switching between themes. - void SetNoneBackdropBackground() + return false; // Mica is not supported on this system + } + + bool TrySetAcrylicBackdrop() + { + if (DesktopAcrylicController.IsSupported()) { - if (currentBackdrop == BackdropType.None && themeComboBox.SelectedIndex != 0) - ((Grid)Content).Background = new SolidColorBrush(themeComboBox.SelectedIndex == 1 ? Colors.White : Colors.Black); - else - ((Grid)Content).Background = new SolidColorBrush(Colors.Transparent); + SystemBackdrop = new DesktopAcrylicBackdrop(); + return true; } + + return false; // Acrylic is not supported on this system + } + + private void BackdropComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + SetBackdrop(backdropComboBox.SelectedIndex switch + { + 1 => BackdropType.MicaAlt, + 2 => BackdropType.Acrylic, + 3 => BackdropType.None, + _ => BackdropType.Mica + }); + } + + private void ThemeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + ((FrameworkElement)Content).RequestedTheme = themeComboBox.SelectedIndex switch + { + 1 => ElementTheme.Light, + 2 => ElementTheme.Dark, + _ => ElementTheme.Default + }; + + TitleBarHelper.SetCaptionButtonColors(this, ((FrameworkElement)Content).ActualTheme == ElementTheme.Dark ? Colors.White : Colors.Black); + SetNoneBackdropBackground(); + } + + //Fixes the background color not changing when switching between themes. + void SetNoneBackdropBackground() + { + if (currentBackdrop == BackdropType.None && themeComboBox.SelectedIndex != 0) + ((Grid)Content).Background = new SolidColorBrush(themeComboBox.SelectedIndex == 1 ? Colors.White : Colors.Black); + else + ((Grid)Content).Background = new SolidColorBrush(Colors.Transparent); } } diff --git a/WinUIGallery/SamplePages/SampleSystemBackdropsWindow.xaml.cs b/WinUIGallery/SamplePages/SampleSystemBackdropsWindow.xaml.cs index 85404d1ef..d09ef0894 100644 --- a/WinUIGallery/SamplePages/SampleSystemBackdropsWindow.xaml.cs +++ b/WinUIGallery/SamplePages/SampleSystemBackdropsWindow.xaml.cs @@ -9,242 +9,241 @@ using Microsoft.UI; using WinUIGallery.DesktopWap.Helper; -namespace WinUIGallery.SamplePages +namespace WinUIGallery.SamplePages; + +public sealed partial class SampleSystemBackdropsWindow : Window { - public sealed partial class SampleSystemBackdropsWindow : Window + public BackdropType[] AllowedBackdrops; + WindowsSystemDispatcherQueueHelper wsdqHelper; + BackdropType currentBackdrop; + MicaController micaController; + DesktopAcrylicController acrylicController; + SystemBackdropConfiguration configurationSource; + + public SampleSystemBackdropsWindow() { - public BackdropType[] AllowedBackdrops; - WindowsSystemDispatcherQueueHelper wsdqHelper; - BackdropType currentBackdrop; - MicaController micaController; - DesktopAcrylicController acrylicController; - SystemBackdropConfiguration configurationSource; - - public SampleSystemBackdropsWindow() + InitializeComponent(); + AppWindow.SetIcon(@"Assets\Tiles\GalleryIcon.ico"); + ExtendsContentIntoTitleBar = true; + ((FrameworkElement)Content).RequestedTheme = ThemeHelper.RootTheme; + wsdqHelper = new WindowsSystemDispatcherQueueHelper(); + wsdqHelper.EnsureWindowsSystemDispatcherQueueController(); + + Activated += (s, e) => { - InitializeComponent(); - AppWindow.SetIcon(@"Assets\Tiles\GalleryIcon.ico"); - ExtendsContentIntoTitleBar = true; - ((FrameworkElement)Content).RequestedTheme = ThemeHelper.RootTheme; - wsdqHelper = new WindowsSystemDispatcherQueueHelper(); - wsdqHelper.EnsureWindowsSystemDispatcherQueueController(); - - Activated += (s, e) => - { - foreach (BackdropType allowedBackdrop in AllowedBackdrops) - backdropComboBox.Items.Add(allowedBackdrop); - }; + foreach (BackdropType allowedBackdrop in AllowedBackdrops) + backdropComboBox.Items.Add(allowedBackdrop); + }; - backdropComboBox.SelectedIndex = 0; - themeComboBox.SelectedIndex = 0; - } + backdropComboBox.SelectedIndex = 0; + themeComboBox.SelectedIndex = 0; + } - public enum BackdropType - { - None, - Mica, - MicaAlt, - Acrylic, - AcrylicThin - } + public enum BackdropType + { + None, + Mica, + MicaAlt, + Acrylic, + AcrylicThin + } - public void SetBackdrop(BackdropType type) + public void SetBackdrop(BackdropType type) + { + // Reset to default color. If the requested type is supported, we'll update to that. + // Note: This sample completely removes any previous controller to reset to the default + // state. This is done so this sample can show what is expected to be the most + // common pattern of an app simply choosing one controller type which it sets at + // startup. If an app wants to toggle between Mica and Acrylic it could simply + // call RemoveSystemBackdropTarget() on the old controller and then setup the new + // controller, reusing any existing m_configurationSource and Activated/Closed + // event handlers. + + //Reset the backdrop + currentBackdrop = BackdropType.None; + tbChangeStatus.Text = ""; + + micaController?.Dispose(); + micaController = null; + acrylicController?.Dispose(); + acrylicController = null; + configurationSource = null; + + //Set the backdrop + if (type == BackdropType.Mica) { - // Reset to default color. If the requested type is supported, we'll update to that. - // Note: This sample completely removes any previous controller to reset to the default - // state. This is done so this sample can show what is expected to be the most - // common pattern of an app simply choosing one controller type which it sets at - // startup. If an app wants to toggle between Mica and Acrylic it could simply - // call RemoveSystemBackdropTarget() on the old controller and then setup the new - // controller, reusing any existing m_configurationSource and Activated/Closed - // event handlers. - - //Reset the backdrop - currentBackdrop = BackdropType.None; - tbChangeStatus.Text = ""; - - micaController?.Dispose(); - micaController = null; - acrylicController?.Dispose(); - acrylicController = null; - configurationSource = null; - - //Set the backdrop - if (type == BackdropType.Mica) + if (TrySetMicaBackdrop(false)) { - if (TrySetMicaBackdrop(false)) - { - currentBackdrop = type; - } - else - { - // Mica isn't supported. Try Acrylic. - type = BackdropType.Acrylic; - tbChangeStatus.Text += " Mica isn't supported. Trying Acrylic."; - } + currentBackdrop = type; } - if (type == BackdropType.MicaAlt) + else { - if (TrySetMicaBackdrop(true)) - { - currentBackdrop = type; - } - else - { - // MicaAlt isn't supported. Try Acrylic. - type = BackdropType.Acrylic; - tbChangeStatus.Text += " MicaAlt isn't supported. Trying Acrylic."; - } + // Mica isn't supported. Try Acrylic. + type = BackdropType.Acrylic; + tbChangeStatus.Text += " Mica isn't supported. Trying Acrylic."; } - if (type == BackdropType.Acrylic) + } + if (type == BackdropType.MicaAlt) + { + if (TrySetMicaBackdrop(true)) { - if (TrySetAcrylicBackdrop(false)) - { - currentBackdrop = type; - } - else - { - // Acrylic isn't supported, so take the next option, which is DefaultColor, which is already set. - tbChangeStatus.Text += " Acrylic Base isn't supported. Switching to default color."; - } + currentBackdrop = type; } - if (type == BackdropType.AcrylicThin) + else { - if (TrySetAcrylicBackdrop(true)) - { - currentBackdrop = type; - } - else - { - // Acrylic isn't supported, so take the next option, which is DefaultColor, which is already set. - tbChangeStatus.Text += " Acrylic Thin isn't supported. Switching to default color."; - } + // MicaAlt isn't supported. Try Acrylic. + type = BackdropType.Acrylic; + tbChangeStatus.Text += " MicaAlt isn't supported. Trying Acrylic."; } - - //Fix the none backdrop - SetNoneBackdropBackground(); - - //Announce visual change to automation - UIHelper.AnnounceActionForAccessibility(backdropComboBox, $"Background changed to {currentBackdrop}", "BackgroundChangedNotificationActivityId"); } - - bool TrySetMicaBackdrop(bool useMicaAlt) + if (type == BackdropType.Acrylic) { - if (MicaController.IsSupported()) + if (TrySetAcrylicBackdrop(false)) { - // Hooking up the policy object. - configurationSource = new SystemBackdropConfiguration(); - Activated += Window_Activated; - Closed += Window_Closed; - ((FrameworkElement)Content).ActualThemeChanged += Window_ThemeChanged; - - // Initial configuration state. - configurationSource.IsInputActive = true; - SetConfigurationSourceTheme(); - - micaController = new MicaController(); - micaController.Kind = useMicaAlt ? MicaKind.BaseAlt : MicaKind.Base; - - // Enable the system backdrop. - // Note: Be sure to have "using WinRT;" to support the Window.As<...>() call. - micaController.AddSystemBackdropTarget(this.As()); - micaController.SetSystemBackdropConfiguration(configurationSource); - return true; // Succeeded. + currentBackdrop = type; + } + else + { + // Acrylic isn't supported, so take the next option, which is DefaultColor, which is already set. + tbChangeStatus.Text += " Acrylic Base isn't supported. Switching to default color."; } - - return false; // Mica is not supported on this system. } - - bool TrySetAcrylicBackdrop(bool useAcrylicThin) + if (type == BackdropType.AcrylicThin) { - if (DesktopAcrylicController.IsSupported()) + if (TrySetAcrylicBackdrop(true)) { - // Hooking up the policy object. - configurationSource = new SystemBackdropConfiguration(); - Activated += Window_Activated; - Closed += Window_Closed; - ((FrameworkElement)Content).ActualThemeChanged += Window_ThemeChanged; - - // Initial configuration state. - configurationSource.IsInputActive = true; - SetConfigurationSourceTheme(); - - acrylicController = new DesktopAcrylicController(); - acrylicController.Kind = useAcrylicThin ? DesktopAcrylicKind.Thin : DesktopAcrylicKind.Base; - - // Enable the system backdrop. - // Note: Be sure to have "using WinRT;" to support the Window.As<...>() call. - acrylicController.AddSystemBackdropTarget(this.As()); - acrylicController.SetSystemBackdropConfiguration(configurationSource); - return true; // Succeeded. + currentBackdrop = type; + } + else + { + // Acrylic isn't supported, so take the next option, which is DefaultColor, which is already set. + tbChangeStatus.Text += " Acrylic Thin isn't supported. Switching to default color."; } - - return false; // Acrylic is not supported on this system } - private void Window_Activated(object sender, WindowActivatedEventArgs args) - { - if(configurationSource != null) - configurationSource.IsInputActive = args.WindowActivationState != WindowActivationState.Deactivated; - } + //Fix the none backdrop + SetNoneBackdropBackground(); - private void Window_Closed(object sender, WindowEventArgs args) - { - // Make sure any Mica/Acrylic controller is disposed so it doesn't try to - // use this closed window. - micaController?.Dispose(); - micaController = null; - acrylicController?.Dispose(); - acrylicController = null; - configurationSource = null; - - Activated -= Window_Activated; - Closed -= Window_Closed; - ((FrameworkElement)Content).ActualThemeChanged -= Window_ThemeChanged; - } + //Announce visual change to automation + UIHelper.AnnounceActionForAccessibility(backdropComboBox, $"Background changed to {currentBackdrop}", "BackgroundChangedNotificationActivityId"); + } - private void Window_ThemeChanged(FrameworkElement sender, object args) + bool TrySetMicaBackdrop(bool useMicaAlt) + { + if (MicaController.IsSupported()) { - if (configurationSource != null) - { - SetConfigurationSourceTheme(); - } + // Hooking up the policy object. + configurationSource = new SystemBackdropConfiguration(); + Activated += Window_Activated; + Closed += Window_Closed; + ((FrameworkElement)Content).ActualThemeChanged += Window_ThemeChanged; + + // Initial configuration state. + configurationSource.IsInputActive = true; + SetConfigurationSourceTheme(); + + micaController = new MicaController(); + micaController.Kind = useMicaAlt ? MicaKind.BaseAlt : MicaKind.Base; + + // Enable the system backdrop. + // Note: Be sure to have "using WinRT;" to support the Window.As<...>() call. + micaController.AddSystemBackdropTarget(this.As()); + micaController.SetSystemBackdropConfiguration(configurationSource); + return true; // Succeeded. } - private void SetConfigurationSourceTheme() + return false; // Mica is not supported on this system. + } + + bool TrySetAcrylicBackdrop(bool useAcrylicThin) + { + if (DesktopAcrylicController.IsSupported()) { - configurationSource.Theme = (SystemBackdropTheme)((FrameworkElement)Content).ActualTheme; + // Hooking up the policy object. + configurationSource = new SystemBackdropConfiguration(); + Activated += Window_Activated; + Closed += Window_Closed; + ((FrameworkElement)Content).ActualThemeChanged += Window_ThemeChanged; + + // Initial configuration state. + configurationSource.IsInputActive = true; + SetConfigurationSourceTheme(); + + acrylicController = new DesktopAcrylicController(); + acrylicController.Kind = useAcrylicThin ? DesktopAcrylicKind.Thin : DesktopAcrylicKind.Base; + + // Enable the system backdrop. + // Note: Be sure to have "using WinRT;" to support the Window.As<...>() call. + acrylicController.AddSystemBackdropTarget(this.As()); + acrylicController.SetSystemBackdropConfiguration(configurationSource); + return true; // Succeeded. } - private void BackdropComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) + return false; // Acrylic is not supported on this system + } + + private void Window_Activated(object sender, WindowActivatedEventArgs args) + { + if(configurationSource != null) + configurationSource.IsInputActive = args.WindowActivationState != WindowActivationState.Deactivated; + } + + private void Window_Closed(object sender, WindowEventArgs args) + { + // Make sure any Mica/Acrylic controller is disposed so it doesn't try to + // use this closed window. + micaController?.Dispose(); + micaController = null; + acrylicController?.Dispose(); + acrylicController = null; + configurationSource = null; + + Activated -= Window_Activated; + Closed -= Window_Closed; + ((FrameworkElement)Content).ActualThemeChanged -= Window_ThemeChanged; + } + + private void Window_ThemeChanged(FrameworkElement sender, object args) + { + if (configurationSource != null) { - SetBackdrop((BackdropType) backdropComboBox.SelectedItem); + SetConfigurationSourceTheme(); } + } + + private void SetConfigurationSourceTheme() + { + configurationSource.Theme = (SystemBackdropTheme)((FrameworkElement)Content).ActualTheme; + } + + private void BackdropComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + SetBackdrop((BackdropType) backdropComboBox.SelectedItem); + } - private void ThemeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) + private void ThemeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + ((FrameworkElement)Content).RequestedTheme = themeComboBox.SelectedIndex switch { - ((FrameworkElement)Content).RequestedTheme = themeComboBox.SelectedIndex switch - { - 1 => ElementTheme.Light, - 2 => ElementTheme.Dark, - _ => ElementTheme.Default - }; + 1 => ElementTheme.Light, + 2 => ElementTheme.Dark, + _ => ElementTheme.Default + }; - TitleBarHelper.SetCaptionButtonColors(this, ((FrameworkElement)Content).ActualTheme == ElementTheme.Dark ? Colors.White : Colors.Black); - SetNoneBackdropBackground(); - } + TitleBarHelper.SetCaptionButtonColors(this, ((FrameworkElement)Content).ActualTheme == ElementTheme.Dark ? Colors.White : Colors.Black); + SetNoneBackdropBackground(); + } - //Fixes the background color not changing when switching between themes. - void SetNoneBackdropBackground() + //Fixes the background color not changing when switching between themes. + void SetNoneBackdropBackground() + { + if (currentBackdrop == BackdropType.None && themeComboBox.SelectedIndex != 0) { - if (currentBackdrop == BackdropType.None && themeComboBox.SelectedIndex != 0) - { - ((Grid)Content).Background = new SolidColorBrush(themeComboBox.SelectedIndex == 1 ? Colors.White : Colors.Black); - } - else - { - ((Grid)Content).Background = new SolidColorBrush(Colors.Transparent); - } + ((Grid)Content).Background = new SolidColorBrush(themeComboBox.SelectedIndex == 1 ? Colors.White : Colors.Black); + } + else + { + ((Grid)Content).Background = new SolidColorBrush(Colors.Transparent); } } } From f6473ced28288aa71050f51fe06826beeb7f64e5 Mon Sep 17 00:00:00 2001 From: Mgg sk Date: Mon, 27 Jan 2025 18:57:03 +0100 Subject: [PATCH 22/22] Cleanup --- .../ControlPages/SystemBackdropsPage.xaml | 5 +- .../ControlPages/SystemBackdropsPage.xaml.cs | 28 ++++++----- ...SampleBuiltInSystemBackdropsWindow.xaml.cs | 16 ++----- .../SampleSystemBackdropsWindow.xaml.cs | 47 +++++-------------- 4 files changed, 36 insertions(+), 60 deletions(-) diff --git a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml index cb2c442c2..b08c74ec9 100644 --- a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml +++ b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml @@ -49,7 +49,10 @@ Manages rendering and system policy for the mica material. The MicaController class provides a very customizable way to apply the Mica material to an app. - This is a list of properties that can be modified: FallbackColor, Kind (Can also be set using the MicaBackdrop class), LuminosityOpacity, State, TintColor and the TintOpacity. + This is a list of properties that can be modified: FallbackColor, Kind, LuminosityOpacity, TintColor and the TintOpacity. + There are 2 types of Mica: + 1. Base (Lighter) + 2. Alt (Darker) diff --git a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs index e093605b0..e7cfdcf2b 100644 --- a/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs +++ b/WinUIGallery/ControlPages/SystemBackdropsPage.xaml.cs @@ -29,23 +29,27 @@ private void createBuiltInWindow_Click(object sender, RoutedEventArgs e) private void createCustomMicaWindow_Click(object sender, RoutedEventArgs e) { - var micaWindow = new SampleSystemBackdropsWindow(); - micaWindow.AllowedBackdrops = [ - SampleSystemBackdropsWindow.BackdropType.Mica, - SampleSystemBackdropsWindow.BackdropType.MicaAlt, - SampleSystemBackdropsWindow.BackdropType.None - ]; + var micaWindow = new SampleSystemBackdropsWindow + { + AllowedBackdrops = [ + SampleSystemBackdropsWindow.BackdropType.Mica, + SampleSystemBackdropsWindow.BackdropType.MicaAlt, + SampleSystemBackdropsWindow.BackdropType.None + ] + }; micaWindow.Activate(); } private void createCustomDesktopAcrylicWindow_Click(object sender, RoutedEventArgs e) { - var acrylicWindow = new SampleSystemBackdropsWindow(); - acrylicWindow.AllowedBackdrops = [ - SampleSystemBackdropsWindow.BackdropType.Acrylic, - SampleSystemBackdropsWindow.BackdropType.AcrylicThin, - SampleSystemBackdropsWindow.BackdropType.None - ]; + var acrylicWindow = new SampleSystemBackdropsWindow + { + AllowedBackdrops = [ + SampleSystemBackdropsWindow.BackdropType.Acrylic, + SampleSystemBackdropsWindow.BackdropType.AcrylicThin, + SampleSystemBackdropsWindow.BackdropType.None + ] + }; acrylicWindow.Activate(); } } diff --git a/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml.cs b/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml.cs index 3e4faf027..98c077454 100644 --- a/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml.cs +++ b/WinUIGallery/SamplePages/SampleBuiltInSystemBackdropsWindow.xaml.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System; using Microsoft.UI; using Microsoft.UI.Composition.SystemBackdrops; using Microsoft.UI.Xaml; @@ -12,6 +12,7 @@ namespace WinUIGallery.SamplePages; public sealed partial class SampleBuiltInSystemBackdropsWindow : Window { BackdropType currentBackdrop; + public SampleBuiltInSystemBackdropsWindow() { InitializeComponent(); @@ -51,9 +52,7 @@ public void SetBackdrop(BackdropType type) if (type == BackdropType.Mica) { if (TrySetMicaBackdrop(false)) - { currentBackdrop = type; - } else { // Mica isn't supported. Try Acrylic. @@ -64,9 +63,7 @@ public void SetBackdrop(BackdropType type) if (type == BackdropType.MicaAlt) { if (TrySetMicaBackdrop(true)) - { currentBackdrop = type; - } else { // MicaAlt isn't supported. Try Acrylic. @@ -77,9 +74,7 @@ public void SetBackdrop(BackdropType type) if (type == BackdropType.Acrylic) { if (TrySetAcrylicBackdrop()) - { currentBackdrop = type; - } else { // Acrylic isn't supported, so take the next option, which is DefaultColor, which is already set. @@ -129,12 +124,7 @@ private void BackdropComboBox_SelectionChanged(object sender, SelectionChangedEv private void ThemeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { - ((FrameworkElement)Content).RequestedTheme = themeComboBox.SelectedIndex switch - { - 1 => ElementTheme.Light, - 2 => ElementTheme.Dark, - _ => ElementTheme.Default - }; + ((FrameworkElement)Content).RequestedTheme = Enum.GetValues()[themeComboBox.SelectedIndex]; TitleBarHelper.SetCaptionButtonColors(this, ((FrameworkElement)Content).ActualTheme == ElementTheme.Dark ? Colors.White : Colors.Black); SetNoneBackdropBackground(); diff --git a/WinUIGallery/SamplePages/SampleSystemBackdropsWindow.xaml.cs b/WinUIGallery/SamplePages/SampleSystemBackdropsWindow.xaml.cs index d09ef0894..2fde7251e 100644 --- a/WinUIGallery/SamplePages/SampleSystemBackdropsWindow.xaml.cs +++ b/WinUIGallery/SamplePages/SampleSystemBackdropsWindow.xaml.cs @@ -2,18 +2,24 @@ using Microsoft.UI.Xaml; using WinRT;// required to support Window.As() using Microsoft.UI.Composition.SystemBackdrops; -using System.Linq; using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Windowing; using Microsoft.UI; using WinUIGallery.DesktopWap.Helper; +using Microsoft.UI.Composition; +using System; namespace WinUIGallery.SamplePages; public sealed partial class SampleSystemBackdropsWindow : Window { - public BackdropType[] AllowedBackdrops; + public BackdropType[] AllowedBackdrops + { + get => (BackdropType[])backdropComboBox.ItemsSource; + set => backdropComboBox.ItemsSource = value; + } + WindowsSystemDispatcherQueueHelper wsdqHelper; BackdropType currentBackdrop; MicaController micaController; @@ -29,12 +35,6 @@ public SampleSystemBackdropsWindow() wsdqHelper = new WindowsSystemDispatcherQueueHelper(); wsdqHelper.EnsureWindowsSystemDispatcherQueueController(); - Activated += (s, e) => - { - foreach (BackdropType allowedBackdrop in AllowedBackdrops) - backdropComboBox.Items.Add(allowedBackdrop); - }; - backdropComboBox.SelectedIndex = 0; themeComboBox.SelectedIndex = 0; } @@ -73,9 +73,7 @@ public void SetBackdrop(BackdropType type) if (type == BackdropType.Mica) { if (TrySetMicaBackdrop(false)) - { currentBackdrop = type; - } else { // Mica isn't supported. Try Acrylic. @@ -86,9 +84,7 @@ public void SetBackdrop(BackdropType type) if (type == BackdropType.MicaAlt) { if (TrySetMicaBackdrop(true)) - { currentBackdrop = type; - } else { // MicaAlt isn't supported. Try Acrylic. @@ -99,9 +95,7 @@ public void SetBackdrop(BackdropType type) if (type == BackdropType.Acrylic) { if (TrySetAcrylicBackdrop(false)) - { currentBackdrop = type; - } else { // Acrylic isn't supported, so take the next option, which is DefaultColor, which is already set. @@ -111,9 +105,7 @@ public void SetBackdrop(BackdropType type) if (type == BackdropType.AcrylicThin) { if (TrySetAcrylicBackdrop(true)) - { currentBackdrop = type; - } else { // Acrylic isn't supported, so take the next option, which is DefaultColor, which is already set. @@ -142,12 +134,11 @@ bool TrySetMicaBackdrop(bool useMicaAlt) configurationSource.IsInputActive = true; SetConfigurationSourceTheme(); - micaController = new MicaController(); - micaController.Kind = useMicaAlt ? MicaKind.BaseAlt : MicaKind.Base; + micaController = new MicaController { Kind = useMicaAlt ? MicaKind.BaseAlt : MicaKind.Base }; // Enable the system backdrop. // Note: Be sure to have "using WinRT;" to support the Window.As<...>() call. - micaController.AddSystemBackdropTarget(this.As()); + micaController.AddSystemBackdropTarget(this.As()); micaController.SetSystemBackdropConfiguration(configurationSource); return true; // Succeeded. } @@ -169,12 +160,11 @@ bool TrySetAcrylicBackdrop(bool useAcrylicThin) configurationSource.IsInputActive = true; SetConfigurationSourceTheme(); - acrylicController = new DesktopAcrylicController(); - acrylicController.Kind = useAcrylicThin ? DesktopAcrylicKind.Thin : DesktopAcrylicKind.Base; + acrylicController = new DesktopAcrylicController { Kind = useAcrylicThin ? DesktopAcrylicKind.Thin : DesktopAcrylicKind.Base }; // Enable the system backdrop. // Note: Be sure to have "using WinRT;" to support the Window.As<...>() call. - acrylicController.AddSystemBackdropTarget(this.As()); + acrylicController.AddSystemBackdropTarget(this.As()); acrylicController.SetSystemBackdropConfiguration(configurationSource); return true; // Succeeded. } @@ -206,9 +196,7 @@ private void Window_Closed(object sender, WindowEventArgs args) private void Window_ThemeChanged(FrameworkElement sender, object args) { if (configurationSource != null) - { SetConfigurationSourceTheme(); - } } private void SetConfigurationSourceTheme() @@ -223,12 +211,7 @@ private void BackdropComboBox_SelectionChanged(object sender, SelectionChangedEv private void ThemeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { - ((FrameworkElement)Content).RequestedTheme = themeComboBox.SelectedIndex switch - { - 1 => ElementTheme.Light, - 2 => ElementTheme.Dark, - _ => ElementTheme.Default - }; + ((FrameworkElement)Content).RequestedTheme = Enum.GetValues()[themeComboBox.SelectedIndex]; TitleBarHelper.SetCaptionButtonColors(this, ((FrameworkElement)Content).ActualTheme == ElementTheme.Dark ? Colors.White : Colors.Black); SetNoneBackdropBackground(); @@ -238,12 +221,8 @@ private void ThemeComboBox_SelectionChanged(object sender, SelectionChangedEvent void SetNoneBackdropBackground() { if (currentBackdrop == BackdropType.None && themeComboBox.SelectedIndex != 0) - { ((Grid)Content).Background = new SolidColorBrush(themeComboBox.SelectedIndex == 1 ? Colors.White : Colors.Black); - } else - { ((Grid)Content).Background = new SolidColorBrush(Colors.Transparent); - } } }