Skip to content

Commit 35995a1

Browse files
committed
Refactor ResultCreator
1 parent 41ad0a4 commit 35995a1

2 files changed

Lines changed: 65 additions & 106 deletions

File tree

Flow.Launcher.Plugin.OneNote/ResultCreator.cs

Lines changed: 62 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ private string GetTitle(IOneNoteItem item, List<int>? highlightData)
4949
return title;
5050
}
5151

52-
private string GetAutoCompleteText(IOneNoteItem item)
53-
=> $"{ActionKeyword} {settings.Keywords.NotebookExplorer}{GetNicePath(item, Keywords.NotebookExplorerSeparator)}{Keywords.NotebookExplorerSeparator}";
54-
52+
private string GetAutoCompleteText(IOneNoteItem item) //Auto complete text if in notebook explorer
53+
{
54+
string slash = item is OneNotePage ? string.Empty : Keywords.NotebookExplorerSeparator;
55+
return $"{ActionKeyword} {settings.Keywords.NotebookExplorer}{GetNicePath(item, Keywords.NotebookExplorerSeparator)}{slash}";
56+
}
57+
5558
public List<Result> EmptyQuery()
5659
{
5760
return new List<Result>
@@ -224,12 +227,33 @@ public Result CreatePageResult(OneNotePage page, string query)
224227

225228
public Result CreateRecentPageResult(OneNotePage page)
226229
{
227-
var result = CreateOneNoteItemResult(page, false, null);
230+
var result = CreateOneNoteItemResult(page, false);
228231
result.SubTitle = $"{page.LastModified.Humanize()} | {result.SubTitle}";
229232
result.IcoPath = iconProvider.Recent;
233+
result.AddSelectedCount = false;
230234
return result;
231235
}
232236

237+
238+
private bool CreateNewItem<T>(T parent, string name, bool validTitle, ActionContext c, Func<T, string, bool, string> createAction) where T : IOneNoteItem
239+
{
240+
if (!validTitle)
241+
{
242+
return false;
243+
}
244+
245+
bool showOneNote = !c.SpecialKeyState.CtrlPressed;
246+
247+
createAction(parent, name, showOneNote);
248+
249+
context.API.ReQuery();
250+
251+
if(showOneNote)
252+
WindowHelper.FocusOneNote();
253+
254+
return showOneNote;
255+
}
256+
233257
public Result CreateNewPageResult(string newPageName, OneNoteSection section)
234258
{
235259
newPageName = newPageName.Trim();
@@ -240,22 +264,11 @@ public Result CreateNewPageResult(string newPageName, OneNoteSection section)
240264
AutoCompleteText = $"{GetAutoCompleteText(section)}{newPageName}",
241265
IcoPath = iconProvider.NewPage,
242266
PreviewPanel = GetNewPagePreviewPanel(section, newPageName),
243-
Action = c =>
244-
{
245-
bool showOneNote = !c.SpecialKeyState.CtrlPressed;
246-
247-
OneNoteApplication.CreatePage(section, newPageName, showOneNote);
248-
Main.ForceReQuery();
249-
250-
if(showOneNote)
251-
WindowHelper.FocusOneNote();
252-
253-
return showOneNote;
254-
},
267+
Action = c => CreateNewItem(section, newPageName, true, c, OneNoteApplication.CreatePage),
255268
};
256269
}
257270

258-
public Result CreateNewSectionResult(string newSectionName, IOneNoteItem parent)
271+
public Result CreateNewSectionResult(string newSectionName, INotebookOrSectionGroup parent)
259272
{
260273
newSectionName = newSectionName.Trim();
261274
bool validTitle = OneNoteApplication.IsSectionNameValid(newSectionName);
@@ -268,35 +281,11 @@ public Result CreateNewSectionResult(string newSectionName, IOneNoteItem parent)
268281
: $"Section names cannot contain: {string.Join(' ', OneNoteApplication.InvalidSectionChars)}",
269282
AutoCompleteText = $"{GetAutoCompleteText(parent)}{newSectionName}",
270283
IcoPath = iconProvider.NewSection,
271-
Action = c =>
272-
{
273-
if (!validTitle)
274-
{
275-
return false;
276-
}
277-
278-
bool showOneNote = !c.SpecialKeyState.CtrlPressed;
279-
280-
switch (parent)
281-
{
282-
case OneNoteNotebook notebook:
283-
OneNoteApplication.CreateSection(notebook, newSectionName, showOneNote);
284-
break;
285-
case OneNoteSectionGroup sectionGroup:
286-
OneNoteApplication.CreateSection(sectionGroup, newSectionName, showOneNote);
287-
break;
288-
}
289-
290-
Main.ForceReQuery();
291-
if(showOneNote)
292-
WindowHelper.FocusOneNote();
293-
294-
return showOneNote;
295-
},
284+
Action = c => CreateNewItem(parent, newSectionName, validTitle, c, OneNoteApplication.CreateSection),
296285
};
297286
}
298287

299-
public Result CreateNewSectionGroupResult(string newSectionGroupName, IOneNoteItem parent)
288+
public Result CreateNewSectionGroupResult(string newSectionGroupName, INotebookOrSectionGroup parent)
300289
{
301290
newSectionGroupName = newSectionGroupName.Trim();
302291
bool validTitle = OneNoteApplication.IsSectionGroupNameValid(newSectionGroupName);
@@ -309,34 +298,10 @@ public Result CreateNewSectionGroupResult(string newSectionGroupName, IOneNoteIt
309298
: $"Section group names cannot contain: {string.Join(' ', OneNoteApplication.InvalidSectionGroupChars)}",
310299
AutoCompleteText = $"{GetAutoCompleteText(parent)}{newSectionGroupName}",
311300
IcoPath = iconProvider.NewSectionGroup,
312-
Action = c =>
313-
{
314-
if (!validTitle)
315-
{
316-
return false;
317-
}
318-
319-
bool showOneNote = !c.SpecialKeyState.CtrlPressed;
320-
321-
switch (parent)
322-
{
323-
case OneNoteNotebook notebook:
324-
OneNoteApplication.CreateSectionGroup(notebook, newSectionGroupName, showOneNote);
325-
break;
326-
case OneNoteSectionGroup sectionGroup:
327-
OneNoteApplication.CreateSectionGroup(sectionGroup, newSectionGroupName, showOneNote);
328-
break;
329-
}
330-
331-
Main.ForceReQuery();
332-
if(showOneNote)
333-
WindowHelper.FocusOneNote();
334-
335-
return showOneNote;
336-
},
301+
Action = c => CreateNewItem(parent, newSectionGroupName, validTitle, c, OneNoteApplication.CreateSectionGroup),
337302
};
338303
}
339-
304+
340305
public Result CreateNewNotebookResult(string newNotebookName)
341306
{
342307
newNotebookName = newNotebookName.Trim();
@@ -350,23 +315,8 @@ public Result CreateNewNotebookResult(string newNotebookName)
350315
: $"Notebook names cannot contain: {string.Join(' ', OneNoteApplication.InvalidNotebookChars)}",
351316
AutoCompleteText = $"{ActionKeyword} {settings.Keywords.NotebookExplorer}{newNotebookName}",
352317
IcoPath = iconProvider.NewNotebook,
353-
Action = c =>
354-
{
355-
if (!validTitle)
356-
{
357-
return false;
358-
}
359-
360-
bool showOneNote = !c.SpecialKeyState.CtrlPressed;
361-
362-
OneNoteApplication.CreateNotebook(newNotebookName, showOneNote);
363-
Main.ForceReQuery();
364-
365-
if (showOneNote)
366-
WindowHelper.FocusOneNote();
367-
368-
return showOneNote;
369-
},
318+
Action = c => CreateNewItem<IOneNoteItem>(null, newNotebookName, validTitle,
319+
c, (_, name, valid) => OneNoteApplication.CreateNotebook(name, valid)),
370320
};
371321
}
372322

@@ -378,12 +328,17 @@ public List<Result> ContextMenu(Result selectedResult)
378328
var result = CreateOneNoteItemResult(item, false);
379329
result.Title = $"Open and sync \"{item.Name}\"";
380330
result.SubTitle = string.Empty;
331+
result.Score = 30;
332+
result.AddSelectedCount = false;
381333
result.ContextData = null;
382334
results.Add(result);
335+
383336
results.Add(new Result
384337
{
385338
Title = "Open in new OneNote window",
386339
IcoPath = IconProvider.Logo,
340+
Score = 20,
341+
AddSelectedCount = false,
387342
Action = _ =>
388343
{
389344
OneNoteApplication.ComObject.NavigateTo(item.ID, fNewWindow: true);
@@ -392,21 +347,21 @@ public List<Result> ContextMenu(Result selectedResult)
392347
}
393348
});
394349

395-
if (item is not OneNotePage)
350+
string autoCompleteText = GetAutoCompleteText(item);
351+
results.Add(new Result
396352
{
397-
results.Add(new Result
353+
Title = "Show in Notebook Explorer",
354+
SubTitle = autoCompleteText,
355+
AddSelectedCount = false,
356+
Score = 10,
357+
IcoPath = iconProvider.NotebookExplorer,
358+
Action = _ =>
398359
{
399-
Title = "Copy Notebook Explorer path to clipboard",
400-
SubTitle = result.AutoCompleteText,
401-
Score = - 1000,
402-
IcoPath = iconProvider.NotebookExplorer,
403-
Action = _ =>
404-
{
405-
context.API.CopyToClipboard(result.AutoCompleteText);
406-
return false;
407-
}
408-
});
409-
}
360+
context.API.BackToQueryResults();
361+
context.API.ChangeQuery(autoCompleteText, true);
362+
return false;
363+
}
364+
});
410365
}
411366
return results;
412367
}
@@ -444,8 +399,8 @@ Result EmptyCollectionResult(string title, string iconPath, string? subTitle = n
444399
}
445400
}
446401

447-
private Lazy<UserControl> GetNewPagePreviewPanel(OneNoteSection? section, string? pageTitle) =>
448-
new(() => new NewOneNotePagePreviewPanel(context, section, pageTitle));
402+
private Lazy<UserControl> GetNewPagePreviewPanel(OneNoteSection? section, string? pageTitle)
403+
=> new(() => new NewOneNotePagePreviewPanel(context, section, pageTitle));
449404

450405
public static List<Result> NoMatchesFound()
451406
{
@@ -461,9 +416,13 @@ public List<Result> InvalidQuery(bool includeSubtitle = true)
461416
: string.Empty,
462417
iconProvider.Warning);
463418
}
464-
public List<Result> SearchingByTitle()
419+
public List<Result> SearchType(string title, string? parentName)
465420
{
466-
return SingleResult("Now searching by title.", string.Empty, iconProvider.Search);
421+
if (!string.IsNullOrWhiteSpace(parentName))
422+
{
423+
title += $" in \"{parentName}\"";
424+
}
425+
return SingleResult(title, string.Empty, iconProvider.Search);
467426
}
468427

469428
private static List<Result> SingleResult(string title, string subTitle, string iconPath)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ private List<Result> Explorer(string search, IOneNoteItem? parent, IEnumerable<I
118118
case null:
119119
results.Add(resultCreator.CreateNewNotebookResult(search));
120120
break;
121-
case INotebookOrSectionGroup:
122-
results.Add(resultCreator.CreateNewSectionResult(search, parent));
123-
results.Add(resultCreator.CreateNewSectionGroupResult(search, parent));
121+
case INotebookOrSectionGroup p:
122+
results.Add(resultCreator.CreateNewSectionResult(search, p));
123+
results.Add(resultCreator.CreateNewSectionGroupResult(search, p));
124124
break;
125125
case OneNoteSection section:
126126
if (!section.Locked)

0 commit comments

Comments
 (0)