// 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 System.Windows.Controls; using ManagedCommon; using Microsoft.PowerToys.Run.Plugin.TimeZone.Classes; using Microsoft.PowerToys.Run.Plugin.TimeZone.Helper; using Microsoft.PowerToys.Run.Plugin.TimeZone.Properties; using Microsoft.PowerToys.Settings.UI.Library; using Wox.Plugin; [assembly: InternalsVisibleTo("Microsoft.PowerToys.Run.Plugin.TimeZone.UnitTests")] namespace Microsoft.PowerToys.Run.Plugin.TimeZone { /// /// A power launcher plugin to search across time zones. /// public class Main : IPlugin, IContextMenu, IPluginI18n, ISettingProvider, IDisposable { /// /// The name of this assembly /// private readonly string _assemblyName; /// /// The settings for this plugin. /// private readonly TimeZoneSettings _timeZoneSettings; /// /// 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 time zones. /// private TimeZoneList? _timeZoneList; /// /// Initializes a new instance of the class. /// public Main() { _assemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? GetTranslatedPluginTitle(); _defaultIconPath = "Images/timeZone.light.png"; _timeZoneSettings = new TimeZoneSettings(); } /// /// Gets the localized name. /// public string Name { get { return Resources.PluginTitle; } } /// /// Gets the localized description. /// public string Description { get { return Resources.PluginDescription; } } /// /// Gets the additional options for this plugin. /// public IEnumerable AdditionalOptions { get { return TimeZoneSettings.GetAdditionalOptions(); } } /// /// 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()); _timeZoneList = JsonHelper.ReadAllPossibleTimeZones(); TranslationHelper.TranslateAllSettings(_timeZoneList); } /// /// 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 (_timeZoneList?.TimeZones is null) { return new List(0); } if (query is null) { return new List(0); } var results = ResultHelper.GetResults(_timeZoneList.TimeZones, _timeZoneSettings, query, _defaultIconPath); return results.ToList(); } /// /// 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/timeZone.light.png" : "Images/timeZone.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 is null)) { _context.API.ThemeChanged -= OnThemeChanged; } _disposed = true; } /// /// Return the translated plugin title. /// /// A translated plugin title. public string GetTranslatedPluginTitle() { return Resources.PluginTitle; } /// /// Return the translated plugin description. /// /// A translated plugin description. public string GetTranslatedPluginDescription() { return Resources.PluginDescription; } /// /// Return a additional setting panel for this plugin. /// /// A additional setting panel. public Control CreateSettingPanel() { throw new NotImplementedException(); } /// /// Update the plugin settings /// /// The settings for all power launcher plugin. public void UpdateSettings(PowerLauncherPluginSettings settings) { _timeZoneSettings.UpdateSettings(settings); } } }