Skip to content

Commit 536e823

Browse files
feat: Add scroll sensitivity configuration and improved wheel event handling (#242)
- Implement scroll sensitivity settings with low, default, and high modes - Add RPC methods for getting and setting scroll sensitivity - Enhance wheel event handling with device-specific sensitivity and clamping - Create a new device settings store for managing scroll and trackpad parameters - Update mouse settings route to include scroll sensitivity selection
1 parent 3b83f4c commit 536e823

File tree

6 files changed

+193
-30
lines changed

6 files changed

+193
-30
lines changed

jsonrpc.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,17 @@ func rpcSetCloudUrl(apiUrl string, appUrl string) error {
762762
return nil
763763
}
764764

765+
var currentScrollSensitivity string = "default"
766+
767+
func rpcGetScrollSensitivity() (string, error) {
768+
return currentScrollSensitivity, nil
769+
}
770+
771+
func rpcSetScrollSensitivity(sensitivity string) error {
772+
currentScrollSensitivity = sensitivity
773+
return nil
774+
}
775+
765776
var rpcHandlers = map[string]RPCHandler{
766777
"ping": {Func: rpcPing},
767778
"getDeviceID": {Func: rpcGetDeviceID},
@@ -821,4 +832,6 @@ var rpcHandlers = map[string]RPCHandler{
821832
"getSerialSettings": {Func: rpcGetSerialSettings},
822833
"setSerialSettings": {Func: rpcSetSerialSettings, Params: []string{"settings"}},
823834
"setCloudUrl": {Func: rpcSetCloudUrl, Params: []string{"apiUrl", "appUrl"}},
835+
"getScrollSensitivity": {Func: rpcGetScrollSensitivity},
836+
"setScrollSensitivity": {Func: rpcSetScrollSensitivity, Params: []string{"sensitivity"}},
824837
}

ui/src/components/WebRTCVideo.tsx

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useCallback, useEffect, useRef, useState } from "react";
22
import {
3+
useDeviceSettingsStore,
34
useHidStore,
45
useMouseStore,
56
useRTCStore,
@@ -144,33 +145,51 @@ export default function WebRTCVideo() {
144145
[sendMouseMovement, videoClientHeight, videoClientWidth, videoWidth, videoHeight],
145146
);
146147

148+
const trackpadSensitivity = useDeviceSettingsStore(state => state.trackpadSensitivity);
149+
const mouseSensitivity = useDeviceSettingsStore(state => state.mouseSensitivity);
150+
const clampMin = useDeviceSettingsStore(state => state.clampMin);
151+
const clampMax = useDeviceSettingsStore(state => state.clampMax);
152+
const blockDelay = useDeviceSettingsStore(state => state.blockDelay);
153+
const trackpadThreshold = useDeviceSettingsStore(state => state.trackpadThreshold);
154+
147155
const mouseWheelHandler = useCallback(
148156
(e: WheelEvent) => {
149157
if (blockWheelEvent) return;
150-
e.preventDefault();
151158

152-
// Define a scaling factor to adjust scrolling sensitivity
153-
const scrollSensitivity = 0.8; // Adjust this value to change scroll speed
159+
// Determine if the wheel event is from a trackpad or a mouse wheel
160+
const isTrackpad = Math.abs(e.deltaY) < trackpadThreshold;
161+
162+
// Apply appropriate sensitivity based on input device
163+
const scrollSensitivity = isTrackpad ? trackpadSensitivity : mouseSensitivity;
154164

155165
// Calculate the scroll value
156166
const scroll = e.deltaY * scrollSensitivity;
157167

158-
// Clamp the scroll value to a reasonable range (e.g., -15 to 15)
159-
const clampedScroll = Math.max(-4, Math.min(4, scroll));
168+
// Apply clamping
169+
const clampedScroll = Math.max(clampMin, Math.min(clampMax, scroll));
160170

161171
// Round to the nearest integer
162172
const roundedScroll = Math.round(clampedScroll);
163173

164174
// Invert the scroll value to match expected behavior
165175
const invertedScroll = -roundedScroll;
166176

167-
console.log("wheelReport", { wheelY: invertedScroll });
168177
send("wheelReport", { wheelY: invertedScroll });
169178

179+
// Apply blocking delay
170180
setBlockWheelEvent(true);
171-
setTimeout(() => setBlockWheelEvent(false), 50);
181+
setTimeout(() => setBlockWheelEvent(false), blockDelay);
172182
},
173-
[blockWheelEvent, send],
183+
[
184+
blockDelay,
185+
blockWheelEvent,
186+
clampMax,
187+
clampMin,
188+
mouseSensitivity,
189+
send,
190+
trackpadSensitivity,
191+
trackpadThreshold,
192+
],
174193
);
175194

176195
const resetMousePosition = useCallback(() => {
@@ -356,7 +375,10 @@ export default function WebRTCVideo() {
356375
videoElmRefValue.addEventListener("pointerdown", mouseMoveHandler, { signal });
357376
videoElmRefValue.addEventListener("pointerup", mouseMoveHandler, { signal });
358377
videoElmRefValue.addEventListener("keyup", videoKeyUpHandler, { signal });
359-
videoElmRefValue.addEventListener("wheel", mouseWheelHandler, { signal });
378+
videoElmRefValue.addEventListener("wheel", mouseWheelHandler, {
379+
signal,
380+
passive: true,
381+
});
360382
videoElmRefValue.addEventListener(
361383
"contextmenu",
362384
(e: MouseEvent) => e.preventDefault(),

ui/src/hooks/stores.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,78 @@ export const useSettingsStore = create(
306306
),
307307
);
308308

309+
export interface DeviceSettingsState {
310+
trackpadSensitivity: number;
311+
mouseSensitivity: number;
312+
clampMin: number;
313+
clampMax: number;
314+
blockDelay: number;
315+
trackpadThreshold: number;
316+
scrollSensitivity: "low" | "default" | "high";
317+
setScrollSensitivity: (sensitivity: DeviceSettingsState["scrollSensitivity"]) => void;
318+
}
319+
320+
export const useDeviceSettingsStore = create<DeviceSettingsState>(set => ({
321+
trackpadSensitivity: 3.0,
322+
mouseSensitivity: 5.0,
323+
clampMin: -8,
324+
clampMax: 8,
325+
blockDelay: 25,
326+
trackpadThreshold: 10,
327+
328+
scrollSensitivity: "default",
329+
setScrollSensitivity: sensitivity => {
330+
const wheelSettings: Record<
331+
DeviceSettingsState["scrollSensitivity"],
332+
{
333+
trackpadSensitivity: DeviceSettingsState["trackpadSensitivity"];
334+
mouseSensitivity: DeviceSettingsState["mouseSensitivity"];
335+
clampMin: DeviceSettingsState["clampMin"];
336+
clampMax: DeviceSettingsState["clampMax"];
337+
blockDelay: DeviceSettingsState["blockDelay"];
338+
trackpadThreshold: DeviceSettingsState["trackpadThreshold"];
339+
}
340+
> = {
341+
low: {
342+
trackpadSensitivity: 2.0,
343+
mouseSensitivity: 3.0,
344+
clampMin: -6,
345+
clampMax: 6,
346+
blockDelay: 30,
347+
trackpadThreshold: 10,
348+
},
349+
default: {
350+
trackpadSensitivity: 3.0,
351+
mouseSensitivity: 5.0,
352+
clampMin: -8,
353+
clampMax: 8,
354+
blockDelay: 25,
355+
trackpadThreshold: 10,
356+
},
357+
high: {
358+
trackpadSensitivity: 4.0,
359+
mouseSensitivity: 6.0,
360+
clampMin: -9,
361+
clampMax: 9,
362+
blockDelay: 20,
363+
trackpadThreshold: 10,
364+
},
365+
};
366+
367+
const settings = wheelSettings[sensitivity];
368+
369+
return set({
370+
trackpadSensitivity: settings.trackpadSensitivity,
371+
trackpadThreshold: settings.trackpadThreshold,
372+
mouseSensitivity: settings.mouseSensitivity,
373+
clampMin: settings.clampMin,
374+
clampMax: settings.clampMax,
375+
blockDelay: settings.blockDelay,
376+
scrollSensitivity: sensitivity,
377+
});
378+
},
379+
}));
380+
309381
export interface RemoteVirtualMediaState {
310382
source: "WebRTC" | "HTTP" | "Storage" | null;
311383
mode: "CDROM" | "Disk" | null;

ui/src/routes/devices.$id.settings.mouse.tsx

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@ import { Checkbox } from "@/components/Checkbox";
44
import { GridCard } from "@/components/Card";
55
import PointingFinger from "@/assets/pointing-finger.svg";
66
import { CheckCircleIcon } from "@heroicons/react/16/solid";
7-
import { useSettingsStore } from "@/hooks/stores";
7+
import { useDeviceSettingsStore, useSettingsStore } from "@/hooks/stores";
88
import notifications from "@/notifications";
9-
import { useEffect, useState } from "react";
9+
import { useCallback, useEffect, useState } from "react";
1010
import { useJsonRpc } from "@/hooks/useJsonRpc";
1111
import { cx } from "../cva.config";
12+
import { SelectMenuBasic } from "../components/SelectMenuBasic";
13+
14+
type ScrollSensitivity = "low" | "default" | "high";
1215

1316
export default function SettingsKeyboardMouseRoute() {
1417
const hideCursor = useSettingsStore(state => state.isCursorHidden);
1518
const setHideCursor = useSettingsStore(state => state.setCursorVisibility);
19+
const scrollSensitivity = useDeviceSettingsStore(state => state.scrollSensitivity);
20+
const setScrollSensitivity = useDeviceSettingsStore(
21+
state => state.setScrollSensitivity,
22+
);
1623

1724
const [jiggler, setJiggler] = useState(false);
1825

@@ -23,7 +30,12 @@ export default function SettingsKeyboardMouseRoute() {
2330
if ("error" in resp) return;
2431
setJiggler(resp.result as boolean);
2532
});
26-
}, [send]);
33+
34+
send("getScrollSensitivity", {}, resp => {
35+
if ("error" in resp) return;
36+
setScrollSensitivity(resp.result as ScrollSensitivity);
37+
});
38+
}, [send, setScrollSensitivity]);
2739

2840
const handleJigglerChange = (enabled: boolean) => {
2941
send("setJigglerState", { enabled }, resp => {
@@ -37,6 +49,22 @@ export default function SettingsKeyboardMouseRoute() {
3749
});
3850
};
3951

52+
const onScrollSensitivityChange = useCallback(
53+
(e: React.ChangeEvent<HTMLSelectElement>) => {
54+
const sensitivity = e.target.value as ScrollSensitivity;
55+
send("setScrollSensitivity", { sensitivity }, resp => {
56+
if ("error" in resp) {
57+
notifications.error(
58+
`Failed to set scroll sensitivity: ${resp.error.data || "Unknown error"}`,
59+
);
60+
}
61+
notifications.success("Scroll sensitivity set successfully");
62+
setScrollSensitivity(sensitivity);
63+
});
64+
},
65+
[send, setScrollSensitivity],
66+
);
67+
4068
return (
4169
<div className="space-y-4">
4270
<SettingsPageHeader
@@ -54,6 +82,26 @@ export default function SettingsKeyboardMouseRoute() {
5482
onChange={e => setHideCursor(e.target.checked)}
5583
/>
5684
</SettingsItem>
85+
<SettingsItem
86+
title="Scroll Sensitivity"
87+
description="Adjust the scroll sensitivity"
88+
>
89+
<SelectMenuBasic
90+
size="SM"
91+
label=""
92+
fullWidth
93+
value={scrollSensitivity}
94+
onChange={onScrollSensitivityChange}
95+
options={
96+
[
97+
{ label: "Low", value: "low" },
98+
{ label: "Default", value: "default" },
99+
{ label: "High", value: "high" },
100+
] as { label: string; value: ScrollSensitivity }[]
101+
}
102+
/>
103+
</SettingsItem>
104+
57105
<SettingsItem
58106
title="Jiggler"
59107
description="Simulate movement of a computer mouse. Prevents sleep mode, standby mode or the screensaver from activating"

ui/src/routes/devices.$id.tsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { useCallback, useEffect, useRef, useState } from "react";
22
import { cx } from "@/cva.config";
33
import {
4+
DeviceSettingsState,
45
HidState,
56
UpdateState,
7+
useDeviceSettingsStore,
68
useDeviceStore,
79
useHidStore,
810
useMountMediaStore,
@@ -428,18 +430,6 @@ export default function KvmIdRoute() {
428430
}
429431
}, [kvmTerminal, peerConnection, serialConsole]);
430432

431-
useEffect(() => {
432-
kvmTerminal?.addEventListener("message", e => {
433-
console.log(e.data);
434-
});
435-
436-
return () => {
437-
kvmTerminal?.removeEventListener("message", e => {
438-
console.log(e.data);
439-
});
440-
};
441-
}, [kvmTerminal]);
442-
443433
const outlet = useOutlet();
444434
const location = useLocation();
445435
const onModalClose = useCallback(() => {
@@ -464,6 +454,21 @@ export default function KvmIdRoute() {
464454
});
465455
}, [appVersion, send, setAppVersion, setSystemVersion]);
466456

457+
const setScrollSensitivity = useDeviceSettingsStore(
458+
state => state.setScrollSensitivity,
459+
);
460+
461+
// Initialize device settings
462+
useEffect(
463+
function initializeDeviceSettings() {
464+
send("getScrollSensitivity", {}, resp => {
465+
if ("error" in resp) return;
466+
setScrollSensitivity(resp.result as DeviceSettingsState["scrollSensitivity"]);
467+
});
468+
},
469+
[send, setScrollSensitivity],
470+
);
471+
467472
return (
468473
<FeatureFlagProvider appVersion={appVersion}>
469474
{!outlet && otaState.updating && (

usb.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,20 +332,23 @@ func rpcWheelReport(wheelY int8) error {
332332
return errors.New("hid not initialized")
333333
}
334334

335-
// Accumulate the wheelY value
336-
accumulatedWheelY += float64(wheelY) / 8.0
335+
// Accumulate the wheelY value with finer granularity
336+
// Reduce divisor from 8.0 to a smaller value (e.g., 2.0 or 4.0)
337+
accumulatedWheelY += float64(wheelY) / 4.0
337338

338-
// Only send a report if the accumulated value is significant
339-
if abs(accumulatedWheelY) >= 1.0 {
340-
scaledWheelY := int8(accumulatedWheelY)
339+
// Lower the threshold for sending a report (0.25 instead of 1.0)
340+
if abs(accumulatedWheelY) >= 0.25 {
341+
// Scale the wheel value appropriately for the HID report
342+
// The descriptor uses an 8-bit signed value (-127 to 127)
343+
scaledWheelY := int8(accumulatedWheelY * 0.5) // Scale down to prevent too much scrolling
341344

342345
_, err := mouseHidFile.Write([]byte{
343346
2, // Report ID 2
344347
byte(scaledWheelY), // Scaled Wheel Y (signed)
345348
})
346349

347350
// Reset the accumulator, keeping any remainder
348-
accumulatedWheelY -= float64(scaledWheelY)
351+
accumulatedWheelY -= float64(scaledWheelY) / 0.5 // Adjust based on the scaling factor
349352

350353
resetUserInputTime()
351354
return err

0 commit comments

Comments
 (0)