Skip to content

Commit

Permalink
fix monochrome #1784
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Jul 5, 2024
1 parent eeee9af commit 52949e3
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 114 deletions.
16 changes: 9 additions & 7 deletions src/Snap.Hutao/Snap.Hutao/Core/Caching/ImageCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using System.IO;
using System.Net;
using System.Net.Http;
using Windows.ApplicationModel.Appointments;
using Windows.Foundation;
using Windows.Graphics.Imaging;
using WinRT;
Expand Down Expand Up @@ -58,7 +57,10 @@ private string CacheFolder
{
get => LazyInitializer.EnsureInitialized(ref cacheFolder, () =>
{
return serviceProvider.GetRequiredService<RuntimeOptions>().GetLocalCacheImageCacheFolder();
string folder = serviceProvider.GetRequiredService<RuntimeOptions>().GetLocalCacheImageCacheFolder();
Directory.CreateDirectory(Path.Combine(folder, "Light"));
Directory.CreateDirectory(Path.Combine(folder, "Dark"));
return folder;
});
}

Expand Down Expand Up @@ -102,13 +104,13 @@ public async ValueTask<ValueFile> GetFileFromCacheAsync(Uri uri, ElementTheme th
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;
Expand Down Expand Up @@ -157,7 +159,7 @@ public async ValueTask<ValueFile> GetFileFromCacheAsync(Uri uri, ElementTheme th
else if (themefileTasks.TryGetValue(key, out Task? task))
{
await task.ConfigureAwait(false);
return key.File;
return themeOrDefaultFilePath;
}
}

Expand Down Expand Up @@ -224,7 +226,7 @@ static void ConvertToMonoChrome(IMemoryBufferByteAccess byteAccess, byte backgro
byteAccess.GetBuffer(out Span<Rgba32> 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;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/UI/Rgba32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public Rgba32(byte r, byte g, byte b, byte a)

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);
Expand Down
23 changes: 20 additions & 3 deletions src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Image/CachedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
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;
Expand All @@ -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";
Expand All @@ -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; }
Expand Down Expand Up @@ -146,7 +150,7 @@ void RemoveImageFailed(ExceptionRoutedEventHandler handler)
}
}

private static void SourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not CachedImage control)
{
Expand All @@ -171,10 +175,12 @@ private static bool IsHttpUri(Uri uri)
SourceName = Path.GetFileName(imageUri.ToString());
IImageCache imageCache = this.ServiceProvider().GetRequiredService<IImageCache>();

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();
Expand All @@ -185,6 +191,17 @@ private static bool IsHttpUri(Uri uri)
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)
Expand Down
57 changes: 0 additions & 57 deletions src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Image/MonoChrome.cs

This file was deleted.

30 changes: 10 additions & 20 deletions src/Snap.Hutao/Snap.Hutao/UI/Xaml/Control/Theme/ThemeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,8 @@

namespace Snap.Hutao.UI.Xaml.Control.Theme;

/// <summary>
/// 主题帮助工具类
/// </summary>
[HighQuality]
internal static class ThemeHelper
{
/// <summary>
/// 从 <see cref="ElementTheme"/> 转换到 <see cref="ApplicationTheme"/>
/// </summary>
/// <param name="applicationTheme">元素主题</param>
/// <returns>应用主题</returns>
public static ApplicationTheme ElementToApplication(ElementTheme applicationTheme)
{
return applicationTheme switch
Expand All @@ -26,23 +17,22 @@ public static ApplicationTheme ElementToApplication(ElementTheme applicationThem
};
}

/// <summary>
/// 检查是否为暗黑模式
/// </summary>
/// <param name="elementTheme">当前元素主题</param>
/// <returns>是否为暗黑模式</returns>
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<App>().RequestedTheme;
return IsDarkMode(elementTheme, appTheme);
}

/// <summary>
/// 检查是否为暗黑模式
/// </summary>
/// <param name="elementTheme">当前元素主题</param>
/// <param name="applicationTheme">当前应用主题</param>
/// <returns>是否为暗黑模式</returns>
public static bool IsDarkMode(ElementTheme elementTheme, ApplicationTheme applicationTheme)
{
return elementTheme switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
<ColumnDefinition Width="auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<shuxci:MonoChrome
<shuxci:CachedImage
Grid.Column="0"
Width="36"
Height="36"
ShowAsMonoChrome="True"
Source="{Binding Icon}"/>
<TextBlock
Grid.Column="1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@
<DataTemplate x:Key="AvatarGridViewSkillTemplate">
<shuxcc:VerticalCard>
<shuxcc:VerticalCard.Top>
<shuxci:MonoChrome
<shuxci:CachedImage
Width="40"
Height="40"
Margin="12"
HorizontalAlignment="Center"
EnableShowHideAnimation="False"
ShowAsMonoChrome="True"
Source="{Binding Icon}"/>
</shuxcc:VerticalCard.Top>
<shuxcc:VerticalCard.Bottom>
Expand Down Expand Up @@ -319,9 +319,10 @@
<RowDefinition/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<shuxci:MonoChrome
<shuxci:CachedImage
Width="16"
Height="16"
ShowAsMonoChrome="True"
Source="{Binding Icon}"/>
<TextBlock
Grid.Row="0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@
<SolidColorBrush Color="{Binding Quality}"/>
</Ellipse.Fill>
</Ellipse>
<shuxci:MonoChrome
<shuxci:CachedImage
Grid.Column="0"
Width="20"
Height="20"
Margin="-3,2,7,0"
ShowAsMonoChrome="True"
Source="{Binding IconUri, Mode=OneWay}"
Visibility="{Binding IconUri, Converter={StaticResource EmptyObjectToVisibilityConverter}}"/>
<shuxci:CachedImage
Expand Down Expand Up @@ -408,16 +409,18 @@
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<shuxci:MonoChrome
<shuxci:CachedImage
Grid.Column="0"
Width="32"
Height="32"
HorizontalAlignment="Left"
ShowAsMonoChrome="True"
Source="{Binding Selected.FetterInfo.VisionBefore, Converter={StaticResource ElementNameIconConverter}}"/>
<shuxci:MonoChrome
<shuxci:CachedImage
Grid.Column="1"
Width="32"
Height="32"
ShowAsMonoChrome="True"
Source="{Binding Selected.Weapon, Converter={StaticResource WeaponTypeIconConverter}}"/>
</Grid>
<Border Style="{ThemeResource BorderCardStyle}">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@
<SolidColorBrush Color="{Binding Quality}"/>
</Ellipse.Fill>
</Ellipse>
<shuxci:MonoChrome
<shuxci:CachedImage
Grid.Column="0"
Width="20"
Height="20"
Margin="-5,2,9,0"
ShowAsMonoChrome="True"
Source="{Binding IconUri, Mode=OneWay}"
Visibility="{Binding IconUri, Converter={StaticResource EmptyObjectToVisibilityConverter}}"/>
<shuxci:CachedImage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

<DataTemplate x:Key="SkillHeaderTemplate">
<StackPanel Background="Transparent" ToolTipService.ToolTip="{Binding Name}">
<shuxci:MonoChrome shux:FrameworkElementHelper.SquareLength="36" Source="{Binding Icon, Converter={StaticResource SkillIconConverter}}"/>
<shuxci:CachedImage
shux:FrameworkElementHelper.SquareLength="36"
ShowAsMonoChrome="True"
Source="{Binding Icon, Converter={StaticResource SkillIconConverter}}"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
Expand Down
16 changes: 1 addition & 15 deletions src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/UserView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -358,21 +358,7 @@
CommandParameter="{Binding ElementName=SignInRewardButton}"
Icon="{shuxm:FontIcon Glyph={StaticResource FontIconContentGiftboxOpen}}"
Label="{shuxm:ResourceString Name=ViewUserCookieOperationSignInRewardAction}"
Style="{StaticResource DefaultAppBarButtonStyle}">
<mxi:Interaction.Behaviors>
<mxic:EventTriggerBehavior EventName="Click">
<shuxba:ShowWebView2WindowAction>
<shuxba:ShowWebView2WindowAction.ContentProvider>
<shuxvww:MiHoYoJSBridgeWebView2ContentProvider>
<shuxvww:MiHoYoJSBridgeWebView2ContentProvider.SourceProvider>
<shvu:SignInJSBridgeUriSourceProvider/>
</shuxvww:MiHoYoJSBridgeWebView2ContentProvider.SourceProvider>
</shuxvww:MiHoYoJSBridgeWebView2ContentProvider>
</shuxba:ShowWebView2WindowAction.ContentProvider>
</shuxba:ShowWebView2WindowAction>
</mxic:EventTriggerBehavior>
</mxi:Interaction.Behaviors>
</AppBarButton>
Style="{StaticResource DefaultAppBarButtonStyle}"/>
<AppBarButton
Width="{StaticResource LargeAppBarButtonWidth}"
MaxWidth="{StaticResource LargeAppBarButtonWidth}"
Expand Down
Loading

0 comments on commit 52949e3

Please # to comment.