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

Code before:
    {
        // if there is no exit timer, create one
        if (!_pointerExitTimer)
        {
            _pointerExitTimer = winrt::Windows::UI::Xaml::DispatcherTimer();
            _pointerExitTimer.Interval(std::chrono::milliseconds(10));
            _pointerExitTimer.Tick([this](auto const&, auto const&) {
                // when the timer ticks, revert the preview to the selected command
                const auto selectedCommand = _filteredActionsView().SelectedItem();
                if (const auto filteredCommand{ selectedCommand.try_as<winrt::TerminalApp::FilteredCommand>() })
                {
                    if (_currentMode == CommandPaletteMode::ActionMode && filteredCommand)
                    {
                        if (const auto actionPaletteItem{ filteredCommand.Item().try_as<winrt::TerminalApp::ActionPaletteItem>() })
                        {
                            PreviewAction.raise(*this, actionPaletteItem.Command());
                        }
                    }
                }
                _pointerExitTimer.Stop();
            });
        }

        // restart the timer
        _pointerExitTimer.Stop();
        _pointerExitTimer.Start();
    }

    void CommandPalette::_listItemSelectionChanged(const Windows::Foundation::IInspectable& /*sender*/, const Windows::UI::Xaml::Controls::SelectionChangedEventArgs& e)
    {
        // We don't care about...
        // - CommandlineMode: it doesn't have any selectable items in the list view
        // - TabSwitchMode: focus and selected item are in sync
        if (_currentMode == CommandPaletteMode::ActionMode || _currentMode == CommandPaletteMode::TabSearchMode)
        {
            if (auto automationPeer{ Automation::Peers::FrameworkElementAutomationPeer::FromElement(_searchBox()) })
            {
                if (const auto selectedList = e.AddedItems(); selectedList.Size() > 0)
                {
                    const auto selectedCommand = selectedList.GetAt(0);
                    if (const auto filteredCmd = selectedCommand.try_as<TerminalApp::FilteredCommand>())
                    {
                        if (const auto paletteItem = filteredCmd.Item().try_as<TerminalApp::PaletteItem>())
                        {
                            automationPeer.RaiseNotificationEvent(
                                Automation::Peers::AutomationNotificationKind::ItemAdded,
                                Automation::Peers::AutomationNotificationProcessing::MostRecent,
                                paletteItem.Name() + L" " + paletteItem.KeyChordText(),
                                L"CommandPaletteSelectedItemChanged" /* unique name for this notification category */);
                        }
                    }

Review comment (suggestion):
FWIW I believe `Start` already resets the timer: https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.dispatchertimer.start

Code after:
    {
        // if there is no exit timer, create one
        if (!_pointerExitTimer)
        {
            _pointerExitTimer = winrt::Windows::UI::Xaml::DispatcherTimer();
            _pointerExitTimer.Interval(std::chrono::milliseconds(10));
            _pointerExitTimer.Tick([this](auto const&, auto const&) {
                // when the timer ticks, revert the preview to the selected command
                const auto selectedCommand = _filteredActionsView().SelectedItem();
                if (const auto filteredCommand{ selectedCommand.try_as<winrt::TerminalApp::FilteredCommand>() })
                {
                    if (_currentMode == CommandPaletteMode::ActionMode && filteredCommand)
                    {
                        if (const auto actionPaletteItem{ filteredCommand.Item().try_as<winrt::TerminalApp::ActionPaletteItem>() })
                        {
                            PreviewAction.raise(*this, actionPaletteItem.Command());
                        }
                    }
                }
                _pointerExitTimer.Stop();
            });
        }

        // restart the timer
        _pointerExitTimer.Start();
    }

    void CommandPalette::_listItemSelectionChanged(const Windows::Foundation::IInspectable& /*sender*/, const Windows::UI::Xaml::Controls::SelectionChangedEventArgs& e)
    {
        // We don't care about...
        // - CommandlineMode: it doesn't have any selectable items in the list view
        // - TabSwitchMode: focus and selected item are in sync
        if (_currentMode == CommandPaletteMode::ActionMode || _currentMode == CommandPaletteMode::TabSearchMode)
        {
            if (auto automationPeer{ Automation::Peers::FrameworkElementAutomationPeer::FromElement(_searchBox()) })
            {
                if (const auto selectedList = e.AddedItems(); selectedList.Size() > 0)
                {
                    const auto selectedCommand = selectedList.GetAt(0);
                    if (const auto filteredCmd = selectedCommand.try_as<TerminalApp::FilteredCommand>())
                    {
                        if (const auto paletteItem = filteredCmd.Item().try_as<TerminalApp::PaletteItem>())
                        {
                            automationPeer.RaiseNotificationEvent(
                                Automation::Peers::AutomationNotificationKind::ItemAdded,
                                Automation::Peers::AutomationNotificationProcessing::MostRecent,
                                paletteItem.Name() + L" " + paletteItem.KeyChordText(),
                                L"CommandPaletteSelectedItemChanged" /* unique name for this notification category */);
                        }
                    }
                }