Skip to content

Commit f7fe9e7

Browse files
committed
move logic to SearchBoxControl for better handling
1 parent b36267a commit f7fe9e7

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

src/cascadia/TerminalControl/Resources/en-US/Resources.resw

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,15 @@ Please either install the missing font or choose another one.</value>
335335
<comment>{Locked="{0}"} {0} will be replaced with a string of input that is suggested for the user to input</comment>
336336
</data>
337337
<data name="TermControl_NumResultsAccessible" xml:space="preserve">
338-
<value>{} of {}</value>
339-
<comment>{Locked="{}"} Read out by the screen reader to announce number of results from a search query. First "{}" is replaced with index of search result. Second "{}" is replaced by total number of results.</comment>
338+
<value>{0} of {1}</value>
339+
<comment>{Locked="{0}"}{Locked="{1}"} Read out by the screen reader to announce number of results from a search query. First "{}" is replaced with index of search result. Second "{}" is replaced by total number of results.</comment>
340+
</data>
341+
<data name="TermControl_UnknownSearchResultIndex" xml:space="preserve">
342+
<value>unknown</value>
343+
<comment>Will be read out by a screen reader when a value for the index of a search result is mismatched and unclear. Displayed as a part of TermControl_NumResultsAccessible.</comment>
344+
</data>
345+
<data name="TermControl_TooManySearchResults" xml:space="preserve">
346+
<value>over 999</value>
347+
<comment>Will be read out by a screen reader when a search returns over 999 search results.</comment>
340348
</data>
341349
</root>

src/cascadia/TerminalControl/SearchBoxControl.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation
463463
// Arguments:
464464
// - totalMatches - total number of matches (search results)
465465
// - currentMatch - the index of the current match (0-based)
466+
// - isAccessible - if true, format the string for screen readers. Defaults to false.
466467
// Return Value:
467468
// - status message
468-
winrt::hstring SearchBoxControl::_FormatStatus(int32_t totalMatches, int32_t currentMatch)
469+
winrt::hstring SearchBoxControl::_FormatStatus(int32_t totalMatches, int32_t currentMatch, bool isAccessible)
469470
{
470471
if (totalMatches < 0)
471472
{
@@ -482,7 +483,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
482483

483484
if (currentMatch < 0 || currentMatch > (MaximumTotalResultsToShowInStatus - 1))
484485
{
485-
currentString = CurrentIndexTooHighStatus;
486+
currentString = isAccessible ? RS_(L"TermControl_UnknownSearchResultIndex") : CurrentIndexTooHighStatus;
486487
}
487488
else
488489
{
@@ -491,13 +492,17 @@ namespace winrt::Microsoft::Terminal::Control::implementation
491492

492493
if (totalMatches > MaximumTotalResultsToShowInStatus)
493494
{
494-
totalString = TotalResultsTooHighStatus;
495+
totalString = isAccessible ? RS_(L"TermControl_TooManySearchResults") : TotalResultsTooHighStatus;
495496
}
496497
else
497498
{
498499
totalString = fmt::to_wstring(totalMatches);
499500
}
500501

502+
if (isAccessible)
503+
{
504+
return winrt::hstring{ RS_fmt(L"TermControl_NumResultsAccessible", currentString, totalString) };
505+
}
501506
return winrt::hstring{ RS_fmt(L"TermControl_NumResults", currentString, totalString) };
502507
}
503508

@@ -535,11 +540,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation
535540
return maxLength;
536541
}
537542

538-
winrt::hstring SearchBoxControl::GetStatusText()
539-
{
540-
return StatusBox().Text();
541-
}
542-
543543
// Method Description:
544544
// - Formats and sets the status message in the status box.
545545
// Increases the size of the box if required.
@@ -562,6 +562,18 @@ namespace winrt::Microsoft::Terminal::Control::implementation
562562
StatusBox().Text(status);
563563
}
564564

565+
// Method Description:
566+
// - Formats and returns an accessible status message representing the search state.
567+
// - Similar to SetStatus but returns a more descriptive string for screen readers.
568+
hstring SearchBoxControl::GetAccessibleStatus(int32_t totalMatches, int32_t currentMatch, bool searchRegexInvalid)
569+
{
570+
if (searchRegexInvalid)
571+
{
572+
return RS_(L"SearchRegexInvalid");
573+
}
574+
return _FormatStatus(totalMatches, currentMatch, true);
575+
}
576+
565577
// Method Description:
566578
// - Removes the status message in the status box.
567579
void SearchBoxControl::ClearStatus()

src/cascadia/TerminalControl/SearchBoxControl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
4343
void SetFocusOnTextbox();
4444
void PopulateTextbox(const winrt::hstring& text);
4545
bool ContainsFocus();
46-
winrt::hstring GetStatusText();
4746
void SetStatus(int32_t totalMatches, int32_t currentMatch, bool searchRegexInvalid);
47+
winrt::hstring GetAccessibleStatus(int32_t totalMatches, int32_t currentMatch, bool searchRegexInvalid);
4848
void ClearStatus();
4949

5050
void GoBackwardClicked(const winrt::Windows::Foundation::IInspectable& /*sender*/, const winrt::Windows::UI::Xaml::RoutedEventArgs& /*e*/);
@@ -78,7 +78,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
7878
void _PlayCloseAnimation();
7979
bool _AnimationEnabled();
8080

81-
static winrt::hstring _FormatStatus(int32_t totalMatches, int32_t currentMatch);
81+
static winrt::hstring _FormatStatus(int32_t totalMatches, int32_t currentMatch, bool isAccessible = false);
8282
static double _TextWidth(winrt::hstring text, double fontSize);
8383
double _GetStatusMaxWidth();
8484

src/cascadia/TerminalControl/TermControl.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3758,12 +3758,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
37583758

37593759
if (auto automationPeer{ FrameworkElementAutomationPeer::FromElement(*this) })
37603760
{
3761-
auto status = _searchBox->GetStatusText();
3762-
if (const auto i = status.size() / 2; !status.empty() && status[i] == '/')
3763-
{
3764-
status = RS_fmt(L"TermControl_NumResultsAccessible", results.CurrentMatch + 1, results.TotalMatches);
3765-
}
3766-
3761+
const auto status = _searchBox->GetAccessibleStatus(results.TotalMatches, results.CurrentMatch, results.SearchRegexInvalid);
37673762
if (!status.empty())
37683763
{
37693764
automationPeer.RaiseNotificationEvent(

0 commit comments

Comments
 (0)