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.Plugin;
|
2020-10-23 13:06:22 -07:00
|
|
|
|
using Wox.Plugin.Logger;
|
2020-08-17 10:00:56 -07:00
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.Plugin.Calculator
|
|
|
|
|
|
{
|
|
|
|
|
|
public class Main : IPlugin, IPluginI18n, IDisposable
|
|
|
|
|
|
{
|
2020-09-10 05:01:30 +02:00
|
|
|
|
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));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-10 05:01:30 +02:00
|
|
|
|
if (!CalculateHelper.InputValid(query.Search))
|
2020-08-17 10:00:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
return new List<Result>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2020-09-10 05:01:30 +02:00
|
|
|
|
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)
|
2020-09-10 05:01:30 +02:00
|
|
|
|
if (result.Equals(default(CalculateResult)))
|
2020-08-17 10:00:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
return new List<Result>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-10 05:01:30 +02:00
|
|
|
|
return new List<Result>
|
2020-08-17 10:00:56 -07:00
|
|
|
|
{
|
2020-10-08 08:57:17 -07:00
|
|
|
|
ResultHelper.CreateResult(result.RoundedResult, IconPath),
|
2020-09-10 05:01:30 +02:00
|
|
|
|
};
|
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
|
|
|
|
|
|
{
|
2020-09-23 16:32:06 -07:00
|
|
|
|
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()
|
|
|
|
|
|
{
|
2020-09-02 15:24:59 -07:00
|
|
|
|
return Properties.Resources.wox_plugin_calculator_plugin_name;
|
2020-08-17 10:00:56 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetTranslatedPluginDescription()
|
|
|
|
|
|
{
|
2020-09-02 15:24:59 -07:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|