-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer-profile.ts
More file actions
64 lines (55 loc) · 1.9 KB
/
Copy pathpointer-profile.ts
File metadata and controls
64 lines (55 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { MAX_POINTER_DELTA, type PointerMovementProfile } from '../../shared/protocol';
type Point = { x: number; y: number };
type Bounds = { x: number; y: number; width: number; height: number };
const TARGET_NATIVE_DELTAS = {
small: 48,
medium: 128,
large: 280
};
export function createPointerMovementProfile(input: {
cursor: Point;
display: {
bounds: Bounds;
scaleFactor: number;
};
maxDelta?: number;
}): PointerMovementProfile {
const bounds = normalizeBounds(input.display.bounds);
const scaleFactor =
Number.isFinite(input.display.scaleFactor) && input.display.scaleFactor > 0 ? input.display.scaleFactor : 1;
const maxDelta = input.maxDelta ?? MAX_POINTER_DELTA;
return {
displayId: `${bounds.x}:${bounds.y}:${bounds.width}:${bounds.height}:${scaleFactor}`,
scaleFactor,
bounds,
maxDelta,
recommendedDeltas: {
small: toLogicalDelta(TARGET_NATIVE_DELTAS.small, scaleFactor, maxDelta),
medium: toLogicalDelta(TARGET_NATIVE_DELTAS.medium, scaleFactor, maxDelta),
large: toLogicalDelta(TARGET_NATIVE_DELTAS.large, scaleFactor, maxDelta)
},
capabilities: {
noAckMouseMove: true
}
};
}
function normalizeBounds(bounds: Bounds): Bounds {
return {
x: finiteOr(bounds.x, 0),
y: finiteOr(bounds.y, 0),
width: positiveFiniteOr(bounds.width, 1),
height: positiveFiniteOr(bounds.height, 1)
};
}
function finiteOr(value: number, fallback: number): number {
return Number.isFinite(value) ? value : fallback;
}
function positiveFiniteOr(value: number, fallback: number): number {
return Number.isFinite(value) && value > 0 ? value : fallback;
}
function clamp(value: number, min: number, max: number): number {
return Math.min(max, Math.max(min, value));
}
function toLogicalDelta(nativePixels: number, scaleFactor: number, maxDelta: number): number {
return clamp(Math.round(nativePixels / scaleFactor), 1, maxDelta);
}