Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PTRun][OneNote] Improve the OneNote plugin #36813

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
Simplify result tooltips
Added the Humanizer package
Moved user facing strings to resources
  • Loading branch information
Odotocodot committed Jan 18, 2025
commit 190773de8f2f4b8ea535914dd6270b43eb172c59
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
<PackageVersion Include="ControlzEx" Version="6.0.0" />
<PackageVersion Include="HelixToolkit" Version="2.24.0" />
<PackageVersion Include="HelixToolkit.Core.Wpf" Version="2.24.0" />
<PackageVersion Include="Humanizer" Version="2.14.1" />
<PackageVersion Include="hyjiacan.pinyin4net" Version="4.1.1" />
<PackageVersion Include="LazyCache" Version="2.4.0" />
<PackageVersion Include="Mages" Version="3.0.0" />
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
using System.Reflection;
using System.Text;
using System.Windows.Input;
using Humanizer;
using Microsoft.PowerToys.Run.Plugin.OneNote.Properties;
using Odotocodot.OneNote.Linq;
using Wox.Infrastructure;
@@ -32,6 +33,7 @@ public class ResultCreator
private static readonly CompositeFormat CreateNotebook = CompositeFormat.Parse(Resources.CreateNotebook);
private static readonly CompositeFormat Location = CompositeFormat.Parse(Resources.Location);
private static readonly CompositeFormat Path = CompositeFormat.Parse(Resources.Path);
private static readonly CompositeFormat LastModified = CompositeFormat.Parse(Resources.LastModified);
private static readonly CompositeFormat SectionNamesCannotContain = CompositeFormat.Parse(Resources.SectionNamesCannotContain);
private static readonly CompositeFormat SectionGroupNamesCannotContain = CompositeFormat.Parse(Resources.SectionGroupNamesCannotContain);
private static readonly CompositeFormat NotebookNamesCannotContain = CompositeFormat.Parse(Resources.NotebookNamesCannotContain);
@@ -48,56 +50,30 @@ internal ResultCreator(PluginInitContext context, OneNoteSettings settings, Icon
private string GetTitle(IOneNoteItem item, List<int>? highlightData)
{
string title = item.Name;
if (item.IsUnread && _settings.ShowUnreadItems)
{
string unread = "\u2022 ";
title = title.Insert(0, unread);

if (highlightData != null)
{
for (int i = 0; i < highlightData.Count; i++)
{
highlightData[i] += unread.Length;
}
}
if (!item.IsUnread || !_settings.ShowUnreadItems)
{
return title;
}

return title;
}
const string unread = "\u2022 ";
title = title.Insert(0, unread);

private string GetQueryTextDisplay(IOneNoteItem item) => $"{Keywords.NotebookExplorer}{GetNicePath(item, Keywords.NotebookExplorerSeparator)}{Keywords.NotebookExplorerSeparator}";

private static string GetLastEdited(TimeSpan diff)
{
string lastEdited = "Last edited ";
if (PluralCheck(diff.TotalDays, "day", ref lastEdited)
|| PluralCheck(diff.TotalHours, "hour", ref lastEdited)
|| PluralCheck(diff.TotalMinutes, "min", ref lastEdited)
|| PluralCheck(diff.TotalSeconds, "sec", ref lastEdited))
if (highlightData == null)
{
return lastEdited;
}
else
{
return lastEdited += "Now.";
return title;
}

static bool PluralCheck(double totalTime, string timeType, ref string lastEdited)
for (int i = 0; i < highlightData.Count; i++)
{
var roundedTime = (int)Math.Round(totalTime);
if (roundedTime > 0)
{
string plural = roundedTime == 1 ? string.Empty : "s";
lastEdited += $"{roundedTime} {timeType}{plural} ago.";
return true;
}
else
{
return false;
}
highlightData[i] += unread.Length;
}

return title;
}

private string GetQueryTextDisplay(IOneNoteItem item) => $"{Keywords.NotebookExplorer}{GetNicePath(item, Keywords.NotebookExplorerSeparator)}{Keywords.NotebookExplorerSeparator}";

internal List<Result> EmptyQuery(Query query)
{
if (_context.CurrentPluginMetadata.IsGlobal && !query.RawUserQuery.StartsWith(query.ActionKeyword, StringComparison.Ordinal))
@@ -177,54 +153,41 @@ internal List<Result> EmptyQuery(Query query)
internal Result CreateOneNoteItemResult(IOneNoteItem item, bool actionIsAutoComplete, List<int>? highlightData = null, int score = 0)
{
string title = GetTitle(item, highlightData);
string toolTip = string.Empty;
string subTitle = GetNicePath(item);
string queryTextDisplay = GetQueryTextDisplay(item);

// TODO: Potential improvement would be to show the children of the OneNote item in its tooltip.
// E.g. for a notebook, it would display the number of section groups, sections and pages.
// Would require even more localisation.
// An example: https://github.com/Odotocodot/Flow.Launcher.Plugin.OneNote/blob/5f56aa81a19641197d4ea4a97dc22cf1aa21f5e6/Flow.Launcher.Plugin.OneNote/ResultCreator.cs#L145
switch (item)
{
case OneNoteNotebook notebook:
toolTip =
$"Last Modified:\t{notebook.LastModified:F}\n" +
$"Sections:\t\t{notebook.Sections.Count()}\n" +
$"Sections Groups:\t{notebook.SectionGroups.Count()}";

subTitle = string.Empty;
break;
case OneNoteSectionGroup sectionGroup:
toolTip =
$"Path:\t\t{subTitle}\n" +
$"Last Modified:\t{sectionGroup.LastModified:F}\n" +
$"Sections:\t\t{sectionGroup.Sections.Count()}\n" +
$"Sections Groups:\t{sectionGroup.SectionGroups.Count()}";

break;
case OneNoteSection section:
if (section.Encrypted)
{
// potentially replace with glyphs if supported
title += $" [Encrypted] {(section.Locked ? "[Locked]" : "[Unlocked]")} \uE72E";
// potentially replace with glyphs when/if supported
title += string.Format(CultureInfo.CurrentCulture, " [{0}]", section.Locked ? Resources.Locked : Resources.Unlocked);
}

toolTip =
$"Path:\t\t{subTitle}\n" +
$"Last Modified:\t{section.LastModified}\n" +
$"Pages:\t\t{section.Pages.Count()}";

break;
case OneNotePage page:
queryTextDisplay = !actionIsAutoComplete ? string.Empty : queryTextDisplay[..^1];

actionIsAutoComplete = false;

subTitle = subTitle[..^(page.Name.Length + PathSeparator.Length)];
toolTip =
$"Path:\t\t {subTitle} \n" +
$"Created:\t\t{page.Created:F}\n" +
$"Last Modified:\t{page.LastModified:F}";
break;
}

var toolTip = string.Format(CultureInfo.CurrentCulture, LastModified, item.LastModified);
if (item is not OneNoteNotebook)
{
toolTip = toolTip.Insert(0, string.Format(CultureInfo.CurrentCulture, Path, subTitle) + "\n");
}

return new Result
{
Title = title,
@@ -258,8 +221,8 @@ internal Result CreatePageResult(OneNotePage page, string? query)
internal Result CreateRecentPageResult(OneNotePage page)
{
var result = CreateOneNoteItemResult(page, false, null);
result.SubTitle = $"{GetLastEdited(DateTime.Now - page.LastModified)}\t{result.SubTitle}";
result.IcoPath = _iconProvider.Page;
result.IcoPath = _iconProvider.Recent;
result.SubTitle = $"{page.LastModified.Humanize(culture: CultureInfo.CurrentCulture)} | {result.SubTitle}";
return result;
}

Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@

<ItemGroup>
<PackageReference Include="LazyCache" />
<PackageReference Include="Humanizer" />
<PackageReference Include="Microsoft.Windows.CsWin32">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -251,4 +251,13 @@
<data name="NoItemsFoundTypeValidName" xml:space="preserve">
<value>No items found. Type a valid name to create one</value>
</data>
<data name="LastModified" xml:space="preserve">
<value>Last Modified: {0:F}</value>
</data>
<data name="Locked" xml:space="preserve">
<value>Locked</value>
</data>
<data name="Unlocked" xml:space="preserve">
<value>Unlocked</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/modules/launcher/PowerLauncher/PowerLauncher.csproj
Original file line number Diff line number Diff line change
@@ -46,6 +46,7 @@
<ItemGroup>
<PackageReference Include="Mages" />
<PackageReference Include="LazyCache" />
<PackageReference Include="Humanizer" />
<PackageReference Include="Microsoft.Data.Sqlite" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" />