Skip to content

Commit 75f23aa

Browse files
authored
Merge pull request #3412 from Jack251970/load_image_async
New API Function LoadImageAsync
2 parents 6ebb73b + 6af79a1 commit 75f23aa

File tree

14 files changed

+53
-46
lines changed

14 files changed

+53
-46
lines changed

Flow.Launcher.Infrastructure/Image/ImageLoader.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public static bool TryGetValue(string path, bool loadFullImage, out ImageSource
284284
return ImageCache.TryGetValue(path, loadFullImage, out image);
285285
}
286286

287-
public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullImage = false)
287+
public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullImage = false, bool cacheImage = true)
288288
{
289289
var imageResult = await LoadInternalAsync(path, loadFullImage);
290290

@@ -300,16 +300,18 @@ public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullI
300300
// image already exists
301301
img = ImageCache[key, loadFullImage] ?? img;
302302
}
303-
else
303+
else if (cacheImage)
304304
{
305-
// new guid
306-
305+
// save guid key
307306
GuidToKey[hash] = path;
308307
}
309308
}
310309

311-
// update cache
312-
ImageCache[path, loadFullImage] = img;
310+
if (cacheImage)
311+
{
312+
// update cache
313+
ImageCache[path, loadFullImage] = img;
314+
}
313315
}
314316

315317
return img;

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading;
99
using System.Threading.Tasks;
1010
using System.Windows;
11+
using System.Windows.Media;
1112

1213
namespace Flow.Launcher.Plugin
1314
{
@@ -346,6 +347,20 @@ public interface IPublicAPI
346347
public void StopLoadingBar();
347348

348349
/// <summary>
350+
/// Load image from path. Support local, remote and data:image url.
351+
/// If image path is missing, it will return a missing icon.
352+
/// </summary>
353+
/// <param name="path">The path of the image.</param>
354+
/// <param name="loadFullImage">
355+
/// Load full image or not.
356+
/// </param>
357+
/// <param name="cacheImage">
358+
/// Cache the image or not. Cached image will be stored in FL cache.
359+
/// If the image is just used one time, it's better to set this to false.
360+
/// </param>
361+
/// <returns></returns>
362+
ValueTask<ImageSource> LoadImageAsync(string path, bool loadFullImage = false, bool cacheImage = true);
363+
349364
/// Update the plugin manifest
350365
/// </summary>
351366
/// <param name="usePrimaryUrlOnly">

Flow.Launcher/MessageBoxEx.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private static async Task SetImageOfMessageBoxAsync(MessageBoxImage icon)
156156
private async Task SetImageAsync(string imageName)
157157
{
158158
var imagePath = Path.Combine(Constant.ProgramDirectory, "Images", imageName);
159-
var imageSource = await ImageLoader.LoadAsync(imagePath);
159+
var imageSource = await App.API.LoadImageAsync(imagePath);
160160
Img.Source = imageSource;
161161
}
162162

Flow.Launcher/Msg.xaml.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public Msg()
4343

4444
private async System.Threading.Tasks.Task LoadImageAsync()
4545
{
46-
imgClose.Source = await ImageLoader.LoadAsync(Path.Combine(Infrastructure.Constant.ProgramDirectory, "Images\\close.png"));
46+
imgClose.Source = await App.API.LoadImageAsync(Path.Combine(Constant.ProgramDirectory, "Images\\close.png"));
4747
}
4848

4949
void imgClose_MouseUp(object sender, MouseButtonEventArgs e)
@@ -71,11 +71,11 @@ public async void Show(string title, string subTitle, string iconPath)
7171

7272
if (!File.Exists(iconPath))
7373
{
74-
imgIco.Source = await ImageLoader.LoadAsync(Path.Combine(Constant.ProgramDirectory, "Images\\app.png"));
74+
imgIco.Source = await App.API.LoadImageAsync(Path.Combine(Constant.ProgramDirectory, "Images\\app.png"));
7575
}
7676
else
7777
{
78-
imgIco.Source = await ImageLoader.LoadAsync(iconPath);
78+
imgIco.Source = await App.API.LoadImageAsync(iconPath);
7979
}
8080

8181
Show();

Flow.Launcher/PublicAPIInstance.cs

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Threading;
1111
using System.Threading.Tasks;
1212
using System.Windows;
13+
using System.Windows.Media;
1314
using CommunityToolkit.Mvvm.DependencyInjection;
1415
using Squirrel;
1516
using Flow.Launcher.Core;
@@ -355,6 +356,9 @@ public MessageBoxResult ShowMsgBox(string messageBoxText, string caption = "", M
355356

356357
public Task ShowProgressBoxAsync(string caption, Func<Action<double>, Task> reportProgressAsync, Action cancelProgress = null) => ProgressBoxEx.ShowAsync(caption, reportProgressAsync, cancelProgress);
357358

359+
public ValueTask<ImageSource> LoadImageAsync(string path, bool loadFullImage = false, bool cacheImage = true) =>
360+
ImageLoader.LoadAsync(path, loadFullImage, cacheImage);
361+
358362
public Task<bool> UpdatePluginManifestAsync(bool usePrimaryUrlOnly = false, CancellationToken token = default) =>
359363
PluginsManifest.UpdateManifestAsync(usePrimaryUrlOnly, token);
360364

Flow.Launcher/ViewModel/MainViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
11681168
else if (plugins.Count == 1)
11691169
{
11701170
PluginIconPath = plugins.Single().Metadata.IcoPath;
1171-
PluginIconSource = await ImageLoader.LoadAsync(PluginIconPath);
1171+
PluginIconSource = await App.API.LoadImageAsync(PluginIconPath);
11721172
SearchIconVisibility = Visibility.Hidden;
11731173
}
11741174
else

Flow.Launcher/ViewModel/PluginViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static string PluginManagerActionKeyword
4545

4646
private async Task LoadIconAsync()
4747
{
48-
Image = await ImageLoader.LoadAsync(PluginPair.Metadata.IcoPath);
48+
Image = await App.API.LoadImageAsync(PluginPair.Metadata.IcoPath);
4949
OnPropertyChanged(nameof(Image));
5050
}
5151

Flow.Launcher/ViewModel/ResultViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private async Task<ImageSource> LoadImageInternalAsync(string imagePath, Result.
199199
}
200200
}
201201

202-
return await ImageLoader.LoadAsync(imagePath, loadFullImage).ConfigureAwait(false);
202+
return await App.API.LoadImageAsync(imagePath, loadFullImage).ConfigureAwait(false);
203203
}
204204

205205
private async Task LoadImageAsync()

Plugins/Flow.Launcher.Plugin.Explorer/Views/PreviewPanel.xaml.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Windows.Controls;
88
using System.Windows.Media;
99
using System.Windows.Media.Imaging;
10-
using Flow.Launcher.Infrastructure.Image;
1110
using Flow.Launcher.Plugin.Explorer.Search;
1211

1312
namespace Flow.Launcher.Plugin.Explorer.Views;
@@ -89,7 +88,7 @@ public PreviewPanel(Settings settings, string filePath)
8988

9089
private async Task LoadImageAsync()
9190
{
92-
PreviewImage = await ImageLoader.LoadAsync(FilePath, true).ConfigureAwait(false);
91+
PreviewImage = await Main.Context.API.LoadImageAsync(FilePath, true).ConfigureAwait(false);
9392
}
9493

9594
public event PropertyChangedEventHandler? PropertyChanged;

Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceViewModel.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Flow.Launcher.Infrastructure.Image;
2-
using System;
1+
using System;
32
using System.IO;
43
using System.Threading.Tasks;
54
#pragma warning disable IDE0005
@@ -41,8 +40,8 @@ public void CopyNewImageToUserDataDirectoryIfRequired(
4140
#if DEBUG
4241
throw;
4342
#else
44-
Main._context.API.ShowMsgBox(string.Format("Copying the selected image file to {0} has failed, changes will now be reverted", destinationFileNameFullPath));
45-
UpdateIconAttributes(selectedSearchSource, fullPathToOriginalImage);
43+
Main._context.API.ShowMsgBox(string.Format("Copying the selected image file to {0} has failed, changes will now be reverted", destinationFileNameFullPath));
44+
UpdateIconAttributes(selectedSearchSource, fullPathToOriginalImage);
4645
#endif
4746
}
4847
}
@@ -61,7 +60,7 @@ internal bool ShouldProvideHint(string fullPathToSelectedImage)
6160

6261
internal async ValueTask<ImageSource> LoadPreviewIconAsync(string pathToPreviewIconImage)
6362
{
64-
return await ImageLoader.LoadAsync(pathToPreviewIconImage);
63+
return await Main._context.API.LoadImageAsync(pathToPreviewIconImage);
6564
}
6665
}
6766
}

Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using System.Text.Json;
55
using System.Text.RegularExpressions;
66
using System.Threading.Tasks;
7-
using Flow.Launcher.Infrastructure.Http;
8-
using Flow.Launcher.Infrastructure.Logger;
97
using System.Net.Http;
108
using System.Threading;
119

@@ -22,11 +20,11 @@ public override async Task<List<string>> SuggestionsAsync(string query, Cancella
2220
try
2321
{
2422
const string api = "http://suggestion.baidu.com/su?json=1&wd=";
25-
result = await Http.GetAsync(api + Uri.EscapeDataString(query), token).ConfigureAwait(false);
23+
result = await Main._context.API.HttpGetStringAsync(api + Uri.EscapeDataString(query), token).ConfigureAwait(false);
2624
}
2725
catch (Exception e) when (e is HttpRequestException or {InnerException: TimeoutException})
2826
{
29-
Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e);
27+
Main._context.API.LogException(nameof(Baidu), "Can't get suggestion from Baidu", e);
3028
return null;
3129
}
3230

@@ -41,7 +39,7 @@ public override async Task<List<string>> SuggestionsAsync(string query, Cancella
4139
}
4240
catch (JsonException e)
4341
{
44-
Log.Exception("|Baidu.Suggestions|can't parse suggestions", e);
42+
Main._context.API.LogException(nameof(Baidu), "Can't parse suggestions", e);
4543
return new List<string>();
4644
}
4745

Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using Flow.Launcher.Infrastructure.Http;
2-
using Flow.Launcher.Infrastructure.Logger;
3-
using System;
1+
using System;
42
using System.Collections.Generic;
53
using System.Net.Http;
64
using System.Threading.Tasks;
@@ -10,16 +8,15 @@
108

119
namespace Flow.Launcher.Plugin.WebSearch.SuggestionSources
1210
{
13-
class Bing : SuggestionSource
11+
public class Bing : SuggestionSource
1412
{
1513
public override async Task<List<string>> SuggestionsAsync(string query, CancellationToken token)
1614
{
17-
1815
try
1916
{
2017
const string api = "https://api.bing.com/qsonhs.aspx?q=";
2118

22-
await using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeDataString(query), token).ConfigureAwait(false);
19+
await using var resultStream = await Main._context.API.HttpGetStreamAsync(api + Uri.EscapeDataString(query), token).ConfigureAwait(false);
2320

2421
using var json = (await JsonDocument.ParseAsync(resultStream, cancellationToken: token));
2522
var root = json.RootElement.GetProperty("AS");
@@ -33,18 +30,15 @@ public override async Task<List<string>> SuggestionsAsync(string query, Cancella
3330
.EnumerateArray()
3431
.Select(s => s.GetProperty("Txt").GetString()))
3532
.ToList();
36-
37-
38-
3933
}
4034
catch (Exception e) when (e is HttpRequestException or { InnerException: TimeoutException })
4135
{
42-
Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e);
36+
Main._context.API.LogException(nameof(Bing), "Can't get suggestion from Bing", e);
4337
return null;
4438
}
4539
catch (JsonException e)
4640
{
47-
Log.Exception("|Bing.Suggestions|can't parse suggestions", e);
41+
Main._context.API.LogException(nameof(Bing), "Can't parse suggestions", e);
4842
return new List<string>();
4943
}
5044
}

Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/DuckDuckGo.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
5-
using Flow.Launcher.Infrastructure.Http;
6-
using Flow.Launcher.Infrastructure.Logger;
75
using System.Net.Http;
86
using System.Threading;
97
using System.Text.Json;
@@ -25,7 +23,7 @@ public override async Task<List<string>> SuggestionsAsync(string query, Cancella
2523
{
2624
const string api = "https://duckduckgo.com/ac/?type=list&q=";
2725

28-
await using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeDataString(query), token: token).ConfigureAwait(false);
26+
await using var resultStream = await Main._context.API.HttpGetStreamAsync(api + Uri.EscapeDataString(query), token: token).ConfigureAwait(false);
2927

3028
using var json = await JsonDocument.ParseAsync(resultStream, cancellationToken: token);
3129

@@ -36,12 +34,12 @@ public override async Task<List<string>> SuggestionsAsync(string query, Cancella
3634
}
3735
catch (Exception e) when (e is HttpRequestException or {InnerException: TimeoutException})
3836
{
39-
Log.Exception("|DuckDuckGo.Suggestions|Can't get suggestion from DuckDuckGo", e);
37+
Main._context.API.LogException(nameof(DuckDuckGo), "Can't get suggestion from DuckDuckGo", e);
4038
return null;
4139
}
4240
catch (JsonException e)
4341
{
44-
Log.Exception("|DuckDuckGo.Suggestions|can't parse suggestions", e);
42+
Main._context.API.LogException(nameof(DuckDuckGo), "Can't parse suggestions", e);
4543
return new List<string>();
4644
}
4745
}

Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
5-
using Flow.Launcher.Infrastructure.Http;
6-
using Flow.Launcher.Infrastructure.Logger;
75
using System.Net.Http;
86
using System.Threading;
97
using System.Text.Json;
@@ -18,7 +16,7 @@ public override async Task<List<string>> SuggestionsAsync(string query, Cancella
1816
{
1917
const string api = "https://www.google.com/complete/search?output=chrome&q=";
2018

21-
await using var resultStream = await Http.GetStreamAsync(api + Uri.EscapeDataString(query), token: token).ConfigureAwait(false);
19+
await using var resultStream = await Main._context.API.HttpGetStreamAsync(api + Uri.EscapeDataString(query), token: token).ConfigureAwait(false);
2220

2321
using var json = await JsonDocument.ParseAsync(resultStream, cancellationToken: token);
2422

@@ -29,12 +27,12 @@ public override async Task<List<string>> SuggestionsAsync(string query, Cancella
2927
}
3028
catch (Exception e) when (e is HttpRequestException or {InnerException: TimeoutException})
3129
{
32-
Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e);
30+
Main._context.API.LogException(nameof(Google), "Can't get suggestion from Google", e);
3331
return null;
3432
}
3533
catch (JsonException e)
3634
{
37-
Log.Exception("|Google.Suggestions|can't parse suggestions", e);
35+
Main._context.API.LogException(nameof(Google), "Can't parse suggestions", e);
3836
return new List<string>();
3937
}
4038
}

0 commit comments

Comments
 (0)