Calculator module is online

This commit is contained in:
Clint Rutkas
2024-08-29 21:22:26 -07:00
parent 99a616ced2
commit 255dc68f4f
4 changed files with 107 additions and 86 deletions

View File

@@ -3,7 +3,6 @@
<PropertyGroup>
<RootNamespace>Calculator</RootNamespace>
<UseWinUI>true</UseWinUI>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>

View File

@@ -0,0 +1,45 @@
// 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 Microsoft.Windows.CommandPalette.Extensions;
using Microsoft.Windows.CommandPalette.Extensions.Helpers;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace Calculator;
public class CalculatorAction : InvokableCommand
{
private bool _success;
private string _result;
public CalculatorAction()
{
Icon = new("\ue8ef");
}
public override ICommandResult Invoke()
{
if (_success)
{
ClipboardHelper.SetText(_result);
}
return ActionResult.KeepOpen();
}
internal void SetResult(string result, bool success)
{
_result = result;
_success = success;
if (success)
{
Name = "Copy";
}
else
{
Name = string.Empty;
}
}
}

View File

@@ -2,94 +2,13 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.Windows.CommandPalette.Extensions;
using Microsoft.Windows.CommandPalette.Extensions.Helpers;
using Windows.Foundation;
using System.Data;
using System;
using Microsoft.Windows.CommandPalette.Extensions;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace Calculator;
public class CalculatorAction : InvokableCommand
{
private bool success;
private string result;
public CalculatorAction()
{
Icon = new("\ue8ef");
}
public override ICommandResult Invoke()
{
if (success)
{
ClipboardHelper.SetText(result);
}
return ActionResult.KeepOpen();
}
internal void SetResult(string result, bool success)
{
this.result = result;
this.success = success;
if (success)
{
this.Name = "Copy";
}
else
{
this.Name = "";
}
}
}
public class CalculatorTopLevelListItem : ListItem, IFallbackHandler
{
public CalculatorTopLevelListItem() : base(new CalculatorAction())
{
// In the case of the calculator, the ListItem itself is the fallback
// handler, so that it can update it's Title and Subtitle accodingly.
this._FallbackHandler = this;
this.Subtitle = "Type an equation";
}
public void UpdateQuery(string query) {
if (query == "")
{
this.Title = "=";
}
else if (query == "=")
{
this.Title = "=";
}
else if (query.StartsWith('='))
{
this.Title = _ParseQuery(query);
this.Subtitle = query;
}
else this.Title = "";
}
private string _ParseQuery(string query)
{
var equation = query.Substring(1);
try
{
var result = new DataTable().Compute(equation, null);
var resultString = result.ToString();
((CalculatorAction)this.Command).SetResult(resultString, true);
return result.ToString();
}
catch (Exception e)
{
((CalculatorAction)this.Command).SetResult("", false);
return $"Error: {e.Message}";
}
}
}
public class CalculatorActionProvider : ICommandProvider
{
public string DisplayName => $"Calculator";
@@ -100,7 +19,7 @@ public class CalculatorActionProvider : ICommandProvider
{
}
public IconDataType Icon => new("");
public IconDataType Icon => new(string.Empty);
#pragma warning disable CA1816 // Dispose methods should call SuppressFinalize
public void Dispose() => throw new NotImplementedException();
@@ -108,7 +27,6 @@ public class CalculatorActionProvider : ICommandProvider
public IListItem[] TopLevelCommands()
{
return [ calculatorCommand ];
return [calculatorCommand];
}
}

View File

@@ -0,0 +1,59 @@
// 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.Data;
using Microsoft.Windows.CommandPalette.Extensions;
using Microsoft.Windows.CommandPalette.Extensions.Helpers;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace Calculator;
public class CalculatorTopLevelListItem : ListItem, IFallbackHandler
{
public CalculatorTopLevelListItem()
: base(new CalculatorAction())
{
// In the case of the calculator, the ListItem itself is the fallback
// handler, so that it can update it's Title and Subtitle accodingly.
_FallbackHandler = this;
Subtitle = "Type an equation";
}
public void UpdateQuery(string query)
{
if (string.IsNullOrEmpty(query) || query == "=")
{
Title = "=";
}
else if (query.StartsWith('='))
{
Title = ParseQuery(query);
Subtitle = query;
}
else
{
Title = string.Empty;
}
}
private string ParseQuery(string query)
{
var equation = query.Substring(1);
try
{
var result = new DataTable().Compute(equation, null);
var resultString = result.ToString();
((CalculatorAction)Command).SetResult(resultString, true);
return result.ToString();
}
catch (Exception e)
{
((CalculatorAction)Command).SetResult(string.Empty, false);
return $"Error: {e.Message}";
}
}
}