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

Code before:
        int viewTop = ::base::saturated_cast<int>(::std::round(_internalScrollbarPosition));
        if (viewTop != _core->ScrollOffset())
        {
            _core->UserScrollViewport(viewTop);

            // _core->ScrollOffset() is now set to newValue
            _ScrollPositionChangedHandlers(*this,
                                           winrt::make<ScrollPositionChangedArgs>(_core->ScrollOffset(),
                                                                                  _core->ViewHeight(),
                                                                                  _core->BufferHeight()));
        }
    }

    // Method Description:
    // - Event handler for the core's ScrollPositionChanged event. This is
    //   called when the core changes its viewport position, due to more text
    //   being output. We'll use this event to update our own internal scrollbar
    //   tracker to the position the viewport is at now.
    // Arguments:
    // - args: args containing infor about the position of the viewport in the buffer.
    // Return Value:
    // - <none>
    void ControlInteractivity::_coreScrollPositionChanged(const IInspectable& /*sender*/,
                                                          const Control::ScrollPositionChangedArgs& args)
    {
        _internalScrollbarPosition = args.ViewTop();
    }

    void ControlInteractivity::_hyperlinkHandler(const std::wstring_view uri)
    {
        _OpenHyperlinkHandlers(*this, winrt::make<OpenHyperlinkEventArgs>(winrt::hstring{ uri }));
    }

    bool ControlInteractivity::_canSendVTMouseInput(const ::Microsoft::Terminal::Core::ControlKeyStates modifiers)
    {
        // If the user is holding down Shift, suppress mouse events
        // TODO GH#4875: disable/customize this functionality
        if (modifiers.IsShiftPressed())
        {
            return false;
        }
        return _core->IsVtMouseModeEnabled();
    }

    // Method Description:
    // - Sets selection's end position to match supplied cursor position, e.g. while mouse dragging.
    // Arguments:
    // - cursorPosition: in pixels, relative to the origin of the control
    void ControlInteractivity::SetEndSelectionPoint(const til::point pixelPosition)
    {
        _core->SetEndSelectionPoint(_getTerminalPosition(pixelPosition));

Review comment (suggestion):
Just making sure: this fires **every single time** a line is printed to the terminal for the first 9001 lines. Is adding another event recipient worth it? Is there a way you can do this without having a double version of the viewport top every time it changes? :/

Code after:
        // Set this as the new value of our internal scrollbar representation.
        // We're doing this so we can accumulate fractional amounts of a row to
        // scroll each time the mouse scrolls.
        _internalScrollbarPosition = std::clamp<double>(newValue, 0.0, _core->BufferHeight());

        // If the new scrollbar position, rounded to an int, is at a different
        // row, then actually update the scroll position in the core, and raise
        // a ScrollPositionChanged to inform the control.
        int viewTop = ::base::saturated_cast<int>(::std::round(_internalScrollbarPosition));
        if (viewTop != _core->ScrollOffset())
        {
            _core->UserScrollViewport(viewTop);

            // _core->ScrollOffset() is now set to newValue
            _ScrollPositionChangedHandlers(*this,
                                           winrt::make<ScrollPositionChangedArgs>(_core->ScrollOffset(),
                                                                                  _core->ViewHeight(),
                                                                                  _core->BufferHeight()));
        }
    }

    void ControlInteractivity::_hyperlinkHandler(const std::wstring_view uri)
    {
        _OpenHyperlinkHandlers(*this, winrt::make<OpenHyperlinkEventArgs>(winrt::hstring{ uri }));
    }

    bool ControlInteractivity::_canSendVTMouseInput(const ::Microsoft::Terminal::Core::ControlKeyStates modifiers)
    {
        // If the user is holding down Shift, suppress mouse events
        // TODO GH#4875: disable/customize this functionality
        if (modifiers.IsShiftPressed())
        {
            return false;
        }
        return _core->IsVtMouseModeEnabled();
    }

    // Method Description:
    // - Sets selection's end position to match supplied cursor position, e.g. while mouse dragging.
    // Arguments:
    // - cursorPosition: in pixels, relative to the origin of the control
    void ControlInteractivity::SetEndSelectionPoint(const til::point pixelPosition)
    {
        _core->SetEndSelectionPoint(_getTerminalPosition(pixelPosition));
        _selectionNeedsToBeCopied = true;
    }

    // Method Description:
    // - Gets the corresponding viewport terminal position for the point in
    //   pixels, by normalizing with the font size.
    // Arguments: