From 8ec705003393f4a4b7430c81682541158e803816 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Tue, 10 Nov 2020 12:17:54 +0200 Subject: macOS: Improved scrolling using mouse wheel This required further SDL hacking. The macOS SDL .diff was updated. IssueID #26 --- sdl2-macos-mouse-scrolling-patch.diff | 28 -------------------------- sdl2-macos-mouse-scrolling.diff | 36 ++++++++++++++++++++++++++++++++++ src/ui/documentwidget.c | 37 +++++++++++++++++++++++------------ 3 files changed, 60 insertions(+), 41 deletions(-) delete mode 100644 sdl2-macos-mouse-scrolling-patch.diff create mode 100644 sdl2-macos-mouse-scrolling.diff diff --git a/sdl2-macos-mouse-scrolling-patch.diff b/sdl2-macos-mouse-scrolling-patch.diff deleted file mode 100644 index 633243ed..00000000 --- a/sdl2-macos-mouse-scrolling-patch.diff +++ /dev/null @@ -1,28 +0,0 @@ -diff -r 4f06c06b6d19 src/events/SDL_mouse.c ---- a/src/events/SDL_mouse.c Wed Aug 05 15:28:51 2020 +0200 -+++ b/src/events/SDL_mouse.c Tue Sep 15 07:54:17 2020 +0300 -@@ -642,8 +642,8 @@ - event.wheel.preciseX = x; - event.wheel.preciseY = y; - #endif -- event.wheel.x = integral_x; -- event.wheel.y = integral_y; -+ event.wheel.x = x; //integral_x; -+ event.wheel.y = y; //integral_y; - event.wheel.direction = (Uint32)direction; - posted = (SDL_PushEvent(&event) > 0); - } -diff -r 4f06c06b6d19 src/video/cocoa/SDL_cocoamouse.m ---- a/src/video/cocoa/SDL_cocoamouse.m Wed Aug 05 15:28:51 2020 +0200 -+++ b/src/video/cocoa/SDL_cocoamouse.m Tue Sep 15 07:54:17 2020 +0300 -@@ -424,8 +424,8 @@ - } - - SDL_MouseID mouseID = mouse->mouseID; -- CGFloat x = -[event deltaX]; -- CGFloat y = [event deltaY]; -+ CGFloat x = -[event scrollingDeltaX]; -+ CGFloat y = [event scrollingDeltaY]; - SDL_MouseWheelDirection direction = SDL_MOUSEWHEEL_NORMAL; - - if ([event respondsToSelector:@selector(isDirectionInvertedFromDevice)]) { diff --git a/sdl2-macos-mouse-scrolling.diff b/sdl2-macos-mouse-scrolling.diff new file mode 100644 index 00000000..e2f30c24 --- /dev/null +++ b/sdl2-macos-mouse-scrolling.diff @@ -0,0 +1,36 @@ +diff -r 4f06c06b6d19 src/events/SDL_mouse.c +--- a/src/events/SDL_mouse.c Wed Aug 05 15:28:51 2020 +0200 ++++ b/src/events/SDL_mouse.c Tue Nov 10 12:16:06 2020 +0200 +@@ -642,8 +642,8 @@ + event.wheel.preciseX = x; + event.wheel.preciseY = y; + #endif +- event.wheel.x = integral_x; +- event.wheel.y = integral_y; ++ event.wheel.x = x; //integral_x; ++ event.wheel.y = y; //integral_y; + event.wheel.direction = (Uint32)direction; + posted = (SDL_PushEvent(&event) > 0); + } +diff -r 4f06c06b6d19 src/video/cocoa/SDL_cocoamouse.m +--- a/src/video/cocoa/SDL_cocoamouse.m Wed Aug 05 15:28:51 2020 +0200 ++++ b/src/video/cocoa/SDL_cocoamouse.m Tue Nov 10 12:16:06 2020 +0200 +@@ -424,10 +424,16 @@ + } + + SDL_MouseID mouseID = mouse->mouseID; +- CGFloat x = -[event deltaX]; +- CGFloat y = [event deltaY]; ++ CGFloat x = -[event scrollingDeltaX]; ++ CGFloat y = [event scrollingDeltaY]; + SDL_MouseWheelDirection direction = SDL_MOUSEWHEEL_NORMAL; + ++ /* HACK: Make a distinction between precise and imprecise scrolling. ++ Trackpad seems to be mouseID 0. */ ++ if (![event hasPreciseScrollingDeltas]) { ++ mouseID = 1; ++ } ++ + if ([event respondsToSelector:@selector(isDirectionInvertedFromDevice)]) { + if ([event isDirectionInvertedFromDevice] == YES) { + direction = SDL_MOUSEWHEEL_FLIPPED; diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c index a4bf38dc..c71758be 100644 --- a/src/ui/documentwidget.c +++ b/src/ui/documentwidget.c @@ -1891,21 +1891,32 @@ static iBool processEvent_DocumentWidget_(iDocumentWidget *d, const SDL_Event *e } } #if defined (iPlatformApple) - /* Momentum scrolling. */ - stop_Anim(&d->scrollY); - scroll_DocumentWidget_(d, -ev->wheel.y * get_Window()->pixelRatio * acceleration); -#else - if (keyMods_Sym(SDL_GetModState()) == KMOD_PRIMARY) { - postCommandf_App("zoom.delta arg:%d", ev->wheel.y > 0 ? 10 : -10); - return iTrue; + /* On macOS, we handle both trackpad and mouse events. We expect SDL to identify + which device is sending the event. */ + if (ev->wheel.which == 0) { /* Trackpad with precise scrolling w/inertia. */ + stop_Anim(&d->scrollY); + scroll_DocumentWidget_(d, -ev->wheel.y * get_Window()->pixelRatio * acceleration); } - smoothScroll_DocumentWidget_( - d, - -3 * ev->wheel.y * lineHeight_Text(paragraph_FontId) * acceleration, - smoothDuration_DocumentWidget_ * - (!isFinished_Anim(&d->scrollY) && pos_Anim(&d->scrollY) < 0.25f ? 0.5f : 1.0f)); - /* accelerated speed for repeated wheelings */ + else +#endif + /* Traditional mouse wheel. */ { +#if defined (iPlatformApple) + /* Disregard wheel acceleration applied by the OS. */ + const int amount = iSign(ev->wheel.y); +#else + const int amount = ev->wheel.y; #endif + if (keyMods_Sym(SDL_GetModState()) == KMOD_PRIMARY) { + postCommandf_App("zoom.delta arg:%d", amount > 0 ? 10 : -10); + return iTrue; + } + smoothScroll_DocumentWidget_( + d, + -3 * amount * lineHeight_Text(paragraph_FontId) * acceleration, + smoothDuration_DocumentWidget_ * + (!isFinished_Anim(&d->scrollY) && pos_Anim(&d->scrollY) < 0.25f ? 0.5f : 1.0f)); + /* accelerated speed for repeated wheelings */ + } iChangeFlags(d->flags, noHoverWhileScrolling_DocumentWidgetFlag, iTrue); return iTrue; } -- cgit v1.2.3