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
@@ -0,0 +1,93 @@
|
||||
package web
|
||||
|
||||
import "testing"
|
||||
|
||||
// Chrome's smooth scrolling splits one physical notch (100px) into a few
|
||||
// fractional wheel events. Each event must not become a notch on its own, and
|
||||
// the whole physical notch must inject exactly one.
|
||||
func TestWheelNotchesSmoothScrollSequence(t *testing.T) {
|
||||
var residue float64
|
||||
seq := []float64{33.33, 33.33, 33.34}
|
||||
total := 0
|
||||
for _, d := range seq {
|
||||
total += abs(wheelNotches(&residue, d, 0))
|
||||
}
|
||||
if total != 1 {
|
||||
t.Fatalf("one 100px notch in %d events gave %d notches, want 1", len(seq), total)
|
||||
}
|
||||
if residue > 0.5 || residue < -0.5 {
|
||||
t.Fatalf("residue must stay within half a notch, got %v", residue)
|
||||
}
|
||||
}
|
||||
|
||||
// Touchpads report sub-pixel deltas: they used to truncate to 0 and the wheel
|
||||
// did nothing at all. They must accumulate into one notch.
|
||||
func TestWheelNotchesSubPixelDeltas(t *testing.T) {
|
||||
var residue float64
|
||||
got := 0
|
||||
for i := 0; i < 167; i++ { // 167 events x 0.6px = 100.2px
|
||||
got += wheelNotches(&residue, 0.6, 0)
|
||||
}
|
||||
if got != 1 {
|
||||
t.Fatalf("167 x 0.6px gave %d notches, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
// The same split in three equal parts must also land on exactly one notch:
|
||||
// 33.33*3 is 99.99 in float, and truncating would never reach a whole notch.
|
||||
func TestWheelNotchesEqualThirds(t *testing.T) {
|
||||
var residue float64
|
||||
got := 0
|
||||
for i := 0; i < 3; i++ {
|
||||
got += wheelNotches(&residue, 33.33, 0)
|
||||
}
|
||||
if got != 1 {
|
||||
t.Fatalf("3 x 33.33px gave %d notches, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWheelNotchesDirection(t *testing.T) {
|
||||
var residue float64
|
||||
if n := wheelNotches(&residue, 100, 0); n != 1 {
|
||||
t.Fatalf("down: got %d notches, want 1", n)
|
||||
}
|
||||
if n := wheelNotches(&residue, -100, 0); n != -1 {
|
||||
t.Fatalf("up: got %d notches, want -1", n)
|
||||
}
|
||||
// A half-scrolled notch does not block a reversal: the carry is a position,
|
||||
// so 40px down then a full notch up is one notch up.
|
||||
residue = 0.4
|
||||
if n := wheelNotches(&residue, -100, 0); n != -1 {
|
||||
t.Fatalf("flip: got %d notches, want -1", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWheelNotchesDeltaModes(t *testing.T) {
|
||||
var pixel, line, page float64
|
||||
if n := wheelNotches(&pixel, 100, 0); n != 1 {
|
||||
t.Fatalf("pixel mode: got %d, want 1", n)
|
||||
}
|
||||
if n := wheelNotches(&line, 3, 1); n != 1 {
|
||||
t.Fatalf("line mode: got %d, want 1", n)
|
||||
}
|
||||
if n := wheelNotches(&page, 1, 2); n != 1 {
|
||||
t.Fatalf("page mode: got %d, want 1", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWheelNotchesClamp(t *testing.T) {
|
||||
var residue float64
|
||||
if n := wheelNotches(&residue, 100000, 0); n != maxWheelNotches {
|
||||
t.Fatalf("huge delta gave %d notches, want %d", n, maxWheelNotches)
|
||||
}
|
||||
if n := wheelNotches(&residue, -100000, 0); n != -maxWheelNotches {
|
||||
t.Fatalf("huge negative delta gave %d notches, want %d", n, -maxWheelNotches)
|
||||
}
|
||||
}
|
||||
|
||||
func abs(n int) int {
|
||||
if n < 0 {
|
||||
return -n
|
||||
}
|
||||
return n
|
||||
}
|
||||
Reference in new issue
Block a user