Skip to content

Commit 1439ee7

Browse files
taoocerosbao-qian
andcommitted
Use a default image to list when the image hasn't been loaded in cache.
Co-authored-by: Bao-Qian <[email protected]>
1 parent 2c14c21 commit 1439ee7

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

Flow.Launcher.Infrastructure/Image/ImageCache.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class ImageCache
2626
private const int MaxCached = 50;
2727
public ConcurrentDictionary<string, ImageUsage> Data { get; private set; } = new ConcurrentDictionary<string, ImageUsage>();
2828
private const int permissibleFactor = 2;
29-
29+
3030
public void Initialization(Dictionary<string, int> usage)
3131
{
3232
foreach (var key in usage.Keys)
@@ -44,14 +44,14 @@ public ImageSource this[string path]
4444
value.usage++;
4545
return value.imageSource;
4646
}
47-
47+
4848
return null;
4949
}
5050
set
5151
{
5252
Data.AddOrUpdate(
53-
path,
54-
new ImageUsage(0, value),
53+
path,
54+
new ImageUsage(0, value),
5555
(k, v) =>
5656
{
5757
v.imageSource = value;
@@ -67,7 +67,8 @@ public ImageSource this[string path]
6767
// To delete the images from the data dictionary based on the resizing of the Usage Dictionary.
6868

6969

70-
foreach (var key in Data.OrderBy(x => x.Value.usage).Take(Data.Count - MaxCached).Select(x => x.Key))
70+
foreach (var key in Data.Where(x => x.Key != Constant.MissingImgIcon)
71+
.OrderBy(x => x.Value.usage).Take(Data.Count - MaxCached).Select(x => x.Key))
7172
{
7273
if (!(key.Equals(Constant.ErrorIcon) || key.Equals(Constant.DefaultIcon)))
7374
{
@@ -80,7 +81,7 @@ public ImageSource this[string path]
8081

8182
public bool ContainsKey(string key)
8283
{
83-
var contains = Data.ContainsKey(key);
84+
var contains = Data.ContainsKey(key) && Data[key] != null;
8485
return contains;
8586
}
8687

Flow.Launcher.Infrastructure/Image/ImageLoader.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static void Save()
6161
{
6262
lock (_storage)
6363
{
64-
_storage.Save(ImageCache.Data.Select(x => (x.Key, x.Value.usage)).ToDictionary(x => x.Key, y => y.usage));
64+
_storage.Save(ImageCache.Data.Select(x => (x.Key, x.Value.usage)).ToDictionary(x => x.Key, x => x.usage));
6565
}
6666
}
6767

@@ -211,6 +211,15 @@ private static BitmapSource GetThumbnail(string path, ThumbnailOptions option =
211211
option);
212212
}
213213

214+
public static bool CacheContainImage(string path)
215+
{
216+
return ImageCache.ContainsKey(path);
217+
}
218+
public static ImageSource LoadDefault(bool loadFullImage = false)
219+
{
220+
return LoadInternal(Constant.MissingImgIcon, loadFullImage).ImageSource;
221+
}
222+
214223
public static ImageSource Load(string path, bool loadFullImage = false)
215224
{
216225
var imageResult = LoadInternal(path, loadFullImage);

Flow.Launcher/ViewModel/ResultViewModel.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using System.Windows;
34
using System.Windows.Media;
45
using System.Windows.Threading;
@@ -26,18 +27,18 @@ public ResultViewModel(Result result, Settings settings)
2627

2728
public Settings Settings { get; private set; }
2829

29-
public Visibility ShowOpenResultHotkey => Settings.ShowOpenResultHotkey ? Visibility.Visible : Visibility.Hidden;
30+
public Visibility ShowOpenResultHotkey => Settings.ShowOpenResultHotkey ? Visibility.Visible : Visibility.Hidden;
3031

3132
public string OpenResultModifiers => Settings.OpenResultModifiers;
3233

3334
public string ShowTitleToolTip => string.IsNullOrEmpty(Result.TitleToolTip)
34-
? Result.Title
35+
? Result.Title
3536
: Result.TitleToolTip;
3637

3738
public string ShowSubTitleToolTip => string.IsNullOrEmpty(Result.SubTitleToolTip)
38-
? Result.SubTitle
39+
? Result.SubTitle
3940
: Result.SubTitleToolTip;
40-
41+
4142
public Lazy<ImageSource> Image { get; set; }
4243

4344
private ImageSource SetImage
@@ -57,9 +58,20 @@ private ImageSource SetImage
5758
imagePath = Constant.MissingImgIcon;
5859
}
5960
}
60-
61-
// will get here either when icoPath has value\icon delegate is null\when had exception in delegate
62-
return ImageLoader.Load(imagePath);
61+
62+
if (ImageLoader.CacheContainImage(imagePath))
63+
// will get here either when icoPath has value\icon delegate is null\when had exception in delegate
64+
return ImageLoader.Load(imagePath);
65+
else
66+
{
67+
Task.Run(() =>
68+
{
69+
Image = new Lazy<ImageSource>(() => ImageLoader.Load(imagePath));
70+
OnPropertyChanged(nameof(Image));
71+
});
72+
73+
return ImageLoader.LoadDefault();
74+
}
6375
}
6476
}
6577

@@ -78,7 +90,7 @@ public override bool Equals(object obj)
7890
}
7991
}
8092

81-
93+
8294
public override int GetHashCode()
8395
{
8496
return Result.GetHashCode();

0 commit comments

Comments
 (0)