Repository: microsoft/terminal
File: src/cascadia/TerminalApp/TerminalWindow.cpp (C++)

Code before:
                }

                if (auto focusedElement{ focusedObject.try_as<Windows::UI::Xaml::FrameworkElement>() })
                {
                    focusedObject = focusedElement.Parent();

                    // Parent() seems to return null when the focusedElement is created from an ItemTemplate.
                    // Use the VisualTreeHelper's GetParent as a fallback.
                    if (!focusedObject)
                    {
                        focusedObject = winrt::Windows::UI::Xaml::Media::VisualTreeHelper::GetParent(focusedElement);

                        // We were unable to find a focused object. Default to the xaml root so that the alt+space menu still works.
                        if (!focusedObject)
                        {
                            focusedObject = _root.try_as<IInspectable>();
                        }
                    }
                }
                else
                {
                    break; // we hit a non-FE object, stop bubbling.
                }
            } while (focusedObject);
        }

        return false;
    }

    // Method Description:
    // - Used to tell the app that the 'X' button has been clicked and
    //   the user wants to close the app. We kick off the close warning
    //   experience.
    // Arguments:
    // - <none>
    // Return Value:
    // - <none>
    void TerminalWindow::CloseWindow(LaunchPosition pos, const bool isLastWindow)
    {
        if (_root)
        {
            // If persisted layout is enabled and we are the last window closing
            // we should save our state.
            if (_settings.GlobalSettings().ShouldUsePersistedLayout() && isLastWindow)
            {
                if (const auto layout = _root->GetWindowLayout())
                {
                    layout.InitialPosition(pos);
                    const auto state = ApplicationState::SharedInstance();
                    state.PersistedWindowLayouts(winrt::single_threaded_vector<WindowLayout>({ layout }));
                }

Review comment (suggestion):
I'm not sure if it would make more sense, but technically you can put this fallback outside the loop, at the end of this function. I guess it would make some sense, given that it only accesses `_root` and has nothing to do with the actual UI tree anymore.

Code after:
                }

                if (auto focusedElement{ focusedObject.try_as<Windows::UI::Xaml::FrameworkElement>() })
                {
                    focusedObject = focusedElement.Parent();

                    // Parent() seems to return null when the focusedElement is created from an ItemTemplate.
                    // Use the VisualTreeHelper's GetParent as a fallback.
                    if (!focusedObject)
                    {
                        focusedObject = winrt::Windows::UI::Xaml::Media::VisualTreeHelper::GetParent(focusedElement);

                        // We were unable to find a focused object. Give the
                        // TerminalPage one last chance to let the alt+space
                        // menu still work.
                        //
                        // We return always, because the TerminalPage handler
                        // will return false for just a bare `alt` press, and
                        // don't want to go around the loop again.
                        if (!focusedObject)
                        {
                            if (auto keyListener{ _root.try_as<IDirectKeyListener>() })
                            {
                                return keyListener.OnDirectKeyEvent(vkey, scanCode, down);
                            }
                        }
                    }
                }
                else
                {
                    break; // we hit a non-FE object, stop bubbling.
                }
            } while (focusedObject);
        }

        return false;
    }

    // Method Description:
    // - Used to tell the app that the 'X' button has been clicked and
    //   the user wants to close the app. We kick off the close warning
    //   experience.
    // Arguments:
    // - <none>
    // Return Value:
    // - <none>
    void TerminalWindow::CloseWindow(LaunchPosition pos, const bool isLastWindow)
    {
        if (_root)
        {
            // If persisted layout is enabled and we are the last window closing