// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using ManagedCommon;
using Microsoft.PowerToys.Run.Plugin.OneNote.Properties;
using ScipBe.Common.Office.OneNote;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.WindowsAndMessaging;
using Wox.Plugin;
namespace Microsoft.PowerToys.Run.Plugin.OneNote
{
///
/// A power launcher plugin to search across time zones.
///
public class Main : IPlugin, IPluginI18n
{
///
/// A value indicating if the OneNote interop library was able to successfully initialize.
///
private bool _oneNoteInstalled;
///
/// The initial context for this plugin (contains API and meta-data)
///
private PluginInitContext? _context;
///
/// The path to the icon for each result
///
private string _iconPath;
///
/// Initializes a new instance of the class.
///
public Main()
{
UpdateIconPath(Theme.Light);
}
///
/// Gets the localized name.
///
public string Name => Resources.PluginTitle;
///
/// Gets the localized description.
///
public string Description => Resources.PluginDescription;
///
/// Initialize the plugin with the given
///
/// The for this plugin
public void Init(PluginInitContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
try
{
_ = OneNoteProvider.PageItems.Any();
_oneNoteInstalled = true;
}
catch (COMException)
{
// OneNote isn't installed, plugin won't do anything.
_oneNoteInstalled = false;
}
_context.API.ThemeChanged += OnThemeChanged;
UpdateIconPath(_context.API.GetCurrentTheme());
}
///
/// Return a filtered list, based on the given query
///
/// The query to filter the list
/// A filtered list, can be empty when nothing was found
public List Query(Query query)
{
if (!_oneNoteInstalled || query is null || string.IsNullOrWhiteSpace(query.Search))
{
return new List(0);
}
var pages = OneNoteProvider.FindPages(query.Search);
return pages.Select(p => new Result
{
IcoPath = _iconPath,
Title = p.Name,
QueryTextDisplay = p.Name,
SubTitle = @$"{p.Notebook.Name}\{p.Section.Name}",
Action = (_) =>
{
p.OpenInOneNote();
ShowOneNote();
return true;
},
ContextData = p,
ToolTipData = new ToolTipData(Name, @$"{p.Notebook.Name}\{p.Section.Name}\{p.Name}"),
}).ToList();
}
///
/// Return the translated plugin title.
///
/// A translated plugin title.
public string GetTranslatedPluginTitle() => Resources.PluginTitle;
///
/// Return the translated plugin description.
///
/// A translated plugin description.
public string GetTranslatedPluginDescription() => Resources.PluginDescription;
private void OnThemeChanged(Theme currentTheme, Theme newTheme)
{
UpdateIconPath(newTheme);
}
[MemberNotNull(nameof(_iconPath))]
private void UpdateIconPath(Theme theme)
{
_iconPath = theme == Theme.Light || theme == Theme.HighContrastWhite ? "Images/oneNote.light.png" : "Images/oneNote.dark.png";
}
///
/// Brings OneNote to the foreground and restores it if minimized.
///
private void ShowOneNote()
{
using var process = Process.GetProcessesByName("onenote").FirstOrDefault();
if (process?.MainWindowHandle != null)
{
HWND handle = (HWND)process.MainWindowHandle;
if (PInvoke.IsIconic(handle))
{
PInvoke.ShowWindow(handle, SHOW_WINDOW_CMD.SW_RESTORE);
}
PInvoke.SetForegroundWindow(handle);
}
}
}
}