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

Code before:
            Clipboard::Flush();
        }
        CATCH_LOG();
    }

// The current UCRT has a neat AVX2 implementation for all of the various strlen() functions.
// This greatly benefits _PasteFromClipboardHandler, where we're calling wcslen while holding a global
// lock onto the clipboard. MSVC treats wcslen as a builtin and so we need to disable it explicitly.
#pragma function(wcslen)

    // Function Description:
    // - This function is called when the `TermControl` requests that we send
    //   it the clipboard's content.
    // - Retrieves the data from the Windows Clipboard and converts it to text.
    // - Shows warnings if the clipboard is too big or contains multiple lines
    //   of text.
    // - Sends the text back to the TermControl through the event's
    //   `HandleClipboardData` member function.
    // - Does some of this in a background thread, as to not hang/crash the UI thread.
    // Arguments:
    // - eventArgs: the PasteFromClipboard event sent from the TermControl
    fire_and_forget TerminalPage::_PasteFromClipboardHandler(const IInspectable /*sender*/,
                                                             const PasteFromClipboardEventArgs eventArgs)
    try
    {
        // Counterintuitively pasting the clipboard should block the UI thread, because that's
        // actually the expectation of users: If you type something, paste and then type something again,
        // text should be written in exactly that order, no matter how loaded the CPU is. See GH#15315.
        assert(Dispatcher().HasThreadAccess());

        if (!IsClipboardFormatAvailable(CF_UNICODETEXT))
        {
            co_return;
        }

        // The Win32 clipboard API allows us to synchronously retrieve the clipboard on the UI thread
        // which is exactly what we want (see above). Additionally...
        //
        // The old Win32 clipboard API as used below is somewhere in the order of 1000x faster than the WinRT one,
        // which has about the same latency as a fibre optics connection across the entire WA state.
        // Don't use the WinRT clipboard API. RPC calls are not funny.
        winrt::hstring text;
        {
            // According to various reports on the internet, OpenClipboard might
            // fail to acquire the internal lock over RDP, due to rdpclip.exe.
            // I haven't seen this happen yet myself however.
            for (int attempts = 0;;)
            {
                if (!OpenClipboard(nullptr))
                {
                    if (++attempts > 5)

Review comment (bug):
Update the function comment which still says "Does some of this in a background thread, as to not hang/crash the UI thread."

Code after:

        if (WI_IsFlagSet(modifiers, VirtualKeyModifiers::Shift))
        {
            buffer += L"Shift+";
        }

        if (WI_IsFlagSet(modifiers, VirtualKeyModifiers::Menu))
        {
            buffer += L"Alt+";
        }

        if (WI_IsFlagSet(modifiers, VirtualKeyModifiers::Windows))
        {
            buffer += L"Win+";
        }

        return buffer;
    }

    // Method Description:
    // - Takes a MenuFlyoutItem and a corresponding KeyChord value and creates the accelerator for UI display.
    //   Takes into account a special case for an error condition for a comma
    // Arguments:
    // - MenuFlyoutItem that will be displayed, and a KeyChord to map an accelerator
    void TerminalPage::_SetAcceleratorForMenuItem(WUX::Controls::MenuFlyoutItem& menuItem,
                                                  const KeyChord& keyChord)
    {
#ifdef DEP_MICROSOFT_UI_XAML_708_FIXED
        // work around https://github.com/microsoft/microsoft-ui-xaml/issues/708 in case of VK_OEM_COMMA
        if (keyChord.Vkey() != VK_OEM_COMMA)
        {
            // use the XAML shortcut to give us the automatic capabilities
            auto menuShortcut = Windows::UI::Xaml::Input::KeyboardAccelerator{};

            // TODO: Modify this when https://github.com/microsoft/terminal/issues/877 is resolved
            menuShortcut.Key(static_cast<Windows::System::VirtualKey>(keyChord.Vkey()));

            // add the modifiers to the shortcut
            menuShortcut.Modifiers(keyChord.Modifiers());

            // add to the menu
            menuItem.KeyboardAccelerators().Append(menuShortcut);
        }
        else // we've got a comma, so need to just use the alternate method
#endif
        {
            // extract the modifier and key to a nice format
            auto overrideString = _FormatOverrideShortcutText(keyChord.Modifiers());
            auto mappedCh = MapVirtualKeyW(keyChord.Vkey(), MAPVK_VK_TO_CHAR);
            if (mappedCh != 0)
            {