@@ -12,22 +12,21 @@ namespace Flow.Launcher.Plugin.OneNote.Icons
12
12
{
13
13
public class IconProvider : BaseModel
14
14
{
15
- public const string Logo = IC . ImagesDirectory + IC . Logo ;
16
- public string Sync => GetIconLocal ( IC . Sync ) ;
17
- public string Search => GetIconLocal ( IC . Search ) ;
18
- public string Recent => GetIconLocal ( IC . Recent ) ;
19
- public string NotebookExplorer => GetIconLocal ( IC . NotebookExplorer ) ;
15
+ public const string Logo = IC . ImagesDirectory + IC . Logo + ".png" ;
16
+ public string Sync => GetIconPath ( IC . Sync ) ;
17
+ public string Search => GetIconPath ( IC . Search ) ;
18
+ public string Recent => GetIconPath ( IC . Recent ) ;
19
+ public string NotebookExplorer => GetIconPath ( IC . NotebookExplorer ) ;
20
20
public string QuickNote => NewPage ;
21
- public string NewPage => GetIconLocal ( IC . NewPage ) ;
22
- public string NewSection => GetIconLocal ( IC . NewSection ) ;
23
- public string NewSectionGroup => GetIconLocal ( IC . NewSectionGroup ) ;
24
- public string NewNotebook => GetIconLocal ( IC . NewNotebook ) ;
21
+ public string NewPage => GetIconPath ( IC . NewPage ) ;
22
+ public string NewSection => GetIconPath ( IC . NewSection ) ;
23
+ public string NewSectionGroup => GetIconPath ( IC . NewSectionGroup ) ;
24
+ public string NewNotebook => GetIconPath ( IC . NewNotebook ) ;
25
25
public string Warning => settings . IconTheme == IconTheme . Color
26
- ? $ "{ IC . ImagesDirectory } { IC . Warning } .{ GetIconThemeString ( IconTheme . Light ) } .png"
27
- : GetIconLocal ( IC . Warning ) ;
26
+ ? $ "{ IC . ImagesDirectory } { IC . Warning } .{ GetIconThemeString ( IconTheme . Dark ) } .png"
27
+ : GetIconPath ( IC . Warning ) ;
28
28
29
29
private readonly Settings settings ;
30
- // May need this? https://stackoverflow.com/questions/21867842/concurrentdictionarys-getoradd-is-not-atomic-any-alternatives-besides-locking
31
30
private readonly ConcurrentDictionary < string , ImageSource > iconCache = new ( ) ;
32
31
private readonly string imagesDirectory ;
33
32
@@ -53,9 +52,9 @@ public IconProvider(PluginInitContext context, Settings settings)
53
52
}
54
53
}
55
54
56
- private string GetIconLocal ( string icon ) => $ "{ IC . ImagesDirectory } { icon } .{ GetIconThemeString ( settings . IconTheme ) } .png";
55
+ private string GetIconPath ( string icon ) => $ "{ IC . ImagesDirectory } { icon } .{ GetIconThemeString ( settings . IconTheme ) } .png";
57
56
58
- private string GetIconThemeString ( IconTheme iconTheme )
57
+ private static string GetIconThemeString ( IconTheme iconTheme )
59
58
{
60
59
if ( iconTheme == IconTheme . System )
61
60
{
@@ -77,31 +76,31 @@ private static IconTheme FlowLauncherThemeToIconTheme()
77
76
}
78
77
79
78
private static BitmapImage BitmapImageFromPath ( string path ) => new BitmapImage ( new Uri ( path ) ) ;
80
-
79
+
81
80
public Result . IconDelegate GetIcon ( IconGeneratorInfo info )
82
- {
83
- return ( ) =>
84
- {
85
- bool generate = ( string . CompareOrdinal ( info . Prefix , IC . Notebook ) == 0
86
- || string . CompareOrdinal ( info . Prefix , IC . Section ) == 0 )
87
- && settings . CreateColoredIcons
88
- && info . Color . HasValue ;
89
-
90
- if ( generate )
91
- {
92
- var imageSource = iconCache . GetOrAdd ( $ "{ info . Prefix } .{ info . Color . Value . ToArgb ( ) } .png", ImageSourceFactory ,
93
- info . Color . Value ) ;
94
- OnPropertyChanged ( nameof ( CachedIconCount ) ) ;
95
- return imageSource ;
96
- }
81
+ {
82
+ bool generate = ( string . CompareOrdinal ( info . Prefix , IC . Notebook ) == 0
83
+ || string . CompareOrdinal ( info . Prefix , IC . Section ) == 0 )
84
+ && settings . CreateColoredIcons
85
+ && info . Color . HasValue ;
97
86
87
+ return generate ? GetIconGenerated : GetIconStatic ;
88
+
89
+ ImageSource GetIconGenerated ( )
90
+ {
91
+ var imageSource = iconCache . GetOrAdd ( $ "{ info . Prefix } .{ info . Color ! . Value . ToArgb ( ) } .png", ImageSourceFactory , info . Color . Value ) ;
92
+ OnPropertyChanged ( nameof ( CachedIconCount ) ) ;
93
+ return imageSource ;
94
+ }
95
+
96
+ ImageSource GetIconStatic ( )
97
+ {
98
98
return iconCache . GetOrAdd ( $ "{ info . Prefix } .{ GetIconThemeString ( settings . IconTheme ) } .png", key =>
99
99
{
100
100
var path = Path . Combine ( imagesDirectory , key ) ;
101
101
return BitmapImageFromPath ( path ) ;
102
102
} ) ;
103
-
104
- } ;
103
+ }
105
104
}
106
105
107
106
private ImageSource ImageSourceFactory ( string key , Color color )
@@ -112,13 +111,11 @@ private ImageSource ImageSourceFactory(string key, Color color)
112
111
var newBitmap = ChangeIconColor ( bitmap , color ) ;
113
112
114
113
path = $ "{ GeneratedImagesDirectoryInfo . FullName } { key } ";
115
- // https://stackoverflow.com/questions/65860129/pngbitmapencoder-failling
116
114
117
115
using var fileStream = new FileStream ( path , FileMode . Create ) ;
118
- var encoder = new PngBitmapEncoder ( ) ; //TODO Lazy load this and only one
116
+ var encoder = new PngBitmapEncoder ( ) ;
119
117
encoder . Frames . Add ( BitmapFrame . Create ( newBitmap ) ) ;
120
118
encoder . Save ( fileStream ) ;
121
- // encoder.Frames.Clear();
122
119
return newBitmap ;
123
120
}
124
121
0 commit comments