Skip to content

Commit

Permalink
fix: infer last pressed button in mouse move (#10067)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf-2 authored Apr 24, 2023
1 parent 23b6ae8 commit a6eaac4
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions packages/puppeteer-core/src/common/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ export const MouseButton = Object.freeze({
Middle: 'middle',
Back: 'back',
Forward: 'forward',
});
}) satisfies Record<string, Protocol.Input.MouseButton>;

/**
* @public
Expand Down Expand Up @@ -425,8 +425,35 @@ const getFlag = (button: MouseButton): MouseButtonFlag => {
}
};

/**
* This should match
* https://source.chromium.org/chromium/chromium/src/+/refs/heads/main:content/browser/renderer_host/input/web_input_event_builders_mac.mm;drc=a61b95c63b0b75c1cfe872d9c8cdf927c226046e;bpv=1;bpt=1;l=221.
*/
const getButtonFromPressedButtons = (
buttons: number
): Protocol.Input.MouseButton => {
if (buttons & MouseButtonFlag.Left) {
return MouseButton.Left;
} else if (buttons & MouseButtonFlag.Right) {
return MouseButton.Right;
} else if (buttons & MouseButtonFlag.Middle) {
return MouseButton.Middle;
} else if (buttons & MouseButtonFlag.Back) {
return MouseButton.Back;
} else if (buttons & MouseButtonFlag.Forward) {
return MouseButton.Forward;
}
return 'none';
};

interface MouseState {
/**
* The current position of the mouse.
*/
position: Point;
/**
* The buttons that are currently being pressed.
*/
buttons: number;
}

Expand Down Expand Up @@ -590,9 +617,7 @@ export class Mouse {
type: 'mouseMoved',
modifiers: this.#keyboard._modifiers,
buttons,
// This should always be 0 (i.e. 'left'). See
// https://w3c.github.io/uievents/#event-type-mousemove
button: MouseButton.Left,
button: getButtonFromPressedButtons(buttons),
...position,
});
});
Expand All @@ -614,7 +639,9 @@ export class Mouse {
throw new Error(`'${button}' is already pressed.`);
}
await this.#withTransaction(updateState => {
updateState({buttons: this.#state.buttons | flag});
updateState({
buttons: this.#state.buttons | flag,
});
const {buttons, position} = this.#state;
return this.#client.send('Input.dispatchMouseEvent', {
type: 'mousePressed',
Expand Down Expand Up @@ -642,7 +669,9 @@ export class Mouse {
throw new Error(`'${button}' is not pressed.`);
}
await this.#withTransaction(updateState => {
updateState({buttons: this.#state.buttons & ~flag});
updateState({
buttons: this.#state.buttons & ~flag,
});
const {buttons, position} = this.#state;
return this.#client.send('Input.dispatchMouseEvent', {
type: 'mouseReleased',
Expand Down

0 comments on commit a6eaac4

Please sign in to comment.