From 52949e3431b35a14c9be288fffdeddb15a2c7683 Mon Sep 17 00:00:00 2001 From: DismissedLight <1686188646@qq.com> Date: Fri, 5 Jul 2024 11:54:45 +0800 Subject: [PATCH] fix monochrome #1784 --- .../Snap.Hutao/Core/Caching/ImageCache.cs | 16 +++--- src/Snap.Hutao/Snap.Hutao/UI/Rgba32.cs | 2 + .../UI/Xaml/Control/Image/CachedImage.cs | 23 +++++++- .../UI/Xaml/Control/Image/MonoChrome.cs | 57 ------------------- .../UI/Xaml/Control/Theme/ThemeHelper.cs | 30 ++++------ .../Dialog/CultivatePromotionDeltaDialog.xaml | 3 +- .../UI/Xaml/View/Page/AvatarPropertyPage.xaml | 7 ++- .../UI/Xaml/View/Page/WikiAvatarPage.xaml | 9 ++- .../UI/Xaml/View/Page/WikiWeaponPage.xaml | 3 +- .../UI/Xaml/View/Specialized/SkillPivot.xaml | 5 +- .../Snap.Hutao/UI/Xaml/View/UserView.xaml | 16 +----- .../ViewModel/User/UserViewModel.cs | 22 ++++++- 12 files changed, 79 insertions(+), 114 deletions(-) delete mode 100644 src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Image/MonoChrome.cs diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Caching/ImageCache.cs b/src/Snap.Hutao/Snap.Hutao/Core/Caching/ImageCache.cs index e4431300..0b738c71 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Caching/ImageCache.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Caching/ImageCache.cs @@ -20,7 +20,6 @@ using System.Diagnostics; using System.IO; using System.Net; using System.Net.Http; -using Windows.ApplicationModel.Appointments; using Windows.Foundation; using Windows.Graphics.Imaging; using WinRT; @@ -58,7 +57,10 @@ internal sealed partial class ImageCache : IImageCache, IImageCacheFilePathOpera { get => LazyInitializer.EnsureInitialized(ref cacheFolder, () => { - return serviceProvider.GetRequiredService().GetLocalCacheImageCacheFolder(); + string folder = serviceProvider.GetRequiredService().GetLocalCacheImageCacheFolder(); + Directory.CreateDirectory(Path.Combine(folder, "Light")); + Directory.CreateDirectory(Path.Combine(folder, "Dark")); + return folder; }); } @@ -102,13 +104,13 @@ internal sealed partial class ImageCache : IImageCache, IImageCacheFilePathOpera using (ScopedTaskCompletionSource themeFileScope = new()) { ElementThemeValueFile key = new(fileName, theme); + string defaultFilePath = Path.Combine(CacheFolder, fileName); + string themeOrDefaultFilePath = theme is ElementTheme.Dark or ElementTheme.Light ? Path.Combine(CacheFolder, $"{theme}", fileName) : defaultFilePath; + if (themefileTasks.TryAdd(key, themeFileScope.Task)) { try { - string defaultFilePath = Path.Combine(CacheFolder, fileName); - string themeOrDefaultFilePath = theme is ElementTheme.Dark or ElementTheme.Light ? Path.Combine(CacheFolder, $"{theme}", fileName) : defaultFilePath; - if (!IsFileInvalid(themeOrDefaultFilePath)) { return themeOrDefaultFilePath; @@ -157,7 +159,7 @@ internal sealed partial class ImageCache : IImageCache, IImageCacheFilePathOpera else if (themefileTasks.TryGetValue(key, out Task? task)) { await task.ConfigureAwait(false); - return key.File; + return themeOrDefaultFilePath; } } @@ -224,7 +226,7 @@ internal sealed partial class ImageCache : IImageCache, IImageCacheFilePathOpera byteAccess.GetBuffer(out Span span); foreach (ref Rgba32 pixel in span) { - pixel.A = (byte)(pixel.Luminance * 255); + pixel.A = (byte)pixel.Luminance255; pixel.R = pixel.G = pixel.B = background; } } diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Rgba32.cs b/src/Snap.Hutao/Snap.Hutao/UI/Rgba32.cs index a856f696..7e250cee 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Rgba32.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Rgba32.cs @@ -39,6 +39,8 @@ internal struct Rgba32 public readonly double Luminance { get => ((0.299 * R) + (0.587 * G) + (0.114 * B)) / 255; } + public readonly double Luminance255 { get => (0.299 * R) + (0.587 * G) + (0.114 * B); } + public static unsafe implicit operator Color(Rgba32 rgba32) { return ColorHelper.ToColor(rgba32); diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Image/CachedImage.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Image/CachedImage.cs index c8dc11a7..a72a148f 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Image/CachedImage.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Image/CachedImage.cs @@ -9,6 +9,8 @@ using Microsoft.UI.Xaml.Media.Imaging; using Snap.Hutao.Core.Caching; using Snap.Hutao.Core.DataTransfer; using Snap.Hutao.Core.ExceptionService; +using Snap.Hutao.UI.Xaml.Control.Theme; +using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using Windows.Graphics.Imaging; @@ -32,7 +34,8 @@ namespace Snap.Hutao.UI.Xaml.Control.Image; [DependencyProperty("PlaceholderSource", typeof(object), default(object))] [DependencyProperty("PlaceholderStretch", typeof(Stretch), Stretch.Uniform)] [DependencyProperty("PlaceholderMargin", typeof(Thickness))] -[DependencyProperty("Source", typeof(object), default(object), nameof(SourceChanged))] +[DependencyProperty("Source", typeof(object), default(object), nameof(OnSourceChanged))] +[DependencyProperty("ShowAsMonoChrome", typeof(bool), false)] internal sealed partial class CachedImage : Microsoft.UI.Xaml.Controls.Control, IAlphaMaskProvider { private const string PartImage = "Image"; @@ -48,6 +51,7 @@ internal sealed partial class CachedImage : Microsoft.UI.Xaml.Controls.Control, public CachedImage() { DefaultStyleKey = typeof(CachedImage); + ActualThemeChanged += OnActualThemeChanged; } public bool IsInitialized { get; private set; } @@ -146,7 +150,7 @@ internal sealed partial class CachedImage : Microsoft.UI.Xaml.Controls.Control, } } - private static void SourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not CachedImage control) { @@ -171,10 +175,12 @@ internal sealed partial class CachedImage : Microsoft.UI.Xaml.Controls.Control, SourceName = Path.GetFileName(imageUri.ToString()); IImageCache imageCache = this.ServiceProvider().GetRequiredService(); + string file = default; try { HutaoException.ThrowIf(string.IsNullOrEmpty(imageUri.Host), SH.ControlImageCachedImageInvalidResourceUri); - string file = await imageCache.GetFileFromCacheAsync(imageUri).ConfigureAwait(true); // BitmapImage need to be created by main thread. + ElementTheme theme = ShowAsMonoChrome ? ThemeHelper.ApplicationToElement(ThemeHelper.ElementToApplication(ActualTheme)) : ElementTheme.Default; + file = await imageCache.GetFileFromCacheAsync(imageUri, theme).ConfigureAwait(true); // BitmapImage need to be created by main thread. CachedName = Path.GetFileName(file); token.ThrowIfCancellationRequested(); // check token state to determine whether the operation should be canceled. return file.ToUri(); @@ -185,6 +191,17 @@ internal sealed partial class CachedImage : Microsoft.UI.Xaml.Controls.Control, imageCache.Remove(imageUri); return default; } + catch (Exception ex) + { + Debug.WriteLine(file); + Debug.WriteLine(ex); + return default; + } + } + + private void OnActualThemeChanged(FrameworkElement sender, object args) + { + SetSource(Source); } private void OnImageOpened(object sender, RoutedEventArgs e) diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Image/MonoChrome.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Image/MonoChrome.cs deleted file mode 100644 index 747efbb4..00000000 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Image/MonoChrome.cs +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (c) DGP Studio. All rights reserved. -// Licensed under the MIT license. - -using Microsoft.Graphics.Canvas.Effects; -using Microsoft.UI; -using Microsoft.UI.Composition; -using Microsoft.UI.Xaml; -using Microsoft.UI.Xaml.Media; -using Snap.Hutao.UI.Composition; -using Snap.Hutao.UI.Xaml.Control.Theme; -using Windows.Foundation; - -namespace Snap.Hutao.UI.Xaml.Control.Image; - -internal sealed class MonoChrome : CompositionImage -{ - private CompositionColorBrush? backgroundBrush; - - public MonoChrome() - { - ActualThemeChanged += OnActualThemeChanged; - } - - protected override SpriteVisual CompositeSpriteVisual(Compositor compositor, LoadedImageSurface imageSurface) - { - CompositionColorBrush blackLayerBrush = compositor.CreateColorBrush(Colors.Black); - CompositionSurfaceBrush imageSurfaceBrush = compositor.CompositeSurfaceBrush(imageSurface, stretch: CompositionStretch.Uniform, vRatio: 0f); - CompositionEffectBrush overlayBrush = compositor.CompositeBlendEffectBrush(blackLayerBrush, imageSurfaceBrush, BlendEffectMode.Overlay); - CompositionEffectBrush opacityBrush = compositor.CompositeLuminanceToAlphaEffectBrush(overlayBrush); - - backgroundBrush = compositor.CreateColorBrush(); - SetBackgroundColor(backgroundBrush); - CompositionEffectBrush alphaMaskEffectBrush = compositor.CompositeAlphaMaskEffectBrush(backgroundBrush, opacityBrush); - - return compositor.CompositeSpriteVisual(alphaMaskEffectBrush); - } - - private void OnActualThemeChanged(FrameworkElement sender, object args) - { - if (backgroundBrush is not null) - { - SetBackgroundColor(backgroundBrush); - } - } - - private void SetBackgroundColor(CompositionColorBrush backgroundBrush) - { - ApplicationTheme theme = ThemeHelper.ElementToApplication(ActualTheme); - - backgroundBrush.Color = theme switch - { - ApplicationTheme.Light => Colors.Black, - ApplicationTheme.Dark => Colors.White, - _ => Colors.Transparent, - }; - } -} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Theme/ThemeHelper.cs b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Theme/ThemeHelper.cs index fe4b28cb..9c691316 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Theme/ThemeHelper.cs +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Theme/ThemeHelper.cs @@ -5,17 +5,8 @@ using Microsoft.UI.Xaml; namespace Snap.Hutao.UI.Xaml.Control.Theme; -/// -/// 主题帮助工具类 -/// -[HighQuality] internal static class ThemeHelper { - /// - /// 从 转换到 - /// - /// 元素主题 - /// 应用主题 public static ApplicationTheme ElementToApplication(ElementTheme applicationTheme) { return applicationTheme switch @@ -26,23 +17,22 @@ internal static class ThemeHelper }; } - /// - /// 检查是否为暗黑模式 - /// - /// 当前元素主题 - /// 是否为暗黑模式 + public static ElementTheme ApplicationToElement(ApplicationTheme applicationTheme) + { + return applicationTheme switch + { + ApplicationTheme.Light => ElementTheme.Light, + ApplicationTheme.Dark => ElementTheme.Dark, + _ => ElementTheme.Default, + }; + } + public static bool IsDarkMode(ElementTheme elementTheme) { ApplicationTheme appTheme = Ioc.Default.GetRequiredService().RequestedTheme; return IsDarkMode(elementTheme, appTheme); } - /// - /// 检查是否为暗黑模式 - /// - /// 当前元素主题 - /// 当前应用主题 - /// 是否为暗黑模式 public static bool IsDarkMode(ElementTheme elementTheme, ApplicationTheme applicationTheme) { return elementTheme switch diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Dialog/CultivatePromotionDeltaDialog.xaml b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Dialog/CultivatePromotionDeltaDialog.xaml index 3049554c..c1539985 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Dialog/CultivatePromotionDeltaDialog.xaml +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Dialog/CultivatePromotionDeltaDialog.xaml @@ -27,10 +27,11 @@ - - @@ -319,9 +319,10 @@ - - - - diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/WikiWeaponPage.xaml b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/WikiWeaponPage.xaml index 28614f32..d6d90fb5 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/WikiWeaponPage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/WikiWeaponPage.xaml @@ -49,11 +49,12 @@ - - + diff --git a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/UserView.xaml b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/UserView.xaml index 874e046d..4449cf0b 100644 --- a/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/UserView.xaml +++ b/src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/UserView.xaml @@ -358,21 +358,7 @@ CommandParameter="{Binding ElementName=SignInRewardButton}" Icon="{shuxm:FontIcon Glyph={StaticResource FontIconContentGiftboxOpen}}" Label="{shuxm:ResourceString Name=ViewUserCookieOperationSignInRewardAction}" - Style="{StaticResource DefaultAppBarButtonStyle}"> - - - - - - - - - - - - - - + Style="{StaticResource DefaultAppBarButtonStyle}"/>