From 50bb86943190fd1f07b03ab69ec35c72aba040e2 Mon Sep 17 00:00:00 2001 From: yaobiao131 <28655758+yaobiao131@users.noreply.github.com> Date: Sun, 20 Apr 2025 18:50:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96asyncImageLoader?= =?UTF-8?q?=E5=86=85=E5=AD=98=E5=8D=A0=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Directory.Packages.props | 36 ++++++++--------- .../AsyncImageLoader/ImageBrushLoader.cs | 40 ++++++++++++++++++- .../AsyncImageLoader/ImageLoader.cs | 37 +++++++++++++++-- DownKyi/PrismExtension/Dialog/Dialog.cs | 3 +- DownKyi/Services/AlertService.cs | 4 +- .../Download/BuiltinDownloadService.cs | 6 ++- .../Views/UserSpace/ViewSeasonsSeries.axaml | 2 + DownKyi/Views/ViewMyBangumiFollow.axaml | 4 +- DownKyi/Views/ViewMyFavorites.axaml | 4 +- DownKyi/Views/ViewMyHistory.axaml | 6 ++- DownKyi/Views/ViewMyToViewVideo.axaml | 8 +++- DownKyi/Views/ViewPublication.axaml | 4 +- DownKyi/Views/ViewSeasonsSeries.axaml | 4 +- 13 files changed, 123 insertions(+), 35 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index bf26f11..a5d9fd6 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -1,20 +1,20 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/DownKyi/CustomControl/AsyncImageLoader/ImageBrushLoader.cs b/DownKyi/CustomControl/AsyncImageLoader/ImageBrushLoader.cs index 3afba63..5da8c71 100644 --- a/DownKyi/CustomControl/AsyncImageLoader/ImageBrushLoader.cs +++ b/DownKyi/CustomControl/AsyncImageLoader/ImageBrushLoader.cs @@ -4,6 +4,7 @@ using Avalonia; using Avalonia.Logging; using Avalonia.Media; using Avalonia.Media.Imaging; +using Avalonia.Threading; using DownKyi.Core.Storage; using DownKyi.CustomControl.AsyncImageLoader.Loaders; @@ -33,7 +34,20 @@ public static class ImageBrushLoader { if (newValue is not null) { - bitmap = await AsyncImageLoader.ProvideImageAsync(newValue); + // 注意缩放比例 + var width = GetWidth(imageBrush); + var height = GetHeight(imageBrush); + if (width > 0 && height > 0) + { + var scale = await Dispatcher.UIThread.InvokeAsync(() => App.Current.MainWindow.DesktopScaling); + var actualWidth = Convert.ToInt32(width * scale); + var actualHeight = Convert.ToInt32(height * scale); + bitmap = (await AsyncImageLoader.ProvideImageAsync(newValue))?.CreateScaledBitmap(new PixelSize(actualWidth, actualHeight)); + } + else + { + bitmap = await AsyncImageLoader.ProvideImageAsync(newValue); + } } } catch (Exception e) @@ -70,4 +84,28 @@ public static class ImageBrushLoader { element.SetValue(IsLoadingProperty, value); } + + public static readonly AttachedProperty WidthProperty = AvaloniaProperty.RegisterAttached("Width", typeof(ImageLoader)); + + public static int GetWidth(ImageBrush element) + { + return element.GetValue(WidthProperty); + } + + public static void SetWidth(ImageBrush element, int value) + { + element.SetValue(WidthProperty, value); + } + + public static readonly AttachedProperty HeightProperty = AvaloniaProperty.RegisterAttached("Height", typeof(ImageLoader)); + + public static int GetHeight(ImageBrush element) + { + return element.GetValue(HeightProperty); + } + + public static void SetHeight(ImageBrush element, int value) + { + element.SetValue(HeightProperty, value); + } } \ No newline at end of file diff --git a/DownKyi/CustomControl/AsyncImageLoader/ImageLoader.cs b/DownKyi/CustomControl/AsyncImageLoader/ImageLoader.cs index 11a1027..a925806 100644 --- a/DownKyi/CustomControl/AsyncImageLoader/ImageLoader.cs +++ b/DownKyi/CustomControl/AsyncImageLoader/ImageLoader.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; using Avalonia; using Avalonia.Controls; using Avalonia.Logging; -using Avalonia.Media.Imaging; +using Avalonia.Threading; using DownKyi.Core.Storage; using DownKyi.CustomControl.AsyncImageLoader.Loaders; @@ -62,6 +62,13 @@ public static class ImageLoader // A small delay allows to cancel early if the image goes out of screen too fast (eg. scrolling) // The Bitmap constructor is expensive and cannot be cancelled await Task.Delay(10, cts.Token); + if (sender.DesiredSize.Width != 0 && sender.DesiredSize.Height != 0) + { + var scale = Dispatcher.UIThread.Invoke(() => App.Current.MainWindow.DesktopScaling); + var actualWidth = Convert.ToInt32(sender.DesiredSize.Width * scale); + var actualHeight = Convert.ToInt32(sender.DesiredSize.Height * scale); + return (await AsyncImageLoader.ProvideImageAsync(url))?.CreateScaledBitmap(new PixelSize(actualWidth, actualHeight)); + } return await AsyncImageLoader.ProvideImageAsync(url); } @@ -75,10 +82,10 @@ public static class ImageLoader return null; } - }); + }, cts.Token); if (bitmap != null && !cts.Token.IsCancellationRequested) - sender.Source = bitmap!; + sender.Source = bitmap; // "It is not guaranteed to be thread safe by ICollection, but ConcurrentDictionary's implementation is. Additionally, we recently exposed this API for .NET 5 as a public ConcurrentDictionary.TryRemove" ((ICollection>)PendingOperations).Remove(new KeyValuePair(sender, cts)); @@ -104,4 +111,28 @@ public static class ImageLoader { element.SetValue(IsLoadingProperty, value); } + + public static readonly AttachedProperty WidthProperty = AvaloniaProperty.RegisterAttached("Width", typeof(ImageLoader)); + + public static int GetWidth(Image element) + { + return element.GetValue(WidthProperty); + } + + public static void SetWidth(Image element, int value) + { + element.SetValue(WidthProperty, value); + } + + public static readonly AttachedProperty HeightProperty = AvaloniaProperty.RegisterAttached("Height", typeof(ImageLoader)); + + public static int GetHeight(Image element) + { + return element.GetValue(HeightProperty); + } + + public static void SetHeight(Image element, int value) + { + element.SetValue(HeightProperty, value); + } } \ No newline at end of file diff --git a/DownKyi/PrismExtension/Dialog/Dialog.cs b/DownKyi/PrismExtension/Dialog/Dialog.cs index 4ed3742..3ba459e 100644 --- a/DownKyi/PrismExtension/Dialog/Dialog.cs +++ b/DownKyi/PrismExtension/Dialog/Dialog.cs @@ -5,8 +5,7 @@ namespace DownKyi.PrismExtension.Dialog; public class Dialog : Prism.Services.Dialogs.Dialog { - public static readonly AvaloniaProperty ThemeProperty = - AvaloniaProperty.RegisterAttached("Theme", typeof(Dialog)); + public static readonly AvaloniaProperty ThemeProperty = AvaloniaProperty.RegisterAttached("Theme", typeof(Dialog)); /// /// Gets the value for the attached property. diff --git a/DownKyi/Services/AlertService.cs b/DownKyi/Services/AlertService.cs index 4dc4bfa..2a34f84 100644 --- a/DownKyi/Services/AlertService.cs +++ b/DownKyi/Services/AlertService.cs @@ -54,7 +54,7 @@ public class AlertService return ShowMessage(image, title, message, 1); } - public async Task ShowMessage(VectorImage image, string type, string message, int buttonNumber) + public async Task ShowMessage(VectorImage image, string title, string message, int buttonNumber) { var result = ButtonResult.None; if (_dialogService == null) @@ -65,7 +65,7 @@ public class AlertService var param = new DialogParameters { { "image", image }, - { "title", type }, + { "title", title }, { "message", message }, { "button_number", buttonNumber } }; diff --git a/DownKyi/Services/Download/BuiltinDownloadService.cs b/DownKyi/Services/Download/BuiltinDownloadService.cs index c748790..7ee5fbf 100644 --- a/DownKyi/Services/Download/BuiltinDownloadService.cs +++ b/DownKyi/Services/Download/BuiltinDownloadService.cs @@ -317,13 +317,15 @@ public class BuiltinDownloadService : DownloadService, IDownloadService { ChunkCount = SettingsManager.GetInstance().GetSplit(), RequestConfiguration = requestConfiguration, - ParallelDownload = true + ParallelDownload = true, + ParallelCount = 2, + MaximumMemoryBufferBytes = 1024 * 1024 * 50 }; foreach (var url in urls) { var downloader = new Downloader.DownloadService(downloadOpt); var isComplete = false; - downloader.DownloadFileCompleted += (_, args) => + downloader.DownloadFileCompleted += (_, _) => { if (File.Exists(Path.Combine(path, localFileName))) { diff --git a/DownKyi/Views/UserSpace/ViewSeasonsSeries.axaml b/DownKyi/Views/UserSpace/ViewSeasonsSeries.axaml index c83551d..569ffb6 100644 --- a/DownKyi/Views/UserSpace/ViewSeasonsSeries.axaml +++ b/DownKyi/Views/UserSpace/ViewSeasonsSeries.axaml @@ -61,6 +61,8 @@ - + diff --git a/DownKyi/Views/ViewMyFavorites.axaml b/DownKyi/Views/ViewMyFavorites.axaml index f43bca1..08bd062 100644 --- a/DownKyi/Views/ViewMyFavorites.axaml +++ b/DownKyi/Views/ViewMyFavorites.axaml @@ -39,7 +39,9 @@ - + @@ -145,7 +147,9 @@ CornerRadius="12"> + asyncImageLoader:ImageBrushLoader.Source="{Binding UpHeader}" + asyncImageLoader:ImageBrushLoader.Width="24" + asyncImageLoader:ImageBrushLoader.Height="24" /> - + @@ -83,7 +85,9 @@ VerticalAlignment="Center" CornerRadius="12"> - + - + - +