Skip to content

Commit ba9a820

Browse files
committed
fix some search highlights scenarios
1 parent 01e4df1 commit ba9a820

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
@@ -493,7 +493,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
493493
}
494494
}
495495

496-
if (_searchBox && _searchBox->Visibility() == Visibility::Visible)
496+
if (_searchBox && _searchBox->IsOpen())
497497
{
498498
const auto core = winrt::get_self<ControlCore>(_core);
499499
const auto& searchMatches = core->SearchResultRows();
@@ -538,6 +538,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
538538
// but since code paths differ, extra work is required to ensure correctness.
539539
if (!_core.HasMultiLineSelection())
540540
{
541+
_core.SnapSearchResultToSelection(true);
541542
const auto selectedLine{ _core.SelectedText(true) };
542543
_searchBox->PopulateTextbox(selectedLine);
543544
}
@@ -554,13 +555,14 @@ namespace winrt::Microsoft::Terminal::Control::implementation
554555
}
555556
}
556557

558+
// This is called when a Find Next/Previous Match action is triggered.
557559
void TermControl::SearchMatch(const bool goForward)
558560
{
559561
if (_IsClosing())
560562
{
561563
return;
562564
}
563-
if (!_searchBox || _searchBox->Visibility() != Visibility::Visible)
565+
if (!_searchBox || !_searchBox->IsOpen())
564566
{
565567
CreateSearchBoxControl();
566568
}
@@ -602,7 +604,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
602604
}
603605

604606
// Method Description:
605-
// - The handler for the "search criteria changed" event. Clears selection and initiates a new search.
607+
// - The handler for the "search criteria changed" event. Initiates a new search.
606608
// Arguments:
607609
// - text: the text to search
608610
// - goForward: indicates whether the search should be performed forward (if set to true) or backward
@@ -616,7 +618,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation
616618
{
617619
if (_searchBox && _searchBox->Visibility() == Visibility::Visible)
618620
{
619-
_handleSearchResults(_core.Search(text, goForward, caseSensitive, regularExpression, false));
621+
// We only want to update the search results based on the new text. Set
622+
// `resetOnly` to true so we don't accidentally update the current match index.
623+
const auto result = _core.Search(text, goForward, caseSensitive, regularExpression, true);
624+
_handleSearchResults(result);
620625
}
621626
}
622627

@@ -634,6 +639,19 @@ namespace winrt::Microsoft::Terminal::Control::implementation
634639
_searchBox->Close();
635640
_core.ClearSearch();
636641

642+
// Clear search highlights scroll marks (by triggering an update after closing the search box)
643+
if (_showMarksInScrollbar)
644+
{
645+
const auto scrollBar = ScrollBar();
646+
ScrollBarUpdate update{
647+
.newValue = scrollBar.Value(),
648+
.newMaximum = scrollBar.Maximum(),
649+
.newMinimum = scrollBar.Minimum(),
650+
.newViewportSize = scrollBar.ViewportSize(),
651+
};
652+
_updateScrollBar->Run(update);
653+
}
654+
637655
// Set focus back to terminal control
638656
this->Focus(FocusState::Programmatic);
639657
}
@@ -3595,7 +3613,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
35953613

35963614
void TermControl::_refreshSearch()
35973615
{
3598-
if (!_searchBox || _searchBox->Visibility() != Visibility::Visible)
3616+
if (!_searchBox || !_searchBox->IsOpen())
35993617
{
36003618
return;
36013619
}
@@ -3619,7 +3637,15 @@ namespace winrt::Microsoft::Terminal::Control::implementation
36193637
return;
36203638
}
36213639

3622-
_searchBox->SetStatus(results.TotalMatches, results.CurrentMatch, results.SearchRegexInvalid);
3640+
// Only show status when we have a search term
3641+
if (_searchBox->Text().empty())
3642+
{
3643+
_searchBox->ClearStatus();
3644+
}
3645+
else
3646+
{
3647+
_searchBox->SetStatus(results.TotalMatches, results.CurrentMatch, results.SearchRegexInvalid);
3648+
}
36233649

36243650
if (results.SearchInvalidated)
36253651
{

0 commit comments

Comments
 (0)