-
Notifications
You must be signed in to change notification settings - Fork 243
/
index.ts
204 lines (169 loc) Β· 5.38 KB
/
index.ts
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {System} from '..'
import {Instance} from '../../setup'
import {Buttons} from './buttons'
import {Device} from './device'
import {Mouse} from './mouse'
import {Pointer} from './pointer'
import {pointerKey, PointerPosition} from './shared'
export type {pointerKey, PointerPosition} from './shared'
export class PointerHost {
readonly system: System
constructor(system: System) {
this.system = system
this.buttons = new Buttons()
this.mouse = new Mouse()
}
private readonly mouse
private readonly buttons
private readonly devices = new (class {
private registry = {} as Record<string, Device>
get(k: string) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
this.registry[k] ??= new Device()
return this.registry[k]
}
})()
private readonly pointers = new (class {
private registry = {
mouse: new Pointer({
pointerId: 1,
pointerType: 'mouse',
isPrimary: true,
}),
} as Record<string, Pointer>
private nextId = 2
new(pointerName: string, keyDef: pointerKey) {
const isPrimary =
keyDef.pointerType !== 'touch' ||
!Object.values(this.registry).some(
p => p.pointerType === 'touch' && !p.isCancelled,
)
if (!isPrimary) {
Object.values(this.registry).forEach(p => {
if (p.pointerType === keyDef.pointerType && !p.isCancelled) {
p.isMultitouch = true
}
})
}
this.registry[pointerName] = new Pointer({
pointerId: this.nextId++,
pointerType: keyDef.pointerType,
isPrimary,
})
return this.registry[pointerName]
}
get(pointerName: string) {
if (!this.has(pointerName)) {
throw new Error(
`Trying to access pointer "${pointerName}" which does not exist.`,
)
}
return this.registry[pointerName]
}
has(pointerName: string) {
return pointerName in this.registry
}
})()
isKeyPressed(keyDef: pointerKey) {
return this.devices.get(keyDef.pointerType).isPressed(keyDef)
}
async press(
instance: Instance,
keyDef: pointerKey,
position: PointerPosition,
) {
const pointerName = this.getPointerName(keyDef)
const pointer =
keyDef.pointerType === 'touch'
? this.pointers.new(pointerName, keyDef).init(instance, position)
: this.pointers.get(pointerName)
// TODO: deprecate the following implicit setting of position
pointer.position = position
if (pointer.pointerType !== 'touch') {
this.mouse.position = position
}
this.devices.get(keyDef.pointerType).addPressed(keyDef)
this.buttons.down(keyDef)
pointer.down(instance, keyDef)
if (pointer.pointerType !== 'touch' && !pointer.isPrevented) {
this.mouse.down(instance, keyDef, pointer)
}
}
async move(
instance: Instance,
pointerName: string,
position: PointerPosition,
) {
const pointer = this.pointers.get(pointerName)
// In (some?) browsers this order of events can be observed.
// This interweaving of events is probably unnecessary.
// While the order of mouse (or pointer) events is defined per spec,
// the order in which they interweave/follow on a user interaction depends on the implementation.
const pointermove = pointer.move(instance, position)
const mousemove =
pointer.pointerType === 'touch' || (pointer.isPrevented && pointer.isDown)
? undefined
: this.mouse.move(instance, position)
pointermove?.leave()
mousemove?.leave()
pointermove?.enter()
mousemove?.enter()
pointermove?.move()
mousemove?.move()
}
async release(
instance: Instance,
keyDef: pointerKey,
position: PointerPosition,
) {
const device = this.devices.get(keyDef.pointerType)
device.removePressed(keyDef)
this.buttons.up(keyDef)
const pointer = this.pointers.get(this.getPointerName(keyDef))
// TODO: deprecate the following implicit setting of position
pointer.position = position
if (pointer.pointerType !== 'touch') {
this.mouse.position = position
}
if (device.countPressed === 0) {
pointer.up(instance, keyDef)
}
if (pointer.pointerType === 'touch') {
pointer.release(instance)
}
if (!pointer.isPrevented) {
if (pointer.pointerType === 'touch' && !pointer.isMultitouch) {
const mousemove = this.mouse.move(instance, pointer.position)
mousemove?.leave()
mousemove?.enter()
mousemove?.move()
this.mouse.down(instance, keyDef, pointer)
}
if (!pointer.isMultitouch) {
const mousemove = this.mouse.move(instance, pointer.position)
mousemove?.leave()
mousemove?.enter()
mousemove?.move()
this.mouse.up(instance, keyDef, pointer)
}
}
}
getPointerName(keyDef: pointerKey) {
return keyDef.pointerType === 'touch' ? keyDef.name : keyDef.pointerType
}
getPreviousPosition(pointerName: string) {
return this.pointers.has(pointerName)
? this.pointers.get(pointerName).position
: undefined
}
resetClickCount() {
this.mouse.resetClickCount()
}
getMouseTarget(instance: Instance) {
return this.mouse.position.target ?? instance.config.document.body
}
setMousePosition(position: PointerPosition) {
this.mouse.position = position
this.pointers.get('mouse').position = position
}
}