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

Input Events - Implement 'capture' API for Mouse events #126

Closed
bryphe opened this issue Dec 20, 2018 · 0 comments
Closed

Input Events - Implement 'capture' API for Mouse events #126

bryphe opened this issue Dec 20, 2018 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@bryphe
Copy link
Member

bryphe commented Dec 20, 2018

Proposal: capture-like API for mouse events

Why?

Often for UI elements, after the initial mousedown, the component needs to track the mouse movement and actions exclusively. Some examples of this:

  • <Button /> - for a click event, you don't want to dispatch it immediately on the mousedown - most UIs will wait for the mouseup before dispatching. If the mouseup occurs elsewhere, the UI does not fire a click event. While in this limbo-state between mousedown and mouseup, hovering over other elements is a no-op.
  • <Slider /> and <Scrollbar /> - once a mousedown has occurred, we want to track the mouse movement, even if the mouse cursor moves away from the slider or scrollbar. We can still update the value of the slider / scrollbar, until the user releases via a mouseup event.
  • Drag and Drop - for an element that supports drag-and-drop, we want to contain the mousemove and mouseup until the drag/drop gesture has completed.

Proposal

Add a Mouse.setCapture API that could be used as follows:

/* While capturing is active, events will _only_ be forwarded to these handlers */
Mouse.setCapture(~onMouseDown, ~onMouseUp, ~onMouseMove);
...
/* Release capture */
Mouse.releaseCapture();

Example usage

For a button element, we could add an onMouseDown handler that looks like this:

let onMouseDown = (evt) => {
    let noop = (_evt) => ();

    let releaseCapture = ref(None);
    let capturedMouseUp = (evt) => {
         dispatchClickEvent(evt);
         Mouse.releaseCapture();
    };
    Mouse.setCapture(~onMouseDown=noop, ~onMouseMove=noop, ~onMouseUp=capturedMouseUp);
};

The <Button /> could do extra validation - like verify the onMouseUp actually occurred over the element, or that it was within a certain distance, etc.

@bryphe bryphe added the enhancement New feature or request label Dec 20, 2018
@bryphe bryphe changed the title UI - Input Events - Implement 'capture' API for Mouse events Input Events - Implement 'capture' API for Mouse events Dec 20, 2018
@bryphe bryphe mentioned this issue Dec 20, 2018
@bryphe bryphe self-assigned this Dec 21, 2018
bryphe added a commit that referenced this issue Dec 21, 2018
* Initial capture API

* Add interface for Mouse

* Add test, refactor API, get test green

* Formatting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant