Skip to content

Commit 71409f8

Browse files
authored
Avoid scrolling when the search is open (#19775)
When text scrolls in and out of view, depending on the length of the scrollback and latency of input, `GetSearchHighlightFocused` would change which would trigger a `ScrollToSearchHighlight` call. This PR changes the behavior such that only actively changing the search mask triggers a search (typing text or pressing enter). Closes #19754 ## Validation Steps Performed * Brining text in and out of view doesn't scroll ✅ * Toggling the aA button scrolls results into view ✅
1 parent 6723ca2 commit 71409f8

File tree

4 files changed

+46
-26
lines changed

4 files changed

+46
-26
lines changed

src/cascadia/TerminalControl/ControlCore.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
17361736
// - resetOnly: If true, only Reset() will be called, if anything. FindNext() will never be called.
17371737
// Return Value:
17381738
// - <none>
1739-
SearchResults ControlCore::Search(SearchRequest request)
1739+
SearchResults ControlCore::Search(const SearchRequest& request)
17401740
{
17411741
const auto lock = _terminal->LockForWriting();
17421742

@@ -1745,15 +1745,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation
17451745
WI_SetFlagIf(flags, SearchFlag::RegularExpression, request.RegularExpression);
17461746
const auto searchInvalidated = _searcher.IsStale(*_terminal.get(), request.Text, flags);
17471747

1748-
if (searchInvalidated || !request.ResetOnly)
1748+
if (searchInvalidated || request.ExecuteSearch)
17491749
{
17501750
std::vector<til::point_span> oldResults;
1751-
til::point_span oldFocused;
1752-
1753-
if (const auto focused = _terminal->GetSearchHighlightFocused())
1754-
{
1755-
oldFocused = *focused;
1756-
}
17571751

17581752
if (searchInvalidated)
17591753
{
@@ -1762,18 +1756,18 @@ namespace winrt::Microsoft::Terminal::Control::implementation
17621756
_terminal->SetSearchHighlights(_searcher.Results());
17631757
}
17641758

1765-
if (!request.ResetOnly)
1759+
if (request.ExecuteSearch)
17661760
{
17671761
_searcher.FindNext(!request.GoForward);
17681762
}
17691763

17701764
_terminal->SetSearchHighlightFocused(gsl::narrow<size_t>(std::max<ptrdiff_t>(0, _searcher.CurrentMatch())));
17711765
_renderer->TriggerSearchHighlight(oldResults);
1766+
}
17721767

1773-
if (const auto focused = _terminal->GetSearchHighlightFocused(); focused && *focused != oldFocused)
1774-
{
1775-
_terminal->ScrollToSearchHighlight(request.ScrollOffset);
1776-
}
1768+
if (request.ScrollIntoView)
1769+
{
1770+
_terminal->ScrollToSearchHighlight(request.ScrollOffset);
17771771
}
17781772

17791773
int32_t totalMatches = 0;

src/cascadia/TerminalControl/ControlCore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
225225
void SetSelectionAnchor(const til::point position);
226226
void SetEndSelectionPoint(const til::point position);
227227

228-
SearchResults Search(SearchRequest request);
228+
SearchResults Search(const SearchRequest& request);
229229
const std::vector<til::point_span>& SearchResultRows() const noexcept;
230230
void ClearSearch();
231231

src/cascadia/TerminalControl/ControlCore.idl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ namespace Microsoft.Terminal.Control
5555
Boolean GoForward;
5656
Boolean CaseSensitive;
5757
Boolean RegularExpression;
58-
Boolean ResetOnly;
58+
Boolean ExecuteSearch;
59+
Boolean ScrollIntoView;
5960
Int32 ScrollOffset;
6061
};
6162

src/cascadia/TerminalControl/TermControl.cpp

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,15 @@ namespace winrt::Microsoft::Terminal::Control::implementation
714714
}
715715
else
716716
{
717-
const auto request = SearchRequest{ _searchBox->Text(), goForward, _searchBox->CaseSensitive(), _searchBox->RegularExpression(), false, _searchScrollOffset };
718-
_handleSearchResults(_core.Search(request));
717+
_handleSearchResults(_core.Search(SearchRequest{
718+
.Text = _searchBox->Text(),
719+
.GoForward = goForward,
720+
.CaseSensitive = _searchBox->CaseSensitive(),
721+
.RegularExpression = _searchBox->RegularExpression(),
722+
.ExecuteSearch = true,
723+
.ScrollIntoView = true,
724+
.ScrollOffset = _searchScrollOffset,
725+
}));
719726
}
720727
}
721728

@@ -749,8 +756,15 @@ namespace winrt::Microsoft::Terminal::Control::implementation
749756
{
750757
if (_searchBox && _searchBox->IsOpen())
751758
{
752-
const auto request = SearchRequest{ text, goForward, caseSensitive, regularExpression, false, _searchScrollOffset };
753-
_handleSearchResults(_core.Search(request));
759+
_handleSearchResults(_core.Search(SearchRequest{
760+
.Text = text,
761+
.GoForward = goForward,
762+
.CaseSensitive = caseSensitive,
763+
.RegularExpression = regularExpression,
764+
.ExecuteSearch = true,
765+
.ScrollIntoView = true,
766+
.ScrollOffset = _searchScrollOffset,
767+
}));
754768
}
755769
}
756770

@@ -769,11 +783,15 @@ namespace winrt::Microsoft::Terminal::Control::implementation
769783
{
770784
if (_searchBox && _searchBox->IsOpen())
771785
{
772-
// We only want to update the search results based on the new text. Set
773-
// `resetOnly` to true so we don't accidentally update the current match index.
774-
const auto request = SearchRequest{ text, goForward, caseSensitive, regularExpression, true, _searchScrollOffset };
775-
const auto result = _core.Search(request);
776-
_handleSearchResults(result);
786+
_handleSearchResults(_core.Search(SearchRequest{
787+
.Text = text,
788+
.GoForward = goForward,
789+
.CaseSensitive = caseSensitive,
790+
.RegularExpression = regularExpression,
791+
.ExecuteSearch = false,
792+
.ScrollIntoView = true,
793+
.ScrollOffset = _searchScrollOffset,
794+
}));
777795
}
778796
}
779797

@@ -3742,8 +3760,15 @@ namespace winrt::Microsoft::Terminal::Control::implementation
37423760
const auto goForward = _searchBox->GoForward();
37433761
const auto caseSensitive = _searchBox->CaseSensitive();
37443762
const auto regularExpression = _searchBox->RegularExpression();
3745-
const auto request = SearchRequest{ text, goForward, caseSensitive, regularExpression, true, _searchScrollOffset };
3746-
_handleSearchResults(_core.Search(request));
3763+
_handleSearchResults(_core.Search(SearchRequest{
3764+
.Text = text,
3765+
.GoForward = goForward,
3766+
.CaseSensitive = caseSensitive,
3767+
.RegularExpression = regularExpression,
3768+
.ExecuteSearch = false,
3769+
.ScrollIntoView = false,
3770+
.ScrollOffset = _searchScrollOffset,
3771+
}));
37473772
}
37483773

37493774
void TermControl::_handleSearchResults(SearchResults results)

0 commit comments

Comments
 (0)