Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: PointerEvent button and buttons set to incorrect values; fixes #1083 #1219

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/system/pointer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class PointerHost {
const pointerName = this.getPointerName(keyDef)
const pointer =
keyDef.pointerType === 'touch'
? this.pointers.new(pointerName, keyDef).init(instance, position)
? this.pointers.new(pointerName, keyDef).init(instance, position, keyDef)
: this.pointers.get(pointerName)

// TODO: deprecate the following implicit setting of position
Expand Down
31 changes: 23 additions & 8 deletions src/system/pointer/pointer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {type Instance} from '../../setup'
import {assertPointerEvents, getTreeDiff, hasPointerEvents} from '../../utils'
import {isDifferentPointerPosition, pointerKey, PointerPosition} from './shared'
import {Buttons, getMouseButtonId, getMouseEventButton, MouseButton} from './buttons'

type PointerInit = {
pointerId: number
Expand All @@ -23,10 +24,14 @@ export class Pointer {
isCancelled: boolean = false
isDown: boolean = false
isPrevented: boolean = false
private buttons: number = 0

position: PointerPosition = {}

init(instance: Instance, position: PointerPosition) {
init(instance: Instance, position: PointerPosition, keyDef: pointerKey) {
if(keyDef.pointerType === 'touch') {
this.buttons = 1
}
this.position = position

const target = this.getTarget(instance)
Expand All @@ -53,7 +58,7 @@ export class Pointer {

const nextTarget = this.getTarget(instance)

const init = this.getEventInit()
const init = this.getEventInit(-1)

const [leave, enter] = getTreeDiff(prevTarget, nextTarget)

Expand Down Expand Up @@ -84,10 +89,15 @@ export class Pointer {
}
}

down(instance: Instance, _keyDef: pointerKey) {
down(instance: Instance, keyDef: pointerKey) {
if (this.isDown) {
return
}
if(keyDef.pointerType === 'touch') {
this.buttons = 1
} else {
this.buttons = 2 ** getMouseButtonId(keyDef.button)
}
const target = this.getTarget(instance)

assertPointerEvents(instance, target)
Expand All @@ -96,26 +106,27 @@ export class Pointer {
this.isPrevented = !instance.dispatchUIEvent(
target,
'pointerdown',
this.getEventInit(),
this.getEventInit(keyDef.button),
)
}

up(instance: Instance, _keyDef: pointerKey) {
up(instance: Instance, keyDef: pointerKey) {
if (!this.isDown) {
return
}
this.buttons = 0
const target = this.getTarget(instance)

assertPointerEvents(instance, target)

this.isDown = false
instance.dispatchUIEvent(target, 'pointerup', this.getEventInit())
instance.dispatchUIEvent(target, 'pointerup', this.getEventInit(keyDef.button))
}

release(instance: Instance) {
const target = this.getTarget(instance)
const [leave] = getTreeDiff(target, null)
const init = this.getEventInit()
const init = this.getEventInit(this.pointerType === 'touch' ? 0 : -1)

// Currently there is no PointerEventsCheckLevel that would
// make this check not use the *asserted* cached value from `up`.
Expand All @@ -132,12 +143,16 @@ export class Pointer {
return this.position.target ?? instance.config.document.body
}

private getEventInit(): PointerEventInit {
private getEventInit(
button?: MouseButton
): PointerEventInit {
return {
...this.position.coords,
pointerId: this.pointerId,
pointerType: this.pointerType,
isPrimary: this.isPrimary,
button: getMouseEventButton(button),
buttons: this.buttons,
}
}
}
2 changes: 1 addition & 1 deletion tests/_helpers/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function addListeners(
isMouseEvent(e)
? `${e.type} - button=${e.button}; buttons=${e.buttons}; detail=${e.detail}`
: isPointerEvent(e)
? `${e.type} - pointerId=${e.pointerId}; pointerType=${e.pointerType}; isPrimary=${e.isPrimary}`
? `${e.type} - pointerId=${e.pointerId}; pointerType=${e.pointerType}; isPrimary=${e.isPrimary}; button=${e.button}; buttons=${e.buttons}`
: e.type,
)
return {snapshot: lines.join('\n')}
Expand Down
70 changes: 35 additions & 35 deletions tests/pointer/click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ test('click element', async () => {
await user.pointer({keys: '[MouseLeft]', target: element})

expect(getClickEventsSnapshot()).toMatchInlineSnapshot(`
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=1
mousedown - button=0; buttons=1; detail=1
pointerup - pointerId=1; pointerType=mouse; isPrimary=true
pointerup - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=0
mouseup - button=0; buttons=0; detail=1
click - button=0; buttons=0; detail=1
`)
Expand All @@ -19,7 +19,7 @@ test('secondary button triggers contextmenu', async () => {
await user.pointer({keys: '[MouseRight>]', target: element})

expect(getClickEventsSnapshot()).toMatchInlineSnapshot(`
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true; button=2; buttons=2
mousedown - button=2; buttons=2; detail=1
contextmenu - button=2; buttons=2; detail=0
`)
Expand All @@ -33,14 +33,14 @@ test('double click', async () => {
await user.pointer({keys: '[MouseLeft][MouseLeft]', target: element})

expect(getClickEventsSnapshot()).toMatchInlineSnapshot(`
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=1
mousedown - button=0; buttons=1; detail=1
pointerup - pointerId=1; pointerType=mouse; isPrimary=true
pointerup - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=0
mouseup - button=0; buttons=0; detail=1
click - button=0; buttons=0; detail=1
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=1
mousedown - button=0; buttons=1; detail=2
pointerup - pointerId=1; pointerType=mouse; isPrimary=true
pointerup - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=0
mouseup - button=0; buttons=0; detail=2
click - button=0; buttons=0; detail=2
dblclick - button=0; buttons=0; detail=2
Expand All @@ -65,14 +65,14 @@ test('two clicks', async () => {
await user.pointer({keys: '[MouseLeft]'})

expect(getClickEventsSnapshot()).toMatchInlineSnapshot(`
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=1
mousedown - button=0; buttons=1; detail=1
pointerup - pointerId=1; pointerType=mouse; isPrimary=true
pointerup - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=0
mouseup - button=0; buttons=0; detail=1
click - button=0; buttons=0; detail=1
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=1
mousedown - button=0; buttons=1; detail=1
pointerup - pointerId=1; pointerType=mouse; isPrimary=true
pointerup - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=0
mouseup - button=0; buttons=0; detail=1
click - button=0; buttons=0; detail=1
`)
Expand All @@ -93,22 +93,22 @@ test('other keys reset click counter', async () => {
})

expect(getClickEventsSnapshot()).toMatchInlineSnapshot(`
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=1
mousedown - button=0; buttons=1; detail=1
pointerup - pointerId=1; pointerType=mouse; isPrimary=true
pointerup - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=0
mouseup - button=0; buttons=0; detail=1
click - button=0; buttons=0; detail=1
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=1
mousedown - button=0; buttons=1; detail=2
mousedown - button=2; buttons=3; detail=1
contextmenu - button=2; buttons=3; detail=0
mouseup - button=2; buttons=1; detail=1
auxclick - button=2; buttons=1; detail=1
pointerup - pointerId=1; pointerType=mouse; isPrimary=true
pointerup - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=0
mouseup - button=0; buttons=0; detail=0
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=1
mousedown - button=0; buttons=1; detail=1
pointerup - pointerId=1; pointerType=mouse; isPrimary=true
pointerup - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=0
mouseup - button=0; buttons=0; detail=1
click - button=0; buttons=0; detail=1
`)
Expand All @@ -124,12 +124,12 @@ test('click per touch device', async () => {
await user.pointer({keys: '[TouchA]', target: element})

expect(getClickEventsSnapshot()).toMatchInlineSnapshot(`
pointerover - pointerId=2; pointerType=touch; isPrimary=true
pointerenter - pointerId=2; pointerType=touch; isPrimary=true
pointerdown - pointerId=2; pointerType=touch; isPrimary=true
pointerup - pointerId=2; pointerType=touch; isPrimary=true
pointerout - pointerId=2; pointerType=touch; isPrimary=true
pointerleave - pointerId=2; pointerType=touch; isPrimary=true
pointerover - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=1
pointerenter - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=1
pointerdown - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=1
pointerup - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=0
pointerout - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=0
pointerleave - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=0
mouseover - button=0; buttons=0; detail=0
mouseenter - button=0; buttons=0; detail=0
mousemove - button=0; buttons=0; detail=0
Expand All @@ -150,24 +150,24 @@ test('double click per touch device', async () => {
await user.pointer({keys: '[TouchA][TouchA]', target: element})

expect(getClickEventsSnapshot()).toMatchInlineSnapshot(`
pointerover - pointerId=2; pointerType=touch; isPrimary=true
pointerenter - pointerId=2; pointerType=touch; isPrimary=true
pointerdown - pointerId=2; pointerType=touch; isPrimary=true
pointerup - pointerId=2; pointerType=touch; isPrimary=true
pointerout - pointerId=2; pointerType=touch; isPrimary=true
pointerleave - pointerId=2; pointerType=touch; isPrimary=true
pointerover - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=1
pointerenter - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=1
pointerdown - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=1
pointerup - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=0
pointerout - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=0
pointerleave - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=0
mouseover - button=0; buttons=0; detail=0
mouseenter - button=0; buttons=0; detail=0
mousemove - button=0; buttons=0; detail=0
mousedown - button=0; buttons=1; detail=1
mouseup - button=0; buttons=0; detail=1
click - button=0; buttons=0; detail=1
pointerover - pointerId=3; pointerType=touch; isPrimary=true
pointerenter - pointerId=3; pointerType=touch; isPrimary=true
pointerdown - pointerId=3; pointerType=touch; isPrimary=true
pointerup - pointerId=3; pointerType=touch; isPrimary=true
pointerout - pointerId=3; pointerType=touch; isPrimary=true
pointerleave - pointerId=3; pointerType=touch; isPrimary=true
pointerover - pointerId=3; pointerType=touch; isPrimary=true; button=0; buttons=1
pointerenter - pointerId=3; pointerType=touch; isPrimary=true; button=0; buttons=1
pointerdown - pointerId=3; pointerType=touch; isPrimary=true; button=0; buttons=1
pointerup - pointerId=3; pointerType=touch; isPrimary=true; button=0; buttons=0
pointerout - pointerId=3; pointerType=touch; isPrimary=true; button=0; buttons=0
pointerleave - pointerId=3; pointerType=touch; isPrimary=true; button=0; buttons=0
mousedown - button=0; buttons=1; detail=2
mouseup - button=0; buttons=0; detail=2
click - button=0; buttons=0; detail=2
Expand Down
20 changes: 10 additions & 10 deletions tests/pointer/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ test('drag sequence', async () => {
])

expect(getClickEventsSnapshot()).toMatchInlineSnapshot(`
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true
pointerdown - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=1
mousedown - button=0; buttons=1; detail=1
pointermove - pointerId=1; pointerType=mouse; isPrimary=true
pointermove - pointerId=1; pointerType=mouse; isPrimary=true; button=-1; buttons=1
mousemove - button=0; buttons=1; detail=0
pointerup - pointerId=1; pointerType=mouse; isPrimary=true
pointerup - pointerId=1; pointerType=mouse; isPrimary=true; button=0; buttons=0
mouseup - button=0; buttons=0; detail=1
click - button=0; buttons=0; detail=1
`)
Expand All @@ -30,13 +30,13 @@ test('drag touch', async () => {
])

expect(getClickEventsSnapshot()).toMatchInlineSnapshot(`
pointerover - pointerId=2; pointerType=touch; isPrimary=true
pointerenter - pointerId=2; pointerType=touch; isPrimary=true
pointerdown - pointerId=2; pointerType=touch; isPrimary=true
pointermove - pointerId=2; pointerType=touch; isPrimary=true
pointerup - pointerId=2; pointerType=touch; isPrimary=true
pointerout - pointerId=2; pointerType=touch; isPrimary=true
pointerleave - pointerId=2; pointerType=touch; isPrimary=true
pointerover - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=1
pointerenter - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=1
pointerdown - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=1
pointermove - pointerId=2; pointerType=touch; isPrimary=true; button=-1; buttons=1
pointerup - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=0
pointerout - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=0
pointerleave - pointerId=2; pointerType=touch; isPrimary=true; button=0; buttons=0
mouseover - button=0; buttons=0; detail=0
mouseenter - button=0; buttons=0; detail=0
mousemove - button=0; buttons=0; detail=0
Expand Down