Add launchMode parameter to ToggleCommandPalette action (#8382)

## PR Checklist
* [x] Closes https://github.com/microsoft/terminal/issues/8322
* [x] CLA signed. 
* [x] Tests added/passed
* [x] Documentation updated - https://github.com/MicrosoftDocs/terminal/pull/202
* [x] Schema updated.
* [x] I've discussed this with core contributors already. 

## Detailed Description of the Pull Request / Additional comments
Added an optional launchMode parameter to "commandPalette" command. 
The values of the launchMode are either "action" (default) or "command line".

## Validation Steps Performed
* Manual tests
This commit is contained in:
Don-Vito 2020-12-03 18:15:31 +02:00 committed by GitHub
parent 3181b6a517
commit f072eaf9d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 164 additions and 12 deletions

View file

@ -167,6 +167,13 @@
],
"type": "string"
},
"CommandPaletteLaunchMode": {
"enum": [
"action",
"commandLine"
],
"type": "string"
},
"NewTerminalArgs": {
"properties": {
"commandline": {
@ -522,6 +529,22 @@
],
"required": [ "direction" ]
},
"CommandPaletteAction": {
"description": "Arguments for a commandPalette action",
"allOf": [
{ "$ref": "#/definitions/ShortcutAction" },
{
"properties": {
"action": { "type": "string", "pattern": "commandPalette" },
"launchMode": {
"$ref": "#/definitions/CommandPaletteLaunchMode",
"default": "action",
"description": "Toggle command palette in either action or command line mode. If no value is provided, the palette will launch in action mode."
}
}
}
]
},
"Keybinding": {
"additionalProperties": false,
"properties": {

View file

@ -48,6 +48,7 @@ namespace SettingsModelLocalTests
TEST_METHOD(TestScrollArgs);
TEST_METHOD(TestToggleCommandPaletteArgs);
TEST_METHOD(TestMoveTabArgs);
TEST_CLASS_SETUP(ClassSetup)
@ -580,4 +581,57 @@ namespace SettingsModelLocalTests
VERIFY_THROWS(invalidKeyMap->LayerJson(bindingsInvalidJson);, std::exception);
}
}
void KeyBindingsTests::TestToggleCommandPaletteArgs()
{
const std::string bindings0String{ R"([
{ "keys": ["up"], "command": "commandPalette" },
{ "keys": ["ctrl+up"], "command": { "action": "commandPalette", "launchMode" : "action" } },
{ "keys": ["ctrl+shift+up"], "command": { "action": "commandPalette", "launchMode" : "commandLine" } }
])" };
const auto bindings0Json = VerifyParseSucceeded(bindings0String);
auto keymap = winrt::make_self<implementation::KeyMapping>();
VERIFY_IS_NOT_NULL(keymap);
VERIFY_ARE_EQUAL(0u, keymap->_keyShortcuts.size());
keymap->LayerJson(bindings0Json);
VERIFY_ARE_EQUAL(3u, keymap->_keyShortcuts.size());
{
KeyChord kc{ false, false, false, static_cast<int32_t>(VK_UP) };
auto actionAndArgs = ::TestUtils::GetActionAndArgs(*keymap, kc);
VERIFY_ARE_EQUAL(ShortcutAction::ToggleCommandPalette, actionAndArgs.Action());
const auto& realArgs = actionAndArgs.Args().try_as<ToggleCommandPaletteArgs>();
VERIFY_IS_NOT_NULL(realArgs);
// Verify the args have the expected value
VERIFY_ARE_EQUAL(realArgs.LaunchMode(), CommandPaletteLaunchMode::Action);
}
{
KeyChord kc{ true, false, false, static_cast<int32_t>(VK_UP) };
auto actionAndArgs = ::TestUtils::GetActionAndArgs(*keymap, kc);
VERIFY_ARE_EQUAL(ShortcutAction::ToggleCommandPalette, actionAndArgs.Action());
const auto& realArgs = actionAndArgs.Args().try_as<ToggleCommandPaletteArgs>();
VERIFY_IS_NOT_NULL(realArgs);
// Verify the args have the expected value
VERIFY_ARE_EQUAL(realArgs.LaunchMode(), CommandPaletteLaunchMode::Action);
}
{
KeyChord kc{ true, false, true, static_cast<int32_t>(VK_UP) };
auto actionAndArgs = ::TestUtils::GetActionAndArgs(*keymap, kc);
VERIFY_ARE_EQUAL(ShortcutAction::ToggleCommandPalette, actionAndArgs.Action());
const auto& realArgs = actionAndArgs.Args().try_as<ToggleCommandPaletteArgs>();
VERIFY_IS_NOT_NULL(realArgs);
// Verify the args have the expected value
VERIFY_ARE_EQUAL(realArgs.LaunchMode(), CommandPaletteLaunchMode::CommandLine);
}
{
const std::string bindingsInvalidString{ R"([{ "keys": ["up"], "command": { "action": "commandPalette", "launchMode": "bad" } }])" };
const auto bindingsInvalidJson = VerifyParseSucceeded(bindingsInvalidString);
auto invalidKeyMap = winrt::make_self<implementation::KeyMapping>();
VERIFY_IS_NOT_NULL(invalidKeyMap);
VERIFY_ARE_EQUAL(0u, invalidKeyMap->_keyShortcuts.size());
VERIFY_THROWS(invalidKeyMap->LayerJson(bindingsInvalidJson);, std::exception);
}
}
}

View file

@ -313,13 +313,14 @@ namespace winrt::TerminalApp::implementation
void TerminalPage::_HandleToggleCommandPalette(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
// TODO GH#6677: When we add support for commandline mode, first set the
// mode that the command palette should be in, before making it visible.
CommandPalette().EnableCommandPaletteMode();
CommandPalette().Visibility(CommandPalette().Visibility() == Visibility::Visible ?
Visibility::Collapsed :
Visibility::Visible);
args.Handled(true);
if (const auto& realArgs = args.ActionArgs().try_as<ToggleCommandPaletteArgs>())
{
CommandPalette().EnableCommandPaletteMode(realArgs.LaunchMode());
CommandPalette().Visibility(CommandPalette().Visibility() == Visibility::Visible ?
Visibility::Collapsed :
Visibility::Visible);
args.Handled(true);
}
}
void TerminalPage::_HandleSetColorScheme(const IInspectable& /*sender*/,

View file

@ -809,9 +809,13 @@ namespace winrt::TerminalApp::implementation
}
}
void CommandPalette::EnableCommandPaletteMode()
void CommandPalette::EnableCommandPaletteMode(CommandPaletteLaunchMode const launchMode)
{
_switchToMode(CommandPaletteMode::ActionMode);
const auto mode = (launchMode == CommandPaletteLaunchMode::CommandLine) ?
CommandPaletteMode::CommandlineMode :
CommandPaletteMode::ActionMode;
_switchToMode(mode);
_updateFilteredActions();
}

View file

@ -33,7 +33,7 @@ namespace winrt::TerminalApp::implementation
void SetTabActions(Windows::Foundation::Collections::IVector<Microsoft::Terminal::Settings::Model::Command> const& tabs, const bool clearList);
void SetKeyBindings(Microsoft::Terminal::TerminalControl::IKeyBindings bindings);
void EnableCommandPaletteMode();
void EnableCommandPaletteMode(Microsoft::Terminal::Settings::Model::CommandPaletteLaunchMode const launchMode);
void SetDispatch(const winrt::TerminalApp::ShortcutActionDispatch& dispatch);

View file

@ -23,7 +23,7 @@ namespace TerminalApp
void SetCommands(Windows.Foundation.Collections.IVector<Microsoft.Terminal.Settings.Model.Command> actions);
void SetTabActions(Windows.Foundation.Collections.IVector<Microsoft.Terminal.Settings.Model.Command> tabs, Boolean clearList);
void SetKeyBindings(Microsoft.Terminal.TerminalControl.IKeyBindings bindings);
void EnableCommandPaletteMode();
void EnableCommandPaletteMode(Microsoft.Terminal.Settings.Model.CommandPaletteLaunchMode launchMode);
void SelectNextItem(Boolean moveDown);

View file

@ -136,6 +136,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{ ShortcutAction::ScrollUp, ScrollUpArgs::FromJson },
{ ShortcutAction::ScrollDown, ScrollDownArgs::FromJson },
{ ShortcutAction::MoveTab, MoveTabArgs::FromJson },
{ ShortcutAction::ToggleCommandPalette, ToggleCommandPaletteArgs::FromJson },
{ ShortcutAction::Invalid, nullptr },
};
@ -296,7 +297,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{ ShortcutAction::SwitchToTab, RS_(L"SwitchToTabCommandKey") },
{ ShortcutAction::TabSearch, RS_(L"TabSearchCommandKey") },
{ ShortcutAction::ToggleAlwaysOnTop, RS_(L"ToggleAlwaysOnTopCommandKey") },
{ ShortcutAction::ToggleCommandPalette, RS_(L"ToggleCommandPaletteCommandKey") },
{ ShortcutAction::ToggleCommandPalette, L"" },
{ ShortcutAction::ToggleFocusMode, RS_(L"ToggleFocusModeCommandKey") },
{ ShortcutAction::ToggleFullscreen, RS_(L"ToggleFullscreenCommandKey") },
{ ShortcutAction::TogglePaneZoom, RS_(L"TogglePaneZoomCommandKey") },

View file

@ -23,6 +23,7 @@
#include "CloseOtherTabsArgs.g.cpp"
#include "CloseTabsAfterArgs.g.cpp"
#include "MoveTabArgs.g.cpp"
#include "ToggleCommandPaletteArgs.g.cpp"
#include <LibraryResources.h>
@ -412,4 +413,13 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
directionString)
};
}
winrt::hstring ToggleCommandPaletteArgs::GenerateName() const
{
if (_LaunchMode == CommandPaletteLaunchMode::CommandLine)
{
return RS_(L"ToggleCommandPaletteCommandLineModeCommandKey");
}
return RS_(L"ToggleCommandPaletteCommandKey");
}
}

View file

@ -25,6 +25,7 @@
#include "ScrollUpArgs.g.h"
#include "ScrollDownArgs.g.h"
#include "MoveTabArgs.g.h"
#include "ToggleCommandPaletteArgs.g.h"
#include "../../cascadia/inc/cppwinrt_utils.h"
#include "JsonUtils.h"
@ -786,6 +787,42 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return *copy;
}
};
struct ToggleCommandPaletteArgs : public ToggleCommandPaletteArgsT<ToggleCommandPaletteArgs>
{
ToggleCommandPaletteArgs() = default;
// To preserve backward compatibility the default is Action.
GETSET_PROPERTY(CommandPaletteLaunchMode, LaunchMode, CommandPaletteLaunchMode::Action);
static constexpr std::string_view LaunchModeKey{ "launchMode" };
public:
hstring GenerateName() const;
bool Equals(const IActionArgs& other)
{
auto otherAsUs = other.try_as<ToggleCommandPaletteArgs>();
if (otherAsUs)
{
return otherAsUs->_LaunchMode == _LaunchMode;
}
return false;
};
static FromJsonResult FromJson(const Json::Value& json)
{
// LOAD BEARING: Not using make_self here _will_ break you in the future!
auto args = winrt::make_self<ToggleCommandPaletteArgs>();
JsonUtils::GetValueForKey(json, LaunchModeKey, args->_LaunchMode);
return { *args, {} };
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<ToggleCommandPaletteArgs>() };
copy->_LaunchMode = _LaunchMode;
return *copy;
}
};
}
namespace winrt::Microsoft::Terminal::Settings::Model::factory_implementation

View file

@ -53,6 +53,12 @@ namespace Microsoft.Terminal.Settings.Model
Backward
};
enum CommandPaletteLaunchMode
{
Action = 0,
CommandLine
};
[default_interface] runtimeclass NewTerminalArgs {
NewTerminalArgs();
NewTerminalArgs(Int32 profileIndex);
@ -179,4 +185,9 @@ namespace Microsoft.Terminal.Settings.Model
{
Windows.Foundation.IReference<UInt32> RowsToScroll { get; };
};
[default_interface] runtimeclass ToggleCommandPaletteArgs : IActionArgs
{
CommandPaletteLaunchMode LaunchMode { get; };
};
}

View file

@ -332,6 +332,9 @@
<data name="ToggleCommandPaletteCommandKey" xml:space="preserve">
<value>Toggle command palette</value>
</data>
<data name="ToggleCommandPaletteCommandLineModeCommandKey" xml:space="preserve">
<value>Toggle command palette in command line mode</value>
</data>
<data name="ToggleFocusModeCommandKey" xml:space="preserve">
<value>Toggle focus mode</value>
<comment>"Focus mode" is a mode with minimal UI elements, for a distraction-free experience</comment>

View file

@ -406,3 +406,11 @@ JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Settings::Model::MoveTabDirection
pair_type{ "backward", ValueType::Backward },
};
};
JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Settings::Model::CommandPaletteLaunchMode)
{
JSON_MAPPINGS(2) = {
pair_type{ "action", ValueType::Action },
pair_type{ "commandLine", ValueType::CommandLine },
};
};