Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add findNext, findPrev actions #8917

Merged
14 commits merged into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions doc/cascadia/profiles.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"copy",
"duplicateTab",
"find",
"findMatch",
"moveFocus",
"moveTab",
"newTab",
Expand Down Expand Up @@ -137,6 +138,13 @@
],
"type": "string"
},
"FindMatchDirection": {
"enum": [
"next",
"prev"
],
"type": "string"
},
"SplitState": {
"enum": [
"vertical",
Expand Down Expand Up @@ -558,6 +566,23 @@
}
]
},
"FindMatchAction": {
"description": "Arguments corresponding to a Find Match Action",
"allOf": [
{ "$ref": "#/definitions/ShortcutAction" },
{
"properties": {
"action": { "type": "string", "pattern": "findMatch" },
"direction": {
"$ref": "#/definitions/FindMatchDirection",
"default": "prev",
"description": "The direction to search in. \"prev\" will search upwards in the buffer, and \"next\" will search downwards."
}
}
}
],
"required": [ "direction" ]
},
"Keybinding": {
"additionalProperties": false,
"properties": {
Expand All @@ -582,6 +607,7 @@
{ "$ref": "#/definitions/ScrollUpAction" },
{ "$ref": "#/definitions/ScrollDownAction" },
{ "$ref": "#/definitions/MoveTabAction" },
{ "$ref": "#/definitions/FindMatchAction" },
{ "type": "null" }
]
},
Expand Down
12 changes: 12 additions & 0 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ namespace winrt::TerminalApp::implementation
args.Handled(true);
}

void TerminalPage::_HandleFindMatch(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
if (const auto& realArgs = args.ActionArgs().try_as<FindMatchArgs>())
{
if (const auto& control{ _GetActiveControl() })
{
control.SearchMatch(realArgs.Direction() == FindMatchDirection::Next);
args.Handled(true);
}
}
}
void TerminalPage::_HandleOpenSettings(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
Expand Down
5 changes: 5 additions & 0 deletions src/cascadia/TerminalApp/ShortcutActionDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ namespace winrt::TerminalApp::implementation
_BreakIntoDebuggerHandlers(*this, eventArgs);
break;
}
case ShortcutAction::FindMatch:
{
_FindMatchHandlers(*this, eventArgs);
break;
}
default:
return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/ShortcutActionDispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace winrt::TerminalApp::implementation
TYPED_EVENT(TabSearch, TerminalApp::ShortcutActionDispatch, Microsoft::Terminal::Settings::Model::ActionEventArgs);
TYPED_EVENT(MoveTab, TerminalApp::ShortcutActionDispatch, Microsoft::Terminal::Settings::Model::ActionEventArgs);
TYPED_EVENT(BreakIntoDebugger, TerminalApp::ShortcutActionDispatch, Microsoft::Terminal::Settings::Model::ActionEventArgs);
TYPED_EVENT(FindMatch, TerminalApp::ShortcutActionDispatch, Microsoft::Terminal::Settings::Model::ActionEventArgs);
// clang-format on

private:
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/ShortcutActionDispatch.idl
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ namespace TerminalApp
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, Microsoft.Terminal.Settings.Model.ActionEventArgs> TabSearch;
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, Microsoft.Terminal.Settings.Model.ActionEventArgs> MoveTab;
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, Microsoft.Terminal.Settings.Model.ActionEventArgs> BreakIntoDebugger;
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, Microsoft.Terminal.Settings.Model.ActionEventArgs> FindMatch;
}
}
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,7 @@ namespace winrt::TerminalApp::implementation
_actionDispatch->TabSearch({ this, &TerminalPage::_HandleOpenTabSearch });
_actionDispatch->MoveTab({ this, &TerminalPage::_HandleMoveTab });
_actionDispatch->BreakIntoDebugger({ this, &TerminalPage::_HandleBreakIntoDebugger });
_actionDispatch->FindMatch({ this, &TerminalPage::_HandleFindMatch });
}

// Method Description:
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/TerminalPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ namespace winrt::TerminalApp::implementation
void _HandleOpenTabSearch(const IInspectable& sender, const Microsoft::Terminal::Settings::Model::ActionEventArgs& args);
void _HandleMoveTab(const IInspectable& sender, const Microsoft::Terminal::Settings::Model::ActionEventArgs& args);
void _HandleBreakIntoDebugger(const IInspectable& sender, const Microsoft::Terminal::Settings::Model::ActionEventArgs& args);
void _HandleFindMatch(const IInspectable& sender, const Microsoft::Terminal::Settings::Model::ActionEventArgs& args);
// Make sure to hook new actions up in _RegisterActionCallbacks!
#pragma endregion

Expand Down
45 changes: 42 additions & 3 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
}
}

void TermControl::SearchMatch(const bool goForward)
{
if (!_searchBox)
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
{
CreateSearchBoxControl();
}
else
{
_Search(_searchBox->TextBox().Text(), goForward, false);
}
}

// Method Description:
// - Search text in text buffer. This is triggered if the user click
// search button or press enter.
Expand All @@ -233,7 +245,9 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// - caseSensitive: boolean that represents if the current search is case sensitive
// Return Value:
// - <none>
void TermControl::_Search(const winrt::hstring& text, const bool goForward, const bool caseSensitive)
void TermControl::_Search(const winrt::hstring& text,
const bool goForward,
const bool caseSensitive)
{
if (text.size() == 0 || _closing)
{
Expand Down Expand Up @@ -266,14 +280,33 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// - RoutedEventArgs: not used
// Return Value:
// - <none>
void TermControl::_CloseSearchBoxControl(const winrt::Windows::Foundation::IInspectable& /*sender*/, RoutedEventArgs const& /*args*/)
void TermControl::_CloseSearchBoxControl(const winrt::Windows::Foundation::IInspectable& /*sender*/,
RoutedEventArgs const& /*args*/)
{
_searchBox->Visibility(Visibility::Collapsed);

// Set focus back to terminal control
this->Focus(FocusState::Programmatic);
}

// Method Description:
// - This event handler works to give the search box an attempt to process
// keybindings. Most notably, we need this for the findPrevious and
// findNext actions to be actionable from within the search box.
void TermControl::_SearchKeyHandler(Windows::Foundation::IInspectable const& /*sender*/,
Input::KeyRoutedEventArgs const& e)
{
const auto modifiers = _GetPressedModifierKeys();
const auto vkey = gsl::narrow_cast<WORD>(e.OriginalKey());
const auto scanCode = gsl::narrow_cast<WORD>(e.KeyStatus().ScanCode);

if (!modifiers.IsAltGrPressed() &&
_TryHandleKeyBinding(vkey, scanCode, modifiers))
{
e.Handled(true);
}
}
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved

// Method Description:
// - Given new settings for this profile, applies the settings to the current terminal.
// Arguments:
Expand Down Expand Up @@ -1087,7 +1120,13 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// modifier key. We'll wait for a real keystroke to dismiss the
// GH #7395 - don't dismiss selection when taking PrintScreen
// selection.
if (_terminal->IsSelectionActive() && !KeyEvent::IsModifierKey(vkey) && vkey != VK_SNAPSHOT)
// GH#8522, GH#3758 - Only dismiss the selection on key _down_. If we
// dismiss on key up, then there's chance that we'll immediately dismiss
// a selection created by an action bound to a keydown.
if (_terminal->IsSelectionActive() &&
!KeyEvent::IsModifierKey(vkey) &&
vkey != VK_SNAPSHOT &&
keyDown)
{
_terminal->ClearSelection();
_renderer->TriggerSelection();
Expand Down
5 changes: 4 additions & 1 deletion src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation

void CreateSearchBoxControl();

void SearchMatch(const bool goForward);

bool OnDirectKeyEvent(const uint32_t vkey, const uint8_t scanCode, const bool down);

bool OnMouseWheel(const Windows::Foundation::Point location, const int32_t delta, const bool leftButtonDown, const bool midButtonDown, const bool rightButtonDown);
Expand Down Expand Up @@ -335,7 +337,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
void _CompositionCompleted(winrt::hstring text);
void _CurrentCursorPositionHandler(const IInspectable& sender, const CursorPositionEventArgs& eventArgs);
void _FontInfoHandler(const IInspectable& sender, const FontInfoEventArgs& eventArgs);

void _SearchKeyHandler(Windows::Foundation::IInspectable const& sender,
Windows::UI::Xaml::Input::KeyRoutedEventArgs const& e);
winrt::fire_and_forget _AsyncCloseConnection();
};
}
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ namespace Microsoft.Terminal.TerminalControl

void CreateSearchBoxControl();

void SearchMatch(Boolean goForward);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a weird public API, but i suppose that this is fine


void AdjustFontSize(Int32 fontSizeDelta);
void ResetFontSize();

Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TermControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
HorizontalAlignment="Right"
VerticalAlignment="Top"
Search="_Search"
PreviewKeyDown="_SearchKeyHandler"
Closed="_CloseSearchBoxControl" />
</Grid>

Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionAndArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static constexpr std::string_view LegacyToggleRetroEffectKey{ "toggleRetroEffect
static constexpr std::string_view ToggleShaderEffectsKey{ "toggleShaderEffects" };
static constexpr std::string_view MoveTabKey{ "moveTab" };
static constexpr std::string_view BreakIntoDebuggerKey{ "breakIntoDebugger" };
static constexpr std::string_view FindMatchKey{ "findMatch" };

static constexpr std::string_view ActionKey{ "action" };

Expand Down Expand Up @@ -115,6 +116,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{ MoveTabKey, ShortcutAction::MoveTab },
{ BreakIntoDebuggerKey, ShortcutAction::BreakIntoDebugger },
{ UnboundKey, ShortcutAction::Invalid },
{ FindMatchKey, ShortcutAction::FindMatch },
};

using ParseResult = std::tuple<IActionArgs, std::vector<SettingsLoadWarnings>>;
Expand Down Expand Up @@ -145,6 +147,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{ ShortcutAction::ScrollDown, ScrollDownArgs::FromJson },
{ ShortcutAction::MoveTab, MoveTabArgs::FromJson },
{ ShortcutAction::ToggleCommandPalette, ToggleCommandPaletteArgs::FromJson },
{ ShortcutAction::FindMatch, FindMatchArgs::FromJson },

{ ShortcutAction::Invalid, nullptr },
};
Expand Down Expand Up @@ -314,6 +317,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{ ShortcutAction::ToggleShaderEffects, RS_(L"ToggleShaderEffectsCommandKey") },
{ ShortcutAction::MoveTab, L"" }, // Intentionally omitted, must be generated by GenerateName
{ ShortcutAction::BreakIntoDebugger, RS_(L"BreakIntoDebuggerCommandKey") },
{ ShortcutAction::FindMatch, L"" }, // Intentionally omitted, must be generated by GenerateName
};
}();

Expand Down
13 changes: 13 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "CloseOtherTabsArgs.g.cpp"
#include "CloseTabsAfterArgs.g.cpp"
#include "MoveTabArgs.g.cpp"
#include "FindMatchArgs.g.cpp"
#include "ToggleCommandPaletteArgs.g.cpp"

#include <LibraryResources.h>
Expand Down Expand Up @@ -431,4 +432,16 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
return RS_(L"ToggleCommandPaletteCommandKey");
}

winrt::hstring FindMatchArgs::GenerateName() const
{
switch (_Direction)
{
case FindMatchDirection::Next:
return winrt::hstring{ RS_(L"FindNextCommandKey") };
case FindMatchDirection::Previous:
return winrt::hstring{ RS_(L"FindPrevCommandKey") };
}
return L"";
}
}
46 changes: 46 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "ScrollDownArgs.g.h"
#include "MoveTabArgs.g.h"
#include "ToggleCommandPaletteArgs.g.h"
#include "FindMatchArgs.g.h"

#include "../../cascadia/inc/cppwinrt_utils.h"
#include "JsonUtils.h"
Expand Down Expand Up @@ -838,6 +839,50 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return *copy;
}
};

struct FindMatchArgs : public FindMatchArgsT<FindMatchArgs>
{
FindMatchArgs() = default;
FindMatchArgs(FindMatchDirection direction) :
_Direction{ direction } {};
GETSET_PROPERTY(FindMatchDirection, Direction, FindMatchDirection::None);

static constexpr std::string_view DirectionKey{ "direction" };

public:
hstring GenerateName() const;

bool Equals(const IActionArgs& other)
{
auto otherAsUs = other.try_as<FindMatchArgs>();
if (otherAsUs)
{
return otherAsUs->_Direction == _Direction;
}
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<FindMatchArgs>();
JsonUtils::GetValueForKey(json, DirectionKey, args->_Direction);
if (args->_Direction == FindMatchDirection::None)
{
return { nullptr, { SettingsLoadWarnings::MissingRequiredParameter } };
}
else
{
return { *args, {} };
}
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<FindMatchArgs>() };
copy->_Direction = _Direction;
return *copy;
}
};

}

namespace winrt::Microsoft::Terminal::Settings::Model::factory_implementation
Expand All @@ -853,4 +898,5 @@ namespace winrt::Microsoft::Terminal::Settings::Model::factory_implementation
BASIC_FACTORY(CloseTabsAfterArgs);
BASIC_FACTORY(MoveTabArgs);
BASIC_FACTORY(OpenSettingsArgs);
BASIC_FACTORY(FindMatchArgs);
}
13 changes: 13 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ namespace Microsoft.Terminal.Settings.Model
Backward
};

enum FindMatchDirection
{
None = 0,
Next,
Previous
};

enum CommandPaletteLaunchMode
{
Action = 0,
Expand Down Expand Up @@ -204,4 +211,10 @@ namespace Microsoft.Terminal.Settings.Model
{
CommandPaletteLaunchMode LaunchMode { get; };
};

[default_interface] runtimeclass FindMatchArgs : IActionArgs
{
FindMatchArgs(FindMatchDirection direction);
FindMatchDirection Direction { get; };
};
}
3 changes: 2 additions & 1 deletion src/cascadia/TerminalSettingsModel/KeyMapping.idl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ namespace Microsoft.Terminal.Settings.Model
CloseTabsAfter,
TabSearch,
MoveTab,
BreakIntoDebugger
BreakIntoDebugger,
FindMatch
};

[default_interface] runtimeclass ActionAndArgs {
Expand Down
Loading