Skip to content

Commit 9fa8d38

Browse files
committed
Protect more places against not having a XamlRoot (#19753)
We're seeing some crashes where we call `GetFocusedElement(null)`. MSFT-54686865 MSFT-53824167 Possibly also #19751 (cherry picked from commit 0ee3cfb) Service-Card-Id: PVTI_lADOAF3p4s4AxadtzgjzNUI Service-Version: 1.23
1 parent 25075a4 commit 9fa8d38

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/cascadia/TerminalApp/TerminalWindow.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,11 @@ namespace winrt::TerminalApp::implementation
888888
{
889889
// Manually bubble the OnDirectKeyEvent event up through the focus tree.
890890
auto xamlRoot{ _root->XamlRoot() };
891+
if (!xamlRoot)
892+
{
893+
return false;
894+
}
895+
891896
auto focusedObject{ Windows::UI::Xaml::Input::FocusManager::GetFocusedElement(xamlRoot) };
892897
do
893898
{

src/cascadia/TerminalControl/SearchBoxControl.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,14 @@ namespace winrt::Microsoft::Terminal::Control::implementation
330330
// - bool: whether the current focus is on the search box
331331
bool SearchBoxControl::ContainsFocus()
332332
{
333-
auto focusedElement = Input::FocusManager::GetFocusedElement(this->XamlRoot());
334-
if (_focusableElements.count(focusedElement) > 0)
333+
// BODGY: It is possible for this to get called with no XAML root. We are unsure why.
334+
if (auto root = XamlRoot())
335335
{
336-
return true;
336+
auto focusedElement = Input::FocusManager::GetFocusedElement(root);
337+
if (_focusableElements.count(focusedElement) > 0)
338+
{
339+
return true;
340+
}
337341
}
338342

339343
return false;

src/cascadia/UIHelpers/TextMenuFlyout.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,13 @@ namespace winrt::Microsoft::Terminal::UI::implementation
162162
IInspectable target = Target();
163163
if (!target)
164164
{
165-
target = FocusManager::GetFocusedElement(XamlRoot());
165+
auto root = XamlRoot();
166+
if (!root)
167+
{
168+
return;
169+
}
170+
target = FocusManager::GetFocusedElement(root);
171+
166172
if (!target)
167173
{
168174
return;

0 commit comments

Comments
 (0)