Fix: Make the Go web console wheel scroll correctly
The Go web server's mouse handler read deltaY as an int, so json.Unmarshal rejected the entire mouse message whenever the browser reported a fractional delta (Chrome's smooth scrolling splits one 100px notch into 33.33 + 33.33 + 33.34), and the wheel was dead while clicks and moves kept working. Treating every event as one notch was wrong the other way: it scrolled three to four times too far, and sub-pixel deltas from a touchpad truncated to zero. The server now accumulates the delta in notch units per client and injects WM_MOUSEWHEEL only once a whole notch is reached. The remainder is rounded, not truncated, so a float sum of 99.99px still lands on exactly one notch and the first notch fires halfway through a physical one, like native scrolling. The page forwards e.deltaMode so line and page based deltas convert correctly, and touchpad flings are capped at 20 notches per event. Verified against a live Go server with a connected client: 100px sends one packet, three 33.33px events send one, 53.33px sends one, sub-pixel events accumulate to none, and the wParam is -120 down / +120 up with SendToScreen returning no error. The MFC web console is untouched. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
c271911fcb
commit
6236ed1eb4
4 files changed
+156
-17
No files matched your search
@@ -3,6 +3,7 @@ package web
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -803,11 +804,12 @@ func (h *wsHub) handleMouse(c *wsClient, raw []byte) {
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
Type string `json:"type"`
|
||||
X int32 `json:"x"`
|
||||
Y int32 `json:"y"`
|
||||
Button int `json:"button"`
|
||||
Delta int `json:"delta"`
|
||||
Type string `json:"type"`
|
||||
X int32 `json:"x"`
|
||||
Y int32 `json:"y"`
|
||||
Button int `json:"button"`
|
||||
Delta float64 `json:"delta"`
|
||||
DeltaMode int `json:"deltaMode"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &in); err != nil {
|
||||
return
|
||||
@@ -845,17 +847,13 @@ func (h *wsHub) handleMouse(c *wsClient, raw []byte) {
|
||||
message = protocol.WMMouseMove
|
||||
case "wheel":
|
||||
message = protocol.WMMouseWheel
|
||||
// Windows expects ±120 per notch in HIWORD(wParam). Browsers report
|
||||
// `deltaY` in pixels with sign flipped relative to Win32 conventions
|
||||
// (positive deltaY = scroll down). Normalize to one notch per call,
|
||||
// signed so up scrolls "up" on the remote.
|
||||
var wheel int16
|
||||
switch {
|
||||
case in.Delta > 0:
|
||||
wheel = -120
|
||||
case in.Delta < 0:
|
||||
wheel = 120
|
||||
// Windows expects ±120 per notch in HIWORD(wParam); wheelNotches turns the
|
||||
// browser's distance into whole notches (see its doc comment).
|
||||
notches := wheelNotches(&c.wheelResidue, in.Delta, in.DeltaMode)
|
||||
if notches == 0 {
|
||||
return // less than a notch so far: keep it for the next event
|
||||
}
|
||||
wheel := int16(-notches * 120) // positive deltaY = scroll down
|
||||
wParam = uint64(uint16(wheel)) << 16
|
||||
case "dblclick":
|
||||
// Windows synthesizes double-click from rapid down/up sequences, so
|
||||
@@ -872,6 +870,47 @@ func (h *wsHub) handleMouse(c *wsClient, raw []byte) {
|
||||
_ = h.devices.SendToScreen(deviceID, pkt)
|
||||
}
|
||||
|
||||
// maxWheelNotches caps how many wheel notches one browser event may inject.
|
||||
// A touchpad fling or a scrollbar drag can report a huge delta; without the cap
|
||||
// the remote jumps pages at a time.
|
||||
const maxWheelNotches = 20
|
||||
|
||||
// wheelNotches converts one browser wheel event into whole Windows wheel
|
||||
// notches, carrying the sub-notch remainder in *residue.
|
||||
//
|
||||
// The browser reports a *distance*, not a notch count: deltaMode 0 = pixels
|
||||
// (Chrome's notch is about 100px), 1 = lines (about 3 per notch), 2 = pages.
|
||||
// Chrome's smooth scrolling splits one physical notch into several events with
|
||||
// fractional deltas (33.33 + 33.33 + 33.34) and touchpads go well below one
|
||||
// pixel per event, so events have to be accumulated: reading the delta as an
|
||||
// int dropped every fractional event (the wheel did nothing at all), and
|
||||
// treating every event as one notch scrolled three to four times too far.
|
||||
//
|
||||
// Rounding, not truncation: the split events do not sum to exactly 1.0 in
|
||||
// float (33.33*3 = 99.99), so an integer cast would sit at 0.999 forever. With
|
||||
// rounding the first notch fires halfway through a physical one, which is how
|
||||
// native scrolling feels, and *residue always stays within ±0.5 — it is a
|
||||
// positional carry, not a bank of credit — so a change of direction needs no
|
||||
// special case.
|
||||
func wheelNotches(residue *float64, delta float64, deltaMode int) int {
|
||||
unit := 100.0
|
||||
switch deltaMode {
|
||||
case 1:
|
||||
unit = 3
|
||||
case 2:
|
||||
unit = 1
|
||||
}
|
||||
*residue += delta / unit
|
||||
notches := int(math.Round(*residue))
|
||||
*residue -= float64(notches)
|
||||
if notches > maxWheelNotches {
|
||||
notches = maxWheelNotches
|
||||
} else if notches < -maxWheelNotches {
|
||||
notches = -maxWheelNotches
|
||||
}
|
||||
return notches
|
||||
}
|
||||
|
||||
// handleKey forwards one keyboard event to the device. lParam is built to
|
||||
// match the Windows convention so applications relying on TranslateMessage
|
||||
// (e.g. text input fields) behave correctly. Mirrors
|
||||
|
||||
Reference in new issue
Block a user