100 lines
3.5 KiB
C#
Raw Normal View History

// 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
{
[CmdPal][UT] Refactor some cmdpal ext's ut and improve the test case (#40896) <!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request 1. Remove some AI generated nonsense case 2. Add ISettingsInterface for those ext for testing purpose. 3. Add query test. <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #40897 - [x] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed --------- Co-authored-by: Yu Leng <yuleng@microsoft.com>
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)
{
// 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);
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.
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(),
};
}
}