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 ;
1213using SharpVectors . Converters ;
1314using SharpVectors . Renderers . Wpf ;
1415
1516namespace Flow . Launcher . Infrastructure . Image
1617{
1718 public static class ImageLoader
1819 {
20+ // We should not initialize API in static constructor because it will create another API instance
21+ private static IPublicAPI api = null ;
22+ private static IPublicAPI API => api ??= Ioc . Default . GetRequiredService < IPublicAPI > ( ) ;
23+
24+ private static readonly string ClassName = nameof ( ImageLoader ) ;
25+
1926 private static readonly ImageCache ImageCache = new ( ) ;
2027 private static SemaphoreSlim storageLock { get ; } = new SemaphoreSlim ( 1 , 1 ) ;
2128 private static BinaryStorage < List < ( string , bool ) > > _storage ;
@@ -51,15 +58,14 @@ public static async Task InitializeAsync()
5158
5259 _ = Task . Run ( async ( ) =>
5360 {
54- await Stopwatch . NormalAsync ( "|ImageLoader.Initialize| Preload images cost", async ( ) =>
61+ await API . StopwatchLogInfoAsync ( ClassName , " Preload images cost", async ( ) =>
5562 {
5663 foreach ( var ( path , isFullImage ) in usage )
5764 {
5865 await LoadAsync ( path , isFullImage ) ;
5966 }
6067 } ) ;
61- Log . Info (
62- $ "|ImageLoader.Initialize|Number of preload images is <{ ImageCache . CacheSize ( ) } >, Images Number: { ImageCache . CacheSize ( ) } , Unique Items { ImageCache . UniqueImagesInCache ( ) } ") ;
68+ API . LogInfo ( ClassName , $ "Number of preload images is <{ ImageCache . CacheSize ( ) } >, Images Number: { ImageCache . CacheSize ( ) } , Unique Items { ImageCache . UniqueImagesInCache ( ) } ") ;
6369 } ) ;
6470 }
6571
@@ -75,7 +81,7 @@ await _storage.SaveAsync(ImageCache.EnumerateEntries()
7581 }
7682 catch ( System . Exception e )
7783 {
78- Log . Exception ( $ "|ImageLoader.SaveAsync| Failed to save image cache to file", e ) ;
84+ API . LogException ( ClassName , " Failed to save image cache to file", e ) ;
7985 }
8086 finally
8187 {
@@ -170,8 +176,8 @@ private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool
170176 }
171177 catch ( System . Exception e2 )
172178 {
173- Log . Exception ( $ "|ImageLoader.Load|Failed to get thumbnail for { path } on first try", e ) ;
174- Log . Exception ( $ "|ImageLoader.Load|Failed to get thumbnail for { path } on second try", e2 ) ;
179+ API . LogException ( ClassName , $ "|ImageLoader.Load|Failed to get thumbnail for { path } on first try", e ) ;
180+ API . LogException ( ClassName , $ "|ImageLoader.Load|Failed to get thumbnail for { path } on second try", e2 ) ;
175181
176182 ImageSource image = ImageCache [ Constant . MissingImgIcon , false ] ;
177183 ImageCache [ path , false ] = image ;
0 commit comments