mirror of
https://github.com/microsoft/PowerToys
synced 2025-08-22 01:58:04 +00:00
What the title says. 😄
Rather than relying on the potentially overloaded `!=` or `==` operators
when checking for null, now we'll use the `is` expression (possibly
combined with the `not` operator) to ensure correct checking. Probably
overkill for many of these classes, but decided to err on the side of
consistency. Would matter more on classes that may be inherited or
extended.
Using `is` and `is not` will provide us a guarantee that no
user-overloaded equality operators (`==`/`!=`) is invoked when a
`expression is null` is evaluated.
In code form, changed all instances of:
```c#
something != null
something == null
```
to:
```c#
something is not null
something is null
```
The one exception was checking null on a `KeyChord`. `KeyChord` is a
struct which is never null so VS will raise an error when trying this
versus just providing a warning when using `keyChord != null`. In
reality, we shouldn't do this check because it can't ever be null. In
the case of a `KeyChord` it **would** be a `KeyChord` equivalent to:
```c#
KeyChord keyChord = new ()
{
Modifiers = 0,
Vkey = 0,
ScanCode = 0
};
```
100 lines
3.5 KiB
C#
100 lines
3.5 KiB
C#
// 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 ManagedCommon;
|
|
using Microsoft.CommandPalette.Extensions.Toolkit;
|
|
using Windows.Foundation;
|
|
|
|
namespace Microsoft.CmdPal.Ext.Calc.Helper;
|
|
|
|
public static class ResultHelper
|
|
{
|
|
public static ListItem CreateResult(decimal? roundedResult, CultureInfo inputCulture, CultureInfo outputCulture, string query, ISettingsInterface settings, TypedEventHandler<object, object> handleSave)
|
|
{
|
|
// Return null when the expression is not a valid calculator query.
|
|
if (roundedResult is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var result = roundedResult?.ToString(outputCulture);
|
|
|
|
// Create a SaveCommand and subscribe to the SaveRequested event
|
|
// This can append the result to the history list.
|
|
var saveCommand = new SaveCommand(result);
|
|
saveCommand.SaveRequested += handleSave;
|
|
|
|
var copyCommandItem = CreateResult(roundedResult, inputCulture, outputCulture, query);
|
|
|
|
// No TextToSuggest on the main save command item. We don't want to keep suggesting what the result is,
|
|
// as the user is typing it.
|
|
return new ListItem(settings.CloseOnEnter ? copyCommandItem.Command : saveCommand)
|
|
{
|
|
// Using CurrentCulture since this is user facing
|
|
Icon = Icons.ResultIcon,
|
|
Title = result,
|
|
Subtitle = query,
|
|
MoreCommands = [
|
|
new CommandContextItem(settings.CloseOnEnter ? saveCommand : copyCommandItem.Command),
|
|
..copyCommandItem.MoreCommands,
|
|
],
|
|
};
|
|
}
|
|
|
|
public static ListItem CreateResult(decimal? roundedResult, CultureInfo inputCulture, CultureInfo outputCulture, string query)
|
|
{
|
|
// Return null when the expression is not a valid calculator query.
|
|
if (roundedResult is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var decimalResult = roundedResult?.ToString(outputCulture);
|
|
|
|
List<CommandContextItem> context = [];
|
|
|
|
if (decimal.IsInteger((decimal)roundedResult))
|
|
{
|
|
var i = decimal.ToInt64((decimal)roundedResult);
|
|
try
|
|
{
|
|
var hexResult = "0x" + i.ToString("X", outputCulture);
|
|
context.Add(new CommandContextItem(new CopyTextCommand(hexResult) { Name = Properties.Resources.calculator_copy_hex })
|
|
{
|
|
Title = hexResult,
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError("Error parsing hex format", ex);
|
|
}
|
|
|
|
try
|
|
{
|
|
var binaryResult = "0b" + i.ToString("B", outputCulture);
|
|
context.Add(new CommandContextItem(new CopyTextCommand(binaryResult) { Name = Properties.Resources.calculator_copy_binary })
|
|
{
|
|
Title = binaryResult,
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError("Error parsing binary format", ex);
|
|
}
|
|
}
|
|
|
|
return new ListItem(new CopyTextCommand(decimalResult))
|
|
{
|
|
// Using CurrentCulture since this is user facing
|
|
Title = decimalResult,
|
|
Subtitle = query,
|
|
TextToSuggest = decimalResult,
|
|
MoreCommands = context.ToArray(),
|
|
};
|
|
}
|
|
}
|