Preserve selected font face after focus changes (#20359)

## Summary

Fixes the font face `AutoSuggestBox` resetting to its previous value, or
stale filter text, after a keyboard-selected font is committed and focus
later leaves the control.

WinUI restores its cached user query when the suggestion list closes.
The existing `LostFocus` path then treated that programmatic restoration
as fresh user input and committed the stale value.

This change distinguishes genuine user edits from programmatic text
restoration, preserving the committed font value while retaining the
queued selected-suggestion commit introduced by #20255.

Fixes #20353.

## Validation

Manually validated with a local `CascadiaPackage` Debug x64 build:

- Reproduced the reset before the fix.
- Selected `Lucida Console` from `Consolas` using keyboard navigation.
- Pressed Enter and confirmed `Lucida Console` was applied.
- Returned focus to the Font face control and tabbed out again.
- Verified `Lucida Console` remained selected.
- Verified stale filter text did not return.
- Verified the #20245 crash did not regress.
- Verified Save and Discard changes both work.
This commit is contained in:
Danyal Ahmed
2026-06-30 23:51:22 +05:00
committed by GitHub
parent dc4ce1c096
commit e58bd4bdab
2 changed files with 19 additions and 2 deletions

View File

@@ -1189,13 +1189,25 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
void Appearances::FontFaceBox_GotFocus(const Windows::Foundation::IInspectable& sender, const RoutedEventArgs&) void Appearances::FontFaceBox_GotFocus(const Windows::Foundation::IInspectable& sender, const RoutedEventArgs&)
{ {
const auto box = sender.as<AutoSuggestBox>();
_updateFontNameFilter({}); _updateFontNameFilter({});
sender.as<AutoSuggestBox>().IsSuggestionListOpen(true); box.IsSuggestionListOpen(true);
_fontFaceBoxHasUserInput = false;
} }
void Appearances::FontFaceBox_LostFocus(const IInspectable& sender, const RoutedEventArgs&) void Appearances::FontFaceBox_LostFocus(const IInspectable& sender, const RoutedEventArgs&)
{ {
_updateFontName(sender.as<AutoSuggestBox>().Text()); const auto box = sender.as<AutoSuggestBox>();
if (_fontFaceBoxHasUserInput)
{
_updateFontName(box.Text());
}
else
{
// AutoSuggestBox restores its cached user query when Tab closes the suggestion list.
// Programmatic Text updates don't synchronize that cache, so restore the committed value.
box.Text(Appearance().FontFace());
}
} }
void Appearances::FontFaceBox_QuerySubmitted(const AutoSuggestBox& sender, const AutoSuggestBoxQuerySubmittedEventArgs& args) void Appearances::FontFaceBox_QuerySubmitted(const AutoSuggestBox& sender, const AutoSuggestBoxQuerySubmittedEventArgs& args)
@@ -1237,6 +1249,8 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
Dispatcher().RunAsync(CoreDispatcherPriority::Normal, [weakThis{ get_weak() }, weakSender{ winrt::make_weak(sender) }, fontSpec{ std::move(fontSpec) }]() { Dispatcher().RunAsync(CoreDispatcherPriority::Normal, [weakThis{ get_weak() }, weakSender{ winrt::make_weak(sender) }, fontSpec{ std::move(fontSpec) }]() {
if (const auto self{ weakThis.get() }) if (const auto self{ weakThis.get() })
{ {
self->_fontFaceBoxHasUserInput = false;
if (const auto box{ weakSender.get() }) if (const auto box{ weakSender.get() })
{ {
box.Text(fontSpec); box.Text(fontSpec);
@@ -1255,6 +1269,8 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
return; return;
} }
_fontFaceBoxHasUserInput = true;
const auto fontSpec = sender.Text(); const auto fontSpec = sender.Text();
std::wstring_view filter{ fontSpec }; std::wstring_view filter{ fontSpec };

View File

@@ -237,6 +237,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
Windows::Foundation::Collections::IObservableVector<winrt::hstring> _FontAxesNames; Windows::Foundation::Collections::IObservableVector<winrt::hstring> _FontAxesNames;
Windows::Foundation::Collections::IObservableVector<winrt::hstring> _FontFeaturesNames; Windows::Foundation::Collections::IObservableVector<winrt::hstring> _FontFeaturesNames;
std::wstring _fontNameFilter; std::wstring _fontNameFilter;
bool _fontFaceBoxHasUserInput = false;
bool _ShowAllFonts = false; bool _ShowAllFonts = false;
static void _ViewModelChanged(const Windows::UI::Xaml::DependencyObject& d, const Windows::UI::Xaml::DependencyPropertyChangedEventArgs& e); static void _ViewModelChanged(const Windows::UI::Xaml::DependencyObject& d, const Windows::UI::Xaml::DependencyPropertyChangedEventArgs& e);