Repository: microsoft/terminal
File: src/cascadia/TerminalControl/TermControl.cpp (C++)

Code before:
    {
        // formats = nullptr -> copy all formats
        _interactivity.CopySelectionToClipboard(false, nullptr);
        ContextMenu().Hide();
        SelectionContextMenu().Hide();
    }
    void TermControl::_SearchCommandHandler(const IInspectable& /*sender*/,
                                            const IInspectable& /*args*/)
    {
        ContextMenu().Hide();
        SelectionContextMenu().Hide();

        // CreateSearchBoxControl will actually create the search box and
        // pre-populate the box with the currently selected text.
        CreateSearchBoxControl();
    }

    void TermControl::_SelectCommandHandler(const IInspectable& /*sender*/,
                                            const IInspectable& /*args*/)
    {
        ContextMenu().Hide();
        SelectionContextMenu().Hide();
        _core.ContextMenuSelectCommand();
    }

    void TermControl::_SelectOutputHandler(const IInspectable& /*sender*/,
                                           const IInspectable& /*args*/)
    {
        ContextMenu().Hide();
        SelectionContextMenu().Hide();
        _core.ContextMenuSelectOutput();
    }

    // Should the text cursor be displayed, even when the control isn't focused?
    // n.b. "blur" is the opposite of "focus".
    bool TermControl::_displayCursorWhileBlurred() const noexcept
    {
        return CursorVisibility() == Control::CursorDisplayState::Shown;
    }
    Control::CursorDisplayState TermControl::CursorVisibility() const noexcept
    {
        return _cursorVisibility;
    }
    void TermControl::CursorVisibility(Control::CursorDisplayState cursorVisibility)
    {
        _cursorVisibility = cursorVisibility;
        if (!_initializedTerminal)
        {
            return;
        }


Review comment (suggestion):
This will need a weak-ref to `this` so that we can safely check if the class instance went away while we switched to the UI thread. Like this (you can change the code style / variable names to whatever you prefer): https://github.com/microsoft/terminal/blob/1511d2c2ad4220721afb3f4689d9f1d2895c0bd7/src/cascadia/TerminalControl/TermControl.cpp#L1010-L1012

Code after:
        // draw the button in the gutter
        const auto& quickFixBtnPosInDips = _toPosInDips({ 0, _quickFixBufferPos });
        Controls::Canvas::SetLeft(quickFixBtn, -termPadding.Left);
        Controls::Canvas::SetTop(quickFixBtn, quickFixBtnPosInDips.y - termPadding.Top);
        quickFixBtn.Visibility(Visibility::Visible);

        if (auto automationPeer{ FrameworkElementAutomationPeer::FromElement(*this) })
        {
            automationPeer.RaiseNotificationEvent(
                AutomationNotificationKind::ItemAdded,
                AutomationNotificationProcessing::ImportantMostRecent,
                RS_(L"QuickFixAvailable"),
                L"QuickFixAvailableAnnouncement" /* unique name for this group of notifications */);
        }
    }

    void TermControl::_bubbleSearchMissingCommand(const IInspectable& /*sender*/, const Control::SearchMissingCommandEventArgs& args)
    {
        _quickFixBufferPos = args.BufferRow();
        SearchMissingCommand.raise(*this, args);
    }

    winrt::fire_and_forget TermControl::_bubbleWindowSizeChanged(const IInspectable& /*sender*/, Control::WindowSizeChangedEventArgs args)
    {
        auto weakThis{ get_weak() };
        co_await wil::resume_foreground(Dispatcher());

        if (auto control{ weakThis.get() })
        {
            winrt::Windows::Foundation::Size cellCount{ static_cast<float>(args.Width()), static_cast<float>(args.Height()) };
            const auto pixelSize = GetNewDimensions(cellCount);

            WindowSizeChanged.raise(*this, winrt::make<implementation::WindowSizeChangedEventArgs>(static_cast<int32_t>(pixelSize.Width), static_cast<int32_t>(pixelSize.Height)));
        }
    }

    til::CoordType TermControl::_calculateSearchScrollOffset() const
    {
        auto result = 0;
        if (_searchBox)
        {
            const auto displayInfo = DisplayInformation::GetForCurrentView();
            const auto scaleFactor = _core.FontSize().Height / displayInfo.RawPixelsPerViewPixel();
            const auto searchBoxRows = _searchBox->ActualHeight() / scaleFactor;
            result = static_cast<int32_t>(std::ceil(searchBoxRows));
        }
        return result;
    }

    void TermControl::ClearQuickFix()
    {