// 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 ManagedCommon;
using Microsoft.PowerToys.Run.Plugin.WindowsSettings.Helper;
using Microsoft.PowerToys.Run.Plugin.WindowsSettings.Properties;
using Wox.Plugin;
namespace Microsoft.PowerToys.Run.Plugin.WindowsSettings
{
///
/// Main class of this plugin that implement all used interfaces.
///
public class Main : IPlugin, IContextMenu, IPluginI18n, IDisposable
{
///
/// The path to the symbol for a light theme.
///
private const string _lightSymbol = "Images/WindowsSettings.light.png";
///
/// The path to the symbol for a dark theme.
///
private const string _darkSymbol = "Images/WindowsSettings.dark.png";
///
/// 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;
///
/// A class that contain all possible windows settings.
///
private WindowsSettings? _windowsSettings;
///
/// Initializes a new instance of the class.
///
public Main()
{
_assemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? Name;
_defaultIconPath = _lightSymbol;
}
///
/// Gets the localized name.
///
public string Name => Resources.PluginTitle;
///
/// Gets the localized description.
///
public string Description => Resources.PluginDescription;
///
/// Gets the plugin ID for validation
///
public static string PluginID => "5043CECEE6A748679CBE02D27D83747A";
///
/// 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());
_windowsSettings = JsonSettingsListHelper.ReadAllPossibleSettings();
UnsupportedSettingsHelper.FilterByBuild(_windowsSettings);
TranslationHelper.TranslateAllSettings(_windowsSettings);
WindowsSettingsPathHelper.GenerateSettingsPathValues(_windowsSettings);
}
///
/// 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 (_windowsSettings?.Settings is null)
{
return new List(0);
}
var filteredList = _windowsSettings.Settings
.Where(Predicate)
.OrderBy(found => found.Name);
var newList = ResultHelper.GetResultList(filteredList, query.Search, _defaultIconPath);
return newList;
bool Predicate(WindowsSetting found)
{
if (string.IsNullOrWhiteSpace(query.Search))
{
// If no search string is entered skip query comparison.
return true;
}
if (found.Name.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase))
{
return true;
}
if (!(found.Areas is null))
{
foreach (var area in found.Areas)
{
// Search for areas on normal queries.
if (area.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase))
{
return true;
}
// Search for Area only on queries with action char.
if (area.Contains(query.Search.Replace(":", string.Empty), StringComparison.CurrentCultureIgnoreCase)
&& query.Search.EndsWith(":", StringComparison.CurrentCultureIgnoreCase))
{
return true;
}
}
}
if (!(found.AltNames is null))
{
foreach (var altName in found.AltNames)
{
if (altName.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase))
{
return true;
}
}
}
// Search by key char '>' for app name and settings path
if (query.Search.Contains('>'))
{
return ResultHelper.FilterBySettingsPath(found, query.Search);
}
return false;
}
}
///
/// 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);
}
///
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;
}
///
/// Gets the localized name.
///
public string GetTranslatedPluginTitle()
{
return Name;
}
///
/// Gets the localized description.
///
public string GetTranslatedPluginDescription()
{
return Description;
}
///
/// 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
? _lightSymbol
: _darkSymbol;
}
}
}