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

Code before:
    // - <none>
    // Return Value:
    // - <none>
    void CommandPalette::_dismissPalette()
    {
        _close();

        TraceLoggingWrite(
            g_hTerminalAppProvider, // handle to TerminalApp tracelogging provider
            "CommandPaletteDismissed",
            TraceLoggingDescription("Event emitted when the user dismisses the Command Palette without selecting an action"),
            TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES),
            TelemetryPrivacyDataTag(PDT_ProductAndServiceUsage));
    }

    // Method Description:
    // - Event handler for when the text in the input box changes. In Action
    //   Mode, we'll update the list of displayed commands, and select the first one.
    // Arguments:
    // - <unused>
    // Return Value:
    // - <none>
    void CommandPalette::_filterTextChanged(const IInspectable& /*sender*/,
                                            const Windows::UI::Xaml::RoutedEventArgs& /*args*/)
    {
        // Only respond to this change if we are visible:
        // _close calls _searchBox().Text(L"") to reset the search text, which lands us
        // in here after the command palette is dismissed. Since we have a code path here that
        // could potentially lead to an action being previewed (specifically if there is a
        // preview-able action as the first entry in the command list), that preview will
        // appear after the palette is dismissed without this check.
        if (Visibility() != Visibility::Visible)
        {
            return;
        }

        // When we are executing the _SelectNextTab in the TabManagement.cpp, this method
        // is getting triggered because we set up the default value for that CommandPalette
        // with an empty string. Therefore, to avoid the reset of the index when executing
        // the Next/Prev tab command, we are skipping this execution.
        // Check issue https://github.com/microsoft/terminal/issues/11146
        if (_currentMode == CommandPaletteMode::TabSwitchMode)
        {
            return;
        }

        if (_currentMode == CommandPaletteMode::CommandlineMode)
        {
            _evaluatePrefix();
        }


Review comment (bug):
```suggestion
        // GH#19884: Only respond to this change if we are visible:
```
Really just a nice way to link up the bug since it provides more context to this fix.

Code after:
    // - <none>
    // Return Value:
    // - <none>
    void CommandPalette::_dismissPalette()
    {
        _close();

        TraceLoggingWrite(
            g_hTerminalAppProvider, // handle to TerminalApp tracelogging provider
            "CommandPaletteDismissed",
            TraceLoggingDescription("Event emitted when the user dismisses the Command Palette without selecting an action"),
            TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES),
            TelemetryPrivacyDataTag(PDT_ProductAndServiceUsage));
    }

    // Method Description:
    // - Event handler for when the text in the input box changes. In Action
    //   Mode, we'll update the list of displayed commands, and select the first one.
    // Arguments:
    // - <unused>
    // Return Value:
    // - <none>
    void CommandPalette::_filterTextChanged(const IInspectable& /*sender*/,
                                            const Windows::UI::Xaml::RoutedEventArgs& /*args*/)
    {
        // GH#18737: Only respond to this change if we are visible:
        // _close calls _searchBox().Text(L"") to reset the search text, which lands us
        // in here after the command palette is dismissed. Since we have a code path here that
        // could potentially lead to an action being previewed (specifically if there is a
        // preview-able action as the first entry in the command list), that preview will
        // appear after the palette is dismissed without this check.
        if (Visibility() != Visibility::Visible)
        {
            return;
        }

        // When we are executing the _SelectNextTab in the TabManagement.cpp, this method
        // is getting triggered because we set up the default value for that CommandPalette
        // with an empty string. Therefore, to avoid the reset of the index when executing
        // the Next/Prev tab command, we are skipping this execution.
        // Check issue https://github.com/microsoft/terminal/issues/11146
        if (_currentMode == CommandPaletteMode::TabSwitchMode)
        {
            return;
        }

        if (_currentMode == CommandPaletteMode::CommandlineMode)
        {
            _evaluatePrefix();
        }