1+ using Odotocodot . OneNote . Linq ;
2+ using System ;
3+ using System . IO ;
4+
5+ namespace Flow . Launcher . Plugin . OneNote
6+ {
7+ public class Icons : BaseModel
8+ {
9+ public const string Logo = "Images/logo.png" ;
10+ public const string Sync = "Images/refresh.png" ;
11+ public const string Warning = "Images/warning.png" ;
12+ public const string Search = Logo ;
13+ public const string RecycleBin = "Images/recycle_bin.png" ;
14+ public const string Recent = "Images/recent.png" ;
15+ public const string RecentPage = "Images/recent_page.png" ;
16+
17+ public const string Page = Logo ;
18+ public const string Section = "Images/section.png" ;
19+ public const string SectionGroup = "Images/section_group.png" ;
20+ public const string Notebook = "Images/notebook.png" ;
21+
22+ public const string NewPage = "Images/new_page.png" ;
23+ public const string NewSection = "Images/new_section.png" ;
24+ public const string NewSectionGroup = "Images/new_section_group.png" ;
25+ public const string NewNotebook = "Images/new_notebook.png" ;
26+
27+ private OneNoteItemIcons notebookIcons ;
28+ private OneNoteItemIcons sectionIcons ;
29+ private Settings settings ;
30+
31+ public int CachedIconCount => notebookIcons . IconCount + sectionIcons . IconCount ;
32+ public string CachedIconsFileSize => GetBytesReadable ( notebookIcons . IconsFileSize + sectionIcons . IconsFileSize ) ;
33+ public static string NotebookIconDirectory { get ; private set ; }
34+ public static string SectionIconDirectory { get ; private set ; }
35+
36+
37+ private static readonly Lazy < Icons > lazy = new ( ) ;
38+ public static Icons Instance => lazy . Value ;
39+
40+ public static void Init ( PluginInitContext context , Settings settings )
41+ {
42+ NotebookIconDirectory = Path . Combine ( context . CurrentPluginMetadata . PluginDirectory , "Images" , "NotebookIcons" ) ;
43+ SectionIconDirectory = Path . Combine ( context . CurrentPluginMetadata . PluginDirectory , "Images" , "SectionIcons" ) ;
44+
45+ Instance . notebookIcons = new OneNoteItemIcons ( NotebookIconDirectory , Path . Combine ( context . CurrentPluginMetadata . PluginDirectory , Notebook ) ) ;
46+ Instance . sectionIcons = new OneNoteItemIcons ( SectionIconDirectory , Path . Combine ( context . CurrentPluginMetadata . PluginDirectory , Section ) ) ;
47+
48+
49+ Instance . notebookIcons . PropertyChanged += Instance . IconCountChanged ;
50+ Instance . sectionIcons . PropertyChanged += Instance . IconCountChanged ;
51+
52+ Instance . settings = settings ;
53+ }
54+
55+ public static void Close ( )
56+ {
57+ Instance . notebookIcons . PropertyChanged -= Instance . IconCountChanged ;
58+ Instance . sectionIcons . PropertyChanged -= Instance . IconCountChanged ;
59+ }
60+ private void IconCountChanged ( object sender , System . ComponentModel . PropertyChangedEventArgs e )
61+ {
62+ OnPropertyChanged ( nameof ( CachedIconCount ) ) ;
63+ OnPropertyChanged ( nameof ( CachedIconsFileSize ) ) ;
64+ }
65+
66+ public static string GetIcon ( IOneNoteItem item )
67+ {
68+ return item switch
69+ {
70+ OneNoteNotebook notebook => Instance . settings . CreateColoredIcons && notebook . Color . HasValue
71+ ? Instance . notebookIcons . GetIcon ( notebook . Color . Value )
72+ : Notebook ,
73+ OneNoteSectionGroup sectionGroup => sectionGroup . IsRecycleBin
74+ ? RecycleBin
75+ : SectionGroup ,
76+ OneNoteSection section => Instance . settings . CreateColoredIcons && section . Color . HasValue
77+ ? Instance . sectionIcons . GetIcon ( section . Color . Value )
78+ : Section ,
79+ OneNotePage => Page ,
80+ _ => Warning ,
81+ } ;
82+ }
83+
84+ public void ClearCachedIcons ( )
85+ {
86+ notebookIcons . ClearCachedIcons ( ) ;
87+ sectionIcons . ClearCachedIcons ( ) ;
88+ }
89+
90+ // Returns the human-readable file size for an arbitrary, 64-bit file size
91+ // The default format is "0.### XB", e.g. "4.2 KB" or "1.434 GB"
92+ private static string GetBytesReadable ( long i )
93+ {
94+ // Get absolute value
95+ long absolute_i = Math . Abs ( i ) ;
96+ // Determine the suffix and readable value
97+ string suffix ;
98+ double readable ;
99+ switch ( absolute_i )
100+ {
101+ case >= 0x40000000 : // Gigabyte
102+ suffix = "GB" ;
103+ readable = i >> 20 ;
104+ break ;
105+ case >= 0x100000 : // Megabyte
106+ suffix = "MB" ;
107+ readable = i >> 10 ;
108+ break ;
109+ case >= 0x400 :
110+ suffix = "KB" ; // Kilobyte
111+ readable = i ;
112+ break ;
113+ default :
114+ return i . ToString ( "0 B" ) ; // Byte
115+ }
116+ // Divide by 1024 to get fractional value
117+ readable /= 1024 ;
118+ // Return formatted number with suffix
119+ return readable . ToString ( "0.## " ) + suffix ;
120+ }
121+
122+ }
123+ }
0 commit comments