From 0f3cb5da8da1c9f3be6cc1bef131a5d9a804714a Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Thu, 18 Jan 2024 17:49:12 -0600 Subject: [PATCH] bind to task instead of the complex loading scheme --- Flow.Launcher/ResultListBox.axaml | 2 +- Flow.Launcher/ViewModel/ResultViewModel.cs | 68 ++++++---------------- 2 files changed, 20 insertions(+), 50 deletions(-) diff --git a/Flow.Launcher/ResultListBox.axaml b/Flow.Launcher/ResultListBox.axaml index ab7ccf110..df4590570 100644 --- a/Flow.Launcher/ResultListBox.axaml +++ b/Flow.Launcher/ResultListBox.axaml @@ -83,7 +83,7 @@ Margin="0,0,0,0" HorizontalAlignment="Center" IsHitTestVisible="False" - Source="{Binding Image, TargetNullValue={x:Null}}" + Source="{Binding Image^, FallbackValue={Binding DefaultImage}, TargetNullValue={x:Null}}" Stretch="Uniform" IsVisible="{Binding ShowIcon}"> diff --git a/Flow.Launcher/ViewModel/ResultViewModel.cs b/Flow.Launcher/ViewModel/ResultViewModel.cs index fa152666f..d6eb2cf6b 100644 --- a/Flow.Launcher/ViewModel/ResultViewModel.cs +++ b/Flow.Launcher/ViewModel/ResultViewModel.cs @@ -58,12 +58,16 @@ namespace Flow.Launcher.ViewModel Glyph = glyph; } } + + LoadImage(); } private Settings Settings { get; } public bool ShowOpenResultHotkey => Settings.ShowOpenResultHotkey; + public Bitmap DefaultImage => ImageLoader.LoadingImage; + public bool ShowDefaultPreview => Result.PreviewPanel != null; public bool ShowCustomizedPreview => Result.PreviewPanel != null; @@ -144,32 +148,9 @@ namespace Flow.Launcher.ViewModel ? Result.SubTitle : Result.SubTitleToolTip; - private volatile bool ImageLoaded; - private volatile bool PreviewImageLoaded; + public Task Image { get; set; } - private Bitmap image = default; //ImageLoader.LoadingImage; - private Bitmap previewImage = default; //ImageLoader.LoadingImage; - - public Bitmap Image - { - get - { - if (!ImageLoaded) - { - ImageLoaded = true; - _ = LoadImageAsync(); - } - - return image; - } - private set => image = value; - } - - public Bitmap PreviewImage - { - get => previewImage; - private set => previewImage = value; - } + public Task PreviewImage { get; set; } /// /// Determines if to use the full width of the preview panel @@ -199,48 +180,37 @@ namespace Flow.Launcher.ViewModel return default; // await ImageLoader.LoadAsync(imagePath, loadFullImage).ConfigureAwait(false); } - private async Task LoadImageAsync() + private void LoadImage() { var imagePath = Result.IcoPath; var iconDelegate = Result.Icon; if (ImageLoader.TryGetValue(imagePath, false, out var img)) { - image = img; + Image = Task.FromResult(img); } else { // We need to modify the property not field here to trigger the OnPropertyChanged event - Image = await LoadImageInternalAsync(imagePath, iconDelegate, 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 var img)) - { - previewImage = img; - } - else - { - // We need to modify the property not field here to trigger the OnPropertyChanged event - PreviewImage = await LoadImageInternalAsync(imagePath, iconDelegate, true).ConfigureAwait(false); + Image = LoadImageInternalAsync(imagePath, iconDelegate, false); } } public void LoadPreviewImage() { - if (ShowDefaultPreview) + var imagePath = Result.Preview.PreviewImagePath ?? Result.IcoPath; + var iconDelegate = Result.Preview.PreviewDelegate ?? Result.Icon; + if (ImageLoader.TryGetValue(imagePath, true, out var img)) { - if (!PreviewImageLoaded && ShowPreviewImage) - { - PreviewImageLoaded = true; - _ = LoadPreviewImageAsync(); - } + PreviewImage = Task.FromResult(img); + } + else + { + // We need to modify the property not field here to trigger the OnPropertyChanged event + PreviewImage = LoadImageInternalAsync(imagePath, iconDelegate, true); } } + public Result Result { get; } public int ResultProgress