[CmdPal][Calc] Add option to choose Copy as the primary command (close on enter) (#40398)

## Summary of the Pull Request
Adds a new setting that lets users swap Save/Copy as primary/secondary
commands. This is done in order to enable closing on enter (similar to
PTRun) and therefore saving three extra steps (see issue). Defaults to
true - Copy is primary.
## PR Checklist

- [x] **Closes:** #40262
- [x] **Communication:** I've discussed this with core contributors
already.
- [ ] **Tests:** Added/updated and all pass
- [ ] **Localization:** No, but that's a more general issue (#40397)
- [x] **Dev docs:** No need
- [x] **New binaries:** None
- [x] **Documentation updated:** No need

## Detailed Description of the Pull Request / Additional comments

![image](https://github.com/user-attachments/assets/6f4ae316-32cd-439a-bfff-50fc2f24a5ff)

| On (default) | Off |
|--------|--------|
|
![image](https://github.com/user-attachments/assets/11ca0d6b-b35c-4c63-a5dc-44244ae1b044)
|
![image](https://github.com/user-attachments/assets/cff3b4d4-5df9-4f49-9872-c2ddee67a7b4)
|
## Validation Steps Performed
Shown in screenshots

---------

Co-authored-by: Mike Griese <migrie@microsoft.com>
This commit is contained in:
PesBandi 2025-07-24 05:36:22 +02:00 committed by GitHub
parent 28c41d0838
commit 23372413fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 37 additions and 9 deletions

View File

@ -63,7 +63,7 @@ public static partial class QueryHelper
return ResultHelper.CreateResult(result.RoundedResult, inputCulture, outputCulture, query);
}
return ResultHelper.CreateResult(result.RoundedResult, inputCulture, outputCulture, query, handleSave);
return ResultHelper.CreateResult(result.RoundedResult, inputCulture, outputCulture, query, settings, handleSave);
}
catch (OverflowException)
{

View File

@ -13,7 +13,7 @@ namespace Microsoft.CmdPal.Ext.Calc.Helper;
public static class ResultHelper
{
public static ListItem CreateResult(decimal? roundedResult, CultureInfo inputCulture, CultureInfo outputCulture, string query, TypedEventHandler<object, object> handleSave)
public static ListItem CreateResult(decimal? roundedResult, CultureInfo inputCulture, CultureInfo outputCulture, string query, SettingsManager settings, TypedEventHandler<object, object> handleSave)
{
// Return null when the expression is not a valid calculator query.
if (roundedResult == null)
@ -32,19 +32,14 @@ public static class ResultHelper
// 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(saveCommand)
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(copyCommandItem.Command)
{
Icon = copyCommandItem.Icon,
Title = copyCommandItem.Title,
Subtitle = copyCommandItem.Subtitle,
},
new CommandContextItem(settings.CloseOnEnter ? saveCommand : copyCommandItem.Command),
..copyCommandItem.MoreCommands,
],
};

View File

@ -39,6 +39,12 @@ public class SettingsManager : JsonSettingsManager
Properties.Resources.calculator_settings_out_en_format_description,
false);
private readonly ToggleSetting _closeOnEnter = new(
Namespaced(nameof(CloseOnEnter)),
Properties.Resources.calculator_settings_close_on_enter,
Properties.Resources.calculator_settings_close_on_enter_description,
true);
public CalculateEngine.TrigMode TrigUnit
{
get
@ -73,6 +79,8 @@ public class SettingsManager : JsonSettingsManager
public bool OutputUseEnglishFormat => _outputUseEnNumberFormat.Value;
public bool CloseOnEnter => _closeOnEnter.Value;
internal static string SettingsJsonPath()
{
var directory = Utilities.BaseSettingsPath("Microsoft.CmdPal");
@ -89,6 +97,7 @@ public class SettingsManager : JsonSettingsManager
Settings.Add(_trigUnit);
Settings.Add(_inputUseEnNumberFormat);
Settings.Add(_outputUseEnNumberFormat);
Settings.Add(_closeOnEnter);
// Load settings from file upon initialization
LoadSettings();

View File

@ -186,6 +186,24 @@ namespace Microsoft.CmdPal.Ext.Calc.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Close on Enter.
/// </summary>
public static string calculator_settings_close_on_enter {
get {
return ResourceManager.GetString("calculator_settings_close_on_enter", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Makes Copy and close the primary command.
/// </summary>
public static string calculator_settings_close_on_enter_description {
get {
return ResourceManager.GetString("calculator_settings_close_on_enter_description", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Use English (United States) number format for input.
/// </summary>

View File

@ -184,6 +184,12 @@
<value>Ignores your system setting and expects numbers in the format '{0}'.</value>
<comment>{0} is a placeholder and will be replaced in code.</comment>
</data>
<data name="calculator_settings_close_on_enter" xml:space="preserve">
<value>Close on Enter</value>
</data>
<data name="calculator_settings_close_on_enter_description" xml:space="preserve">
<value>Makes Copy and close the primary command</value>
</data>
<data name="calculator_settings_replace_input" xml:space="preserve">
<value>Replace input if query ends with '='</value>
</data>