Repository: microsoft/terminal
File: src/cascadia/QueryExtension/ExtensionPalette.cpp (C++)

Code before:
            {
                concatenatedMessages += chatMessage.as<ChatMessage>()->MessageContent();
                concatenatedMessages += L"\n";
            }
        }
        if (!concatenatedMessages.empty())
        {
            _ExportChatHistoryRequestedHandlers(*this, concatenatedMessages);
        }
    }

    // Method Description:
    // - This event is triggered when someone clicks anywhere in the bounds of
    //   the window that's _not_ the query palette UI. When that happens,
    //   we'll want to dismiss the palette.
    // Arguments:
    // - <unused>
    // Return Value:
    // - <none>
    void ExtensionPalette::_rootPointerPressed(const Windows::Foundation::IInspectable& /*sender*/,
                                               const Windows::UI::Xaml::Input::PointerRoutedEventArgs& /*e*/)
    {
        if (Visibility() != Visibility::Collapsed)
        {
            _close();
        }
    }

    void ExtensionPalette::_backdropPointerPressed(const Windows::Foundation::IInspectable& /*sender*/,
                                                   const Windows::UI::Xaml::Input::PointerRoutedEventArgs& e)
    {
        e.Handled(true);
    }

    // Method Description:
    // - The purpose of this event handler is to hide the palette if it loses focus.
    // We say we lost focus if our root element and all its descendants lost focus.
    // This handler is invoked when our root element or some descendant loses focus.
    // At this point we need to learn if the newly focused element belongs to this palette.
    // To achieve this:
    // - We start with the newly focused element and traverse its visual ancestors up to the Xaml root.
    // - If one of the ancestors is this ExtensionPalette, then by our definition the focus is not lost
    // - If we reach the Xaml root without meeting this ExtensionPalette,
    // then the focus is not contained in it anymore and it should be dismissed
    // Arguments:
    // - <unused>
    // Return Value:
    // - <none>
    void ExtensionPalette::_lostFocusHandler(const Windows::Foundation::IInspectable& /*sender*/,
                                             const Windows::UI::Xaml::RoutedEventArgs& /*args*/)
    {

Review comment (suggestion):
this design is not ideal - it effectively *assumes* that the recipient of this event will populate `ActiveCommandline`, via some "behind the scenes magic".

I'm not asking that you change it, but more FYI. "I send out a random event, and then check my own data" is a VEEEERY shaky implicit contract.

Code after:
                concatenatedMessages += L"\n";
            }
        }
        if (!concatenatedMessages.empty())
        {
            _ExportChatHistoryRequestedHandlers(*this, concatenatedMessages);
        }
    }

    void ExtensionPalette::_closeChat(const Windows::Foundation::IInspectable& /*sender*/,
                                      const Windows::UI::Xaml::RoutedEventArgs& /*args*/)
    {
        Visibility(Visibility::Collapsed);
    }

    void ExtensionPalette::_backdropPointerPressed(const Windows::Foundation::IInspectable& /*sender*/,
                                                   const Windows::UI::Xaml::Input::PointerRoutedEventArgs& e)
    {
        _setFocusAndPlaceholderTextHelper();
        e.Handled(true);
    }

    void ExtensionPalette::_queryBoxGotFocusHandler(const Windows::Foundation::IInspectable& /*sender*/,
                                                    const Windows::UI::Xaml::RoutedEventArgs& /*args*/)
    {
        _ActiveControlInfoRequestedHandlers(nullptr, nullptr);
        const auto context = winrt::make<TerminalContext>(_ActiveCommandline);
        _lmProvider.SetContext(std::move(context));
    }

    void ExtensionPalette::_previewKeyDownHandler(const IInspectable& /*sender*/,
                                                  const Windows::UI::Xaml::Input::KeyRoutedEventArgs& e)
    {
        const auto key = e.OriginalKey();
        const auto coreWindow = CoreWindow::GetForCurrentThread();
        const auto ctrlDown = WI_IsFlagSet(coreWindow.GetKeyState(winrt::Windows::System::VirtualKey::Control), CoreVirtualKeyStates::Down);
        const auto shiftDown = WI_IsFlagSet(coreWindow.GetKeyState(winrt::Windows::System::VirtualKey::Shift), CoreVirtualKeyStates::Down);

        if (key == VirtualKey::Escape)
        {
            // Dismiss the palette if the text is empty
            if (_queryBox().Text().empty())
            {
                _closeChat(nullptr, nullptr);
            }

            e.Handled(true);
        }
        else if (key == VirtualKey::Enter && !shiftDown)
        {
            if (const auto& textBox = e.OriginalSource().try_as<TextBox>())