77using System . Threading . Tasks ;
88using System . Windows . Media ;
99using System . Windows . Media . Imaging ;
10- using Flow . Launcher . Infrastructure . Logger ;
10+ using CommunityToolkit . Mvvm . DependencyInjection ;
1111using Flow . Launcher . Infrastructure . Storage ;
12+ using Flow . Launcher . Plugin ;
1213
1314namespace Flow . Launcher . Infrastructure . Image
1415{
1516 public static class ImageLoader
1617 {
18+ // We should not initialize API in static constructor because it will create another API instance
19+ private static IPublicAPI api = null ;
20+ private static IPublicAPI API => api ??= Ioc . Default . GetRequiredService < IPublicAPI > ( ) ;
21+
22+ private static readonly string ClassName = nameof ( ImageLoader ) ;
23+
1724 private static readonly ImageCache ImageCache = new ( ) ;
1825 private static SemaphoreSlim storageLock { get ; } = new SemaphoreSlim ( 1 , 1 ) ;
1926 private static BinaryStorage < List < ( string , bool ) > > _storage ;
@@ -47,15 +54,14 @@ public static async Task InitializeAsync()
4754
4855 _ = Task . Run ( async ( ) =>
4956 {
50- await Stopwatch . NormalAsync ( "|ImageLoader.Initialize| Preload images cost", async ( ) =>
57+ await API . StopwatchLogInfoAsync ( ClassName , " Preload images cost", async ( ) =>
5158 {
5259 foreach ( var ( path , isFullImage ) in usage )
5360 {
5461 await LoadAsync ( path , isFullImage ) ;
5562 }
5663 } ) ;
57- Log . Info (
58- $ "|ImageLoader.Initialize|Number of preload images is <{ ImageCache . CacheSize ( ) } >, Images Number: { ImageCache . CacheSize ( ) } , Unique Items { ImageCache . UniqueImagesInCache ( ) } ") ;
64+ API . LogInfo ( ClassName , $ "Number of preload images is <{ ImageCache . CacheSize ( ) } >, Images Number: { ImageCache . CacheSize ( ) } , Unique Items { ImageCache . UniqueImagesInCache ( ) } ") ;
5965 } ) ;
6066 }
6167
@@ -71,7 +77,7 @@ await _storage.SaveAsync(ImageCache.EnumerateEntries()
7177 }
7278 catch ( System . Exception e )
7379 {
74- Log . Exception ( $ "|ImageLoader.SaveAsync| Failed to save image cache to file", e ) ;
80+ API . LogException ( ClassName , " Failed to save image cache to file", e ) ;
7581 }
7682 finally
7783 {
@@ -166,8 +172,8 @@ private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool
166172 }
167173 catch ( System . Exception e2 )
168174 {
169- Log . Exception ( $ "|ImageLoader.Load|Failed to get thumbnail for { path } on first try", e ) ;
170- Log . Exception ( $ "|ImageLoader.Load|Failed to get thumbnail for { path } on second try", e2 ) ;
175+ API . LogException ( ClassName , $ "|ImageLoader.Load|Failed to get thumbnail for { path } on first try", e ) ;
176+ API . LogException ( ClassName , $ "|ImageLoader.Load|Failed to get thumbnail for { path } on second try", e2 ) ;
171177
172178 ImageSource image = ImageCache [ Constant . MissingImgIcon , false ] ;
173179 ImageCache [ path , false ] = image ;
0 commit comments