mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-04 10:16:24 +02:00
[PTRun][OneNote]Add query caching and delayedExecution (#18919)
* Adding OneNote plugin for PowerToys Run * Updating to 3.0.1 dependency, updating md, spellcheck, ready for PR * Updating spelling and using localized string * Adding OneNote link to readme * Adding OneNote requirement to description * removing 'open' from description * Updating interop version, PR feedback * Adding query caching and delayedExecution to PT Run OneNote plugin * Adding binaries to signing, and updating doc * Adding dependency nuget package binaries to installer * PR feedback and .editorconfig fix to ignore IDE rules that conflict with repo styling * Fixing spelling
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.InteropServices;
|
||||
using LazyCache;
|
||||
using ManagedCommon;
|
||||
using Microsoft.PowerToys.Run.Plugin.OneNote.Properties;
|
||||
using ScipBe.Common.Office.OneNote;
|
||||
@@ -18,13 +19,18 @@ namespace Microsoft.PowerToys.Run.Plugin.OneNote
|
||||
/// <summary>
|
||||
/// A power launcher plugin to search across time zones.
|
||||
/// </summary>
|
||||
public class Main : IPlugin, IPluginI18n
|
||||
public class Main : IPlugin, IDelayedExecutionPlugin, IPluginI18n
|
||||
{
|
||||
/// <summary>
|
||||
/// A value indicating if the OneNote interop library was able to successfully initialize.
|
||||
/// </summary>
|
||||
private bool _oneNoteInstalled;
|
||||
|
||||
/// <summary>
|
||||
/// LazyCache CachingService instance to speed up repeated queries.
|
||||
/// </summary>
|
||||
private CachingService? _cache;
|
||||
|
||||
/// <summary>
|
||||
/// The initial context for this plugin (contains API and meta-data)
|
||||
/// </summary>
|
||||
@@ -65,6 +71,9 @@ namespace Microsoft.PowerToys.Run.Plugin.OneNote
|
||||
{
|
||||
_ = OneNoteProvider.PageItems.Any();
|
||||
_oneNoteInstalled = true;
|
||||
|
||||
_cache = new CachingService();
|
||||
_cache.DefaultCachePolicy.DefaultCacheDurationSeconds = (int)TimeSpan.FromDays(1).TotalSeconds;
|
||||
}
|
||||
catch (COMException)
|
||||
{
|
||||
@@ -83,28 +92,47 @@ namespace Microsoft.PowerToys.Run.Plugin.OneNote
|
||||
/// <returns>A filtered list, can be empty when nothing was found</returns>
|
||||
public List<Result> Query(Query query)
|
||||
{
|
||||
if (!_oneNoteInstalled || query is null || string.IsNullOrWhiteSpace(query.Search))
|
||||
if (!_oneNoteInstalled || query is null || string.IsNullOrWhiteSpace(query.Search) || _cache is null)
|
||||
{
|
||||
return new List<Result>(0);
|
||||
}
|
||||
|
||||
var pages = OneNoteProvider.FindPages(query.Search);
|
||||
// If there's cached results for this query, return immediately, otherwise wait for delayedExecution.
|
||||
var results = _cache.Get<List<Result>>(query.Search);
|
||||
return results ?? Query(query, false);
|
||||
}
|
||||
|
||||
return pages.Select(p => new Result
|
||||
/// <summary>
|
||||
/// Return a filtered list, based on the given query
|
||||
/// </summary>
|
||||
/// <param name="query">The query to filter the list</param>
|
||||
/// <param name="delayedExecution">False if this is the first pass through plugins, true otherwise. Slow plugins should run delayed.</param>
|
||||
/// <returns>A filtered list, can be empty when nothing was found</returns>
|
||||
public List<Result> Query(Query query, bool delayedExecution)
|
||||
{
|
||||
if (!delayedExecution || !_oneNoteInstalled || query is null || string.IsNullOrWhiteSpace(query.Search) || _cache is null)
|
||||
{
|
||||
IcoPath = _iconPath,
|
||||
Title = p.Name,
|
||||
QueryTextDisplay = p.Name,
|
||||
SubTitle = @$"{p.Notebook.Name}\{p.Section.Name}",
|
||||
Action = (_) =>
|
||||
return new List<Result>(0);
|
||||
}
|
||||
|
||||
// Get results from cache if they already exist for this query, otherwise query OneNote. Results will be cached for 1 day.
|
||||
var results = _cache.GetOrAdd(query.Search, () =>
|
||||
{
|
||||
var pages = OneNoteProvider.FindPages(query.Search);
|
||||
|
||||
return pages.Select(p => new Result
|
||||
{
|
||||
p.OpenInOneNote();
|
||||
ShowOneNote();
|
||||
return true;
|
||||
},
|
||||
ContextData = p,
|
||||
ToolTipData = new ToolTipData(Name, @$"{p.Notebook.Name}\{p.Section.Name}\{p.Name}"),
|
||||
}).ToList();
|
||||
IcoPath = _iconPath,
|
||||
Title = p.Name,
|
||||
QueryTextDisplay = p.Name,
|
||||
SubTitle = @$"{p.Notebook.Name}\{p.Section.Name}",
|
||||
Action = (_) => OpenPageInOneNote(p),
|
||||
ContextData = p,
|
||||
ToolTipData = new ToolTipData(Name, @$"{p.Notebook.Name}\{p.Section.Name}\{p.Name}"),
|
||||
}).ToList();
|
||||
});
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -130,6 +158,21 @@ namespace Microsoft.PowerToys.Run.Plugin.OneNote
|
||||
_iconPath = theme == Theme.Light || theme == Theme.HighContrastWhite ? "Images/oneNote.light.png" : "Images/oneNote.dark.png";
|
||||
}
|
||||
|
||||
private bool OpenPageInOneNote(IOneNoteExtPage page)
|
||||
{
|
||||
try
|
||||
{
|
||||
page.OpenInOneNote();
|
||||
ShowOneNote();
|
||||
return true;
|
||||
}
|
||||
catch (COMException)
|
||||
{
|
||||
// The page, section or even notebook may no longer exist, ignore and do nothing.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Brings OneNote to the foreground and restores it if minimized.
|
||||
/// </summary>
|
||||
|
||||
@@ -52,6 +52,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Interop.Microsoft.Office.Interop.OneNote" Version="1.1.0.2" />
|
||||
<PackageReference Include="LazyCache" Version="2.4.0" />
|
||||
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.1.691-beta">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
||||
@@ -81,6 +81,7 @@
|
||||
<PackageReference Include="Interop.Microsoft.Office.Interop.OneNote" Version="1.1.0.2" />
|
||||
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" />
|
||||
<PackageReference Include="Mages" Version="2.0.1" />
|
||||
<PackageReference Include="LazyCache" Version="2.4.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
||||
Reference in New Issue
Block a user