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

Enable Multi-MouseButton Support for dragPan #5354

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion src/ui/handler/drag_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
* If true, handler will be enabled during construction
*/
enable?: boolean;

buttons?: number[];
};

/**
Expand Down Expand Up @@ -95,6 +97,9 @@

options.assignEvents(this);

if (options.buttons) {
this._moveStateManager.updateButtons(options.buttons);

Check warning on line 101 in src/ui/handler/drag_handler.ts

View check run for this annotation

Codecov / codecov/patch

src/ui/handler/drag_handler.ts#L101

Added line #L101 was not covered by tests
}
this.reset();
}

Expand Down Expand Up @@ -155,7 +160,10 @@
this.reset(e);
}

enable() {
enable(options?: { buttons?: number[] }): void {
if (options && options.buttons) {
this._moveStateManager.updateButtons(options.buttons);
}

Check warning on line 166 in src/ui/handler/drag_handler.ts

View check run for this annotation

Codecov / codecov/patch

src/ui/handler/drag_handler.ts#L165-L166

Added lines #L165 - L166 were not covered by tests
this._enabled = true;
}

Expand Down
18 changes: 13 additions & 5 deletions src/ui/handler/drag_move_state_manager.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {DOM} from '../../util/dom';

const LEFT_BUTTON = 0;
const MIDDLE_BUTTON = 1;
const RIGHT_BUTTON = 2;

// the values for each button in MouseEvent.buttons
const BUTTONS_FLAGS = {
[LEFT_BUTTON]: 1,
[RIGHT_BUTTON]: 2
[MIDDLE_BUTTON]: 4,
[RIGHT_BUTTON]: 2,
};

function buttonNoLongerPressed(e: MouseEvent, button: number) {
Expand Down Expand Up @@ -36,14 +38,16 @@
isValidStartEvent: (e: E) => boolean;
isValidMoveEvent: (e: E) => boolean;
isValidEndEvent: (e?: E) => boolean;
updateButtons?: (buttons: number[]) => void;
}

export class MouseMoveStateManager implements DragMoveStateManager<MouseEvent> {
_eventButton: number | undefined;
_correctEvent: (e: MouseEvent) => boolean;
_correctEvent: (e: MouseEvent, buttons?: number[]) => boolean;
_buttons: number[] = [LEFT_BUTTON];

constructor(options: {
checkCorrectEvent: (e: MouseEvent) => boolean;
checkCorrectEvent: (e: MouseEvent, buttons?:number[]) => boolean;
}) {
this._correctEvent = options.checkCorrectEvent;
}
Expand All @@ -58,7 +62,7 @@
}

isValidStartEvent(e: MouseEvent) {
return this._correctEvent(e);
return this._correctEvent(e, this._buttons);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of passing buttons it might be better to pass a predicate to override the default one - what happens if someone wants to have right click with control button for panning?

Copy link
Author

Choose a reason for hiding this comment

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

Good to point that out! I've tried setting the right click for panning and it looks like if dragRotate is enabled it will not pan the map. So it is safe for now?.
I would gladly accept any help with a better approach since I'm not good with typescript and have not much experience in large projects in general.

Copy link
Collaborator

Choose a reason for hiding this comment

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

_correctEvent is basically what you want to override.
I would try and find a way to pass it down instead if adding a buttons parameter and pass that down...

}

isValidMoveEvent(e: MouseEvent) {
Expand All @@ -75,6 +79,10 @@
const eventButton = DOM.mouseButton(e);
return eventButton === this._eventButton;
}

updateButtons(buttons: number[]) {
this._buttons = buttons;

Check warning on line 84 in src/ui/handler/drag_move_state_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/ui/handler/drag_move_state_manager.ts#L84

Added line #L84 was not covered by tests
}
}

export class OneFingerTouchMoveStateManager implements DragMoveStateManager<TouchEvent> {
Expand Down Expand Up @@ -116,7 +124,7 @@

export class MouseOrTouchMoveStateManager implements DragMoveStateManager<MouseEvent | TouchEvent> {
constructor(
private mouseMoveStateManager = new MouseMoveStateManager({checkCorrectEvent: () => true}),
private mouseMoveStateManager = new MouseMoveStateManager({checkCorrectEvent: () => true}),
private oneFingerTouchMoveStateManager = new OneFingerTouchMoveStateManager()
) {}

Expand Down
20 changes: 14 additions & 6 deletions src/ui/handler/mouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,31 @@
};
};

export function generateMousePanHandler({enable, clickTolerance}: {
export function generateMousePanHandler({enable, clickTolerance, dragPan}: {
clickTolerance: number;
enable?: boolean;
dragPan?: any;
}): MousePanHandler {
const mouseMoveStateManager = new MouseMoveStateManager({
checkCorrectEvent: (e: MouseEvent) => DOM.mouseButton(e) === LEFT_BUTTON && !e.ctrlKey,
checkCorrectEvent: (e: MouseEvent, buttons?:number[]) => {
const button = DOM.mouseButton(e);
return buttons.includes(button) && !e.ctrlKey;
}
});
return new DragHandler<DragPanResult, MouseEvent>({
const handler = new DragHandler<DragPanResult, MouseEvent>({
clickTolerance,
move: (lastPoint: Point, point: Point) =>
({around: point, panDelta: point.sub(lastPoint)}),
activateOnStart: true,
moveStateManager: mouseMoveStateManager,
enable,
assignEvents,
});
};
if(enable) {
handler.enable(dragPan??{});

Check warning on line 57 in src/ui/handler/mouse.ts

View check run for this annotation

Codecov / codecov/patch

src/ui/handler/mouse.ts#L57

Added line #L57 was not covered by tests
}

return handler;
}

export function generateMouseRotationHandler({enable, clickTolerance, aroundCenter = true, minPixelCenterThreshold = 100, rotateDegreesPerPixelMoved = 0.8}: {
clickTolerance: number;
Expand Down Expand Up @@ -98,7 +106,7 @@
});
return new DragHandler<DragPitchResult, MouseEvent>({
clickTolerance,
move: (lastPoint: Point, point: Point) =>
move: (lastPoint: Point, point: Point) =>
({pitchDelta: (point.y - lastPoint.y) * pitchDegreesPerPixelMoved}),
// prevent browser context menu when necessary; we don't allow it with rotation
// because we can't discern rotation gesture start from contextmenu on Mac
Expand Down
9 changes: 8 additions & 1 deletion src/ui/handler/shim/drag_pan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export type DragPanOptions = {
* @defaultValue 2500
*/
maxSpeed?: number;
/**
* the buttons that can be used to drag the map.
* @defaultValue [0]
* @see https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button
*/
buttons?: number[];
};

/**
Expand Down Expand Up @@ -60,12 +66,13 @@ export class DragPanHandler {
* easing: bezier(0, 0, 0.3, 1),
* maxSpeed: 1400,
* deceleration: 2500,
* buttons: [0,1] // left and middle mouse buttons
* });
* ```
*/
enable(options?: DragPanOptions | boolean) {
this._inertiaOptions = options || {};
this._mousePan.enable();
this._mousePan.enable(this._inertiaOptions);
this._touchPan.enable();
this._el.classList.add('maplibregl-touch-drag-pan');
}
Expand Down
1 change: 1 addition & 0 deletions src/ui/handler_inertia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type InertiaOptions = {
easing: (t: number) => number;
deceleration: number;
maxSpeed: number;
buttons?: number[]; // buttons that can be used to trigger pan
};

export class HandlerInertia {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/handler_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class RenderFrameEvent extends Event {
* would return a `panDelta` on the mousemove.
*/
export interface Handler {
enable(): void;
enable(options?): void;
disable(): void;
isEnabled(): boolean;
/**
Expand Down
Loading