Skip to content

Commit 1c8e996

Browse files
committed
PRE-MERGE microsoft#17352 Fix some search highlights scenarios
2 parents 274010f + 3d9d32b commit 1c8e996

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

src/cascadia/TerminalControl/SearchBoxControl.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,18 @@ namespace winrt::Microsoft::Terminal::Control::implementation
155155
// search box remains in Visible state (though not really *visible*) during the
156156
// first load. So, we only need to apply this check here (after checking that
157157
// we're done initializing).
158-
if (Visibility() == Visibility::Visible)
158+
if (IsOpen())
159159
{
160160
callback();
161161
return;
162162
}
163163

164+
// Stop ongoing close animation if any
165+
if (CloseAnimation().GetCurrentState() == Media::Animation::ClockState::Active)
166+
{
167+
CloseAnimation().Stop();
168+
}
169+
164170
VisualStateManager::GoToState(*this, L"Opened", false);
165171

166172
// Call the callback only after we're in Opened state. Setting focus
@@ -196,6 +202,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation
196202
}
197203
}
198204

205+
bool SearchBoxControl::IsOpen()
206+
{
207+
return Visibility() == Visibility::Visible && CloseAnimation().GetCurrentState() != Media::Animation::ClockState::Active;
208+
}
209+
199210
winrt::hstring SearchBoxControl::Text()
200211
{
201212
return TextBox().Text();

src/cascadia/TerminalControl/SearchBoxControl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
3434
void TextBoxKeyDown(const winrt::Windows::Foundation::IInspectable& /*sender*/, const winrt::Windows::UI::Xaml::Input::KeyRoutedEventArgs& e);
3535
void Open(std::function<void()> callback);
3636
void Close();
37+
bool IsOpen();
3738

3839
winrt::hstring Text();
3940
bool GoForward();

src/cascadia/TerminalControl/TermControl.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
522522
}
523523
}
524524

525-
if (_searchBox && _searchBox->Visibility() == Visibility::Visible)
525+
if (_searchBox && _searchBox->IsOpen())
526526
{
527527
const auto core = winrt::get_self<ControlCore>(_core);
528528
const auto& searchMatches = core->SearchResultRows();
@@ -567,6 +567,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
567567
// but since code paths differ, extra work is required to ensure correctness.
568568
if (!_core.HasMultiLineSelection())
569569
{
570+
_core.SnapSearchResultToSelection(true);
570571
const auto selectedLine{ _core.SelectedText(true) };
571572
_searchBox->PopulateTextbox(selectedLine);
572573
}
@@ -583,13 +584,14 @@ namespace winrt::Microsoft::Terminal::Control::implementation
583584
}
584585
}
585586

587+
// This is called when a Find Next/Previous Match action is triggered.
586588
void TermControl::SearchMatch(const bool goForward)
587589
{
588590
if (_IsClosing())
589591
{
590592
return;
591593
}
592-
if (!_searchBox || _searchBox->Visibility() != Visibility::Visible)
594+
if (!_searchBox || !_searchBox->IsOpen())
593595
{
594596
CreateSearchBoxControl();
595597
}
@@ -631,7 +633,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
631633
}
632634

633635
// Method Description:
634-
// - The handler for the "search criteria changed" event. Clears selection and initiates a new search.
636+
// - The handler for the "search criteria changed" event. Initiates a new search.
635637
// Arguments:
636638
// - text: the text to search
637639
// - goForward: indicates whether the search should be performed forward (if set to true) or backward
@@ -645,7 +647,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation
645647
{
646648
if (_searchBox && _searchBox->Visibility() == Visibility::Visible)
647649
{
648-
_handleSearchResults(_core.Search(text, goForward, caseSensitive, regularExpression, false));
650+
// We only want to update the search results based on the new text. Set
651+
// `resetOnly` to true so we don't accidentally update the current match index.
652+
const auto result = _core.Search(text, goForward, caseSensitive, regularExpression, true);
653+
_handleSearchResults(result);
649654
}
650655
}
651656

@@ -663,6 +668,19 @@ namespace winrt::Microsoft::Terminal::Control::implementation
663668
_searchBox->Close();
664669
_core.ClearSearch();
665670

671+
// Clear search highlights scroll marks (by triggering an update after closing the search box)
672+
if (_showMarksInScrollbar)
673+
{
674+
const auto scrollBar = ScrollBar();
675+
ScrollBarUpdate update{
676+
.newValue = scrollBar.Value(),
677+
.newMaximum = scrollBar.Maximum(),
678+
.newMinimum = scrollBar.Minimum(),
679+
.newViewportSize = scrollBar.ViewportSize(),
680+
};
681+
_updateScrollBar->Run(update);
682+
}
683+
666684
// Set focus back to terminal control
667685
this->Focus(FocusState::Programmatic);
668686
}
@@ -3646,7 +3664,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
36463664

36473665
void TermControl::_refreshSearch()
36483666
{
3649-
if (!_searchBox || _searchBox->Visibility() != Visibility::Visible)
3667+
if (!_searchBox || !_searchBox->IsOpen())
36503668
{
36513669
return;
36523670
}
@@ -3670,7 +3688,15 @@ namespace winrt::Microsoft::Terminal::Control::implementation
36703688
return;
36713689
}
36723690

3673-
_searchBox->SetStatus(results.TotalMatches, results.CurrentMatch, results.SearchRegexInvalid);
3691+
// Only show status when we have a search term
3692+
if (_searchBox->Text().empty())
3693+
{
3694+
_searchBox->ClearStatus();
3695+
}
3696+
else
3697+
{
3698+
_searchBox->SetStatus(results.TotalMatches, results.CurrentMatch, results.SearchRegexInvalid);
3699+
}
36743700

36753701
if (results.SearchInvalidated)
36763702
{

0 commit comments

Comments
 (0)