Skip to content

Commit

Permalink
Search - add search box control and implement search experience (#3590)
Browse files Browse the repository at this point in the history
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? -->
This is the PR for feature Search: #605 
This PR includes the newly introduced SearchBoxControl in TermControl dir, which is the search bar for the search experience. And the codes that enable Search in Windows Terminal. 

<!-- Other than the issue solved, is this relevant to any other issues/existing PRs? --> 
The PR that migrates the Conhost search module: microsoft/terminal#3279
Spec (still actively updating): microsoft/terminal#3299
<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist
* [x] Closes #605 
* [ ] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA
* [ ] Tests added/passed
* [ ] Requires documentation to be updated
* [ ] I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: #xxx

<!-- Provide a more detailed description of the PR, other things fixed or any additional comments/features here -->
These functionalities are included in the search experience. 
1. Search in Terminal text buffer. 
2. Automatic wrap-around. 
3. Search up or down switch by clicking different buttons.
4. Search case sensitively/insensitively by clicking a button.                                                                                                                                                S. Move the search box to the top/bottom by clicking a button. 
6. Close by clicking 'X'. 
7. Open search by ctrl + F.

When the searchbox is open, the user could still interact with the terminal by clicking the terminal input area. 

While I already have the search functionalities, currently there are still some known to-do works and I will keep updating my PR:

1. Optimize the search box UI, this includes:
                                                  1) Theme adaptation. The search box background and font color 
                                                       should change according to the theme, 
                                                  2) Add background. Currently the elements in search box are all
                                                      transparent. However, we need a background. 
                                                  3) Move button should be highlighted once clicked. 
2. Accessibility: search process should be able to performed without mouse. Once the search box is focused, the user should be able to navigate between all interactive elements on the searchbox using keyboard. 

<!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well -->

To test:
1. checkout this branch.
2. Build the project. 
3. Start Windows Terminal and press Ctrl+F
4. The search box should appear on the top right corner.
  • Loading branch information
donno2048 committed Dec 17, 2019
1 parent 3229aac commit 4c46327
Show file tree
Hide file tree
Showing 28 changed files with 757 additions and 29 deletions.
3 changes: 2 additions & 1 deletion doc/cascadia/SettingsSchema.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ Commands listed below are per the implementation in [`src/cascadia/TerminalApp/A
- moveFocusRight
- moveFocusUp
- moveFocusDown
- toggleFullscreen
- toggleFullscreen
- find

## Example Keys
- ctrl+1
Expand Down
34 changes: 27 additions & 7 deletions src/buffer/out/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,7 @@ bool Search::FindNext()
// - Takes the found word and selects it in the screen buffer
void Search::Select() const
{
// Only select if we've found something.
if (_coordSelStart != _coordSelEnd)
{
_uiaData.SelectNewRegion(_coordSelStart, _coordSelEnd);
}
_uiaData.SelectNewRegion(_coordSelStart, _coordSelEnd);
}

// Routine Description:
Expand Down Expand Up @@ -142,6 +138,7 @@ std::pair<COORD, COORD> Search::GetFoundLocation() const noexcept
COORD Search::s_GetInitialAnchor(IUiaData& uiaData, const Direction direction)
{
const auto& textBuffer = uiaData.GetTextBuffer();
const COORD textBufferEndPosition = uiaData.GetTextBufferEndPosition();
if (uiaData.IsSelectionActive())
{
auto anchor = uiaData.GetSelectionAnchor();
Expand All @@ -152,6 +149,10 @@ COORD Search::s_GetInitialAnchor(IUiaData& uiaData, const Direction direction)
else
{
textBuffer.GetSize().DecrementInBoundsCircular(anchor);
// If the selection starts at (0, 0), we need to make sure
// it does not exceed the text buffer end position
anchor.X = std::min(textBufferEndPosition.X, anchor.X);
anchor.Y = std::min(textBufferEndPosition.Y, anchor.Y);
}
return anchor;
}
Expand All @@ -163,8 +164,7 @@ COORD Search::s_GetInitialAnchor(IUiaData& uiaData, const Direction direction)
}
else
{
const auto bufferSize = textBuffer.GetSize().Dimensions();
return { bufferSize.X - 1, bufferSize.Y - 1 };
return textBufferEndPosition;
}
}
}
Expand Down Expand Up @@ -293,6 +293,26 @@ void Search::_UpdateNextPosition()
{
THROW_HR(E_NOTIMPL);
}

// To reduce wrap-around time, if the next position is larger than
// the end position of the written text
// We put the next position to:
// Forward: (0, 0)
// Backward: the position of the end of the text buffer
const COORD bufferEndPosition = _uiaData.GetTextBufferEndPosition();

if (_coordNext.Y > bufferEndPosition.Y ||
(_coordNext.Y == bufferEndPosition.Y && _coordNext.X > bufferEndPosition.X))
{
if (_direction == Direction::Forward)
{
_coordNext = { 0 };
}
else
{
_coordNext = bufferEndPosition;
}
}
}

// Routine Description:
Expand Down
2 changes: 1 addition & 1 deletion src/buffer/out/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Search final

private:
wchar_t _ApplySensitivity(const wchar_t wch) const noexcept;
bool Search::_FindNeedleInHaystackAt(const COORD pos, COORD& start, COORD& end) const;
bool _FindNeedleInHaystackAt(const COORD pos, COORD& start, COORD& end) const;
bool _CompareChars(const std::wstring_view one, const std::wstring_view two) const noexcept;
void _UpdateNextPosition();

Expand Down
8 changes: 7 additions & 1 deletion src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ namespace winrt::TerminalApp::implementation
}
}

void TerminalPage::_HandleFind(const IInspectable& /*sender*/,
const TerminalApp::ActionEventArgs& args)
{
_Find();
args.Handled(true);
}

void TerminalPage::_HandleResetFontSize(const IInspectable& /*sender*/,
const TerminalApp::ActionEventArgs& args)
{
Expand All @@ -228,5 +235,4 @@ namespace winrt::TerminalApp::implementation
_ToggleFullscreen();
args.Handled(true);
}

}
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/AppKeyBindingsSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ static constexpr std::string_view MoveFocusLeftKey{ "moveFocusLeft" }; // Legacy
static constexpr std::string_view MoveFocusRightKey{ "moveFocusRight" }; // Legacy
static constexpr std::string_view MoveFocusUpKey{ "moveFocusUp" }; // Legacy
static constexpr std::string_view MoveFocusDownKey{ "moveFocusDown" }; // Legacy
static constexpr std::string_view FindKey{ "find" };
static constexpr std::string_view ToggleFullscreenKey{ "toggleFullscreen" };

// Specifically use a map here over an unordered_map. We want to be able to
Expand Down Expand Up @@ -142,6 +143,7 @@ static const std::map<std::string_view, ShortcutAction, std::less<>> commandName
{ ToggleFullscreenKey, ShortcutAction::ToggleFullscreen },
{ SplitPaneKey, ShortcutAction::SplitPane },
{ UnboundKey, ShortcutAction::Invalid },
{ FindKey, ShortcutAction::Find },
};

// Function Description:
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 @@ -180,6 +180,11 @@ namespace winrt::TerminalApp::implementation
_AdjustFontSizeHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::Find:
{
_FindHandlers(*this, *eventArgs);
break;
}
case ShortcutAction::ResetFontSize:
{
_ResetFontSizeHandlers(*this, *eventArgs);
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 @@ -44,6 +44,7 @@ namespace winrt::TerminalApp::implementation
TYPED_EVENT(ScrollDownPage, TerminalApp::ShortcutActionDispatch, TerminalApp::ActionEventArgs);
TYPED_EVENT(OpenSettings, TerminalApp::ShortcutActionDispatch, TerminalApp::ActionEventArgs);
TYPED_EVENT(ResizePane, TerminalApp::ShortcutActionDispatch, TerminalApp::ActionEventArgs);
TYPED_EVENT(Find, TerminalApp::ShortcutActionDispatch, TerminalApp::ActionEventArgs);
TYPED_EVENT(MoveFocus, TerminalApp::ShortcutActionDispatch, TerminalApp::ActionEventArgs);
TYPED_EVENT(ToggleFullscreen, TerminalApp::ShortcutActionDispatch, TerminalApp::ActionEventArgs);
// clang-format on
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/ShortcutActionDispatch.idl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace TerminalApp
MoveFocusRight, // Legacy
MoveFocusUp, // Legacy
MoveFocusDown, // Legacy
Find,
ToggleFullscreen,
OpenSettings
};
Expand Down Expand Up @@ -97,6 +98,7 @@ namespace TerminalApp
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, ActionEventArgs> ScrollDownPage;
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, ActionEventArgs> OpenSettings;
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, ActionEventArgs> ResizePane;
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, ActionEventArgs> Find;
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, ActionEventArgs> MoveFocus;
event Windows.Foundation.TypedEventHandler<ShortcutActionDispatch, ActionEventArgs> ToggleFullscreen;
}
Expand Down
16 changes: 15 additions & 1 deletion src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,6 @@ namespace winrt::TerminalApp::implementation
// Hook up the ShortcutActionDispatch object's events to our handlers.
// They should all be hooked up here, regardless of whether or not
// there's an actual keychord for them.

_actionDispatch->OpenNewTabDropdown({ this, &TerminalPage::_HandleOpenNewTabDropdown });
_actionDispatch->DuplicateTab({ this, &TerminalPage::_HandleDuplicateTab });
_actionDispatch->CloseTab({ this, &TerminalPage::_HandleCloseTab });
Expand All @@ -651,6 +650,7 @@ namespace winrt::TerminalApp::implementation
_actionDispatch->MoveFocus({ this, &TerminalPage::_HandleMoveFocus });
_actionDispatch->CopyText({ this, &TerminalPage::_HandleCopyText });
_actionDispatch->AdjustFontSize({ this, &TerminalPage::_HandleAdjustFontSize });
_actionDispatch->Find({ this, &TerminalPage::_HandleFind });
_actionDispatch->ResetFontSize({ this, &TerminalPage::_HandleResetFontSize });
_actionDispatch->ToggleFullscreen({ this, &TerminalPage::_HandleToggleFullscreen });
}
Expand Down Expand Up @@ -1417,6 +1417,20 @@ namespace winrt::TerminalApp::implementation
}
}

// Method Description:
// - Called when the user tries to do a search using keybindings.
// This will tell the current focused terminal control to create
// a search box and enable find process.
// Arguments:
// - <none>
// Return Value:
// - <none>
void TerminalPage::_Find()
{
const auto termControl = _GetActiveControl();
termControl.CreateSearchBoxControl();
}

// Method Description:
// - Toggles fullscreen mode. Hides the tab row, and raises our
// ToggleFullscreen event.
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ namespace winrt::TerminalApp::implementation
void _OnContentSizeChanged(const IInspectable& /*sender*/, Windows::UI::Xaml::SizeChangedEventArgs const& e);
void _OnTabCloseRequested(const IInspectable& sender, const Microsoft::UI::Xaml::Controls::TabViewTabCloseRequestedEventArgs& eventArgs);

void _Find();

void _RefreshUIForSettingsReload();

void _ToggleFullscreen();
Expand All @@ -148,6 +150,7 @@ namespace winrt::TerminalApp::implementation
void _HandleCopyText(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleCloseWindow(const IInspectable&, const TerminalApp::ActionEventArgs& args);
void _HandleAdjustFontSize(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleFind(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleResetFontSize(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleToggleFullscreen(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
#pragma endregion
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
{ "command": { "action": "switchToTab", "index": 6 }, "keys": ["ctrl+alt+7"] },
{ "command": { "action": "switchToTab", "index": 7 }, "keys": ["ctrl+alt+8"] },
{ "command": { "action": "switchToTab", "index": 8 }, "keys": ["ctrl+alt+9"] },
{ "command": "find", "keys": [ "ctrl+shift+f" ] },
{ "command": "toggleFullscreen", "keys": [ "alt+enter" ] },
{ "command": "toggleFullscreen", "keys": [ "f11" ] }
]
Expand Down
Loading

0 comments on commit 4c46327

Please sign in to comment.