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

Code before:
    //   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)
                    {
                        co_return;
                    }

                    Sleep(5);
                    continue;
                }

                break;
            }

            const auto clipboardCleanup = wil::scope_exit([]() {
                CloseClipboard();
            });


Review comment (performance):
Question: I know you said you tested this, but I'm still surprised. Is calling UWP Clipboard.GetContent() or DataPackageView.GetWhateverAsync() really that slow? I'd be a bit interested to see how you tested and measured this.

My memory is rusty but my belief is that UWP DataTransfer for clipboard shouldn't be a cross-process RPC call in all cases - just ones where the source data is hosted in an app that clipped it using the UWP or OLE clipboard APIs, forcing a cross-process COM call (IDataObject::Whatever()). (As opposed to when the source item came from the Win32 clipboard; in that case, UWP clipboard impl. in the destination should "just" do the win32k system call.)

Code after:

        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)
            {
                menuItem.KeyboardAcceleratorTextOverride(overrideString + gsl::narrow_cast<wchar_t>(mappedCh));
            }
        }
    }

    // Method Description:
    // - Calculates the appropriate size to snap to in the given direction, for
    //   the given dimension. If the global setting `snapToGridOnResize` is set
    //   to `false`, this will just immediately return the provided dimension,
    //   effectively disabling snapping.
    // - See Pane::CalcSnappedDimension
    float TerminalPage::CalcSnappedDimension(const bool widthOrHeight, const float dimension) const
    {
        if (_settings && _settings.GlobalSettings().SnapToGridOnResize())
        {