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

Teach Find command to search selected text #8510

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 additions & 0 deletions doc/cascadia/profiles.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@
],
"type": "string"
},
"FindSelectionMode": {
"enum": [
"none",
"singleLine",
"multiLine"
],
"type": "string"
},
"NewTerminalArgs": {
"properties": {
"commandline": {
Expand Down Expand Up @@ -545,6 +553,22 @@
}
]
},
"FindAction": {
"description": "Arguments for a find action",
"allOf": [
{ "$ref": "#/definitions/ShortcutAction" },
{
"properties": {
"action": { "type": "string", "pattern": "find" },
"selectionMode": {
"$ref": "#/definitions/FindSelectionMode",
"default": "none",
"description": "Defines whether selected text should be used when invoking find. In \"singleLine\" mode the find will use only single-line selections. In \"multiLine\" mode the entire selected text will be used (lines will be concatenated with trailing line-breaks omitted). If no value or \"none\" is provided, the selected text will not be used."
}
}
}
]
},
"Keybinding": {
"additionalProperties": false,
"properties": {
Expand Down
7 changes: 5 additions & 2 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,11 @@ namespace winrt::TerminalApp::implementation
void TerminalPage::_HandleFind(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
_Find();
args.Handled(true);
if (const auto& realArgs = args.ActionArgs().try_as<FindArgs>())
{
_Find(realArgs.SelectionMode());
args.Handled(true);
}
}

void TerminalPage::_HandleResetFontSize(const IInspectable& /*sender*/,
Expand Down
8 changes: 6 additions & 2 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2435,10 +2435,14 @@ namespace winrt::TerminalApp::implementation
// - <none>
// Return Value:
// - <none>
void TerminalPage::_Find()
void TerminalPage::_Find(winrt::Microsoft::Terminal::Settings::Model::FindSelectionMode selectionMode)
{
const auto termControl = _GetActiveControl();
termControl.CreateSearchBoxControl();

const auto populateFromSelection = selectionMode != winrt::Microsoft::Terminal::Settings::Model::FindSelectionMode::None;
const auto allowMultiSelection = selectionMode == winrt::Microsoft::Terminal::Settings::Model::FindSelectionMode::MultiLine;

termControl.CreateSearchBoxControl(populateFromSelection, allowMultiSelection);
}

// Method Description:
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/TerminalPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ namespace winrt::TerminalApp::implementation
void _OnFirstLayout(const IInspectable& sender, const IInspectable& eventArgs);
void _UpdatedSelectedTab(const int32_t index);

void _Find();
void _Find(winrt::Microsoft::Terminal::Settings::Model::FindSelectionMode selectionMode);

winrt::fire_and_forget _RefreshUIForSettingsReload();

Expand Down
14 changes: 14 additions & 0 deletions src/cascadia/TerminalControl/SearchBoxControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
}
}

// Method Description:
// - Allows to set the value of the text to search
// Arguments:
// - text: string value to populate in the TextBox
// Return Value:
// - <none>
void SearchBoxControl::PopulateTextbox(winrt::hstring const& text)
{
if (TextBox())
{
TextBox().Text(text);
}
}

// Method Description:
// - Check if the current focus is on any element within the
// search box
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/SearchBoxControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
void TextBoxKeyDown(winrt::Windows::Foundation::IInspectable const& /*sender*/, winrt::Windows::UI::Xaml::Input::KeyRoutedEventArgs const& e);

void SetFocusOnTextbox();
void PopulateTextbox(winrt::hstring const& text);
bool ContainsFocus();

void GoBackwardClicked(winrt::Windows::Foundation::IInspectable const& /*sender*/, winrt::Windows::UI::Xaml::RoutedEventArgs const& /*e*/);
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/SearchBoxControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.Terminal.TerminalControl
{
SearchBoxControl();
void SetFocusOnTextbox();
void PopulateTextbox(String text);
Boolean ContainsFocus();

event SearchHandler Search;
Expand Down
27 changes: 26 additions & 1 deletion src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,15 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation

// Method Description:
// - Loads the search box from the xaml UI and focuses it.
void TermControl::CreateSearchBoxControl()
// Arguments:
// - populateFromSelection: if this flag set to true and there is a text is selected inside terminal,
// we should try to use the selected text to populate the search box.
// - allowMultiLineSelection: if this flag set to true and multiple lines are selected,
// we should use all the lines to populate the search box.
// If the flag is set to false and multiple lines are selected,
// we should not use the selected text to populate the search box.
// - caseSensitive: boolean that represents if the current search is case sensitive
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// - caseSensitive: boolean that represents if the current search is case sensitive

I don't see a caseSensitive param here haha

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad. Will fix it (in any case need to work on docs)

Copy link
Member

Choose a reason for hiding this comment

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

Oh yeah, I completely forgot to ask you to make a PR on the docs site haha.

void TermControl::CreateSearchBoxControl(bool populateFromSelection, bool allowMultiLineSelection)
carlos-zamora marked this conversation as resolved.
Show resolved Hide resolved
{
// Lazy load the search box control.
if (auto loadedSearchBox{ FindName(L"SearchBox") })
Expand All @@ -203,6 +211,23 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// get at its private implementation
_searchBox.copy_from(winrt::get_self<implementation::SearchBoxControl>(searchBox));
_searchBox->Visibility(Visibility::Visible);

// If the search box already contains a value, it will be overridden.
if (populateFromSelection && _terminal->IsSelectionActive())
{
const auto bufferData = _terminal->RetrieveSelectedTextFromBuffer(true);
if (allowMultiLineSelection || bufferData.text.size() == 1)
{
std::wstring textData;
for (const auto& text : bufferData.text)
{
textData += text;
}

_searchBox->PopulateTextbox(textData.data());
}
}

_searchBox->SetFocusOnTextbox();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
winrt::fire_and_forget _RendererEnteredErrorState();
void _RenderRetryButton_Click(IInspectable const& button, IInspectable const& args);

void CreateSearchBoxControl();
void CreateSearchBoxControl(bool populateFromSelection, bool allowMultilineSelection);

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

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace Microsoft.Terminal.TerminalControl
Int32 GetViewHeight();
event ScrollPositionChangedEventArgs ScrollPositionChanged;

void CreateSearchBoxControl();
void CreateSearchBoxControl(Boolean populateFromSelection, Boolean allowMultiLineSelection);

void AdjustFontSize(Int32 fontSizeDelta);
void ResetFontSize();
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/ActionAndArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{ ShortcutAction::ScrollDown, ScrollDownArgs::FromJson },
{ ShortcutAction::MoveTab, MoveTabArgs::FromJson },
{ ShortcutAction::ToggleCommandPalette, ToggleCommandPaletteArgs::FromJson },
{ ShortcutAction::Find, FindArgs::FromJson },

{ ShortcutAction::Invalid, nullptr },
};
Expand Down
14 changes: 14 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "CloseTabsAfterArgs.g.cpp"
#include "MoveTabArgs.g.cpp"
#include "ToggleCommandPaletteArgs.g.cpp"
#include "FindArgs.g.cpp"

#include <LibraryResources.h>

Expand Down Expand Up @@ -422,4 +423,17 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
return RS_(L"ToggleCommandPaletteCommandKey");
}

winrt::hstring FindArgs::GenerateName() const
{
switch (_SelectionMode)
{
case FindSelectionMode::SingleLine:
return RS_(L"FindSelectionSingleLineCommandKey");
case FindSelectionMode::MultiLine:
return RS_(L"FindSelectionMultiLineCommandKey");
default:
return RS_(L"FindCommandKey");
}
}
}
37 changes: 37 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 "FindArgs.g.h"

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

struct FindArgs : public FindArgsT<FindArgs>
{
FindArgs() = default;

// To preserve backward compatibility the default is Action.
GETSET_PROPERTY(FindSelectionMode, SelectionMode, FindSelectionMode::None);

static constexpr std::string_view SelectionModeKey{ "selectionMode" };

public:
hstring GenerateName() const;

bool Equals(const IActionArgs& other)
{
auto otherAsUs = other.try_as<FindArgs>();
if (otherAsUs)
{
return otherAsUs->_SelectionMode == _SelectionMode;
}
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<FindArgs>();
JsonUtils::GetValueForKey(json, SelectionModeKey, args->_SelectionMode);
return { *args, {} };
}
IActionArgs Copy() const
{
auto copy{ winrt::make_self<FindArgs>() };
copy->_SelectionMode = _SelectionMode;
return *copy;
}
};
}

namespace winrt::Microsoft::Terminal::Settings::Model::factory_implementation
Expand Down
12 changes: 12 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ namespace Microsoft.Terminal.Settings.Model
CommandLine
};

enum FindSelectionMode
{
None = 0,
SingleLine,
MultiLine
};

[default_interface] runtimeclass NewTerminalArgs {
NewTerminalArgs();
NewTerminalArgs(Int32 profileIndex);
Expand Down Expand Up @@ -190,4 +197,9 @@ namespace Microsoft.Terminal.Settings.Model
{
CommandPaletteLaunchMode LaunchMode { get; };
};

[default_interface] runtimeclass FindArgs : IActionArgs
{
FindSelectionMode SelectionMode { get; };
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@
<data name="FindCommandKey" xml:space="preserve">
<value>Find</value>
</data>
<data name="FindSelectionSingleLineCommandKey" xml:space="preserve">
<value>Find selected text (single-line)</value>
</data>
<data name="FindSelectionMultiLineCommandKey" xml:space="preserve">
<value>Find selected text</value>
</data>
<data name="IncreaseFontSizeCommandKey" xml:space="preserve">
<value>Increase font size</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,12 @@ JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Settings::Model::CommandPaletteLa
pair_type{ "commandLine", ValueType::CommandLine },
};
};

JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Settings::Model::FindSelectionMode)
{
JSON_MAPPINGS(3) = {
pair_type{ "none", ValueType::None },
pair_type{ "singleLine", ValueType::SingleLine },
pair_type{ "multiLine", ValueType::MultiLine },
};
};