fix notify icon context menu theme

This commit is contained in:
Lightczx
2024-05-23 14:59:40 +08:00
parent 9d47082f47
commit 055846dfd6
6 changed files with 40 additions and 24 deletions

View File

@@ -7,6 +7,8 @@ namespace Snap.Hutao.Control.Helper;
[SuppressMessage("", "SH001")] [SuppressMessage("", "SH001")]
[DependencyProperty("SquareLength", typeof(double), 0D, nameof(OnSquareLengthChanged), IsAttached = true, AttachedType = typeof(FrameworkElement))] [DependencyProperty("SquareLength", typeof(double), 0D, nameof(OnSquareLengthChanged), IsAttached = true, AttachedType = typeof(FrameworkElement))]
[DependencyProperty("IsActualThemeBindingEnabled", typeof(bool), false, nameof(OnIsActualThemeBindingEnabled), IsAttached = true, AttachedType = typeof(FrameworkElement))]
[DependencyProperty("ActualTheme", typeof(ElementTheme), ElementTheme.Default, IsAttached = true, AttachedType = typeof(FrameworkElement))]
public sealed partial class FrameworkElementHelper public sealed partial class FrameworkElementHelper
{ {
private static void OnSquareLengthChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e) private static void OnSquareLengthChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
@@ -15,4 +17,22 @@ public sealed partial class FrameworkElementHelper
element.Width = (double)e.NewValue; element.Width = (double)e.NewValue;
element.Height = (double)e.NewValue; element.Height = (double)e.NewValue;
} }
private static void OnIsActualThemeBindingEnabled(DependencyObject dp, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = (FrameworkElement)dp;
if ((bool)e.NewValue)
{
element.ActualThemeChanged += OnActualThemeChanged;
}
else
{
element.ActualThemeChanged -= OnActualThemeChanged;
}
static void OnActualThemeChanged(FrameworkElement sender, object args)
{
SetActualTheme(sender, sender.ActualTheme);
}
}
} }

View File

@@ -10,7 +10,7 @@ using System.Collections.Concurrent;
namespace Snap.Hutao.Core.Windowing.Backdrop; namespace Snap.Hutao.Core.Windowing.Backdrop;
// https://github.com/microsoft/microsoft-ui-xaml/blob/winui3/release/1.5-stable/controls/dev/Materials/DesktopAcrylicBackdrop/DesktopAcrylicBackdrop.cpp // https://github.com/microsoft/microsoft-ui-xaml/blob/winui3/release/1.5-stable/controls/dev/Materials/DesktopAcrylicBackdrop/DesktopAcrylicBackdrop.cpp
internal sealed class InputActiveDesktopAcrylicBackdrop : SystemBackdrop internal sealed partial class InputActiveDesktopAcrylicBackdrop : SystemBackdrop
{ {
private readonly ConcurrentDictionary<ICompositionSupportsSystemBackdrop, DesktopAcrylicController> controllers = []; private readonly ConcurrentDictionary<ICompositionSupportsSystemBackdrop, DesktopAcrylicController> controllers = [];
@@ -18,11 +18,10 @@ internal sealed class InputActiveDesktopAcrylicBackdrop : SystemBackdrop
{ {
base.OnTargetConnected(target, xamlRoot); base.OnTargetConnected(target, xamlRoot);
DesktopAcrylicController newController = new();
SystemBackdropConfiguration configuration = GetDefaultSystemBackdropConfiguration(target, xamlRoot); SystemBackdropConfiguration configuration = GetDefaultSystemBackdropConfiguration(target, xamlRoot);
configuration.IsInputActive = true; configuration.IsInputActive = true;
DesktopAcrylicController newController = new();
newController.AddSystemBackdropTarget(target); newController.AddSystemBackdropTarget(target);
newController.SetSystemBackdropConfiguration(configuration); newController.SetSystemBackdropConfiguration(configuration);
controllers.TryAdd(target, newController); controllers.TryAdd(target, newController);

View File

@@ -39,15 +39,19 @@ internal sealed class TransparentBackdrop : SystemBackdrop, IBackdropNeedEraseBa
} }
} }
protected override void OnTargetConnected(ICompositionSupportsSystemBackdrop connectedTarget, XamlRoot xamlRoot) protected override void OnTargetConnected(ICompositionSupportsSystemBackdrop target, XamlRoot xamlRoot)
{ {
base.OnTargetConnected(target, xamlRoot);
brush ??= Compositor.CreateColorBrush(tintColor); brush ??= Compositor.CreateColorBrush(tintColor);
connectedTarget.SystemBackdrop = brush; target.SystemBackdrop = brush;
} }
protected override void OnTargetDisconnected(ICompositionSupportsSystemBackdrop disconnectedTarget) protected override void OnTargetDisconnected(ICompositionSupportsSystemBackdrop target)
{ {
disconnectedTarget.SystemBackdrop = null; base.OnTargetDisconnected(target);
target.SystemBackdrop = null;
if (compositorLock is not null) if (compositorLock is not null)
{ {

View File

@@ -2,8 +2,10 @@
x:Class="Snap.Hutao.Core.Windowing.NotifyIcon.NotifyIconContextMenu" x:Class="Snap.Hutao.Core.Windowing.NotifyIcon.NotifyIconContextMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cw="using:CommunityToolkit.WinUI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:shch="using:Snap.Hutao.Control.Helper"
xmlns:shcm="using:Snap.Hutao.Control.Markup" xmlns:shcm="using:Snap.Hutao.Control.Markup"
xmlns:shcwb="using:Snap.Hutao.Core.Windowing.Backdrop" xmlns:shcwb="using:Snap.Hutao.Core.Windowing.Backdrop"
xmlns:shv="using:Snap.Hutao.ViewModel" xmlns:shv="using:Snap.Hutao.ViewModel"
@@ -30,7 +32,10 @@
<RowDefinition/> <RowDefinition/>
<RowDefinition/> <RowDefinition/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<TextBlock Margin="8" Text="{Binding Title}"/> <TextBlock
Margin="8"
Style="{StaticResource BodyTextBlockStyle}"
Text="{Binding Title}"/>
<Grid Grid.Row="1" Background="{ThemeResource CardBackgroundFillColorSecondaryBrush}"> <Grid Grid.Row="1" Background="{ThemeResource CardBackgroundFillColorSecondaryBrush}">
<StackPanel <StackPanel
Margin="4,0" Margin="4,0"

View File

@@ -30,7 +30,7 @@ internal sealed class NotifyIconController : IDisposable
icon = new(iconFile.Path); icon = new(iconFile.Path);
id = Unsafe.As<byte, Guid>(ref MemoryMarshal.GetArrayDataReference(MD5.HashData(Encoding.UTF8.GetBytes(iconFile.Path)))); id = Unsafe.As<byte, Guid>(ref MemoryMarshal.GetArrayDataReference(MD5.HashData(Encoding.UTF8.GetBytes(iconFile.Path))));
xamlHostWindow = new(); xamlHostWindow = new(serviceProvider);
messageWindow = new() messageWindow = new()
{ {
@@ -46,8 +46,6 @@ internal sealed class NotifyIconController : IDisposable
messageWindow.Dispose(); messageWindow.Dispose();
NotifyIconMethods.Delete(id); NotifyIconMethods.Delete(id);
icon.Dispose(); icon.Dispose();
xamlHostWindow.Dispose();
} }
public RECT GetRect() public RECT GetRect()

View File

@@ -15,11 +15,9 @@ using static Snap.Hutao.Win32.User32;
namespace Snap.Hutao.Core.Windowing.NotifyIcon; namespace Snap.Hutao.Core.Windowing.NotifyIcon;
internal sealed class NotifyIconXamlHostWindow : Window, IDisposable, IWindowNeedEraseBackground internal sealed class NotifyIconXamlHostWindow : Window, IWindowNeedEraseBackground
{ {
private readonly XamlWindowSubclass subclass; public NotifyIconXamlHostWindow(IServiceProvider serviceProvider)
public NotifyIconXamlHostWindow()
{ {
Content = new Border(); Content = new Border();
@@ -36,10 +34,7 @@ internal sealed class NotifyIconXamlHostWindow : Window, IDisposable, IWindowNee
presenter.SetBorderAndTitleBar(false, false); presenter.SetBorderAndTitleBar(false, false);
} }
subclass = new(this); this.InitializeController(serviceProvider);
subclass.Initialize();
Activate();
} }
public void ShowFlyoutAt(FlyoutBase flyout, Point point, RECT icon) public void ShowFlyoutAt(FlyoutBase flyout, Point point, RECT icon)
@@ -60,9 +55,4 @@ internal sealed class NotifyIconXamlHostWindow : Window, IDisposable, IWindowNee
ShowMode = FlyoutShowMode.Transient, ShowMode = FlyoutShowMode.Transient,
}); });
} }
public void Dispose()
{
subclass.Dispose();
}
} }