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

Code before:
    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();
            });

            const auto data = GetClipboardData(CF_UNICODETEXT);
            if (!data)
            {
                co_return;
            }


Review comment (bug):
I wonder if this might be more readable in this way:
```c++
// bring the break; next to the break condition
// eliminate the continue; - now only 2 ways out of the loop body
// avoid mutating in an if () test
for (auto attempt = 1;;)
{
    if (OpenClipboard(nullptr))
    {
        break;
    }

    if (attempt > 5)
    {
        co_return;
    }

    attempt++;
    Sleep(5);
}
```

The fact that we only want the thread to sleep _between_ attempts and not after the last attempt makes things hard to write as a more standard C for loop, which would otherwise be my preference...

Also why sleep 5 milliseconds in particular? Just felt right?

Code after:
    //   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())
        {
            if (const auto terminalTab{ _GetFocusedTabImpl() })
            {
                return terminalTab->CalcSnappedDimension(widthOrHeight, dimension);
            }
        }
        return dimension;