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

115 lines
3.4 KiB
C#
Raw Normal View History

2020-08-17 10:00:56 -07: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.Globalization;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
namespace Microsoft.Plugin.Calculator
{
public class Main : IPlugin, IPluginI18n, IDisposable
{
private static readonly CalculateEngine CalculateEngine = new CalculateEngine();
2020-08-17 10:00:56 -07:00
private PluginInitContext Context { get; set; }
private string IconPath { get; set; }
2020-08-21 12:40:31 -07:00
private bool _disposed;
2020-08-17 10:00:56 -07:00
public List<Result> Query(Query query)
{
if (query == null)
{
throw new ArgumentNullException(paramName: nameof(query));
}
if (!CalculateHelper.InputValid(query.Search))
2020-08-17 10:00:56 -07:00
{
return new List<Result>();
}
try
{
var result = CalculateEngine.Interpret(query.Search, CultureInfo.CurrentUICulture);
2020-08-17 10:00:56 -07:00
// This could happen for some incorrect queries, like pi(2)
if (result.Equals(default(CalculateResult)))
2020-08-17 10:00:56 -07:00
{
return new List<Result>();
}
return new List<Result>
2020-08-17 10:00:56 -07:00
{
ResultHelper.CreateResult(result.Result, result.RoundedResult, IconPath),
};
2020-08-17 10:00:56 -07:00
} // We want to keep the process alive if any the mages library throws any exceptions.
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception e)
#pragma warning restore CA1031 // Do not catch general exception types
{
Log.Exception("Exception when query for <{query}>", e, GetType());
2020-08-17 10:00:56 -07:00
}
return new List<Result>();
}
public void Init(PluginInitContext context)
{
2020-09-09 15:33:18 -07:00
Context = context ?? throw new ArgumentNullException(paramName: nameof(context));
2020-08-17 10:00:56 -07:00
Context.API.ThemeChanged += OnThemeChanged;
UpdateIconPath(Context.API.GetCurrentTheme());
}
// Todo : Update with theme based IconPath
private void UpdateIconPath(Theme theme)
{
if (theme == Theme.Light || theme == Theme.HighContrastWhite)
{
IconPath = "Images/calculator.light.png";
}
else
{
IconPath = "Images/calculator.dark.png";
}
}
private void OnThemeChanged(Theme currentTheme, Theme newTheme)
{
UpdateIconPath(newTheme);
}
public string GetTranslatedPluginTitle()
{
return Properties.Resources.wox_plugin_calculator_plugin_name;
2020-08-17 10:00:56 -07:00
}
public string GetTranslatedPluginDescription()
{
return Properties.Resources.wox_plugin_calculator_plugin_description;
2020-08-17 10:00:56 -07:00
}
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;
}
}
}
}
}