Skip to content

Commit f683c2e

Browse files
committed
migrate to LinqToOneNote [2/2]
1 parent 8694387 commit f683c2e

1 file changed

Lines changed: 63 additions & 98 deletions

File tree

Flow.Launcher.Plugin.OneNote/ResultCreator.cs

Lines changed: 63 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
5-
using System.Windows.Controls;
65
using Flow.Launcher.Plugin.OneNote.Icons;
76
using Flow.Launcher.Plugin.OneNote.UI.Views;
87
using Humanizer;
9-
using Odotocodot.OneNote.Linq;
10-
using Odotocodot.OneNote.Linq.Abstractions;
8+
using LinqToOneNote;
9+
using LinqToOneNote.Abstractions;
10+
using OneNoteApp = LinqToOneNote.OneNote;
1111

1212
namespace Flow.Launcher.Plugin.OneNote
1313
{
@@ -28,8 +28,7 @@ public ResultCreator(PluginInitContext context, Settings settings, IconProvider
2828
this.context = context;
2929
}
3030

31-
private static string GetNicePath(IOneNoteItem item, string separator = PathSeparator) =>
32-
item.RelativePath.Replace(OneNoteApplication.RelativePathSeparator.ToString(), separator);
31+
private static string GetNicePath(IOneNoteItem item, string separator = PathSeparator) => item.GetRelativePath(false, separator);
3332

3433
private string GetTitle(IOneNoteItem item, List<int>? highlightData)
3534
{
@@ -51,7 +50,7 @@ private string GetTitle(IOneNoteItem item, List<int>? highlightData)
5150

5251
private string GetAutoCompleteText(IOneNoteItem item) //Auto complete text if in notebook explorer
5352
{
54-
string slash = item is OneNotePage ? string.Empty : Keywords.NotebookExplorerSeparator;
53+
string slash = item is Page ? string.Empty : Keywords.NotebookExplorerSeparator;
5554
return $"{ActionKeyword} {settings.Keywords.NotebookExplorer}{GetNicePath(item, Keywords.NotebookExplorerSeparator)}{slash}";
5655
}
5756

@@ -105,7 +104,7 @@ public List<Result> EmptyQuery()
105104
PreviewPanel = GetNewPagePreviewPanel(null, null),
106105
Action = _ =>
107106
{
108-
OneNoteApplication.CreateQuickNote(true);
107+
OneNoteApp.CreateQuickNote(OpenMode.ExistingOrNewWindow);
109108
WindowHelper.FocusOneNote();
110109
return true;
111110
},
@@ -118,17 +117,17 @@ public List<Result> EmptyQuery()
118117
Score = int.MinValue,
119118
Action = _ =>
120119
{
121-
var notebooks = OneNoteApplication.GetNotebooks();
120+
var notebooks = OneNoteApp.GetFullHierarchy().Notebooks;
122121
foreach (var notebook in notebooks)
123122
{
124123
notebook.Sync();
125124
}
126125

127-
notebooks.GetPages()
126+
notebooks.GetAllPages()
128127
.Where(i => !i.IsInRecycleBin)
129128
.OrderByDescending(pg => pg.LastModified)
130129
.First()
131-
.OpenInOneNote();
130+
.Open();
132131

133132
WindowHelper.FocusOneNote();
134133
return true;
@@ -154,17 +153,17 @@ public Result CreateOneNoteItemResult(IOneNoteItem item, bool actionIsAutoComple
154153
{TrianglePoint}{i.LastModified:F}
155154
156155
Contains:
157-
{TrianglePoint}{"section group".ToQuantity(i.SectionGroups.Count())}
158-
{TrianglePoint}{"section".ToQuantity(i.Sections.Count())}
159-
{TrianglePoint}{"page".ToQuantity(i.GetPages().Count())}
156+
{TrianglePoint}{"section group".ToQuantity(i.SectionGroups.Count)}
157+
{TrianglePoint}{"section".ToQuantity(i.Sections.Count)}
158+
{TrianglePoint}{"page".ToQuantity(i.GetAllPages().Count())}
160159
""";
161160

162-
if (i is OneNoteNotebook)
161+
if (i is Notebook)
163162
{
164163
subTitle = string.Empty;
165164
}
166165
break;
167-
case OneNoteSection section:
166+
case Section section:
168167
if (section.Encrypted)
169168
{
170169
title += $" [Encrypted] {(section.Locked ? "[Locked]" : "[Unlocked]")}";
@@ -176,10 +175,10 @@ public Result CreateOneNoteItemResult(IOneNoteItem item, bool actionIsAutoComple
176175
{TrianglePoint}{section.LastModified:F}
177176
178177
Contains:
179-
{TrianglePoint}{"page".ToQuantity(section.GetPages().Count())}
178+
{TrianglePoint}{"page".ToQuantity(section.GetAllPages().Count())}
180179
""";
181180
break;
182-
case OneNotePage page:
181+
case Page page:
183182
autoCompleteText = actionIsAutoComplete ? autoCompleteText[..^1] : string.Empty;
184183

185184
actionIsAutoComplete = false;
@@ -214,18 +213,18 @@ public Result CreateOneNoteItemResult(IOneNoteItem item, bool actionIsAutoComple
214213
await Task.Run(() =>
215214
{
216215
item.Sync();
217-
item.OpenInOneNote();
216+
item.Open();
218217
});
219218
WindowHelper.FocusOneNote();
220219
return true;
221220
},
222221
};
223222
}
224223

225-
public Result CreatePageResult(OneNotePage page, string query)
224+
public Result CreatePageResult(Page page, string query)
226225
=> CreateOneNoteItemResult(page, false, string.IsNullOrWhiteSpace(query) ? null : context.API.FuzzySearch(query, page.Name).MatchData);
227226

228-
public Result CreateRecentPageResult(OneNotePage page)
227+
public Result CreateRecentPageResult(Page page)
229228
{
230229
var result = CreateOneNoteItemResult(page, false);
231230
result.SubTitle = $"{page.LastModified.Humanize()} | {result.SubTitle}";
@@ -234,92 +233,59 @@ public Result CreateRecentPageResult(OneNotePage page)
234233
return result;
235234
}
236235

237-
238-
private bool CreateNewItem<T>(T parent, string name, bool validTitle, ActionContext c, Func<T, string, bool, string> createAction) where T : IOneNoteItem
236+
//When name can have invalid chars
237+
private Result CreateNewItemResult<TNew, TParent>(string newName, TParent? parent, string iconPath, Func<TParent?, string, OpenMode, TNew> createFunc)
238+
where TNew : IOneNoteItem, INameInvalidCharacters
239+
where TParent : IOneNoteItem
240+
=> CreateNewItemResult(newName, parent, iconPath, createFunc, OneNoteApp.IsValidName<TNew>(newName), TNew.InvalidCharacters);
241+
242+
private Result CreateNewItemResult<TNew, TParent>(string newName, TParent? parent, string iconPath, Func<TParent?, string, OpenMode, TNew> createFunc, bool validTitle = true, IReadOnlyList<char>? invalidChars = null)
243+
where TNew : IOneNoteItem
244+
where TParent : IOneNoteItem
239245
{
240-
if (!validTitle)
246+
newName = newName.Trim();
247+
string type = nameof(TNew);
248+
return new Result
241249
{
242-
return false;
243-
}
250+
Title = $"Create {type.Transform(To.LowerCase)} \"{newName}\"",
251+
SubTitle = validTitle
252+
? parent == null // parent is null if trying to create a notebook
253+
? $"Location: {OneNoteApp.GetDefaultNotebookLocation()}"
254+
: $"Path: {GetNicePath(parent)}{PathSeparator}{newName}"
255+
: $"{type.Transform(To.SentenceCase)} names cannot contain: {string.Join(' ', invalidChars!)}",
256+
AutoCompleteText = parent == null ? $"{ActionKeyword} {settings.Keywords.NotebookExplorer}{newName}" : $"{GetAutoCompleteText(parent)}{newName}",
257+
IcoPath = iconPath,
258+
Action = c =>
259+
{
260+
if (!validTitle)
261+
return false;
244262

245-
bool showOneNote = !c.SpecialKeyState.CtrlPressed;
263+
bool showOneNote = !c.SpecialKeyState.CtrlPressed;
264+
createFunc(parent, newName, showOneNote ? OpenMode.ExistingOrNewWindow : OpenMode.None);
246265

247-
createAction(parent, name, showOneNote);
248-
249-
context.API.ReQuery();
250-
251-
if(showOneNote)
252-
WindowHelper.FocusOneNote();
253-
254-
return showOneNote;
255-
}
266+
context.API.ReQuery();
256267

257-
public Result CreateNewPageResult(string newPageName, OneNoteSection section)
258-
{
259-
newPageName = newPageName.Trim();
260-
return new Result
261-
{
262-
Title = $"Create page: \"{newPageName}\"",
263-
SubTitle = $"Path: {GetNicePath(section)}{PathSeparator}{newPageName}",
264-
AutoCompleteText = $"{GetAutoCompleteText(section)}{newPageName}",
265-
IcoPath = iconProvider.NewPage,
266-
PreviewPanel = GetNewPagePreviewPanel(section, newPageName),
267-
Action = c => CreateNewItem(section, newPageName, true, c, OneNoteApplication.CreatePage),
268+
if (showOneNote)
269+
WindowHelper.FocusOneNote();
270+
271+
return showOneNote;
272+
},
268273
};
269274
}
270275

271-
public Result CreateNewSectionResult(string newSectionName, INotebookOrSectionGroup parent)
276+
public Result CreateNewPageResult(string newPageName, Section section)
272277
{
273-
newSectionName = newSectionName.Trim();
274-
bool validTitle = OneNoteApplication.IsSectionNameValid(newSectionName);
275-
276-
return new Result
277-
{
278-
Title = $"Create section: \"{newSectionName}\"",
279-
SubTitle = validTitle
280-
? $"Path: {GetNicePath(parent)}{PathSeparator}{newSectionName}"
281-
: $"Section names cannot contain: {string.Join(' ', OneNoteApplication.InvalidSectionChars)}",
282-
AutoCompleteText = $"{GetAutoCompleteText(parent)}{newSectionName}",
283-
IcoPath = iconProvider.NewSection,
284-
Action = c => CreateNewItem(parent, newSectionName, validTitle, c, OneNoteApplication.CreateSection),
285-
};
278+
var result = CreateNewItemResult(newPageName, section, iconProvider.NewPage, OneNoteApp.CreatePage);
279+
result.PreviewPanel = GetNewPagePreviewPanel(section, newPageName);
280+
return result;
286281
}
287282

288-
public Result CreateNewSectionGroupResult(string newSectionGroupName, INotebookOrSectionGroup parent)
289-
{
290-
newSectionGroupName = newSectionGroupName.Trim();
291-
bool validTitle = OneNoteApplication.IsSectionGroupNameValid(newSectionGroupName);
283+
public Result CreateNewSectionResult(string newSectionName, INotebookOrSectionGroup parent) => CreateNewItemResult(newSectionName, parent, iconProvider.NewSection, OneNoteApp.CreateSection);
292284

293-
return new Result
294-
{
295-
Title = $"Create section group: \"{newSectionGroupName}\"",
296-
SubTitle = validTitle
297-
? $"Path: {GetNicePath(parent)}{PathSeparator}{newSectionGroupName}"
298-
: $"Section group names cannot contain: {string.Join(' ', OneNoteApplication.InvalidSectionGroupChars)}",
299-
AutoCompleteText = $"{GetAutoCompleteText(parent)}{newSectionGroupName}",
300-
IcoPath = iconProvider.NewSectionGroup,
301-
Action = c => CreateNewItem(parent, newSectionGroupName, validTitle, c, OneNoteApplication.CreateSectionGroup),
302-
};
303-
}
304-
305-
public Result CreateNewNotebookResult(string newNotebookName)
306-
{
307-
newNotebookName = newNotebookName.Trim();
308-
bool validTitle = OneNoteApplication.IsNotebookNameValid(newNotebookName);
285+
public Result CreateNewSectionGroupResult(string newSectionGroupName, INotebookOrSectionGroup parent) => CreateNewItemResult(newSectionGroupName, parent, iconProvider.NewSectionGroup, OneNoteApp.CreateSectionGroup);
286+
287+
public Result CreateNewNotebookResult(string newNotebookName) => CreateNewItemResult<Notebook, IOneNoteItem>(newNotebookName, null, iconProvider.NewNotebook, (_, name, openMode) => OneNoteApp.CreateNotebook(name, openMode));
309288

310-
return new Result
311-
{
312-
Title = $"Create notebook: \"{newNotebookName}\"",
313-
SubTitle = validTitle
314-
? $"Location: {OneNoteApplication.GetDefaultNotebookLocation()}"
315-
: $"Notebook names cannot contain: {string.Join(' ', OneNoteApplication.InvalidNotebookChars)}",
316-
AutoCompleteText = $"{ActionKeyword} {settings.Keywords.NotebookExplorer}{newNotebookName}",
317-
IcoPath = iconProvider.NewNotebook,
318-
Action = c => CreateNewItem<IOneNoteItem>(null, newNotebookName, validTitle,
319-
c, (_, name, valid) => OneNoteApplication.CreateNotebook(name, valid)),
320-
};
321-
}
322-
323289
public List<Result> ContextMenu(Result selectedResult)
324290
{
325291
var results = new List<Result>();
@@ -341,7 +307,7 @@ public List<Result> ContextMenu(Result selectedResult)
341307
AddSelectedCount = false,
342308
Action = _ =>
343309
{
344-
OneNoteApplication.ComObject.NavigateTo(item.ID, fNewWindow: true);
310+
OneNoteApp.Open(item, true);
345311
WindowHelper.FocusOneNote();
346312
return true;
347313
}
@@ -376,7 +342,7 @@ public List<Result> EmptyCollection(List<Result> results, IOneNoteItem? parent)
376342
results.Add(EmptyCollectionResult("section", iconProvider.NewSection, "(unencrypted) section"));
377343
results.Add(EmptyCollectionResult("section group", iconProvider.NewSectionGroup));
378344
break;
379-
case OneNoteSection section:
345+
case Section section:
380346
// Can create page
381347
if (!section.Locked)
382348
{
@@ -387,7 +353,7 @@ public List<Result> EmptyCollection(List<Result> results, IOneNoteItem? parent)
387353

388354
return results;
389355

390-
Result EmptyCollectionResult(string title, string iconPath, string? subTitle = null, OneNoteSection? section = null)
356+
Result EmptyCollectionResult(string title, string iconPath, string? subTitle = null, Section? section = null)
391357
{
392358
return new Result
393359
{
@@ -399,7 +365,7 @@ Result EmptyCollectionResult(string title, string iconPath, string? subTitle = n
399365
}
400366
}
401367

402-
private Lazy<UserControl> GetNewPagePreviewPanel(OneNoteSection? section, string? pageTitle)
368+
private Lazy<System.Windows.Controls.UserControl> GetNewPagePreviewPanel(Section? section, string? pageTitle)
403369
=> new(() => new NewOneNotePagePreviewPanel(context, section, pageTitle));
404370

405371
public static List<Result> NoMatchesFound()
@@ -437,6 +403,5 @@ private static List<Result> SingleResult(string title, string subTitle, string i
437403
}
438404
};
439405
}
440-
441406
}
442407
}

0 commit comments

Comments
 (0)