From 0a4a1af80ef8969278436943dd18196a91769ac6 Mon Sep 17 00:00:00 2001 From: rmschooley Date: Mon, 19 May 2025 06:03:33 -0500 Subject: [PATCH] Improve/Simplify Mouse Wheel Scroll Behavior (#470) * Improve/Simplify Mouse Wheel Scroll Behavior * Update hid_mouse_absolute.go Attempt to fix line reported as improperly formatted by lint. * Update utils.go Removed abs() function since lint states it is no longer used. --- internal/usbgadget/hid_mouse_absolute.go | 16 ++++----------- internal/usbgadget/utils.go | 8 -------- ui/src/components/WebRTCVideo.tsx | 26 ++++++++++++------------ 3 files changed, 17 insertions(+), 33 deletions(-) diff --git a/internal/usbgadget/hid_mouse_absolute.go b/internal/usbgadget/hid_mouse_absolute.go index 6629caa..b1c7428 100644 --- a/internal/usbgadget/hid_mouse_absolute.go +++ b/internal/usbgadget/hid_mouse_absolute.go @@ -107,24 +107,16 @@ func (u *UsbGadget) AbsMouseWheelReport(wheelY int8) error { u.absMouseLock.Lock() defer u.absMouseLock.Unlock() - // Accumulate the wheelY value - u.absMouseAccumulatedWheelY += float64(wheelY) / 8.0 - - // Only send a report if the accumulated value is significant - if abs(u.absMouseAccumulatedWheelY) < 1.0 { + // Only send a report if the value is non-zero + if wheelY == 0 { return nil } - scaledWheelY := int8(u.absMouseAccumulatedWheelY) - err := u.absMouseWriteHidFile([]byte{ - 2, // Report ID 2 - byte(scaledWheelY), // Scaled Wheel Y (signed) + 2, // Report ID 2 + byte(wheelY), // Wheel Y (signed) }) - // Reset the accumulator, keeping any remainder - u.absMouseAccumulatedWheelY -= float64(scaledWheelY) - u.resetUserInputTime() return err } diff --git a/internal/usbgadget/utils.go b/internal/usbgadget/utils.go index 0e796c8..28a9e37 100644 --- a/internal/usbgadget/utils.go +++ b/internal/usbgadget/utils.go @@ -7,14 +7,6 @@ import ( "path/filepath" ) -// Helper function to get absolute value of float64 -func abs(x float64) float64 { - if x < 0 { - return -x - } - return x -} - func joinPath(basePath string, paths []string) string { pathArr := append([]string{basePath}, paths...) return filepath.Join(pathArr...) diff --git a/ui/src/components/WebRTCVideo.tsx b/ui/src/components/WebRTCVideo.tsx index 891555e..aa4a397 100644 --- a/ui/src/components/WebRTCVideo.tsx +++ b/ui/src/components/WebRTCVideo.tsx @@ -259,25 +259,25 @@ export default function WebRTCVideo() { (e: WheelEvent) => { if (blockWheelEvent) return; - // Determine if the wheel event is from a trackpad or a mouse wheel - const isTrackpad = Math.abs(e.deltaY) < trackpadThreshold; + // Determine if the wheel event is an accel scroll value + const isAccel = Math.abs(e.deltaY) >= 100; - // Apply appropriate sensitivity based on input device - const scrollSensitivity = isTrackpad ? trackpadSensitivity : mouseSensitivity; + // Calculate the accel scroll value + const accelScrollValue = e.deltaY / 100; - // Calculate the scroll value - const scroll = e.deltaY * scrollSensitivity; + // Calculate the no accel scroll value + const noAccelScrollValue = e.deltaY > 0 ? 1 : (e.deltaY < 0 ? -1 : 0); - // Apply clamping - const clampedScroll = Math.max(clampMin, Math.min(clampMax, scroll)); + // Get scroll value + const scrollValue = isAccel ? accelScrollValue : noAccelScrollValue; - // Round to the nearest integer - const roundedScroll = Math.round(clampedScroll); + // Apply clamping (i.e. min and max mouse wheel hardware value) + const clampedScrollValue = Math.max(-127, Math.min(127, scrollValue)); - // Invert the scroll value to match expected behavior - const invertedScroll = -roundedScroll; + // Invert the clamped scroll value to match expected behavior + const invertedScrollValue = -clampedScrollValue; - send("wheelReport", { wheelY: invertedScroll }); + send("wheelReport", { wheelY : invertedScrollValue }); // Apply blocking delay setBlockWheelEvent(true);