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

Code before:
                }

                break;
            }

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

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

            const auto str = static_cast<const wchar_t*>(GlobalLock(data));
            if (!str)
            {
                co_return;
            }

            const auto dataCleanup = wil::scope_exit([&]() {
                GlobalUnlock(data);
            });

            text = winrt::hstring{ str, gsl::narrow_cast<uint32_t>(wcslen(str)) };
        }

        if (_settings.GlobalSettings().TrimPaste())
        {
            text = { Utils::TrimPaste(text) };
            if (text.empty())
            {
                // Text is all white space, nothing to paste
                co_return;
            }
        }

        // If the requesting terminal is in bracketed paste mode, then we don't need to warn about a multi-line paste.
        auto warnMultiLine = _settings.GlobalSettings().WarnAboutMultiLinePaste() &&
                             !eventArgs.BracketedPasteEnabled();
        if (warnMultiLine)
        {
            const auto isNewLineLambda = [](auto c) { return c == L'\n' || c == L'\r'; };
            const auto hasNewLine = std::find_if(text.cbegin(), text.cend(), isNewLineLambda) != text.cend();
            warnMultiLine = hasNewLine;
        }

        constexpr const std::size_t minimumSizeForWarning = 1024 * 5; // 5 KiB
        const auto warnLargeText = text.size() > minimumSizeForWarning &&
                                   _settings.GlobalSettings().WarnAboutLargePaste();

Review comment (refactor):
I feel like the new code is very verbose, but also very easy to follow. If it's too verbose for you I wouldn't mind abstracting away the `OpenClipboard` / `CloseClipboard` calls into a safer wrapper like `wil::open_clipboard` (which lacks the retrying logic) and something like `wil::unique_hglobal_locked` (which unfortunately is `void*` and not `wchar_t*`).

IMO it's fine this way because this is the only use of this code and I wouldn't introduce an abstraction until we got 2-3 uses of this (at which point we'll probably have a better idea how the data types should look like, especially in respect to `SetClipboardData`).

Code after:
    // - 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;
    }

    // Method Description:
    // - Place `copiedData` into the clipboard as text. Triggered when a
    //   terminal control raises its CopyToClipboard event.
    // Arguments:
    // - copiedData: the new string content to place on the clipboard.
    winrt::fire_and_forget TerminalPage::_CopyToClipboardHandler(const IInspectable /*sender*/,
                                                                 const CopyToClipboardEventArgs copiedData)
    {
        co_await wil::resume_foreground(Dispatcher(), CoreDispatcherPriority::High);

        auto dataPack = DataPackage();
        dataPack.RequestedOperation(DataPackageOperation::Copy);

        // The EventArgs.Formats() is an override for the global setting "copyFormatting"
        //   iff it is set
        auto useGlobal = copiedData.Formats() == nullptr;
        auto copyFormats = useGlobal ?
                               _settings.GlobalSettings().CopyFormatting() :
                               copiedData.Formats().Value();

        // copy text to dataPack
        dataPack.SetText(copiedData.Text());

        if (WI_IsFlagSet(copyFormats, CopyFormat::HTML))
        {
            // copy html to dataPack
            const auto htmlData = copiedData.Html();
            if (!htmlData.empty())
            {
                dataPack.SetHtmlFormat(htmlData);
            }
        }

        if (WI_IsFlagSet(copyFormats, CopyFormat::RTF))