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

Use MouseEvent.buttons for button state tracking #1921

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

CendioHalim
Copy link

@CendioHalim CendioHalim commented Nov 29, 2024

Instead of keeping track of button states ourselves by looking at MouseEvent.button, we can use the MouseEvent.buttons which already contains the state of all buttons.

Note that in MouseEvent.button, button numbers are mapped as follows:

0 -> Left
1 -> Middle
2 -> Right
3 -> Back
4 -> Forward

But when looking at the MouseEvent.buttons property, the bits middle and right are switched, which is confusing:

0 -> Left
1 -> Right
2 -> Middle
3 -> Back
4 -> Forward

With this change, #1919 could be modified to work with Safari as well.

Unfortunately, the internal _mouseButtonStateMask state is not removed in this PR as it would require a large rewrite of the general architecture.

Tested on:

  • Linux
    • Firefox
    • Chrome
    • Epiphany
  • Windows 11
    • Edge
    • Chrome
    • Firefox
  • macOS
    • Safari
    • Edge
    • Chrome
    • Firefox
  • iPad
    • Safari

Instead of keeping track of button states ourselves by looking at
MouseEvent.button, we can use the MouseEvent.buttons which already
contains the state of all buttons.

this._sendMouse(x, y, this._mouseButtonMask);
this._sendMouse(x, y, bmask);
this._mouseButtonMask = bmask;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change the order of these two, then you can retain that _sendMouse() is always called with this._mouseButtonMask.

this._handleMouseButton(pos.x, pos.y, true, 1 << 5);
this._handleMouseButton(pos.x, pos.y, false, 1 << 5);
this._handleMouseButton(pos.x, pos.y, true, this._mouseButtonMask | 1 << 5);
this._handleMouseButton(pos.x, pos.y, false, this._mouseButtonMask & ~(1 << 5));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need the unset of the bit? Shouldn't this method be in full control of the wheel button bits?

I feel it might imply that the wheel bits might be set in other places as well.

switch (ev.type) {
case 'mousedown':
setCapture(this._canvas);
this._handleMouseButton(pos.x, pos.y,
true, 1 << ev.button);
this._handleMouseButton(pos.x, pos.y, true, bmask);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit weird keeping the down argument now that we have a full mask. But that's a cleanup I suppose we can do later.

@@ -2941,6 +2936,35 @@ export default class RFB extends EventTargetMixin {
"raw", passwordChars, { name: "DES-ECB" }, false, ["encrypt"]);
return legacyCrypto.encrypt({ name: "DES-ECB" }, key, challenge);
}

static convertButtonMask(buttons) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an internal method, so it should have a _ prefix.

@@ -2941,6 +2936,35 @@ export default class RFB extends EventTargetMixin {
"raw", passwordChars, { name: "DES-ECB" }, false, ["encrypt"]);
return legacyCrypto.encrypt({ name: "DES-ECB" }, key, challenge);
}

static convertButtonMask(buttons) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that it's an internal function, as is _handleMouse(), can't we put them next to each other to make it easier to read?

@@ -3508,6 +3508,9 @@ describe('Remote Frame Buffer protocol client', function () {
// client coordinate calculations
client.focusOnClick = false;

// We need to keep track of MouseEvent.buttons state
client._buttonsState = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it obfuscating a lot of what the tests are supposed to do. Can't we make sure the tests provide this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants