Skip to content

Commit beaa2ff

Browse files
committed
Small tweaks and code cleanup
Reduce compiler warnings. Improve nomenclature
1 parent 35995a1 commit beaa2ff

17 files changed

Lines changed: 69 additions & 76 deletions

Flow.Launcher.Plugin.OneNote/Icons/IconGeneratorInfo.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@
33

44
namespace Flow.Launcher.Plugin.OneNote.Icons
55
{
6-
public record struct IconGeneratorInfo
6+
public struct IconGeneratorInfo
77
{
8-
public string Prefix { get; }
9-
public Color? Color { get; }
8+
public readonly string prefix = string.Empty;
9+
public readonly Color? color;
1010

1111
public IconGeneratorInfo(IOneNoteItem item)
1212
{
1313
switch (item)
1414
{
1515
case OneNoteNotebook n:
16-
Prefix = IconConstants.Notebook;
17-
Color = n.Color;
16+
prefix = IconConstants.Notebook;
17+
color = n.Color;
1818
break;
1919
case OneNoteSectionGroup sg:
20-
Prefix = sg.IsRecycleBin ? IconConstants.RecycleBin : IconConstants.SectionGroup;
20+
prefix = sg.IsRecycleBin ? IconConstants.RecycleBin : IconConstants.SectionGroup;
2121
break;
2222
case OneNoteSection s:
23-
Prefix = IconConstants.Section;
24-
Color = s.Color;
23+
prefix = IconConstants.Section;
24+
color = s.Color;
2525
break;
2626
case OneNotePage:
27-
Prefix = IconConstants.Page;
27+
prefix = IconConstants.Page;
2828
break;
2929
}
3030
}

Flow.Launcher.Plugin.OneNote/Icons/IconProvider.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ namespace Flow.Launcher.Plugin.OneNote.Icons
1212
{
1313
public class IconProvider : BaseModel
1414
{
15+
private readonly PluginInitContext context;
16+
private readonly Settings settings;
17+
private readonly string imagesDirectory;
18+
private readonly ConcurrentDictionary<string, ImageSource> iconCache = new();
19+
1520
public const string Logo = IC.ImagesDirectory + IC.Logo + ".png";
1621
public string Sync => GetIconPath(IC.Sync);
1722
public string Search => GetIconPath(IC.Search);
@@ -23,17 +28,12 @@ public class IconProvider : BaseModel
2328
public string NewSectionGroup => GetIconPath(IC.NewSectionGroup);
2429
public string NewNotebook => GetIconPath(IC.NewNotebook);
2530
public string Warning => settings.IconTheme == IconTheme.Color
26-
? $"{IC.ImagesDirectory}{IC.Warning}.{GetIconThemeString(IconTheme.Dark)}.png"
27-
: GetIconPath(IC.Warning);
28-
29-
private readonly Settings settings;
30-
private readonly ConcurrentDictionary<string,ImageSource> iconCache = new();
31-
private readonly string imagesDirectory;
31+
? $"{IC.ImagesDirectory}{IC.Warning}.{GetIconThemeString(IconTheme.Dark)}.png"
32+
: GetIconPath(IC.Warning);
3233

33-
public DirectoryInfo GeneratedImagesDirectoryInfo { get; }
3434
public int CachedIconCount => iconCache.Keys.Count(k => char.IsDigit(k.Split('.')[1][1]));
35-
36-
private readonly PluginInitContext context;
35+
public DirectoryInfo GeneratedImagesDirectoryInfo { get; }
36+
3737

3838
public IconProvider(PluginInitContext context, Settings settings)
3939
{
@@ -60,7 +60,7 @@ private static string GetIconThemeString(IconTheme iconTheme)
6060
{
6161
iconTheme = FlowLauncherThemeToIconTheme();
6262
}
63-
return Enum.GetName(iconTheme).ToLower();
63+
return iconTheme.ToString().ToLower();
6464
}
6565

6666
private static IconTheme FlowLauncherThemeToIconTheme()
@@ -79,38 +79,38 @@ private static IconTheme FlowLauncherThemeToIconTheme()
7979

8080
public Result.IconDelegate GetIcon(IconGeneratorInfo info)
8181
{
82-
bool generate = (string.CompareOrdinal(info.Prefix, IC.Notebook) == 0
83-
|| string.CompareOrdinal(info.Prefix, IC.Section) == 0)
82+
bool generate = (string.CompareOrdinal(info.prefix, IC.Notebook) == 0
83+
|| string.CompareOrdinal(info.prefix, IC.Section) == 0)
8484
&& settings.CreateColoredIcons
85-
&& info.Color.HasValue;
85+
&& info.color.HasValue;
8686

8787
return generate ? GetIconGenerated : GetIconStatic;
8888

8989
ImageSource GetIconGenerated()
9090
{
91-
var imageSource = iconCache.GetOrAdd($"{info.Prefix}.{info.Color!.Value.ToArgb()}.png", ImageSourceFactory, info.Color.Value);
91+
var imageSource = iconCache.GetOrAdd($"{info.prefix}.{info.color!.Value.ToArgb()}.png",
92+
static (key, t) => ImageSourceFactory(t.self, key, t.Color),
93+
(Color: info.color.Value, self: this));
9294
OnPropertyChanged(nameof(CachedIconCount));
9395
return imageSource;
9496
}
9597

9698
ImageSource GetIconStatic()
9799
{
98-
return iconCache.GetOrAdd($"{info.Prefix}.{GetIconThemeString(settings.IconTheme)}.png", key =>
99-
{
100-
var path = Path.Combine(imagesDirectory, key);
101-
return BitmapImageFromPath(path);
102-
});
100+
return iconCache.GetOrAdd($"{info.prefix}.{GetIconThemeString(settings.IconTheme)}.png",
101+
static (key,dir) => BitmapImageFromPath(Path.Combine(dir, key)),
102+
imagesDirectory);
103103
}
104104
}
105105

106-
private ImageSource ImageSourceFactory(string key, Color color)
106+
private static ImageSource ImageSourceFactory(IconProvider self, string key, Color color)
107107
{
108108
var prefix = key.Split('.')[0];
109-
var path = Path.Combine(imagesDirectory, $"{prefix}.dark.png");
109+
var path = Path.Combine(self.imagesDirectory, $"{prefix}.dark.png");
110110
var bitmap = BitmapImageFromPath(path);
111111
var newBitmap = ChangeIconColor(bitmap, color);
112112

113-
path = $"{GeneratedImagesDirectoryInfo.FullName}{key}";
113+
path = $"{self.GeneratedImagesDirectoryInfo.FullName}{key}";
114114

115115
using var fileStream = new FileStream(path, FileMode.Create);
116116
var encoder = new PngBitmapEncoder();

Flow.Launcher.Plugin.OneNote/Keywords.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void ChangeKeyword(string newValue)
3939
public class KeywordJsonConverter : JsonConverter<Keyword>
4040
{
4141
public override Keyword Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
42-
=> new(JsonSerializer.Deserialize<string>(ref reader, options));
42+
=> new(JsonSerializer.Deserialize<string>(ref reader, options)!);
4343

4444
public override void Write(Utf8JsonWriter writer, Keyword value, JsonSerializerOptions options)
4545
=> JsonSerializer.Serialize(writer, value.Value, options);

Flow.Launcher.Plugin.OneNote/Main.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ public class Main : IAsyncPlugin, IContextMenu, ISettingProvider, IDisposable
2020
private IconProvider iconProvider;
2121

2222
private static SemaphoreSlim semaphore;
23-
private static Main instance;
24-
25-
private Query currentQuery;
2623

24+
2725
public Task InitAsync(PluginInitContext context)
2826
{
2927
this.context = context;
@@ -34,7 +32,6 @@ public Task InitAsync(PluginInitContext context)
3432
searchManager = new SearchManager(context, settings, resultCreator);
3533
semaphore = new SemaphoreSlim(1,1);
3634
context.API.VisibilityChanged += OnVisibilityChanged;
37-
instance = this;
3835
return Task.CompletedTask;
3936
}
4037

@@ -46,19 +43,26 @@ private void OnVisibilityChanged(object _, VisibilityChangedEventArgs e)
4643
}
4744
}
4845

49-
private static async Task OneNoteInitAsync(CancellationToken token = default)
46+
private static async Task OneNoteInitAsync(CancellationToken token)
5047
{
5148
if (OneNoteApplication.HasComObject)
5249
return;
5350

54-
if (await semaphore.WaitAsync(0,token))
51+
if (!await semaphore.WaitAsync(0,token))
52+
return;
53+
54+
try
55+
{
5556
OneNoteApplication.InitComObject();
57+
}
58+
finally
59+
{
60+
semaphore.Release();
61+
}
5662

57-
semaphore.Release();
5863
}
5964
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
6065
{
61-
currentQuery = query;
6266
Task init = OneNoteInitAsync(token);
6367

6468
if (string.IsNullOrEmpty(query.Search))
@@ -69,9 +73,6 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
6973
return searchManager.Query(query.Search);
7074
}
7175

72-
[Obsolete("Use PluginInitContext.API.ReQuery")]
73-
public static void ForceReQuery() => instance.context.API.ChangeQuery(instance.currentQuery.RawQuery, true);
74-
7576
public List<Result> LoadContextMenus(Result selectedResult)
7677
{
7778
return resultCreator.ContextMenu(selectedResult);

Flow.Launcher.Plugin.OneNote/Search/NotebookExplorer.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ public override List<Result> GetResults(string query)
3333

3434
Result result = resultCreator.CreateOneNoteItemResult(parent, false, score: Result.MaxScore);
3535
result.Title = $"Open \"{parent.Name}\" in OneNote";
36-
result.SubTitle = search switch
37-
{
38-
{ } when search.StartsWithOrd(Keywords.TitleSearch) => $"Now searching by title in \"{parent.Name}\"",
39-
{ } when search.StartsWithOrd(Keywords.ScopedSearch) => $"Now searching all pages in \"{parent.Name}\"",
40-
_ => $"Use \'{Keywords.ScopedSearch}\' to search this item. Use \'{Keywords.TitleSearch}\' to search by title in this item",
41-
};
42-
36+
result.SubTitle = $"Use \'{Keywords.ScopedSearch}\' to search this item. Use \'{Keywords.TitleSearch}\' to search by title in this item";
4337
results.Add(result);
4438
return results;
4539
}
@@ -84,7 +78,7 @@ private List<Result> ShowAll(IOneNoteItem? parent, IEnumerable<IOneNoteItem> col
8478
private List<Result> ScopedSearch(string query, IOneNoteItem parent)
8579
{
8680
if (query.Length == Keywords.ScopedSearch.Length)
87-
return new List<Result>(0);
81+
return resultCreator.SearchType("Now searching all pages", parent.Name);
8882

8983
if (!char.IsLetterOrDigit(query[Keywords.ScopedSearch.Length]))
9084
return resultCreator.InvalidQuery();
@@ -102,7 +96,7 @@ private List<Result> Explorer(string search, IOneNoteItem? parent, IEnumerable<I
10296
{
10397
var results = collection.FilterBySettings(settings)
10498
.FuzzySearch(search, context)
105-
.Select(r => resultCreator.CreateOneNoteItemResult(r.item, true, r.highlightData, r.score))
99+
.Select(r => resultCreator.CreateOneNoteItemResult(r.Item, true, r.HighlightData, r.Score))
106100
.ToList();
107101

108102
// If parent is a section, pages inside can have the same name

Flow.Launcher.Plugin.OneNote/Search/RecentPages.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public override List<Result> GetResults(string query)
1313
{
1414
int count = settings.DefaultRecentsCount;
1515

16-
if (query.Length > Keyword.Length && int.TryParse(query[Keyword.Length..], out int userChosenCount))
16+
if (query.Length > keyword.Length && int.TryParse(query[keyword.Length..], out int userChosenCount))
1717
count = userChosenCount;
1818

1919
return OneNoteApplication.GetNotebooks()

Flow.Launcher.Plugin.OneNote/Search/SearchBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ public abstract class SearchBase
77
protected readonly PluginInitContext context;
88
protected readonly Settings settings;
99
protected readonly ResultCreator resultCreator;
10-
public readonly Keyword Keyword;
10+
public readonly Keyword keyword;
1111
protected SearchBase(PluginInitContext context, Settings settings, ResultCreator resultCreator, Keyword keyword)
1212
{
1313
this.context = context;
1414
this.settings = settings;
1515
this.resultCreator = resultCreator;
16-
Keyword = keyword;
16+
this.keyword = keyword;
1717
}
1818
protected Keywords Keywords => settings.Keywords;
1919
public abstract List<Result> GetResults(string query);

Flow.Launcher.Plugin.OneNote/Search/SearchExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Flow.Launcher.Plugin.OneNote.Search
77
{
8-
public record struct SearchResult<T>(T item, List<int>? highlightData, int score) where T : IOneNoteItem;
8+
public record struct SearchResult<T>(T Item, List<int>? HighlightData, int Score) where T : IOneNoteItem;
99
public static class SearchExtensions
1010
{
1111
public static IEnumerable<SearchResult<T>> FuzzySearch<T>(this IEnumerable<T> source, string search, PluginInitContext context) where T: IOneNoteItem

Flow.Launcher.Plugin.OneNote/Search/SearchManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public List<Result> Query(string search)
2222
{
2323
return search switch
2424
{
25-
{ } when search.StartsWithOrd(titleSearch.Keyword) => titleSearch.GetResults(search),
26-
{ } when search.StartsWithOrd(notebookExplorer.Keyword) => notebookExplorer.GetResults(search),
27-
{ } when search.StartsWithOrd(recentPages.Keyword) => recentPages.GetResults(search),
25+
{ } when search.StartsWithOrd(titleSearch.keyword) => titleSearch.GetResults(search),
26+
{ } when search.StartsWithOrd(notebookExplorer.keyword) => notebookExplorer.GetResults(search),
27+
{ } when search.StartsWithOrd(recentPages.keyword) => recentPages.GetResults(search),
2828
_ => defaultSearch.GetResults(search!),
2929
};
3030
}

Flow.Launcher.Plugin.OneNote/Search/TitleSearch.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Linq;
43
using Odotocodot.OneNote.Linq;
@@ -14,15 +13,15 @@ public TitleSearch(PluginInitContext context, Settings settings, ResultCreator r
1413

1514
public List<Result> Filter(string query, IOneNoteItem? parent, IEnumerable<IOneNoteItem> collection)
1615
{
17-
if (query.Length == Keyword.Length || parent == null)
18-
return resultCreator.SearchingByTitle();
16+
if (query.Length == keyword.Length)
17+
return resultCreator.SearchType("Now searching by title", parent?.Name);
1918

20-
var currentSearch = query[Keyword.Length..];
19+
var currentSearch = query[keyword.Length..];
2120

2221
var results = collection.Traverse()
2322
.FilterBySettings(settings)
2423
.FuzzySearch(currentSearch, context)
25-
.Select(x => resultCreator.CreateOneNoteItemResult(x.item, false, x.highlightData, x.score))
24+
.Select(x => resultCreator.CreateOneNoteItemResult(x.Item, false, x.HighlightData, x.Score))
2625
.ToList();
2726

2827
return results.Any() ? results : ResultCreator.NoMatchesFound();

0 commit comments

Comments
 (0)