diff --git a/src/Snap.Hutao/Snap.Hutao/Control/Behavior/PeriodicInvokeCommandOrOnActualThemeChangedBehavior.cs b/src/Snap.Hutao/Snap.Hutao/Control/Behavior/PeriodicInvokeCommandOrOnActualThemeChangedBehavior.cs index 9cfd7a1a..609c2b7c 100644 --- a/src/Snap.Hutao/Snap.Hutao/Control/Behavior/PeriodicInvokeCommandOrOnActualThemeChangedBehavior.cs +++ b/src/Snap.Hutao/Snap.Hutao/Control/Behavior/PeriodicInvokeCommandOrOnActualThemeChangedBehavior.cs @@ -66,8 +66,11 @@ internal sealed partial class PeriodicInvokeCommandOrOnActualThemeChangedBehavio break; } + ITaskContext taskContext = Ioc.Default.GetRequiredService(); + await taskContext.SwitchToMainThreadAsync(); TryExecuteCommand(); + await taskContext.SwitchToBackgroundAsync(); try { Task nextTickTask = timer.WaitForNextTickAsync(periodicTimerCancellationTokenSource.Token).AsTask(); diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx index 558b8e75..588d9262 100644 --- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx +++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx @@ -2357,6 +2357,9 @@ 背景材质 + + 图片版权信息 + 更改窗体的背景图片来源,重启胡桃以尽快生效 diff --git a/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImage.cs b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImage.cs index 03f60135..1ceff24a 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImage.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImage.cs @@ -2,11 +2,6 @@ // Licensed under the MIT license. using Microsoft.UI.Xaml.Media.Imaging; -using Snap.Hutao.Control.Media; -using Snap.Hutao.Core; -using System.IO; -using System.Runtime.InteropServices; -using Windows.Graphics.Imaging; using Windows.UI; namespace Snap.Hutao.Service.BackgroundImage; diff --git a/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageOptions.cs b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageOptions.cs new file mode 100644 index 00000000..a004d25b --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageOptions.cs @@ -0,0 +1,15 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using CommunityToolkit.Mvvm.ComponentModel; +using Snap.Hutao.Web.Hutao.Wallpaper; + +namespace Snap.Hutao.Service.BackgroundImage; + +[Injection(InjectAs.Singleton)] +internal sealed partial class BackgroundImageOptions : ObservableObject +{ + private Wallpaper? wallpaper; + + public Wallpaper? Wallpaper { get => wallpaper; set => SetProperty(ref wallpaper, value); } +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageService.cs b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageService.cs index 8421c5db..fce2983e 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageService.cs @@ -20,6 +20,7 @@ internal sealed partial class BackgroundImageService : IBackgroundImageService { private static readonly HashSet AllowedFormats = [".bmp", ".gif", ".ico", ".jpg", ".jpeg", ".png", ".tiff", ".webp"]; + private readonly BackgroundImageOptions backgroundImageOptions; private readonly IServiceProvider serviceProvider; private readonly RuntimeOptions runtimeOptions; private readonly ITaskContext taskContext; @@ -95,6 +96,7 @@ internal sealed partial class BackgroundImageService : IBackgroundImageService .ToHashSet(); } + backgroundImageOptions.Wallpaper = default; break; } @@ -116,10 +118,17 @@ internal sealed partial class BackgroundImageService : IBackgroundImageService { HutaoWallpaperClient wallpaperClient = serviceProvider.GetRequiredService(); Response response = await responseFactory(wallpaperClient).ConfigureAwait(false); - if (response is { Data.Url: Uri url }) + if (response is { Data: Wallpaper wallpaper }) { - ValueFile file = await serviceProvider.GetRequiredService().GetFileFromCacheAsync(url).ConfigureAwait(false); - currentBackgroundPathSet = [file]; + await taskContext.SwitchToMainThreadAsync(); + backgroundImageOptions.Wallpaper = wallpaper; + + await taskContext.SwitchToBackgroundAsync(); + if (wallpaper.Url is { } url) + { + ValueFile file = await serviceProvider.GetRequiredService().GetFileFromCacheAsync(url).ConfigureAwait(false); + currentBackgroundPathSet = [file]; + } } } } diff --git a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml index d8c45e0f..ff1dd372 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Page/SettingPage.xaml @@ -3,10 +3,12 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cw="using:CommunityToolkit.WinUI" + xmlns:cwb="using:CommunityToolkit.WinUI.Behaviors" xmlns:cwc="using:CommunityToolkit.WinUI.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mxi="using:Microsoft.Xaml.Interactivity" + xmlns:mxic="using:Microsoft.Xaml.Interactions.Core" xmlns:shc="using:Snap.Hutao.Control" xmlns:shcb="using:Snap.Hutao.Control.Behavior" xmlns:shch="using:Snap.Hutao.Control.Helper" @@ -312,17 +314,31 @@ SelectedItem="{Binding SelectedBackdropType, Mode=TwoWay}"/> - - + HeaderIcon="{shcm:FontIcon Glyph=}" + IsExpanded="{Binding BackgroundImageOptions.Wallpaper, Converter={StaticResource EmptyObjectToBoolConverter}}"> + - + + + + + + + + + + diff --git a/src/Snap.Hutao/Snap.Hutao/ViewModel/Setting/SettingViewModel.cs b/src/Snap.Hutao/Snap.Hutao/ViewModel/Setting/SettingViewModel.cs index d175ccfa..5ba8cffd 100644 --- a/src/Snap.Hutao/Snap.Hutao/ViewModel/Setting/SettingViewModel.cs +++ b/src/Snap.Hutao/Snap.Hutao/ViewModel/Setting/SettingViewModel.cs @@ -41,6 +41,7 @@ internal sealed partial class SettingViewModel : Abstraction.ViewModel private readonly IFileSystemPickerInteraction fileSystemPickerInteraction; private readonly HutaoPassportViewModel hutaoPassportViewModel; + private readonly BackgroundImageOptions backgroundImageOptions; private readonly IContentDialogFactory contentDialogFactory; private readonly INavigationService navigationService; private readonly IShellLinkInterop shellLinkInterop; @@ -75,6 +76,8 @@ internal sealed partial class SettingViewModel : Abstraction.ViewModel public LaunchOptions LaunchOptions { get => launchOptions; } + public BackgroundImageOptions BackgroundImageOptions { get => backgroundImageOptions; } + public HutaoPassportViewModel Passport { get => hutaoPassportViewModel; } public NameValue? SelectedBackdropType