Skip to content

fix(drag-drop): sorting too often if pointer is inside element after position swap #12837

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

Merged
merged 1 commit into from
Aug 27, 2018

Conversation

crisbeto
Copy link
Member

Currently we check whether the user's pointer overlaps an item for each pixel that they've moved. This works fine when the items have a similar height, however it breaks down if there's a position swap between a very tall item and a smaller one. These changes rework the logic to take into account the direction that the user is moving in, before doing a position swap.

Fixes #12694.

For reference, here's what the current behavior looks like:
demo

@crisbeto crisbeto added pr: merge safe target: major This PR is targeted for the next major release labels Aug 26, 2018
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Aug 26, 2018
* Amount the pixels the user should drag before we
* consider them to have changed the drag direction.
*/
const DELTA_CHANGE_THRESHOLD = 5;
Copy link
Member

Choose a reason for hiding this comment

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

How about POINTER_DIRECTION_CHANGE_THRESHOLD?

@@ -114,6 +120,12 @@ export class CdkDrag<T = any> implements OnDestroy {
*/
private _moveEventSubscriptions = 0;

/** Keeps track of the direction in which the user is dragging along each axis. */
private _delta: {
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 not immediately clear to me what _delta.x / _delta.y represents

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure what else to call it. Something like pointerDirection isn't much better.

Copy link
Member

Choose a reason for hiding this comment

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

Is it just the x/y delta from the last move event?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it's either 1 or -1, depending on whether the user is going up/down or left/right, similar to the deltaX and deltaY on the WheelEvent.

Copy link
Member

Choose a reason for hiding this comment

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

How about splitting it up into two properties?

private _pointerDirectionDelta: {x: -1 | 1, y: -1 | 1};
private _pointerPositionAtLastDirectionChange: Point;

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good. Done.

@@ -590,6 +605,30 @@ export class CdkDrag<T = any> implements OnDestroy {

this._placeholder = this._placeholderRef = null!;
}

/** Updates the current drag delta, based on the user's current pointer position on the page. */
private _updateDelta(pointerPositionOnPage: Point) {
Copy link
Member

Choose a reason for hiding this comment

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

_updatePointerDirectionDelta?

pointerPositionOnPage -> pointerPosition? (isn't it always on the page?)

I'm also wondering if would make sense to refactor most of the pointer-related code into a separate class like PointerState; this file is getting pretty hefty, and it would be nice to separate the logic of the drag/drop behavior from the Angular-specific stuff.

Copy link
Member Author

Choose a reason for hiding this comment

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

WRT the parameter naming, I wanted to make the distinction that it's relative to the page, rather than within the element, which we also keep track of inside the drag.ts.

Regarding refactoring, I agree that we should do it before we end up in a situation like mat-select. I've added it to my list.

@crisbeto crisbeto force-pushed the drag-drop-uneven-height-sorting branch from ff5cb6f to 6d77a47 Compare August 26, 2018 17:15
@crisbeto
Copy link
Member Author

Feedback has been addressed.

…position swap

Currently we check whether the user's pointer overlaps an item for each pixel that they've moved. This works fine when the items have a similar height, however it breaks down if there's a position swap between a very tall item and a smaller one. These changes rework the logic to take into account the direction that the user is moving in, before doing a position swap.

Fixes angular#12694.
@crisbeto crisbeto force-pushed the drag-drop-uneven-height-sorting branch from 6d77a47 to dfead91 Compare August 27, 2018 15:26
Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

LGTM

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker labels Aug 27, 2018
@jelbourn jelbourn merged commit 5278f72 into angular:master Aug 27, 2018
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Drag and drop drag animation bugs
3 participants