From bab156763db2a7ef3097b9a1bbbfd280e8bccd34 Mon Sep 17 00:00:00 2001 From: Niels Laute Date: Tue, 18 Mar 2025 15:18:38 +0100 Subject: [PATCH] ToastWindows tweak (#569) - Using default acrylic which is fine for notifications (it's not activated anyways because the window does not have focus..), and it removes a bunch of C# code. - Minor tweaks to the padding of the toastwindow so it's more similar to OS toasts --- .../Microsoft.CmdPal.UI/ToastWindow.xaml | 19 ++--- .../Microsoft.CmdPal.UI/ToastWindow.xaml.cs | 77 ------------------- 2 files changed, 8 insertions(+), 88 deletions(-) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/ToastWindow.xaml b/src/modules/cmdpal/Microsoft.CmdPal.UI/ToastWindow.xaml index a1513e06aa..06bcdd66fc 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/ToastWindow.xaml +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/ToastWindow.xaml @@ -8,19 +8,16 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ui="using:CommunityToolkit.WinUI" Title="Command Palette Toast" - Activated="OnActivated" - Closed="OnClosed" mc:Ignorable="d"> - - + + + + + - + TextAlignment="Center" /> + diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/ToastWindow.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/ToastWindow.xaml.cs index d28edbb359..20d2f477d8 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/ToastWindow.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/ToastWindow.xaml.cs @@ -30,9 +30,6 @@ public sealed partial class ToastWindow : Window, private readonly DispatcherQueueTimer _debounceTimer = DispatcherQueue.GetForCurrentThread().CreateTimer(); - private DesktopAcrylicController? _acrylicController; - private SystemBackdropConfiguration? _configurationSource; - public ToastWindow() { this.InitializeComponent(); @@ -48,9 +45,6 @@ public sealed partial class ToastWindow : Window, PInvoke.EnableWindow(_hwnd, false); WeakReferenceMessenger.Default.Register(this); - - SetAcrylic(); - ToastText.ActualThemeChanged += (s, e) => UpdateAcrylic(); } private void PositionCentered() @@ -102,75 +96,4 @@ public sealed partial class ToastWindow : Window, // This might come in on a background thread DispatcherQueue.TryEnqueue(() => Close()); } - - ////// Literally everything below here is for acrylic ////// - - internal void OnClosed(object sender, WindowEventArgs args) => DisposeAcrylic(); - - // We want to use DesktopAcrylicKind.Thin and custom colors as this is the default material - // other Shell surfaces are using, this cannot be set in XAML however. - private void SetAcrylic() - { - if (DesktopAcrylicController.IsSupported()) - { - // Hooking up the policy object. - _configurationSource = new SystemBackdropConfiguration - { - // Initial configuration state. - IsInputActive = true, - }; - UpdateAcrylic(); - } - } - - private void UpdateAcrylic() - { - _acrylicController = GetAcrylicConfig(Content); - - // Enable the system backdrop. - // Note: Be sure to have "using WinRT;" to support the Window.As<...>() call. - _acrylicController.AddSystemBackdropTarget(this.As()); - _acrylicController.SetSystemBackdropConfiguration(_configurationSource); - } - - private static DesktopAcrylicController GetAcrylicConfig(UIElement content) - { - var feContent = content as FrameworkElement; - - return feContent?.ActualTheme == ElementTheme.Light - ? new DesktopAcrylicController() - { - Kind = DesktopAcrylicKind.Thin, - TintColor = Color.FromArgb(255, 243, 243, 243), - LuminosityOpacity = 0.90f, - TintOpacity = 0.0f, - FallbackColor = Color.FromArgb(255, 238, 238, 238), - } - : new DesktopAcrylicController() - { - Kind = DesktopAcrylicKind.Thin, - TintColor = Color.FromArgb(255, 32, 32, 32), - LuminosityOpacity = 0.96f, - TintOpacity = 0.5f, - FallbackColor = Color.FromArgb(255, 28, 28, 28), - }; - } - - private void DisposeAcrylic() - { - if (_acrylicController != null) - { - _acrylicController.Dispose(); - _acrylicController = null!; - _configurationSource = null!; - } - } - - internal void OnActivated(object sender, WindowActivatedEventArgs args) - { - if (_configurationSource != null) - { - _configurationSource.IsInputActive = args.WindowActivationState != WindowActivationState.Deactivated; - } - } }