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

Support auto switching wheel behavior #4612

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

trygve-aaberge-adsk
Copy link

This implements support for setting wheelBehavior to auto. What this means is that it will try to detect if you're using a mouse or a touchpad, and use wheelBehavior zoom when a mouse is used and pan when a touchpad is used. This is more intuitive behavior than always having it set to wheel or pan in my opinion.

As far as I know, it's unfortunately not possible to know if wheel events are generated from a mouse or a touchpad. The heuristic implemented is that if you scroll horizontally it assumes you use a touchpad and switch to pan, and if you pan with the middle mouse button it assumes you're using a mouse and switches to zoom. This seems to be similar to what other similar software does.

Fixes #4579

Change type

  • bugfix
  • improvement
  • feature
  • api
  • other

Test plan

  1. Use a touchpad to pan the board by scrolling
  2. Switch to a mouse and pan the board by dragging with the middle mouse button
  3. Scroll and observe that it now zooms on scroll
  4. Switch back to the touchpad, scroll horizontally and observe that it pans again
  • Unit tests
  • End to end tests

Release notes

  • Support automatically switching wheelBehavior when switching between touchpad and mouse (as determined by a heuristic). Use pan when using a touchpad and zoom when using a mouse.

Copy link

vercel bot commented Sep 26, 2024

@trygveaa is attempting to deploy a commit to the tldraw Team on Vercel.

A member of the Team first needs to authorize it.

@huppy-bot huppy-bot bot added the improvement Product improvement label Sep 26, 2024
@huppy-bot
Copy link
Contributor

huppy-bot bot commented Sep 26, 2024

trygve-aaberge-adsk has signed the Contributor License Agreement. Thanks!

@@ -4,7 +4,7 @@ import { EASINGS } from './primitives/easings'
/** @internal */
export const DEFAULT_CAMERA_OPTIONS: TLCameraOptions = {
isLocked: false,
wheelBehavior: 'pan',
wheelBehavior: 'auto',

Choose a reason for hiding this comment

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

I changed the default to auto because I think that's the most intuitive behavior, but that's of course up to you if you want to do.

@mimecuvalo
Copy link
Member

don't mind my test commits, we're testing something internally :) sry for the noise!

@trygve-aaberge-adsk
Copy link
Author

No worries. And sorry for not signing the CLA yet, I had to talk to legal in my company (nothing particular about your CLA, just general policy for CLAs), but hopefully we'll get it sorted soon.

@@ -9165,6 +9165,9 @@ export class Editor extends EventEmitter<TLEventMap> {
/** @internal */
private _restoreToolId = 'select'

/** @internal */
private _autoWheelBehavior: 'pan' | 'zoom' = 'pan'

Choose a reason for hiding this comment

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

Whether the auto behavior starts off as pan or zoom should perhaps also be configurable.

It would also be nice to remember the last state the user was in in localstorage, so the same one can be used initially when loading the page again. Not sure if this is something tldraw should do automatically, or if it should emit an event on changes and allow this state to be set, so it can be implemented outside of tldraw?

Choose a reason for hiding this comment

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

I added a config option for auto behavior and I added an event that is emitted when it's changed.

This implements support for setting wheelBehavior to auto. What this
means is that it will try to detect if you're using a mouse or a
touchpad, and use wheelBehavior zoom when a mouse is used and pan when a
touchpad is used. This is more intuitive behavior than always having it
set to wheel or pan in my opinion.

As far as I know, it's unfortunately not possible to know if wheel
events are generated from a mouse or a touchpad. The heuristic
implemented is that if you scroll horizontally it assumes you use a
touchpad and switch to pan, and if you pan with the middle mouse button
it assumes you're using a mouse and switches to zoom. This seems to be
similar to what other similar software does.

Fixes tldraw#4579
wheelBehavior: 'zoom' | 'pan' | 'none'
wheelBehavior: 'auto' | 'zoom' | 'pan' | 'none'
/** The behavior used for the wheel when `wheelBehavior` is set to auto. */
autoWheelBehavior?: 'zoom' | 'pan'

Choose a reason for hiding this comment

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

Do you think it makes sense to have this in camera options?

When `wheelBehavior` is set to `'auto'`, this controls whether the
behavior used is `'pan'` or `'zoom'`. The value will automatically be
changed on certain user interactions. Specifically it will change to
`'pan'` when scrolling horizontally and change to `'zoom'` when the
canvas is panned by either spacebar dragging or middle mouse dragging.
This allows you to listen to changes to `autoWheelBehavior` and store it
in e.g. local storage so you can initialize it to the same value the
next time tldraw is laoded.
The reason for this is that pinch to zoom on a touchpad generates
vertical scroll events with ctrlKey set to true.
@trygve-aaberge-adsk
Copy link
Author

/huppy check cla

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Product improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: Make scroll zoom with mouse, but pan with trackpad
2 participants