Skip to content

Commit 9da1f5f

Browse files
committed
Use new API
1 parent 2e1e21c commit 9da1f5f

5 files changed

Lines changed: 97 additions & 88 deletions

File tree

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@ public record struct IconGeneratorInfo
88
public string Prefix { get; }
99
public Color? Color { get; }
1010

11-
public IconGeneratorInfo(OneNoteNotebook notebook)
11+
public IconGeneratorInfo(IOneNoteItem item)
1212
{
13-
Prefix = IconConstants.Notebook;
14-
Color = notebook.Color;
15-
}
16-
public IconGeneratorInfo(OneNoteSectionGroup sectionGroup)
17-
{
18-
Prefix = sectionGroup.IsRecycleBin ? IconConstants.RecycleBin : IconConstants.SectionGroup;
19-
}
20-
public IconGeneratorInfo(OneNoteSection section)
21-
{
22-
Prefix = IconConstants.Section;
23-
Color = section.Color;
24-
}
25-
public IconGeneratorInfo(OneNotePage page)
26-
{
27-
Prefix = IconConstants.Page;
13+
switch (item)
14+
{
15+
case OneNoteNotebook n:
16+
Prefix = IconConstants.Notebook;
17+
Color = n.Color;
18+
break;
19+
case OneNoteSectionGroup sg:
20+
Prefix = sg.IsRecycleBin ? IconConstants.RecycleBin : IconConstants.SectionGroup;
21+
break;
22+
case OneNoteSection s:
23+
Prefix = IconConstants.Section;
24+
Color = s.Color;
25+
break;
26+
case OneNotePage:
27+
Prefix = IconConstants.Page;
28+
break;
29+
}
2830
}
2931
}
3032
}

Flow.Launcher.Plugin.OneNote/Main.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Odotocodot.OneNote.Linq;
99
namespace Flow.Launcher.Plugin.OneNote
1010
{
11+
#nullable disable
1112
public class Main : IAsyncPlugin, IContextMenu, ISettingProvider, IDisposable
1213
{
1314
private PluginInitContext context;
@@ -21,10 +22,12 @@ public class Main : IAsyncPlugin, IContextMenu, ISettingProvider, IDisposable
2122
private static Main instance;
2223

2324
private Query currentQuery;
25+
2426
public Task InitAsync(PluginInitContext context)
2527
{
2628
this.context = context;
2729
settings = context.API.LoadSettingJsonStorage<Settings>();
30+
2831
iconProvider = new IconProvider(context, settings);
2932
resultCreator = new ResultCreator(context, settings, iconProvider);
3033
searchManager = new SearchManager(context, settings, resultCreator);
@@ -34,27 +37,28 @@ public Task InitAsync(PluginInitContext context)
3437
return Task.CompletedTask;
3538
}
3639

37-
public void OnVisibilityChanged(object _, VisibilityChangedEventArgs e)
40+
private void OnVisibilityChanged(object _, VisibilityChangedEventArgs e)
3841
{
3942
if (context.CurrentPluginMetadata.Disabled || !e.IsVisible)
4043
{
41-
OneNoteApplication.ReleaseComObject();
44+
Task.Run(OneNoteApplication.ReleaseComObject);
4245
}
4346
}
4447

4548
private static async Task OneNoteInitAsync(CancellationToken token = default)
4649
{
47-
if (semaphore.CurrentCount == 0 || OneNoteApplication.HasComObject)
50+
if (OneNoteApplication.HasComObject)
4851
return;
49-
50-
await semaphore.WaitAsync(token);
51-
OneNoteApplication.InitComObject();
52+
53+
if (await semaphore.WaitAsync(0,token))
54+
OneNoteApplication.InitComObject();
55+
5256
semaphore.Release();
5357
}
5458
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
5559
{
5660
currentQuery = query;
57-
var init = OneNoteInitAsync(token);
61+
Task init = OneNoteInitAsync(token);
5862

5963
if (string.IsNullOrEmpty(query.Search))
6064
return resultCreator.EmptyQuery();
@@ -64,6 +68,7 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
6468
return searchManager.Query(query);
6569
}
6670

71+
[Obsolete("Use PluginInitContext.API.ReQuery")]
6772
public static void ForceReQuery() => instance.context.API.ChangeQuery(instance.currentQuery.RawQuery, true);
6873

6974
public List<Result> LoadContextMenus(Result selectedResult)

Flow.Launcher.Plugin.OneNote/ResultCreator.cs

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Flow.Launcher.Plugin.OneNote.UI.Views;
88
using Humanizer;
99
using Odotocodot.OneNote.Linq;
10+
using Odotocodot.OneNote.Linq.Abstractions;
1011

1112
namespace Flow.Launcher.Plugin.OneNote
1213
{
@@ -19,7 +20,6 @@ public class ResultCreator
1920
private const string PathSeparator = " > ";
2021
private const string BulletPoint = "\u2022 ";
2122
private const string TrianglePoint = "\u2023 ";
22-
2323
private string ActionKeyword => context.CurrentPluginMetadata.ActionKeyword;
2424
public ResultCreator(PluginInitContext context, Settings settings, IconProvider iconProvider)
2525
{
@@ -31,7 +31,7 @@ public ResultCreator(PluginInitContext context, Settings settings, IconProvider
3131
private static string GetNicePath(IOneNoteItem item, string separator = PathSeparator) =>
3232
item.RelativePath.Replace(OneNoteApplication.RelativePathSeparator.ToString(), separator);
3333

34-
private string GetTitle(IOneNoteItem item, List<int> highlightData)
34+
private string GetTitle(IOneNoteItem item, List<int>? highlightData)
3535
{
3636
string title = item.Name;
3737
if (!item.IsUnread || !settings.ShowUnread)
@@ -51,8 +51,7 @@ private string GetTitle(IOneNoteItem item, List<int> highlightData)
5151

5252
private string GetAutoCompleteText(IOneNoteItem item)
5353
=> $"{ActionKeyword} {settings.Keywords.NotebookExplorer}{GetNicePath(item, Keywords.NotebookExplorerSeparator)}{Keywords.NotebookExplorerSeparator}";
54-
55-
54+
5655
public List<Result> EmptyQuery()
5756
{
5857
return new List<Result>
@@ -63,14 +62,16 @@ public List<Result> EmptyQuery()
6362
SubTitle = "Try typing something!",
6463
AutoCompleteText = ActionKeyword,
6564
IcoPath = iconProvider.Search,
66-
Score = 5000,
65+
AddSelectedCount = false,
66+
Score = Result.MaxScore,
6767
},
6868
new Result
6969
{
7070
Title = "View notebook explorer",
7171
SubTitle = $"Type \"{settings.Keywords.NotebookExplorer}\" or select this option to search by notebook structure",
7272
AutoCompleteText = $"{ActionKeyword} {settings.Keywords.NotebookExplorer}",
7373
IcoPath = iconProvider.NotebookExplorer,
74+
AddSelectedCount = false,
7475
Score = 2000,
7576
Action = _ =>
7677
{
@@ -84,6 +85,7 @@ public List<Result> EmptyQuery()
8485
SubTitle = $"Type \"{settings.Keywords.RecentPages}\" or select this option to see recently modified pages",
8586
AutoCompleteText = $"{ActionKeyword} {settings.Keywords.RecentPages}",
8687
IcoPath = iconProvider.Recent,
88+
AddSelectedCount = false,
8789
Score = -1000,
8890
Action = _ =>
8991
{
@@ -95,6 +97,7 @@ public List<Result> EmptyQuery()
9597
{
9698
Title = "New quick note",
9799
IcoPath = iconProvider.QuickNote,
100+
AddSelectedCount = false,
98101
Score = -4000,
99102
PreviewPanel = GetNewPagePreviewPanel(null, null),
100103
Action = _ =>
@@ -108,6 +111,7 @@ public List<Result> EmptyQuery()
108111
{
109112
Title = "Open and sync notebooks",
110113
IcoPath = iconProvider.Sync,
114+
AddSelectedCount = false,
111115
Score = int.MinValue,
112116
Action = _ =>
113117
{
@@ -130,45 +134,32 @@ public List<Result> EmptyQuery()
130134
};
131135
}
132136

133-
public Result CreateOneNoteItemResult(IOneNoteItem item, bool actionIsAutoComplete, List<int> highlightData = null, int score = 0)
137+
public Result CreateOneNoteItemResult(IOneNoteItem item, bool actionIsAutoComplete, List<int>? highlightData = null, int score = 0)
134138
{
135-
string title = GetTitle(item, highlightData);
136-
string toolTip = string.Empty;
137-
string subTitle = GetNicePath(item);
138-
string autoCompleteText = GetAutoCompleteText(item);
139-
140-
IconGeneratorInfo iconInfo;
139+
var title = GetTitle(item, highlightData);
140+
var toolTip = string.Empty;
141+
var subTitle = GetNicePath(item);
142+
var autoCompleteText = GetAutoCompleteText(item);
143+
var iconInfo = new IconGeneratorInfo(item);
141144

142145
switch (item)
143146
{
144-
case OneNoteNotebook notebook:
147+
case INotebookOrSectionGroup i:
145148
toolTip =
146149
$"""
147150
Last Modified:
148-
{TrianglePoint}{notebook.LastModified:F}
151+
{TrianglePoint}{i.LastModified:F}
149152
150153
Contains:
151-
{TrianglePoint}{"section group".ToQuantity(notebook.SectionGroups.Count())}
152-
{TrianglePoint}{"section".ToQuantity(notebook.Sections.Count())}
153-
{TrianglePoint}{"page".ToQuantity(notebook.GetPages().Count())}
154+
{TrianglePoint}{"section group".ToQuantity(i.SectionGroups.Count())}
155+
{TrianglePoint}{"section".ToQuantity(i.Sections.Count())}
156+
{TrianglePoint}{"page".ToQuantity(i.GetPages().Count())}
154157
""";
155158

156-
subTitle = string.Empty;
157-
iconInfo = new IconGeneratorInfo(notebook);
158-
break;
159-
case OneNoteSectionGroup sectionGroup:
160-
toolTip =
161-
$"""
162-
Last Modified:
163-
{TrianglePoint}{sectionGroup.LastModified:F}
164-
165-
Contains:
166-
{TrianglePoint}{"section group".ToQuantity(sectionGroup.SectionGroups.Count())}
167-
{TrianglePoint}{"section".ToQuantity(sectionGroup.Sections.Count())}
168-
{TrianglePoint}{"page".ToQuantity(sectionGroup.GetPages().Count())}
169-
""";
170-
171-
iconInfo = new IconGeneratorInfo(sectionGroup);
159+
if (i is OneNoteNotebook)
160+
{
161+
subTitle = string.Empty;
162+
}
172163
break;
173164
case OneNoteSection section:
174165
if (section.Encrypted)
@@ -184,8 +175,6 @@ public Result CreateOneNoteItemResult(IOneNoteItem item, bool actionIsAutoComple
184175
Contains:
185176
{TrianglePoint}{"page".ToQuantity(section.GetPages().Count())}
186177
""";
187-
188-
iconInfo = new IconGeneratorInfo(section);
189178
break;
190179
case OneNotePage page:
191180
autoCompleteText = actionIsAutoComplete ? autoCompleteText[..^1] : string.Empty;
@@ -198,10 +187,6 @@ public Result CreateOneNoteItemResult(IOneNoteItem item, bool actionIsAutoComple
198187
{"Created:",-15} {page.Created:F}
199188
{"Last Modified:",-15} {page.LastModified:F}
200189
""";
201-
iconInfo = new IconGeneratorInfo(page);
202-
break;
203-
default:
204-
iconInfo = default;
205190
break;
206191
}
207192

@@ -222,7 +207,7 @@ public Result CreateOneNoteItemResult(IOneNoteItem item, bool actionIsAutoComple
222207
context.API.ChangeQuery($"{autoCompleteText}", true);
223208
return false;
224209
}
225-
210+
226211
await Task.Run(() =>
227212
{
228213
item.Sync();
@@ -426,13 +411,12 @@ public List<Result> ContextMenu(Result selectedResult)
426411
return results;
427412
}
428413

429-
public List<Result> NoItemsInCollection(List<Result> results, IOneNoteItem parent)
414+
public List<Result> NoItemsInCollection(List<Result> results, IOneNoteItem? parent)
430415
{
431416
// parent can be null if the collection only contains notebooks.
432417
switch (parent)
433418
{
434-
case OneNoteNotebook:
435-
case OneNoteSectionGroup:
419+
case INotebookOrSectionGroup:
436420
// Can create section/section group
437421
results.Add(NoItemsInCollectionResult("section", iconProvider.NewSection, "(unencrypted) section"));
438422
results.Add(NoItemsInCollectionResult("section group", iconProvider.NewSectionGroup));
@@ -448,7 +432,7 @@ public List<Result> NoItemsInCollection(List<Result> results, IOneNoteItem paren
448432

449433
return results;
450434

451-
Result NoItemsInCollectionResult(string title, string iconPath, string subTitle = null, OneNoteSection section = null)
435+
Result NoItemsInCollectionResult(string title, string iconPath, string? subTitle = null, OneNoteSection? section = null)
452436
{
453437
return new Result
454438
{
@@ -460,7 +444,7 @@ Result NoItemsInCollectionResult(string title, string iconPath, string subTitle
460444
}
461445
}
462446

463-
private Lazy<UserControl> GetNewPagePreviewPanel(OneNoteSection section, string pageTitle) =>
447+
private Lazy<UserControl> GetNewPagePreviewPanel(OneNoteSection? section, string? pageTitle) =>
464448
new(() => new NewOneNotePagePreviewPanel(context, section, pageTitle));
465449

466450
public static List<Result> NoMatchesFound()
@@ -469,10 +453,12 @@ public static List<Result> NoMatchesFound()
469453
"Try searching something else, or syncing your notebooks",
470454
IconProvider.Logo);
471455
}
472-
public List<Result> InvalidQuery()
456+
public List<Result> InvalidQuery(bool includeSubtitle = true)
473457
{
474458
return SingleResult("Invalid query",
475-
"The first character of the search must be a letter or a digit",
459+
includeSubtitle
460+
? "The first character of the search must be a letter or a digit"
461+
: string.Empty,
476462
iconProvider.Warning);
477463
}
478464
public List<Result> SearchingByTitle()

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

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,45 @@ internal List<Result> Query(Query query)
2525

2626
string fullSearch = query.Search[(query.Search.IndexOf(Keywords.NotebookExplorer, StringComparison.Ordinal) + Keywords.NotebookExplorer.Length)..];
2727

28-
IOneNoteItem parent = null;
28+
IOneNoteItem? parent = null;
2929
IEnumerable<IOneNoteItem> collection = OneNoteApplication.GetNotebooks();
3030

31+
// // Validate Search Version 1
3132
string[] searches = fullSearch.Split(Keywords.NotebookExplorerSeparator, StringSplitOptions.None);
32-
33-
for (int i = -1; i < searches.Length - 1; i++)
33+
// for (int i = -1; i < searches.Length - 1; i++)
34+
// {
35+
// if (i < 0)
36+
// {
37+
// continue;
38+
// }
39+
//
40+
// parent = collection.FirstOrDefault(item => item.Name.Equals(searches[i]));
41+
// if (parent == null)
42+
// {
43+
// return results;
44+
// }
45+
//
46+
// collection = parent.Children;
47+
// }
48+
49+
// Validate Search Version 2
50+
var separator = Keywords.NotebookExplorerSeparator;
51+
var currIndex = fullSearch.IndexOf(separator, StringComparison.Ordinal);
52+
var prevIndex = 0;
53+
while (currIndex != -1)
3454
{
35-
if (i < 0)
36-
{
37-
continue;
38-
}
39-
40-
parent = collection.FirstOrDefault(item => item.Name.Equals(searches[i]));
55+
//var itemName = fullSearch[prevIndex..currIndex];
56+
57+
parent = collection.FirstOrDefault(item => item.Name == fullSearch[prevIndex..currIndex]);
4158
if (parent == null)
42-
{
43-
return results;
44-
}
45-
59+
return resultCreator.InvalidQuery(false);
60+
4661
collection = parent.Children;
62+
63+
prevIndex = currIndex + 1;
64+
currIndex = fullSearch.IndexOf(separator, currIndex + separator.Length, StringComparison.Ordinal);
4765
}
66+
4867

4968
string lastSearch = searches[^1];
5069

@@ -140,14 +159,10 @@ private List<Result> Explorer(string search, IOneNoteItem? parent, IEnumerable<I
140159
private void AddCreateNewOneNoteItemResults(string newItemName, IOneNoteItem? parent, List<Result> results)
141160
{
142161
if (results.Any(result => string.Equals(newItemName.Trim(), result.Title, StringComparison.OrdinalIgnoreCase)))
143-
{
144162
return;
145-
}
146163

147164
if (parent?.IsInRecycleBin() == true)
148-
{
149165
return;
150-
}
151166

152167
switch (parent)
153168
{

0 commit comments

Comments
 (0)