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

Allow pinch trackpad gesture while cooperative gestures is enabled #4465

Merged
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## main

- Allow trackpad pinch gestures to break through the `cooperativeGestures` setting, bringing it in line with other embedded map behaviours, such as Google Maps and Mapbox. ([#4465](https://github.com/maplibre/maplibre-gl-js/pull/4465))

### ✨ Features and improvements

- Expose projection matrix parameters ([#3136](https://github.com/maplibre/maplibre-gl-js/pull/3136))
Expand Down
33 changes: 33 additions & 0 deletions src/ui/handler/cooperative_gestures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,39 @@ describe('CoopGesturesHandler', () => {
map.remove();
});

test('Zooms on trackpad pinch when metaKey is the bypass key', () => {
// NOTE: This should pass regardless of whether cooperativeGestures is enabled or not
const browserNow = jest.spyOn(browser, 'now');
let now = 1555555555555;
browserNow.mockReturnValue(now);

const map = createMap(true);

// pretend we're on a Mac, where the ctrlKey isn't the bypassKey
map.cooperativeGestures._bypassKey = 'metaKey';
tomhicks marked this conversation as resolved.
Show resolved Hide resolved
map._renderTaskQueue.run();

const startZoom = map.getZoom();
// simulate a single 'wheel' trackpad pinch event
simulate.wheel(map.getCanvas(), {
type: 'wheel',
deltaY: -simulate.magicWheelZoomDelta,

// this is how a browser identifies a trackpad pinch
ctrlKey: true
});
map._renderTaskQueue.run();

now += 400;
browserNow.mockReturnValue(now);
map._renderTaskQueue.run();

const endZoom = map.getZoom();
expect(endZoom - startZoom).toBeCloseTo(0.0285, 3);

map.remove();
});

test('Does not show message if scrollZoom is disabled', () => {
// NOTE: This should pass regardless of whether cooperativeGestures is enabled or not
const browserNow = jest.spyOn(browser, 'now');
Expand Down
15 changes: 14 additions & 1 deletion src/ui/handler/cooperative_gestures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,20 @@ export class CooperativeGesturesHandler implements Handler {
if (!this._map.scrollZoom.isEnabled()) {
return;
}
this._onCooperativeGesture(!e[this._bypassKey]);

const isPrevented = this.shouldPreventWheelEvent(e);
HarelM marked this conversation as resolved.
Show resolved Hide resolved
this._onCooperativeGesture(isPrevented);
}

shouldPreventWheelEvent(e: WheelEvent) {
if (!this.isEnabled()) {
return false;
}

const isTrackpadPinch = e.ctrlKey;
const isBypassed = e[this._bypassKey] || isTrackpadPinch;

return !isBypassed;
}

_onCooperativeGesture(showNotification: boolean) {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/handler/scroll_zoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class ScrollZoomHandler implements Handler {

wheel(e: WheelEvent) {
if (!this.isEnabled()) return;
if (this._map.cooperativeGestures.isEnabled() && !e[this._map.cooperativeGestures._bypassKey]) {
if (this._map.cooperativeGestures.shouldPreventWheelEvent(e)) {
return;
}
let value = e.deltaMode === WheelEvent.DOM_DELTA_LINE ? e.deltaY * 40 : e.deltaY;
Expand Down
4 changes: 2 additions & 2 deletions test/build/min.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import packageJson from '../../package.json' with {type: 'json'};
import packageJson from '../../package.json' with { type: 'json' };

const minBundle = fs.readFileSync('dist/maplibre-gl.js', 'utf8');

Expand Down Expand Up @@ -36,7 +36,7 @@ describe('test min build', () => {
const decreaseQuota = 4096;

// feel free to update this value after you've checked that it has changed on purpose :-)
const expectedBytes = 799999;
const expectedBytes = 800300;

expect(actualBytes - expectedBytes).toBeLessThan(increaseQuota);
expect(expectedBytes - actualBytes).toBeLessThan(decreaseQuota);
Expand Down