Skip to content

Commit d1ac375

Browse files
e82ericDHowett
authored andcommitted
Avoid covering current search highlight with search box (#17516)
Adds a scroll offset to avoid hiding the current search highlight with the search box. - Offset is based on the number of rows that the search box takes up. (I am not totally sure I am calculating this right) - This won't help when the current highlight is in the first couple rows of the buffer. Fixes: #4407 (cherry picked from commit 0bafab9) Service-Card-Id: PVTI_lADOAF3p4s4AmhmszgTTNTE Service-Version: 1.21
1 parent 87c87b5 commit d1ac375

File tree

7 files changed

+53
-18
lines changed

7 files changed

+53
-18
lines changed

src/cascadia/TerminalControl/ControlCore.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -1654,22 +1654,22 @@ namespace winrt::Microsoft::Terminal::Control::implementation
16541654
// - resetOnly: If true, only Reset() will be called, if anything. FindNext() will never be called.
16551655
// Return Value:
16561656
// - <none>
1657-
SearchResults ControlCore::Search(const std::wstring_view& text, const bool goForward, const bool caseSensitive, const bool regularExpression, const bool resetOnly)
1657+
SearchResults ControlCore::Search(SearchRequest request)
16581658
{
16591659
const auto lock = _terminal->LockForWriting();
16601660
SearchFlag flags{};
1661-
WI_SetFlagIf(flags, SearchFlag::CaseInsensitive, !caseSensitive);
1662-
WI_SetFlagIf(flags, SearchFlag::RegularExpression, regularExpression);
1663-
const auto searchInvalidated = _searcher.IsStale(*_terminal.get(), text, flags);
1661+
WI_SetFlagIf(flags, SearchFlag::CaseInsensitive, !request.CaseSensitive);
1662+
WI_SetFlagIf(flags, SearchFlag::RegularExpression, request.RegularExpression);
1663+
const auto searchInvalidated = _searcher.IsStale(*_terminal.get(), request.Text, flags);
16641664

1665-
if (searchInvalidated || !resetOnly)
1665+
if (searchInvalidated || !request.Reset)
16661666
{
16671667
std::vector<til::point_span> oldResults;
16681668

16691669
if (searchInvalidated)
16701670
{
16711671
oldResults = _searcher.ExtractResults();
1672-
_searcher.Reset(*_terminal.get(), text, flags, !goForward);
1672+
_searcher.Reset(*_terminal.get(), request.Text, flags, !request.GoForward);
16731673

16741674
if (SnapSearchResultToSelection())
16751675
{
@@ -1681,12 +1681,12 @@ namespace winrt::Microsoft::Terminal::Control::implementation
16811681
}
16821682
else
16831683
{
1684-
_searcher.FindNext(!goForward);
1684+
_searcher.FindNext(!request.GoForward);
16851685
}
16861686

16871687
if (const auto idx = _searcher.CurrentMatch(); idx >= 0)
16881688
{
1689-
_terminal->SetSearchHighlightFocused(gsl::narrow<size_t>(idx));
1689+
_terminal->SetSearchHighlightFocused(gsl::narrow<size_t>(idx), request.ScrollOffset);
16901690
}
16911691
_renderer->TriggerSearchHighlight(oldResults);
16921692
}
@@ -1716,7 +1716,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
17161716
{
17171717
const auto lock = _terminal->LockForWriting();
17181718
_terminal->SetSearchHighlights({});
1719-
_terminal->SetSearchHighlightFocused({});
1719+
_terminal->SetSearchHighlightFocused({}, 0);
17201720
_renderer->TriggerSearchHighlight(_searcher.Results());
17211721
_searcher = {};
17221722
}

src/cascadia/TerminalControl/ControlCore.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
219219
void SetSelectionAnchor(const til::point position);
220220
void SetEndSelectionPoint(const til::point position);
221221

222-
SearchResults Search(const std::wstring_view& text, bool goForward, bool caseSensitive, bool regularExpression, bool reset);
222+
SearchResults Search(SearchRequest request);
223223
const std::vector<til::point_span>& SearchResultRows() const noexcept;
224224
void ClearSearch();
225225
void SnapSearchResultToSelection(bool snap) noexcept;

src/cascadia/TerminalControl/ControlCore.idl

+11-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ namespace Microsoft.Terminal.Control
4949
Boolean EndAtRightBoundary;
5050
};
5151

52+
struct SearchRequest
53+
{
54+
String Text;
55+
Boolean GoForward;
56+
Boolean CaseSensitive;
57+
Boolean RegularExpression;
58+
Boolean Reset;
59+
Int32 ScrollOffset;
60+
};
61+
5262
struct SearchResults
5363
{
5464
Int32 TotalMatches;
@@ -135,7 +145,7 @@ namespace Microsoft.Terminal.Control
135145
void ResumeRendering();
136146
void BlinkAttributeTick();
137147

138-
SearchResults Search(String text, Boolean goForward, Boolean caseSensitive, Boolean regularExpression, Boolean reset);
148+
SearchResults Search(SearchRequest request);
139149
void ClearSearch();
140150
Boolean SnapSearchResultToSelection;
141151

src/cascadia/TerminalControl/TermControl.cpp

+24-4
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
547547
_searchBox->Open([weakThis = get_weak()]() {
548548
if (const auto self = weakThis.get(); self && !self->_IsClosing())
549549
{
550+
self->_searchScrollOffset = self->_calculateSearchScrollOffset();
550551
self->_searchBox->SetFocusOnTextbox();
551552
self->_refreshSearch();
552553
}
@@ -568,7 +569,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
568569
}
569570
else
570571
{
571-
_handleSearchResults(_core.Search(_searchBox->Text(), goForward, _searchBox->CaseSensitive(), _searchBox->RegularExpression(), false));
572+
const auto request = SearchRequest{ _searchBox->Text(), goForward, _searchBox->CaseSensitive(), _searchBox->RegularExpression(), false, _searchScrollOffset };
573+
_handleSearchResults(_core.Search(request));
572574
}
573575
}
574576

@@ -602,7 +604,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
602604
{
603605
if (_searchBox && _searchBox->IsOpen())
604606
{
605-
_handleSearchResults(_core.Search(text, goForward, caseSensitive, regularExpression, false));
607+
const auto request = SearchRequest{ text, goForward, caseSensitive, regularExpression, false, _searchScrollOffset };
608+
_handleSearchResults(_core.Search(request));
606609
}
607610
}
608611

@@ -623,7 +626,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
623626
{
624627
// We only want to update the search results based on the new text. Set
625628
// `resetOnly` to true so we don't accidentally update the current match index.
626-
const auto result = _core.Search(text, goForward, caseSensitive, regularExpression, true);
629+
const auto request = SearchRequest{ text, goForward, caseSensitive, regularExpression, true, _searchScrollOffset };
630+
const auto result = _core.Search(request);
627631
_handleSearchResults(result);
628632
}
629633
}
@@ -3629,6 +3633,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
36293633
};
36303634
scaleMarker(SelectionStartMarker());
36313635
scaleMarker(SelectionEndMarker());
3636+
3637+
_searchScrollOffset = _calculateSearchScrollOffset();
36323638
}
36333639

36343640
void TermControl::_coreRaisedNotice(const IInspectable& /*sender*/,
@@ -3750,7 +3756,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
37503756
const auto goForward = _searchBox->GoForward();
37513757
const auto caseSensitive = _searchBox->CaseSensitive();
37523758
const auto regularExpression = _searchBox->RegularExpression();
3753-
_handleSearchResults(_core.Search(text, goForward, caseSensitive, regularExpression, true));
3759+
const auto request = SearchRequest{ text, goForward, caseSensitive, regularExpression, true, _calculateSearchScrollOffset() };
3760+
_handleSearchResults(_core.Search(request));
37543761
}
37553762

37563763
void TermControl::_handleSearchResults(SearchResults results)
@@ -3919,6 +3926,19 @@ namespace winrt::Microsoft::Terminal::Control::implementation
39193926
_showContextMenuAt(_toControlOrigin(cursorPos));
39203927
}
39213928

3929+
til::CoordType TermControl::_calculateSearchScrollOffset() const
3930+
{
3931+
auto result = 0;
3932+
if (_searchBox)
3933+
{
3934+
const auto displayInfo = DisplayInformation::GetForCurrentView();
3935+
const auto scaleFactor = _core.FontSize().Height / displayInfo.RawPixelsPerViewPixel();
3936+
const auto searchBoxRows = _searchBox->ActualHeight() / scaleFactor;
3937+
result = static_cast<int32_t>(std::ceil(searchBoxRows));
3938+
}
3939+
return result;
3940+
}
3941+
39223942
void TermControl::_PasteCommandHandler(const IInspectable& /*sender*/,
39233943
const IInspectable& /*args*/)
39243944
{

src/cascadia/TerminalControl/TermControl.h

+3
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
303303

304304
bool _isBackgroundLight{ false };
305305
bool _detached{ false };
306+
til::CoordType _searchScrollOffset = 0;
306307

307308
Windows::Foundation::Collections::IObservableVector<Windows::UI::Xaml::Controls::ICommandBarElement> _originalPrimaryElements{ nullptr };
308309
Windows::Foundation::Collections::IObservableVector<Windows::UI::Xaml::Controls::ICommandBarElement> _originalSecondaryElements{ nullptr };
@@ -420,6 +421,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
420421
void _contextMenuHandler(IInspectable sender, Control::ContextMenuRequestedEventArgs args);
421422
void _showContextMenuAt(const til::point& controlRelativePos);
422423

424+
til::CoordType _calculateSearchScrollOffset() const;
425+
423426
void _PasteCommandHandler(const IInspectable& sender, const IInspectable& args);
424427
void _CopyCommandHandler(const IInspectable& sender, const IInspectable& args);
425428
void _SearchCommandHandler(const IInspectable& sender, const IInspectable& args);

src/cascadia/TerminalCore/Terminal.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ void Terminal::SetSearchHighlights(const std::vector<til::point_span>& highlight
12381238
// Method Description:
12391239
// - Stores the focused search highlighted region in the terminal
12401240
// - If the region isn't empty, it will be brought into view
1241-
void Terminal::SetSearchHighlightFocused(const size_t focusedIdx)
1241+
void Terminal::SetSearchHighlightFocused(const size_t focusedIdx, til::CoordType searchScrollOffset)
12421242
{
12431243
_assertLocked();
12441244
_searchHighlightFocused = focusedIdx;
@@ -1247,7 +1247,9 @@ void Terminal::SetSearchHighlightFocused(const size_t focusedIdx)
12471247
if (focusedIdx < _searchHighlights.size())
12481248
{
12491249
const auto focused = til::at(_searchHighlights, focusedIdx);
1250-
_ScrollToPoints(focused.start, focused.end);
1250+
const auto adjustedStart = til::point{ focused.start.x, std::max(0, focused.start.y - searchScrollOffset) };
1251+
const auto adjustedEnd = til::point{ focused.end.x, std::max(0, focused.end.y - searchScrollOffset) };
1252+
_ScrollToPoints(adjustedStart, adjustedEnd);
12511253
}
12521254
}
12531255

src/cascadia/TerminalCore/Terminal.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class Microsoft::Terminal::Core::Terminal final :
231231
void SetPlayMidiNoteCallback(std::function<void(const int, const int, const std::chrono::microseconds)> pfn) noexcept;
232232
void CompletionsChangedCallback(std::function<void(std::wstring_view, unsigned int)> pfn) noexcept;
233233
void SetSearchHighlights(const std::vector<til::point_span>& highlights) noexcept;
234-
void SetSearchHighlightFocused(const size_t focusedIdx);
234+
void SetSearchHighlightFocused(const size_t focusedIdx, til::CoordType searchScrollOffset);
235235

236236
void BlinkCursor() noexcept;
237237
void SetCursorOn(const bool isOn) noexcept;

0 commit comments

Comments
 (0)