From 2eda64aa7af74dad5f3cf21fda8e8a8e6230fe64 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Fri, 11 Apr 2025 15:51:26 +0800 Subject: [PATCH] Support badge icon loading --- Flow.Launcher/ResultListBox.xaml | 2 +- Flow.Launcher/ViewModel/ResultViewModel.cs | 46 ++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/Flow.Launcher/ResultListBox.xaml b/Flow.Launcher/ResultListBox.xaml index 03bff03eb..63c461c43 100644 --- a/Flow.Launcher/ResultListBox.xaml +++ b/Flow.Launcher/ResultListBox.xaml @@ -145,7 +145,7 @@ HorizontalAlignment="Right" VerticalAlignment="Bottom" RenderOptions.BitmapScalingMode="Fant" - Source="{Binding Image, TargetNullValue={x:Null}}" + Source="{Binding BadgeImage, TargetNullValue={x:Null}}" Visibility="{Binding ShowBadge}" /> diff --git a/Flow.Launcher/ViewModel/ResultViewModel.cs b/Flow.Launcher/ViewModel/ResultViewModel.cs index 61e228de1..4137d5f58 100644 --- a/Flow.Launcher/ViewModel/ResultViewModel.cs +++ b/Flow.Launcher/ViewModel/ResultViewModel.cs @@ -125,13 +125,21 @@ namespace Flow.Launcher.ViewModel public Visibility ShowBadge { - get => Settings.ShowBadges ? Visibility.Visible : Visibility.Collapsed; + get + { + if (Settings.ShowBadges && BadgeIconAvailable) + return Visibility.Visible; + + return Visibility.Collapsed; + } } private bool GlyphAvailable => Glyph is not null; private bool ImgIconAvailable => !string.IsNullOrEmpty(Result.IcoPath) || Result.Icon is not null; + private bool BadgeIconAvailable => !string.IsNullOrEmpty(Result.BadgeIcoPath) || Result.BadgeIcon is not null; + private bool PreviewImageAvailable => !string.IsNullOrEmpty(Result.Preview.PreviewImagePath) || Result.Preview.PreviewDelegate != null; public string OpenResultModifiers => Settings.OpenResultModifiers; @@ -145,9 +153,11 @@ namespace Flow.Launcher.ViewModel : Result.SubTitleToolTip; private volatile bool _imageLoaded; + private volatile bool _badgeImageLoaded; private volatile bool _previewImageLoaded; private ImageSource _image = ImageLoader.LoadingImage; + private ImageSource _badgeImage = ImageLoader.LoadingImage; private ImageSource _previewImage = ImageLoader.LoadingImage; public ImageSource Image @@ -165,6 +175,21 @@ namespace Flow.Launcher.ViewModel private set => _image = value; } + public ImageSource BadgeImage + { + get + { + if (!_badgeImageLoaded) + { + _badgeImageLoaded = true; + _ = LoadBadgeImageAsync(); + } + + return _badgeImage; + } + private set => _badgeImage = value; + } + public ImageSource PreviewImage { get @@ -210,7 +235,7 @@ namespace Flow.Launcher.ViewModel { var imagePath = Result.IcoPath; var iconDelegate = Result.Icon; - if (ImageLoader.TryGetValue(imagePath, false, out ImageSource img)) + if (ImageLoader.TryGetValue(imagePath, false, out var img)) { _image = img; } @@ -221,11 +246,26 @@ namespace Flow.Launcher.ViewModel } } + private async Task LoadBadgeImageAsync() + { + var badgeImagePath = Result.BadgeIcoPath; + var badgeIconDelegate = Result.BadgeIcon; + if (ImageLoader.TryGetValue(badgeImagePath, false, out var img)) + { + _badgeImage = img; + } + else + { + // We need to modify the property not field here to trigger the OnPropertyChanged event + BadgeImage = await LoadImageInternalAsync(badgeImagePath, badgeIconDelegate, false).ConfigureAwait(false); + } + } + private async Task LoadPreviewImageAsync() { var imagePath = Result.Preview.PreviewImagePath ?? Result.IcoPath; var iconDelegate = Result.Preview.PreviewDelegate ?? Result.Icon; - if (ImageLoader.TryGetValue(imagePath, true, out ImageSource img)) + if (ImageLoader.TryGetValue(imagePath, true, out var img)) { _previewImage = img; }