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

macOS: Add a hint for smooth momentum values in SDL_EVENT_MOUSE_WHEEL events #10740

Merged
merged 2 commits into from
Oct 6, 2024

Conversation

jansedivy
Copy link
Contributor

@jansedivy jansedivy commented Sep 6, 2024

This PR adds a new hint for smooth momentum values in SDL_EVENT_MOUSE_WHEEL events on MacOS.

Description

When scrolling on a touchpad on MacOS the scroll values usually contain momentum inside all native applications. But inside SDL the scroll values were without momentum. This PR adds a simple hint to control "AppleMomentumScrollSupported" MacOS API for requesting the native momentum.

Please let me know if there is a better name for the hint or if you have better wording for the documentation part.

Demo

Here is a simple "browser" test application with scrolling to see the difference.

Momentum Video
OFF
Kapture.2024-09-07.at.11.25.43.mp4
ON
Kapture.2024-09-07.at.11.26.18.mp4

Existing Issue(s)

fixes #2176 which mentions adding a hint for this in SDL 3.0.

@jansedivy
Copy link
Contributor Author

jansedivy commented Sep 7, 2024

I was looking at the macOS documentation and Apple recommends using scrollingDeltaX/scrollingDeltaY over deltaX/deltaY properties when reading scroll events. This does provide smoother values that more closely match native applications.

https://developer.apple.com/documentation/appkit/nsevent/1524505-scrollingdeltax?language=objc

x = -[event deltaX];
y = [event deltaY];

Maybe we could switch to that property as well. From my testing, it behaves more closely to what I would expect on MacOS.

Looks like Chromium is using scrollDelta{X,Y} when hasPreciseScrollingDeltas is true and delta{X,Y} when not. But the issue with this is scrollDelta and delta have different sensitivity so we would need to scale it to not break existing SDL applications, or this could all be new behavior behind the hint flag.

https://source.chromium.org/chromium/chromium/src/+/main:ui/events/cocoa/events_mac.mm;l=144-164;drc=ae99ae281ffc2fe19024f86c5c64939efc1b2e87

@jansedivy jansedivy changed the title Add a hint for smooth momentum values in SDL_EVENT_MOUSE_WHEEL events on MacOS MacOS: Add a hint for smooth momentum values in SDL_EVENT_MOUSE_WHEEL events Sep 7, 2024
@jansedivy jansedivy changed the title MacOS: Add a hint for smooth momentum values in SDL_EVENT_MOUSE_WHEEL events macOS: Add a hint for smooth momentum values in SDL_EVENT_MOUSE_WHEEL events Sep 7, 2024
@slouken slouken added this to the 3.2.0 milestone Sep 9, 2024
* - "0": The mouse wheel events will have no momentum. (default)
* - "1": The mouse wheel events will have momentum.
*
* This hint should be set before applicationDidFinishLaunching() is called.
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does this mean in terms of SDL application life cycle? We should answer that for the SDL_HINT_MAC_BACKGROUND_APP hint as well. :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

@jansedivy, can you update the documentation here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@slouken I did some digging to figure out when the hints are used.

There are two flows within the macOS cocoa code to call registerUserDefaults where the momentum is set.

  1. macOS applicationDidFinishLaunching event that's called sometime after SDL_Init/SDL_CreateWindow.
    [SDL3Application registerUserDefaults];
  2. Manual registerUserDefaults under some condition which I am not fully sure about. This is called within SDL_Init.
    if ([NSApp delegate]) {
    /* The SDL app delegate calls this in didFinishLaunching if it's
    * attached to the NSApp, otherwise we need to call it manually.
    */
    [SDL3Application registerUserDefaults];
    }

This means if we want to guarantee the hint will be read in both cases it needs to be set before SDL_Init(). I have updated both documentation comments to reflect that. SDL_HINT_MAC_BACKGROUND_APP has the same code flow.

But it's still possible to set the hint after SDL_Init/SDL_CreateWindow and have applicationDidFinishLaunching event callback pick it up. Do we want to put some guard code to prevent that or are we good with just having the documentation say to set it before SDL_Init?

@slouken slouken added the waiting Waiting on user response label Sep 26, 2024
@slouken slouken removed the waiting Waiting on user response label Oct 6, 2024
@slouken slouken merged commit bf54edd into libsdl-org:main Oct 6, 2024
37 of 38 checks passed
@slouken
Copy link
Collaborator

slouken commented Oct 6, 2024

Merged, thanks!

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.

OS X Scroll Momentum
2 participants