Skip to content

Commit 5165ce8

Browse files
committed
Add LoadImageAsync api function
1 parent a3c18a6 commit 5165ce8

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

Flow.Launcher.Infrastructure/Image/ImageLoader.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public static bool TryGetValue(string path, bool loadFullImage, out ImageSource
277277
return ImageCache.TryGetValue(path, loadFullImage, out image);
278278
}
279279

280-
public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullImage = false)
280+
public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullImage = false, bool cacheImage = true)
281281
{
282282
var imageResult = await LoadInternalAsync(path, loadFullImage);
283283

@@ -293,16 +293,18 @@ public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullI
293293
// image already exists
294294
img = ImageCache[key, loadFullImage] ?? img;
295295
}
296-
else
296+
else if (cacheImage)
297297
{
298-
// new guid
299-
298+
// save guid key
300299
GuidToKey[hash] = path;
301300
}
302301
}
303302

304-
// update cache
305-
ImageCache[path, loadFullImage] = img;
303+
if (cacheImage)
304+
{
305+
// update cache
306+
ImageCache[path, loadFullImage] = img;
307+
}
306308
}
307309

308310
return img;

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 16 additions & 0 deletions
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
{
@@ -344,5 +345,20 @@ public interface IPublicAPI
344345
/// Stop the loading bar in main window
345346
/// </summary>
346347
public void StopLoadingBar();
348+
349+
/// <summary>
350+
/// Load image from path. Support local, remote and data:image url.
351+
/// If image path is missing, it will retun 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);
347363
}
348364
}

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 4 additions & 0 deletions
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;
@@ -342,6 +343,9 @@ public MessageBoxResult ShowMsgBox(string messageBoxText, string caption = "", M
342343

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

346+
public ValueTask<ImageSource> LoadImageAsync(string path, bool loadFullImage = false, bool cacheImage = true) =>
347+
ImageLoader.LoadAsync(path, loadFullImage, cacheImage);
348+
345349
#endregion
346350

347351
#region Private Methods

0 commit comments

Comments
 (0)