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

Improve single vs. multi-touch zoom & pan interaction (#7196) #8100

Merged
merged 16 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
48 changes: 45 additions & 3 deletions src/ui/handler/drag_pan.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class DragPanHandler {
'_onMove',
'_onMouseUp',
'_onTouchEnd',
'_onMultiTouchEnd',
'_onBlur',
'_onDragFrame'
], this);
Expand Down Expand Up @@ -125,8 +126,18 @@ class DragPanHandler {
}

onTouchStart(e: TouchEvent) {
if (this._state !== 'enabled') return;
if (e.touches.length > 1) return;
if (!this.isEnabled()) return;
if (e.touches && e.touches.length > 1) { // multi-finger touch
// Listen for the multi-touch end in case it's reduced to a single touch
DOM.addEventListener(window.document, 'touchend', this._onMultiTouchEnd);

// If we're already dragging with one finger and the user adds another finger(s),
// let TouchZoomRotateHandler handler take over if it's enabled by stopping the drag
if ((this._state === 'pending' || this._state === 'active') && this._map.touchZoomRotate.isEnabled()) {
this._onMultiTouchStart(e);
}
return;
}

// Bind window-level event listeners for touchmove/end events. In the absence of
// the pointer capture API, which is not supported by all necessary platforms,
Expand Down Expand Up @@ -231,7 +242,29 @@ class DragPanHandler {
}
}

_onBlur(e: FocusEvent) {
_onMultiTouchEnd(e: TouchEvent) {
// A multi-finger touch is ending; decide whether to activate dragging

if (e.touches && e.touches.length > 1) return; // there are still multiple fingers touching; wait
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe move this logic to something like isMultiTouch(e)?


DOM.removeEventListener(window.document, 'touchend', this._onMultiTouchEnd);

if (!e.touches || e.touches.length === 0) return; // all fingers were removed at once; nothing left to do

// Back to single-touch (e.touches.length === 1); treat this like a touchstart & activate drag

// Bind window-level event listeners for touchmove/end events. In the absence of
// the pointer capture API, which is not supported by all necessary platforms,
// window-level event listeners give us the best shot at capturing events that
// fall outside the map canvas element. Use `{capture: true}` for the move event
// to prevent map move events from being fired during a drag.
DOM.addEventListener(window.document, 'touchmove', this._onMove, {capture: true, passive: false});
DOM.addEventListener(window.document, 'touchend', this._onTouchEnd);

this._start(e);
}

_abort(e: FocusEvent | TouchEvent) {
switch (this._state) {
case 'active':
this._state = 'enabled';
Expand All @@ -250,6 +283,15 @@ class DragPanHandler {
}
}

_onMultiTouchStart(e: TouchEvent) {
// Additional fingers are now touching; treat this almost like a touchend & stop dragging,
this._abort(e);
}

_onBlur(e: FocusEvent) {
this._abort(e);
}

_unbind() {
DOM.removeEventListener(window.document, 'touchmove', this._onMove, {capture: true, passive: false});
DOM.removeEventListener(window.document, 'touchend', this._onTouchEnd);
Expand Down
3 changes: 2 additions & 1 deletion test/node_modules/mapbox-gl-js-test/simulate_interaction.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

185 changes: 185 additions & 0 deletions test/unit/ui/handler/drag_pan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Map from '../../../../src/ui/map';
import DOM from '../../../../src/util/dom';
import simulate from 'mapbox-gl-js-test/simulate_interaction';


function createMap(t, clickTolerance) {
t.stub(Map.prototype, '_detectMissingCSS');
return new Map({ container: DOM.create('div', '', window.document.body), clickTolerance: clickTolerance || 0 });
Expand Down Expand Up @@ -741,3 +742,187 @@ test('DragPanHandler does not begin a touch drag if moved less than click tolera
map.remove();
t.end();
});


test('DragPanHandler does not begin a touch drag on multi-finger touchstart event', (t) => {
const map = createMap(t);

const dragstart = t.spy();
const drag = t.spy();
const dragend = t.spy();

map.on('dragstart', dragstart);
map.on('drag', drag);
map.on('dragend', dragend);

simulate.touchstart(map.getCanvas(), {touches: [{clientX: 10, clientY: 10}, {clientX: 20, clientY: 20}]});
map._renderTaskQueue.run();
t.equal(dragstart.callCount, 0);
t.equal(drag.callCount, 0);
t.equal(dragend.callCount, 0);

simulate.touchmove(map.getCanvas(), {touches: [{clientX: 20, clientY: 20}, {clientX: 30, clientY: 30}]});
map._renderTaskQueue.run();
t.equal(dragstart.callCount, 0);
t.equal(drag.callCount, 0);
t.equal(dragend.callCount, 0);

simulate.touchend(map.getCanvas());
map._renderTaskQueue.run();
t.equal(dragstart.callCount, 0);
t.equal(drag.callCount, 0);
t.equal(dragend.callCount, 0);

map.remove();
t.end();
});


test('DragPanHandler ends a touch-triggered drag if a second touch is started & touchZoomRotate is enabled', (t) => {
const map = createMap(t);
map.touchZoomRotate.enable();

const dragend = t.spy();
map.on('dragend', dragend);

simulate.touchstart(map.getCanvas(), {touches: [{clientX: 10, clientY: 10}]});
map._renderTaskQueue.run();

simulate.touchmove(map.getCanvas(), {touches: [{clientX: 20, clientY: 20}]});
map._renderTaskQueue.run();
t.ok(map.dragPan.isActive());

simulate.touchstart(map.getCanvas(), {touches: [{clientX: 20, clientY: 20}, {clientX: 30, clientY: 30}]});
map._renderTaskQueue.run();
t.notOk(map.dragPan.isActive());
t.equal(dragend.callCount, 1);

map.remove();
t.end();
});

test('DragPanHandler does not end a touch-triggered drag if a second touch is started but touchZoomRotate is disabled', (t) => {
const map = createMap(t);
map.touchZoomRotate.disable();

const dragend = t.spy();
map.on('dragend', dragend);

simulate.touchstart(map.getCanvas(), {touches: [{clientX: 10, clientY: 10}]});
map._renderTaskQueue.run();

simulate.touchmove(map.getCanvas(), {touches: [{clientX: 20, clientY: 20}]});
map._renderTaskQueue.run();
t.ok(map.dragPan.isActive());

simulate.touchstart(map.getCanvas(), {touches: [{clientX: 20, clientY: 20}, {clientX: 30, clientY: 30}]});
map._renderTaskQueue.run();
t.ok(map.dragPan.isActive());
t.equal(dragend.callCount, 0);

map.remove();
t.end();
});

test('DragPanHandler starts a touch-triggered drag if a multi-finger touch becomes a single-finger touch', (t) => {
const map = createMap(t);

const dragstart = t.spy();
map.on('dragstart', dragstart);

simulate.touchstart(map.getCanvas(), {touches: [{clientX: 20, clientY: 20}, {clientX: 30, clientY: 30}]});
map._renderTaskQueue.run();
t.notOk(map.dragPan.isActive());
t.notOk(dragstart.called);

simulate.touchmove(map.getCanvas(), {touches: [{clientX: 10, clientY: 10}, {clientX: 40, clientY: 40}]});
map._renderTaskQueue.run();
t.notOk(map.dragPan.isActive());
t.notOk(dragstart.called);

simulate.touchend(map.getCanvas(), {touches: [{clientX: 10, clientY: 10}]});
map._renderTaskQueue.run();
t.notOk(map.dragPan.isActive());
t.equals(map.dragPan._state, 'pending');
t.notOk(dragstart.called);

simulate.touchmove(map.getCanvas(), {touches: [{clientX: 20, clientY: 20}]});
map._renderTaskQueue.run();
t.ok(map.dragPan.isActive());
t.equal(dragstart.callCount, 1);

map.remove();
t.end();
});





test('DragPanHandler stops/starts touch-triggered drag appropriately when transitioning between single- and multi-finger touch', (t) => {
const map = createMap(t);

const dragstart = t.spy();
const drag = t.spy();
const dragend = t.spy();

map.on('dragstart', dragstart);
map.on('drag', drag);
map.on('dragend', dragend);

// Single-finger touch starts drag
simulate.touchstart(map.getCanvas(), {touches: [{clientX: 10, clientY: 10}]});
map._renderTaskQueue.run();
t.notOk(map.dragPan.isActive());
t.equal(dragstart.callCount, 0);
t.equal(drag.callCount, 0);
t.equal(dragend.callCount, 0);

simulate.touchmove(map.getCanvas(), {touches: [{clientX: 20, clientY: 20}]});
map._renderTaskQueue.run();
t.ok(map.dragPan.isActive());
t.equal(dragstart.callCount, 1);
t.equal(drag.callCount, 1);
t.equal(dragend.callCount, 0);

// Adding a second finger stops drag (will trigger touchZoomRotate instead)
simulate.touchstart(map.getCanvas(), {touches: [{clientX: 10, clientY: 10}, {clientX: 20, clientY: 20}]});
map._renderTaskQueue.run();
t.notOk(map.dragPan.isActive());
t.equal(dragstart.callCount, 1);
t.equal(drag.callCount, 1);
t.equal(dragend.callCount, 1);

simulate.touchmove(map.getCanvas(), {touches: [{clientX: 20, clientY: 20}, {clientX: 30, clientY: 30}]});
map._renderTaskQueue.run();
t.notOk(map.dragPan.isActive());
t.equal(dragstart.callCount, 1);
t.equal(drag.callCount, 1);
t.equal(dragend.callCount, 1);

// Removing all but one finger starts another drag
simulate.touchend(map.getCanvas(), {touches: [{clientX: 30, clientY: 30}]});
map._renderTaskQueue.run();
t.notOk(map.dragPan.isActive());
t.equal(dragstart.callCount, 1);
t.equal(drag.callCount, 1);
t.equal(dragend.callCount, 1);

simulate.touchmove(map.getCanvas(), {touches: [{clientX: 20, clientY: 20}]});
map._renderTaskQueue.run();
t.ok(map.dragPan.isActive());
t.equal(dragstart.callCount, 2);
t.equal(drag.callCount, 2);
t.equal(dragend.callCount, 1);

// Removing last finger stops drag
simulate.touchend(map.getCanvas());
map._renderTaskQueue.run();
t.notOk(map.dragPan.isActive());
t.equal(dragstart.callCount, 2);
t.equal(drag.callCount, 2);
t.equal(dragend.callCount, 2);

map.remove();
t.end();
});