Files
PowerToys/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.TimeZone/Main.cs

221 lines
7.2 KiB
C#
Raw Normal View History

[PT Run][New Plugin] Time zone plugin (#11431) * Initial commit - simple idea for a time zone plugin * Translations, better search results, copy to clipboard, cleanup * fix typo * Add shortcut search and prepare JSON for later usage * Fix typo * Use timezone Data only from JSON * Exclude json file from spell checker * fix wrong dst * Improved results (title, subtitle, tooltip) and fix namespace/class problem * Always show full offset (-##:## and +##:##) * Add and show timezone names (first pass) * Fix typos * fix build * JSON: fix wrong minus sign and put extra country info the end * Improved Subtitle for many matched countries and allow full offset search (+ and -) * Allow more than one names for time zones and remove leftover * Add military time zone names, and fix name result * Only use one JSON entry for one time zone * Use TimeSpan for offset, use build-in calculation for time in time zone * add descriptions for JSON schema * Fix typos * Split out names in separate properties * Add many time names, time zone names and shortcuts * Add additional options and most code documentation * Fix unreadable TimeSpans in JSON and rename helper class * Fix not allowed commas in JSON file * Cut to long time and time zone names in title * Fix missing results for names and offsets * Better result and show only one result when offset are identical (respect daylight saving time) * Show generic name fot time zones without names * Typo fixes * Fix not working serach by shortcuts * Fix german resx file -> english resx file * Translate all names and countires * Fix not working context menu * Typo fixes, fix wrong shortcut in names, comments, few better variable names * New symbols - thx to niels9001 * Search by shortcuts for time names * update schema * Add more time zone names and shortcuts (second pass), make spell checker happy * Reduce matching checks * Show shortcuts in tool-tips, avoid string converting * Show only names that match the query * Make all translatable (Part 1) * Make all translatable (part 2 of 2) * XML Doc * Fix plugin name (type) * Fix Typos * Add TimeZone Plugint to WXS * Add TimeZone plugin to sign pipeline * Add Documentation * Remove double spell entries * Remove TODO leftovers * Fix for results with no countries * Fix typos * fix typos * Fix broken siolution after rebase * Update target framework to make build happy * fix wrong guid count in WXS * fix wrong output folder (setup wasn’t found files) * Address feedback from @jsoref - fix spell check * typo fix - one leftover in expect.txt * Switch to .NET6 and update dokumentation * Address feedbacks, and fix search bug * fix installer build error * fix spellchecker * Address feedback from @htcfreek Co-authored-by: Sekan, Tobias <tobias.sekan@axp-consulting.de>
2022-02-23 14:26:48 +00:00
// 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;
[PT Run][New Plugin] Time zone plugin (#11431) * Initial commit - simple idea for a time zone plugin * Translations, better search results, copy to clipboard, cleanup * fix typo * Add shortcut search and prepare JSON for later usage * Fix typo * Use timezone Data only from JSON * Exclude json file from spell checker * fix wrong dst * Improved results (title, subtitle, tooltip) and fix namespace/class problem * Always show full offset (-##:## and +##:##) * Add and show timezone names (first pass) * Fix typos * fix build * JSON: fix wrong minus sign and put extra country info the end * Improved Subtitle for many matched countries and allow full offset search (+ and -) * Allow more than one names for time zones and remove leftover * Add military time zone names, and fix name result * Only use one JSON entry for one time zone * Use TimeSpan for offset, use build-in calculation for time in time zone * add descriptions for JSON schema * Fix typos * Split out names in separate properties * Add many time names, time zone names and shortcuts * Add additional options and most code documentation * Fix unreadable TimeSpans in JSON and rename helper class * Fix not allowed commas in JSON file * Cut to long time and time zone names in title * Fix missing results for names and offsets * Better result and show only one result when offset are identical (respect daylight saving time) * Show generic name fot time zones without names * Typo fixes * Fix not working serach by shortcuts * Fix german resx file -> english resx file * Translate all names and countires * Fix not working context menu * Typo fixes, fix wrong shortcut in names, comments, few better variable names * New symbols - thx to niels9001 * Search by shortcuts for time names * update schema * Add more time zone names and shortcuts (second pass), make spell checker happy * Reduce matching checks * Show shortcuts in tool-tips, avoid string converting * Show only names that match the query * Make all translatable (Part 1) * Make all translatable (part 2 of 2) * XML Doc * Fix plugin name (type) * Fix Typos * Add TimeZone Plugint to WXS * Add TimeZone plugin to sign pipeline * Add Documentation * Remove double spell entries * Remove TODO leftovers * Fix for results with no countries * Fix typos * fix typos * Fix broken siolution after rebase * Update target framework to make build happy * fix wrong guid count in WXS * fix wrong output folder (setup wasn’t found files) * Address feedback from @jsoref - fix spell check * typo fix - one leftover in expect.txt * Switch to .NET6 and update dokumentation * Address feedbacks, and fix search bug * fix installer build error * fix spellchecker * Address feedback from @htcfreek Co-authored-by: Sekan, Tobias <tobias.sekan@axp-consulting.de>
2022-02-23 14:26:48 +00:00
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")]
[PT Run][New Plugin] Time zone plugin (#11431) * Initial commit - simple idea for a time zone plugin * Translations, better search results, copy to clipboard, cleanup * fix typo * Add shortcut search and prepare JSON for later usage * Fix typo * Use timezone Data only from JSON * Exclude json file from spell checker * fix wrong dst * Improved results (title, subtitle, tooltip) and fix namespace/class problem * Always show full offset (-##:## and +##:##) * Add and show timezone names (first pass) * Fix typos * fix build * JSON: fix wrong minus sign and put extra country info the end * Improved Subtitle for many matched countries and allow full offset search (+ and -) * Allow more than one names for time zones and remove leftover * Add military time zone names, and fix name result * Only use one JSON entry for one time zone * Use TimeSpan for offset, use build-in calculation for time in time zone * add descriptions for JSON schema * Fix typos * Split out names in separate properties * Add many time names, time zone names and shortcuts * Add additional options and most code documentation * Fix unreadable TimeSpans in JSON and rename helper class * Fix not allowed commas in JSON file * Cut to long time and time zone names in title * Fix missing results for names and offsets * Better result and show only one result when offset are identical (respect daylight saving time) * Show generic name fot time zones without names * Typo fixes * Fix not working serach by shortcuts * Fix german resx file -> english resx file * Translate all names and countires * Fix not working context menu * Typo fixes, fix wrong shortcut in names, comments, few better variable names * New symbols - thx to niels9001 * Search by shortcuts for time names * update schema * Add more time zone names and shortcuts (second pass), make spell checker happy * Reduce matching checks * Show shortcuts in tool-tips, avoid string converting * Show only names that match the query * Make all translatable (Part 1) * Make all translatable (part 2 of 2) * XML Doc * Fix plugin name (type) * Fix Typos * Add TimeZone Plugint to WXS * Add TimeZone plugin to sign pipeline * Add Documentation * Remove double spell entries * Remove TODO leftovers * Fix for results with no countries * Fix typos * fix typos * Fix broken siolution after rebase * Update target framework to make build happy * fix wrong guid count in WXS * fix wrong output folder (setup wasn’t found files) * Address feedback from @jsoref - fix spell check * typo fix - one leftover in expect.txt * Switch to .NET6 and update dokumentation * Address feedbacks, and fix search bug * fix installer build error * fix spellchecker * Address feedback from @htcfreek Co-authored-by: Sekan, Tobias <tobias.sekan@axp-consulting.de>
2022-02-23 14:26:48 +00:00
namespace Microsoft.PowerToys.Run.Plugin.TimeZone
{
/// <summary>
/// A power launcher plugin to search across time zones.
/// </summary>
public class Main : IPlugin, IContextMenu, IPluginI18n, ISettingProvider, IDisposable
{
/// <summary>
/// The name of this assembly
/// </summary>
private readonly string _assemblyName;
/// <summary>
/// The settings for this plugin.
/// </summary>
private readonly TimeZoneSettings _timeZoneSettings;
/// <summary>
/// The initial context for this plugin (contains API and meta-data)
/// </summary>
private PluginInitContext? _context;
/// <summary>
/// The path to the icon for each result
/// </summary>
private string _defaultIconPath;
/// <summary>
/// Indicate that the plugin is disposed
/// </summary>
private bool _disposed;
/// <summary>
/// A class that contain all possible time zones.
/// </summary>
private TimeZoneList? _timeZoneList;
/// <summary>
/// Initializes a new instance of the <see cref="Main"/> class.
/// </summary>
public Main()
{
_assemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? GetTranslatedPluginTitle();
_defaultIconPath = "Images/timeZone.light.png";
_timeZoneSettings = new TimeZoneSettings();
}
/// <summary>
/// Gets the localized name.
/// </summary>
public string Name
{
get { return Resources.PluginTitle; }
}
/// <summary>
/// Gets the localized description.
/// </summary>
public string Description
{
get { return Resources.PluginDescription; }
}
/// <summary>
/// Gets the additional options for this plugin.
/// </summary>
public IEnumerable<PluginAdditionalOption> AdditionalOptions
{
get { return TimeZoneSettings.GetAdditionalOptions(); }
}
/// <summary>
/// Initialize the plugin with the given <see cref="PluginInitContext"/>
/// </summary>
/// <param name="context">The <see cref="PluginInitContext"/> for this plugin</param>
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);
}
/// <summary>
/// Return a filtered list, based on the given query
/// </summary>
/// <param name="query">The query to filter the list</param>
/// <returns>A filtered list, can be empty when nothing was found</returns>
public List<Result> Query(Query query)
{
if (_timeZoneList?.TimeZones is null)
{
return new List<Result>(0);
}
if (query is null)
{
return new List<Result>(0);
}
var results = ResultHelper.GetResults(_timeZoneList.TimeZones, _timeZoneSettings, query, _defaultIconPath);
return results.ToList();
}
/// <summary>
/// Return a list context menu entries for a given <see cref="Result"/> (shown at the right side of the result)
/// </summary>
/// <param name="selectedResult">The <see cref="Result"/> for the list with context menu entries</param>
/// <returns>A list context menu entries</returns>
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
return ContextMenuHelper.GetContextMenu(selectedResult, _assemblyName);
}
/// <summary>
/// Change all theme-based elements (typical called when the plugin theme has changed)
/// </summary>
/// <param name="oldtheme">The old <see cref="Theme"/></param>
/// <param name="newTheme">The new <see cref="Theme"/></param>
private void OnThemeChanged(Theme oldtheme, Theme newTheme)
{
UpdateIconPath(newTheme);
}
/// <summary>
/// Update all icons (typical called when the plugin theme has changed)
/// </summary>
/// <param name="theme">The new <see cref="Theme"/> for the icons</param>
private void UpdateIconPath(Theme theme)
{
_defaultIconPath = theme == Theme.Light || theme == Theme.HighContrastWhite
? "Images/timeZone.light.png"
: "Images/timeZone.dark.png";
}
/// <inheritdoc/>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Wrapper method for <see cref="Dispose"/> that dispose additional objects and events form the plugin itself
/// </summary>
/// <param name="disposing">Indicate that the plugin is disposed</param>
protected virtual void Dispose(bool disposing)
{
if (_disposed || !disposing)
{
return;
}
if (!(_context is null))
{
_context.API.ThemeChanged -= OnThemeChanged;
}
_disposed = true;
}
/// <summary>
/// Return the translated plugin title.
/// </summary>
/// <returns>A translated plugin title.</returns>
public string GetTranslatedPluginTitle()
{
return Resources.PluginTitle;
}
/// <summary>
/// Return the translated plugin description.
/// </summary>
/// <returns>A translated plugin description.</returns>
public string GetTranslatedPluginDescription()
{
return Resources.PluginDescription;
}
/// <summary>
/// Return a additional setting panel for this plugin.
/// </summary>
/// <returns>A additional setting panel.</returns>
public Control CreateSettingPanel()
{
throw new NotImplementedException();
}
/// <summary>
/// Update the plugin settings
/// </summary>
/// <param name="settings">The settings for all power launcher plugin.</param>
public void UpdateSettings(PowerLauncherPluginSettings settings)
{
_timeZoneSettings.UpdateSettings(settings);
}
}
}