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

Code before:
            }
        }

        constexpr std::array<KeyModifier, 3> modalities{ {
            { VirtualKey::CapitalLock, ControlKeyStates::CapslockOn },
            { VirtualKey::NumberKeyLock, ControlKeyStates::NumlockOn },
            { VirtualKey::Scroll, ControlKeyStates::ScrolllockOn },
        } };

        for (const auto& mod : modalities)
        {
            const auto state = window.GetKeyState(mod.vkey);
            const auto isLocked = WI_IsFlagSet(state, CoreVirtualKeyStates::Locked);

            if (isLocked)
            {
                flags |= mod.flags;
            }
        }

        return flags;
    }

    til::point TermControl::_toControlOrigin(const til::point& terminalPos)
    {
        const til::size fontSize{ til::math::flooring, CharacterDimensions() };

        // Convert text buffer cursor position to client coordinate position
        // within the window. This point is in _pixels_
        const til::point clientCursorPos{ terminalPos * fontSize };

        // Get scale factor for view
        const double scaleFactor = SwapChainPanel().CompositionScaleX();

        const til::point clientCursorInDips{ til::math::flooring, clientCursorPos.x / scaleFactor, clientCursorPos.y / scaleFactor };

        auto padding{ GetPadding() };
        til::point relativeToOrigin{ til::math::flooring,
                                     clientCursorInDips.x + padding.Left,
                                     clientCursorInDips.y + padding.Top };
        return relativeToOrigin;
    }

    // Method Description:
    // - Gets the corresponding viewport pixel position for the cursor
    //    by excluding the padding.
    // Arguments:
    // - cursorPosition: the (x,y) position of a given cursor (i.e.: mouse cursor).
    //    NOTE: origin (0,0) is top-left.
    // Return Value:
    // - the corresponding viewport terminal position (in pixels) for the given Point parameter

Review comment (bug):
This results in rounding bugs at fractional display scalings, because `CharacterDimensions()` is in DIPs (at least according to its description).

For code based in DIPs I'd avoid using `til::size` and `til::point` entirely and just do the math with 2 regular floats (since the `til` structs are integers). And at the final step, I wouldn't use `flooring` but rather `rounding`, because `flooring` rounds down even if its `.9999999987f` or something.

Code after:
        ControlKeyStates flags;

        for (const auto& mod : modifiers)
        {
            const auto state = window.GetKeyState(mod.vkey);
            const auto isDown = WI_IsFlagSet(state, CoreVirtualKeyStates::Down);

            if (isDown)
            {
                flags |= mod.flags;
            }
        }

        constexpr std::array<KeyModifier, 3> modalities{ {
            { VirtualKey::CapitalLock, ControlKeyStates::CapslockOn },
            { VirtualKey::NumberKeyLock, ControlKeyStates::NumlockOn },
            { VirtualKey::Scroll, ControlKeyStates::ScrolllockOn },
        } };

        for (const auto& mod : modalities)
        {
            const auto state = window.GetKeyState(mod.vkey);
            const auto isLocked = WI_IsFlagSet(state, CoreVirtualKeyStates::Locked);

            if (isLocked)
            {
                flags |= mod.flags;
            }
        }

        return flags;
    }

    til::point TermControl::_toControlOrigin(const til::point terminalPos)
    {
        const auto fontSize{ CharacterDimensions() };

        // Convert text buffer cursor position to client coordinate position
        // within the window. This point is in _pixels_
        const double clientCursorPosX = terminalPos.x * fontSize.Width;
        const double clientCursorPosY = terminalPos.y * fontSize.Height;

        // Get scale factor for view
        const double scaleFactor = SwapChainPanel().CompositionScaleX();

        const double clientCursorInDipsX = clientCursorPosX / scaleFactor;
        const double clientCursorInDipsY = clientCursorPosY / scaleFactor;

        auto padding{ GetPadding() };
        til::point relativeToOrigin{ til::math::rounding,
                                     clientCursorInDipsX + padding.Left,