// 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;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using ManagedCommon;
using Microsoft.PowerToys.Run.Plugin.Registry.Classes;
using Microsoft.PowerToys.Run.Plugin.Registry.Helper;
using Microsoft.PowerToys.Run.Plugin.Registry.Properties;
using Wox.Plugin;
[assembly: InternalsVisibleTo("Microsoft.PowerToys.Run.Plugin.Registry.UnitTests")]
namespace Microsoft.PowerToys.Run.Plugin.Registry
{
///
/// Main class of this plugin that implement all used interfaces
///
public class Main : IPlugin, IContextMenu, IPluginI18n, IDisposable
{
///
/// The name of this assembly
///
private readonly string _assemblyName;
///
/// The initial context for this plugin (contains API and meta-data)
///
private PluginInitContext? _context;
///
/// The path to the icon for each result
///
private string _defaultIconPath;
///
/// Indicate that the plugin is disposed
///
private bool _disposed;
public string Name => Resources.PluginTitle;
public string Description => Resources.PluginDescription;
///
/// Initializes a new instance of the class.
///
public Main()
{
_assemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? GetTranslatedPluginTitle();
_defaultIconPath = "Images/reg.light.png";
}
///
/// Initialize the plugin with the given
///
/// The for this plugin
public void Init(PluginInitContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_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 (query?.Search is null)
{
return new List(0);
}
var searchForValueName = QueryHelper.GetQueryParts(query.Search, out var queryKey, out var queryValueName);
var (baseKeyList, subKey) = RegistryHelper.GetRegistryBaseKey(queryKey);
if (baseKeyList is null)
{
// no base key found
return ResultHelper.GetResultList(RegistryHelper.GetAllBaseKeys(), _defaultIconPath);
}
else if (baseKeyList.Count() == 1)
{
// only one base key was found -> start search for the sub-key
var list = RegistryHelper.SearchForSubKey(baseKeyList.First(), subKey);
// when only one sub-key was found and a user search for values ("\\")
// show the filtered list of values of one sub-key
if (searchForValueName && list.Count == 1)
{
return ResultHelper.GetValuesFromKey(list.First().Key, _defaultIconPath, queryValueName);
}
return ResultHelper.GetResultList(list, _defaultIconPath);
}
else if (baseKeyList.Count() > 1)
{
// more than one base key was found -> show results
return ResultHelper.GetResultList(baseKeyList.Select(found => new RegistryEntry(found)), _defaultIconPath);
}
return new List();
}
///
/// Return a list context menu entries for a given (shown at the right side of the result)
///
/// The for the list with context menu entries
/// A list context menu entries
public List LoadContextMenus(Result selectedResult)
{
return ContextMenuHelper.GetContextMenu(selectedResult, _assemblyName);
}
///
/// Change all theme-based elements (typical called when the plugin theme has changed)
///
/// The old
/// The new
private void OnThemeChanged(Theme oldtheme, Theme newTheme)
{
UpdateIconPath(newTheme);
}
///
/// Update all icons (typical called when the plugin theme has changed)
///
/// The new for the icons
private void UpdateIconPath(Theme theme)
{
_defaultIconPath = theme == Theme.Light || theme == Theme.HighContrastWhite
? "Images/reg.light.png"
: "Images/reg.dark.png";
}
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Wrapper method for that dispose additional objects and events form the plugin itself
///
/// Indicate that the plugin is disposed
protected virtual void Dispose(bool disposing)
{
if (_disposed || !disposing)
{
return;
}
if (_context != null && _context.API != null)
{
_context.API.ThemeChanged -= OnThemeChanged;
}
_disposed = true;
}
///
public string GetTranslatedPluginTitle()
{
return Resources.PluginTitle;
}
///
public string GetTranslatedPluginDescription()
{
return Resources.PluginDescription;
}
}
}