terminal/src/cascadia/TerminalControl/SearchBoxControl.cpp
Mike Griese d749df70ed
Rename Microsoft.Terminal.TerminalControl to .Control; Split into dll & lib (#9472)
**BE NOT AFRAID**. I know that there's 107 files in this PR, but almost
all of it is just find/replacing `TerminalControl` with `Control`.

This is the start of the work to move TermControl into multiple pieces,
for #5000. The PR starts this work by:
* Splits `TerminalControl` into separate lib and dll projects. We'll
  want control tests in the future, and for that, we'll need a lib.
* Moves `ICoreSettings` back into the `Microsoft.Terminal.Core`
  namespace. We'll have other types in there soon too. 
  * I could not tell you why this works suddenly. New VS versions? New
    cppwinrt version? Maybe we're just better at dealing with mdmerge
    bugs these days.
* RENAMES  `Microsoft.Terminal.TerminalControl` to
  `Microsoft.Terminal.Control`. This touches pretty much every file in
  the sln. Sorry about that (not sorry). 

An upcoming PR will move much of the logic in TermControl into a new
`ControlCore` class that we'll add in `Microsoft.Terminal.Core`.
`ControlCore` will then be unittest-able in the
`UnitTests_TerminalCore`, which will help prevent regressions like #9455 

## Detailed Description of the Pull Request / Additional comments
You're really gonna want to clean the sln first, then merge this into
your branch, then rebuild. It's very likely that old winmds will get
left behind. If you see something like 

```
Error    MDM2007    Cannot create type
Microsoft.Terminal.TerminalControl.KeyModifiers in read-only metadata
file Microsoft.Terminal.TerminalControl.
```

then that's what happened to you.
2021-03-17 20:47:24 +00:00

213 lines
6.7 KiB
C++

// Copyright (c) Microsoft Corporation
// Licensed under the MIT license.
#include "pch.h"
#include "SearchBoxControl.h"
#include "SearchBoxControl.g.cpp"
using namespace winrt;
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Core;
namespace winrt::Microsoft::Terminal::Control::implementation
{
// Constructor
SearchBoxControl::SearchBoxControl()
{
InitializeComponent();
this->CharacterReceived({ this, &SearchBoxControl::_CharacterHandler });
this->KeyDown({ this, &SearchBoxControl::_KeyDownHandler });
_focusableElements.insert(TextBox());
_focusableElements.insert(CloseButton());
_focusableElements.insert(CaseSensitivityButton());
_focusableElements.insert(GoForwardButton());
_focusableElements.insert(GoBackwardButton());
}
// Method Description:
// - Check if the current search direction is forward
// Arguments:
// - <none>
// Return Value:
// - bool: the current search direction, determined by the
// states of the two direction buttons
bool SearchBoxControl::_GoForward()
{
return GoForwardButton().IsChecked().GetBoolean();
}
// Method Description:
// - Check if the current search is case sensitive
// Arguments:
// - <none>
// Return Value:
// - bool: whether the current search is case sensitive (case button is checked )
// or not
bool SearchBoxControl::_CaseSensitive()
{
return CaseSensitivityButton().IsChecked().GetBoolean();
}
// Method Description:
// - Handler for pressing Enter on TextBox, trigger
// text search
// Arguments:
// - sender: not used
// - e: event data
// Return Value:
// - <none>
void SearchBoxControl::TextBoxKeyDown(winrt::Windows::Foundation::IInspectable const& /*sender*/, Input::KeyRoutedEventArgs const& e)
{
if (e.OriginalKey() == winrt::Windows::System::VirtualKey::Enter)
{
auto const state = CoreWindow::GetForCurrentThread().GetKeyState(winrt::Windows::System::VirtualKey::Shift);
if (WI_IsFlagSet(state, CoreVirtualKeyStates::Down))
{
_SearchHandlers(TextBox().Text(), !_GoForward(), _CaseSensitive());
}
else
{
_SearchHandlers(TextBox().Text(), _GoForward(), _CaseSensitive());
}
e.Handled(true);
}
}
// Method Description:
// - Handler for pressing "Esc" when focusing
// on the search dialog, this triggers close
// event of the Search dialog
// Arguments:
// - sender: not used
// - e: event data
// Return Value:
// - <none>
void SearchBoxControl::_KeyDownHandler(winrt::Windows::Foundation::IInspectable const& /*sender*/,
Input::KeyRoutedEventArgs const& e)
{
if (e.OriginalKey() == winrt::Windows::System::VirtualKey::Escape)
{
_ClosedHandlers(*this, e);
e.Handled(true);
}
}
// Method Description:
// - Handler for pressing Enter on TextBox, trigger
// text search
// Arguments:
// - <none>
// Return Value:
// - <none>
void SearchBoxControl::SetFocusOnTextbox()
{
if (TextBox())
{
Input::FocusManager::TryFocusAsync(TextBox(), FocusState::Keyboard);
TextBox().SelectAll();
}
}
// 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
// Arguments:
// - <none>
// Return Value:
// - bool: whether the current focus is on the search box
bool SearchBoxControl::ContainsFocus()
{
auto focusedElement = Input::FocusManager::GetFocusedElement(this->XamlRoot());
if (_focusableElements.count(focusedElement) > 0)
{
return true;
}
return false;
}
// Method Description:
// - Handler for clicking the GoBackward button. This change the value of _goForward,
// mark GoBackward button as checked and ensure GoForward button
// is not checked
// Arguments:
// - sender: not used
// - e: not used
// Return Value:
// - <none>
void SearchBoxControl::GoBackwardClicked(winrt::Windows::Foundation::IInspectable const& /*sender*/, RoutedEventArgs const& /*e*/)
{
GoBackwardButton().IsChecked(true);
if (GoForwardButton().IsChecked())
{
GoForwardButton().IsChecked(false);
}
// kick off search
_SearchHandlers(TextBox().Text(), _GoForward(), _CaseSensitive());
}
// Method Description:
// - Handler for clicking the GoForward button. This change the value of _goForward,
// mark GoForward button as checked and ensure GoBackward button
// is not checked
// Arguments:
// - sender: not used
// - e: not used
// Return Value:
// - <none>
void SearchBoxControl::GoForwardClicked(winrt::Windows::Foundation::IInspectable const& /*sender*/, RoutedEventArgs const& /*e*/)
{
GoForwardButton().IsChecked(true);
if (GoBackwardButton().IsChecked())
{
GoBackwardButton().IsChecked(false);
}
// kick off search
_SearchHandlers(TextBox().Text(), _GoForward(), _CaseSensitive());
}
// Method Description:
// - Handler for clicking the close button. This destructs the
// search box object in TermControl
// Arguments:
// - sender: not used
// - e: event data
// Return Value:
// - <none>
void SearchBoxControl::CloseClick(winrt::Windows::Foundation::IInspectable const& /*sender*/, RoutedEventArgs const& e)
{
_ClosedHandlers(*this, e);
}
// Method Description:
// - To avoid Characters input bubbling up to terminal, we implement this handler here,
// simply mark the key input as handled
// Arguments:
// - sender: not used
// - e: event data
// Return Value:
// - <none>
void SearchBoxControl::_CharacterHandler(winrt::Windows::Foundation::IInspectable const& /*sender*/, Input::CharacterReceivedRoutedEventArgs const& e)
{
e.Handled(true);
}
}