Skip to content

Commit

Permalink
Teach Find command to populate the search box with selected text (mic…
Browse files Browse the repository at this point in the history
…rosoft#8521)

If a single line of text is selected, use it to populate the search box.

Closes microsoft#8307
  • Loading branch information
Don-Vito authored and mpela81 committed Jan 28, 2021
1 parent 15d71b3 commit 7a37ded
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
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
16 changes: 16 additions & 0 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ 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 a text is selected inside terminal, use it to populate the search box.
// If the search box already contains a value, it will be overridden.
if (_terminal->IsSelectionActive())
{
// Currently we populate the search box only if a single line is selected.
// Empirically, multi-line selection works as well on sample scenarios,
// but since code paths differ, extra work is required to ensure correctness.
const auto bufferData = _terminal->RetrieveSelectedTextFromBuffer(true);
if (bufferData.text.size() == 1)
{
const auto selectedLine{ til::at(bufferData.text, 0) };
_searchBox->PopulateTextbox(selectedLine.data());
}
}

_searchBox->SetFocusOnTextbox();
}
}
Expand Down

0 comments on commit 7a37ded

Please sign in to comment.