Skip to content

Commit

Permalink
Reduce the value :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Don-Vito committed Jan 20, 2021
1 parent 8e5efb7 commit 160716d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/cascadia/TerminalControl/SearchBoxControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// - the size in pixels
double SearchBoxControl::_TextWidth(winrt::hstring text, double fontSize)
{
auto t = winrt::Windows::UI::Xaml::Controls::TextBlock();
winrt::Windows::UI::Xaml::Controls::TextBlock t;
t.FontSize(fontSize);
t.Text(text);
t.Measure({ FLT_MAX, FLT_MAX });
Expand Down
35 changes: 16 additions & 19 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// We avoid navigation to the first result to prevent auto-scrolling.
if (control->_searchState.has_value())
{
const SearchState searchState{ control->_searchState.value().Text, control->_searchState.value().CaseSensitive };
const SearchState searchState{ control->_searchState->Text, control->_searchState->Sensitivity };
control->_searchState.emplace(searchState);
control->_SearchAsync(std::nullopt, SearchAfterOutputDelay);
}
Expand Down Expand Up @@ -283,11 +283,11 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
return;
}

const auto originalSearchId = _searchState.value().SearchId;
const auto originalSearchId = _searchState->SearchId;
auto weakThis{ this->get_weak() };

// If no matches were computed it means we need to perform the search
if (!_searchState.value().Matches.has_value())
if (!_searchState->Matches.has_value())
{
// Before we search, let's wait a bit:
// probably the search criteria or the data are still modified.
Expand All @@ -298,7 +298,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
if (auto control{ weakThis.get() })
{
// If search box was collapsed or the new one search was triggered - let's cancel this one
if (!_searchState.has_value() || _searchState.value().SearchId != originalSearchId)
if (!_searchState.has_value() || _searchState->SearchId != originalSearchId)
{
co_return;
}
Expand All @@ -311,19 +311,15 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
}

std::vector<std::pair<COORD, COORD>> matches;
if (!_searchState.value().Text.empty())
if (!_searchState->Text.empty())
{
const Search::Sensitivity sensitivity = _searchState.value().CaseSensitive ?
Search::Sensitivity::CaseSensitive :
Search::Sensitivity::CaseInsensitive;

// We perform explicit search forward, so the first result will also be the earliest buffer location
// We will use goForward later to decide if we need to select 1 of n or n of n.
Search search(*GetUiaData(), _searchState.value().Text.c_str(), Search::Direction::Forward, sensitivity);
Search search(*GetUiaData(), _searchState->Text.c_str(), Search::Direction::Forward, _searchState->Sensitivity);
while (co_await _SearchOne(search))
{
// if search box was collapsed or the new one search was triggered - let's cancel this one
if (!_searchState.has_value() || _searchState.value().SearchId != originalSearchId)
if (!_searchState.has_value() || _searchState->SearchId != originalSearchId)
{
co_return;
}
Expand All @@ -332,12 +328,12 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
}

// if search box was collapsed or the new one search was triggered - let's cancel this one
if (!_searchState.has_value() || _searchState.value().SearchId != originalSearchId)
if (!_searchState.has_value() || _searchState->SearchId != originalSearchId)
{
co_return;
}
}
_searchState.value().Matches.emplace(matches);
_searchState->Matches.emplace(std::move(matches));
}
}

Expand All @@ -357,7 +353,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// - <none>
void TermControl::_SelectSearchResult(std::optional<bool> goForward)
{
if (_searchState.has_value() && _searchState.value().Matches.has_value())
if (_searchState.has_value() && _searchState->Matches.has_value())
{
auto& state = _searchState.value();
auto& matches = state.Matches.value();
Expand All @@ -371,15 +367,15 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
{
auto lock = _terminal->LockForWriting();
_terminal->SetBlockSelection(false);
_terminal->SelectNewRegion(currentMatch.value().first, currentMatch.value().second);
_terminal->SelectNewRegion(currentMatch->first, currentMatch->second);
_renderer->TriggerSelection();
}
}

if (_searchBox)
{
_searchBox->SetStatus(gsl::narrow<int32_t>(matches.size()), state.CurrentMatchIndex);
_searchBox->SetNavigationEnabled(!_searchState.value().Matches.value().empty());
_searchBox->SetNavigationEnabled(!_searchState->Matches->empty());
}
}
}
Expand Down Expand Up @@ -424,7 +420,8 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
_terminal->ClearSelection();
_renderer->TriggerSelection();

const SearchState searchState{ text, caseSensitive };
const auto sensitivity = caseSensitive ? Search::Sensitivity::CaseSensitive : Search::Sensitivity::CaseInsensitive;
const SearchState searchState{ text, sensitivity };
_searchState.emplace(searchState);
_SearchAsync(goForward, SearchAfterChangeDelay);
}
Expand Down Expand Up @@ -3453,7 +3450,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
{
if (Matches.has_value())
{
const int numMatches = ::base::saturated_cast<int>(Matches.value().size());
const int numMatches = ::base::saturated_cast<int>(Matches->size());
if (numMatches > 0)
{
if (CurrentMatchIndex == -1)
Expand All @@ -3477,7 +3474,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// (e.g., when the index is -1 or there are no matches)
std::optional<std::pair<COORD, COORD>> SearchState::GetCurrentMatch()
{
if (Matches.has_value() && CurrentMatchIndex > -1 && CurrentMatchIndex < Matches.value().size())
if (Matches.has_value() && CurrentMatchIndex > -1 && CurrentMatchIndex < Matches->size())
{
return til::at(Matches.value(), CurrentMatchIndex);
}
Expand Down
6 changes: 3 additions & 3 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
public:
static std::atomic<size_t> _searchIdGenerator;

SearchState(const winrt::hstring& text, const bool caseSensitive) :
SearchState(const winrt::hstring& text, const Search::Sensitivity sensitivity) :
Text(text),
CaseSensitive(caseSensitive),
Sensitivity(sensitivity),
SearchId(_searchIdGenerator.fetch_add(1))
{
}

const winrt::hstring Text;
const bool CaseSensitive;
const Search::Sensitivity Sensitivity;
const size_t SearchId;
std::optional<std::vector<std::pair<COORD, COORD>>> Matches;
int32_t CurrentMatchIndex{ -1 };
Expand Down

0 comments on commit 160716d

Please sign in to comment.