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: infer last pressed button in mouse move #10067

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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