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

configurable drag pan threshold #6809

Merged
merged 5 commits into from
Jun 18, 2018
Merged

Conversation

msbarry
Copy link
Contributor

@msbarry msbarry commented Jun 13, 2018

This is an attempt to fix #1832. I added a dragThreshold option which lets you set the threshold above which a drag is considered a drag and below which a drag is considered a click. It defaults to 0, which retains the existing behavior. This is my first pull request here, so let me know if there's a better way to do this or anything that I'm missing, thanks!

  • briefly describe the changes in this PR
  • write tests for all new functionality
  • document any changes to public APIs
  • post benchmark scores
  • manually test the debug page
  • tagged @mapbox/studio and/or @mapbox/maps-design if this PR includes style spec changes

@ryanhamley
Copy link
Contributor

Hi @msbarry thanks for submitting this PR! I added a few comments but overall I think it looks pretty good.

@@ -663,3 +663,81 @@ test('DragPanHandler does not begin a drag on spurious touchmove events', (t) =>
map.remove();
t.end();
});

test('DragPanHandler does not beging a mouse drag if moved less than threshold', (t) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nitpick but there's a typo in begin

t.end();
});

test('DragPanHandler does not beging a touch drag if moved less than threshold', (t) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

@@ -0,0 +1,32 @@
<!DOCTYPE html>
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it's necessary to create a separate debug page for this since it's really just a simple option.

src/ui/map.js Outdated
@@ -170,6 +172,8 @@ const defaultOptions = {
* bearing will snap to north. For example, with a `bearingSnap` of 7, if the user rotates
* the map within 7 degrees of north, the map will automatically snap to exact north.
* @param {boolean} [options.pitchWithRotate=true] If `false`, the map's pitch (tilt) control with "drag to rotate" interaction will be disabled.
* @param {number} [options.dragThreshold=0] The threshold, measured in pixels, that determines how far a drag must be before it is considered a drag
Copy link
Contributor

Choose a reason for hiding this comment

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

I have two related suggestions here:

  1. I think clickTolerance might be a more accurate name for this option. For one thing, that's what this option is called in Leaflet (even though it's part of the Draggable class) which might help people who are migrating to GL-JS or searching for a solution to this problem. Additionally, the original issue for this PR was how to get a click event instead of a drag event when the shift was only a couple pixels, so really this is about setting a tolerance limit around the click event to prevent it turning into a drag.

  2. I think the description could be clearer. Consider how Leaflet describes clickTolerance: "The max number of pixels a user can shift the mouse pointer during a click for it to be considered a valid click (as opposed to a mouse drag)." You may want to make it clear in your description that the value of the option is the max number inclusive since you're checking for <= while Leaflet just checks <.

@@ -553,3 +553,35 @@ test(`Map#on mousedown doesn't fire subsequent click event if mousepos changes`,
map.remove();
t.end();
});

test(`Map#on mousedown fires subsequent click event if mousepos changes less than drag threshold`, (t) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor again, but I'd spell out mouse position instead of mousePos in these two test descriptions.

@msbarry
Copy link
Contributor Author

msbarry commented Jun 14, 2018

Thanks @ryanhamley! I renamed the option to clickTolerance and used the same description from leaflet. I also made the check exclusive instead of inclusive for consistency (mapbox-gl-draw appears to do the same).

What do you think for the default value? Leaflet uses 3, do you think we should switch to that or retain existing behavior for users who don't set the option?

Also, should we apply the threshold to other built-in handlers that look for drags like box zoom and drag rotate? It seems less critical since if you control or shift click it doesn't fire a click event so the drag handlers don't need to differentiate from a click.

@mourner
Copy link
Member

mourner commented Jun 14, 2018

Awesome PR! No one complained about Leaflet's default in years, so I'd vote for 3 as a default here as well — will make clicking GL JS maps much easier, especially on a trackpad.

@ryanhamley
Copy link
Contributor

Switching the default from 0 to 3 would be a breaking change in the API but I'm not sure if there's really a use case where you'd depend on this value being 0 so it seems like a fairly safe breaking change to me.

It doesn't seem like Leaflet uses any type of tolerance detection in BoxZoom and it doesn't have a concept of rotation of the map. That being said, I think including a tolerance could be useful. Accidentally turning a click into a drag would be a pretty minor nuisance with drag rotate, but on box zoom it might be possible to inadvertently zoom in close on a very small portion of the map which could confuse a user. Thoughts @mourner?

@msbarry
Copy link
Contributor Author

msbarry commented Jun 15, 2018

Updated default click tolerance to 3px. Let me know what you decide about box zoom, I think it's fairly straightforward to add if needed.

@jfirebaugh
Copy link
Contributor

I think using the same threshold for box zoom is a good idea. It was requested in #6132.

@msbarry
Copy link
Contributor Author

msbarry commented Jun 16, 2018

Sounds good. I applied click tolerance to box zoom handler as well.

@jfirebaugh jfirebaugh merged commit 975f29e into mapbox:master Jun 18, 2018
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.

Add a movement threshold for drag panning
4 participants