January 29th, 2021

Autoscrolling on drag, part 5: Adding wiggle-to-scroll to escape velocity

Last time, we implemented autoscroll speed based on the mouse’s velocity. But our implementation didn’t support the popular bug-that-is-now-a-feature where wiggling the mouse makes the window scroll faster. Let’s bring that back.

void OnMouseMove(HWND hwnd, int x, int y, UINT keyFlags)
{
    if (g_fDragging) {
        if (g_dyAutoScroll == 0) {
            int direction = DetectAutoScroll({ x, y });
            if (direction) {
                DWORD tmTimer = GetDoubleClickTime() / 5;
                auto [distance, time] = GetMouseVelocity(hwnd, { x, y });
                if (time != 0) {
                    g_dyAutoScroll = MulDiv(distance, tmTimer, time);
                } else {
                    g_dyAutoScroll = 0;
                }
                if (g_dyAutoScroll > -g_cyLine && g_dyAutoScroll < g_cyLine) {
                    g_dyAutoScroll = direction * g_cyLine;
                }
                SetTimer(hwnd, IDT_AUTOSCROLL, tmTimer, OnAutoScroll);
            }
        } else {
            HandleDragMouseMove(hwnd, { x, y });
        }
    }
}

In the case where the mouse moved when autoscroll was already active (g_dyAutoScroll is nonzero), we let Handle­Drag­Mouse­Move do the work of processing the mouse movement. Recall that that function checks whether the mouse is in a place that activates autoscroll. If so, then it triggers an autoscroll immediately; otherwise, it cancels autoscroll.

And there you have it, scrolling faster by wiggling the mouse. The bug that’s now a feature.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Piotr Siódmak

    Now make moving the cursor over progress bars speed up loading 🙂