mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 11:46:30 +02:00
This reverts commit 1dabd761e1,
to give proper attribution to the author.
This commit is contained in:
@@ -1,180 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using ManagedCommon;
|
||||
using UnitsNet;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Community.PowerToys.Run.Plugin.UnitConverter
|
||||
{
|
||||
public class Main : IPlugin, IPluginI18n, IContextMenu, IDisposable
|
||||
{
|
||||
public string Name => Properties.Resources.plugin_name;
|
||||
|
||||
public string Description => Properties.Resources.plugin_description;
|
||||
|
||||
private PluginInitContext _context;
|
||||
private static string _icon_path;
|
||||
private bool _disposed;
|
||||
|
||||
public void Init(PluginInitContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
throw new ArgumentNullException(paramName: nameof(context));
|
||||
}
|
||||
|
||||
_context = context;
|
||||
_context.API.ThemeChanged += OnThemeChanged;
|
||||
UpdateIconPath(_context.API.GetCurrentTheme());
|
||||
}
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
{
|
||||
if (query == null)
|
||||
{
|
||||
throw new ArgumentNullException(paramName: nameof(query));
|
||||
}
|
||||
|
||||
// Parse
|
||||
ConvertModel convertModel = InputInterpreter.Parse(query);
|
||||
if (convertModel == null)
|
||||
{
|
||||
return new List<Result>();
|
||||
}
|
||||
|
||||
// Convert
|
||||
return UnitHandler.Convert(convertModel)
|
||||
.Select(x => GetResult(x))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private Result GetResult(UnitConversionResult result)
|
||||
{
|
||||
return new Result
|
||||
{
|
||||
ContextData = result,
|
||||
Title = string.Format("{0} {1}", result.ConvertedValue, result.UnitName),
|
||||
IcoPath = _icon_path,
|
||||
Score = 300,
|
||||
SubTitle = string.Format(Properties.Resources.copy_to_clipboard, result.QuantityType),
|
||||
Action = c =>
|
||||
{
|
||||
var ret = false;
|
||||
var thread = new Thread(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Clipboard.SetText(result.ConvertedValue.ToString());
|
||||
ret = true;
|
||||
}
|
||||
catch (ExternalException)
|
||||
{
|
||||
MessageBox.Show(Properties.Resources.copy_failed);
|
||||
}
|
||||
});
|
||||
thread.SetApartmentState(ApartmentState.STA);
|
||||
thread.Start();
|
||||
thread.Join();
|
||||
return ret;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
private ContextMenuResult CreateContextMenuEntry(UnitConversionResult result)
|
||||
{
|
||||
return new ContextMenuResult
|
||||
{
|
||||
PluginName = Name,
|
||||
Title = Properties.Resources.context_menu_copy,
|
||||
Glyph = "\xE8C8",
|
||||
FontFamily = "Segoe MDL2 Assets",
|
||||
AcceleratorKey = Key.Enter,
|
||||
Action = _ =>
|
||||
{
|
||||
bool ret = false;
|
||||
var thread = new Thread(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Clipboard.SetText(result.ConvertedValue.ToString());
|
||||
ret = true;
|
||||
}
|
||||
catch (ExternalException)
|
||||
{
|
||||
MessageBox.Show(Properties.Resources.copy_failed);
|
||||
}
|
||||
});
|
||||
thread.SetApartmentState(ApartmentState.STA);
|
||||
thread.Start();
|
||||
thread.Join();
|
||||
return ret;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
|
||||
{
|
||||
if (!(selectedResult?.ContextData is UnitConversionResult))
|
||||
{
|
||||
return new List<ContextMenuResult>();
|
||||
}
|
||||
|
||||
List<ContextMenuResult> contextResults = new List<ContextMenuResult>();
|
||||
UnitConversionResult result = selectedResult.ContextData as UnitConversionResult;
|
||||
contextResults.Add(CreateContextMenuEntry(result));
|
||||
|
||||
return contextResults;
|
||||
}
|
||||
|
||||
public string GetTranslatedPluginTitle()
|
||||
{
|
||||
return Properties.Resources.plugin_name;
|
||||
}
|
||||
|
||||
public string GetTranslatedPluginDescription()
|
||||
{
|
||||
return Properties.Resources.plugin_description;
|
||||
}
|
||||
|
||||
private void OnThemeChanged(Theme _, Theme newTheme)
|
||||
{
|
||||
UpdateIconPath(newTheme);
|
||||
}
|
||||
|
||||
private static void UpdateIconPath(Theme theme)
|
||||
{
|
||||
if (theme == Theme.Light || theme == Theme.HighContrastWhite)
|
||||
{
|
||||
_icon_path = "Images/unitconverter.light.png";
|
||||
}
|
||||
else
|
||||
{
|
||||
_icon_path = "Images/unitconverter.dark.png";
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_context.API.ThemeChanged -= OnThemeChanged;
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user