2025-04-17 14:59:10 +08: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 ManagedCommon;
|
|
|
|
|
using Microsoft.CommandPalette.Extensions.Toolkit;
|
|
|
|
|
using Windows.Foundation;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.CmdPal.Ext.Calc.Helper;
|
|
|
|
|
|
|
|
|
|
public static class ResultHelper
|
|
|
|
|
{
|
2025-08-12 18:27:10 +08:00
|
|
|
|
public static ListItem CreateResult(decimal? roundedResult, CultureInfo inputCulture, CultureInfo outputCulture, string query, ISettingsInterface settings, TypedEventHandler<object, object> handleSave)
|
2025-04-17 14:59:10 +08:00
|
|
|
|
{
|
|
|
|
|
// Return null when the expression is not a valid calculator query.
|
2025-08-18 06:07:28 -05:00
|
|
|
|
if (roundedResult is null)
|
2025-04-17 14:59:10 +08:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
CmdPal: entirely redo the Run page (#39955)
This entirely rewrites the shell page. It feels a lot more like the old
run dialog now.
* It's got icons for files & exes
* it can handle network paths
* it can handle `commands /with args...`
* it'll suggest files in that path as you type
* it handles `%environmentVariables%`
* it handles `"Paths with\spaces in them"`
* it shows you the path as a suggestion, in the text box, as you move
the selection
References:
Closes #39044
Closes #39419
Closes #38298
Closes #40311
### Remaining todo's
* [x] Remove the `GenerateAppxManifest` change, and file something to
fix that. We are still generating msix's on every build, wtf
* [x] Clean-up code
* [x] Double-check loc
* [x] Remove a bunch of debug printing that we don't need anymore
* [ ] File a separate PR for moving the file (indexer) commands into a
common project, and re-use those here
* [x] Add history support again! I totally tore that out
* did that in #40427
* [x] make `shell:` paths and weird URI's just work. Good test is
`x-cmdpal://settings`
### further optimizations that probably aren't blocking
* [x] Our fast up-to-date is clearly broken, but I think that's been
broken since early 0.91
* [x] If the exe doesn't change, we don't need to create a new ListItem
for it. We can just re-use the current one, and just change the args
* [ ] if the directory hasn't changed, but we typed more chars (e.g.
`c:\windows\s` -> `c:\windows\sys`), we should cache the ListItem's from
the first query, and re-use them if possible.
2025-07-22 14:47:31 -05:00
|
|
|
|
// 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.
|
2025-07-24 05:36:22 +02:00
|
|
|
|
return new ListItem(settings.CloseOnEnter ? copyCommandItem.Command : saveCommand)
|
2025-04-17 14:59:10 +08:00
|
|
|
|
{
|
|
|
|
|
// Using CurrentCulture since this is user facing
|
2025-07-15 14:33:25 -05:00
|
|
|
|
Icon = Icons.ResultIcon,
|
2025-04-17 14:59:10 +08:00
|
|
|
|
Title = result,
|
|
|
|
|
Subtitle = query,
|
|
|
|
|
MoreCommands = [
|
2025-07-24 05:36:22 +02:00
|
|
|
|
new CommandContextItem(settings.CloseOnEnter ? saveCommand : copyCommandItem.Command),
|
2025-04-17 14:59:10 +08:00
|
|
|
|
..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.
|
2025-08-18 06:07:28 -05:00
|
|
|
|
if (roundedResult is null)
|
2025-04-17 14:59:10 +08:00
|
|
|
|
{
|
|
|
|
|
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(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|