objective-ccocoansevent

How can I change a scrollWheel: handler into a magnifyWithEvent: call?


First post here!

I have an NSScrollView subclass. I want a mouse's option-scrollwheel events to cause the same zooming that trackpad pinch-zooming causes. I tried the simple solution below, but that just causes lots of assertions, probably because the scroll event is the wrong type and doesn't have the needed data for a magnify event. I don't see handy NSEvent class methods for creating magnification events, like they offer for mouse, key, enter/exit, and other events.

-(void) scrollWheel:(NSEvent*)event
{
    if(event.modifierFlags & NSAlternateKeyMask)
        [self magnifyWithEvent:event];
    else
        [super scrollWheel:event];
}

Error output:

2018-01-18 11:35:59.509 BoF[71240:2227373] -_continuousScroll is deprecated for NSScrollWheel. Please use -hasPreciseScrollingDeltas.
2018-01-18 11:35:59.509 BoF[71240:2227373] -deviceDeltaX is deprecated for NSScrollWheel. Please use -scrollingDeltaX.
2018-01-18 11:35:59.509 BoF[71240:2227373] -deviceDeltaY is deprecated for NSScrollWheel. Please use -scrollingDeltaY.
2018-01-18 11:35:59.509 BoF[71240:2227373] *** Assertion failure in -[NSEvent magnification], /SourceCache/AppKit/AppKit-1348.17/AppKit.subproj/NSEvent.m:1890
2018-01-18 11:35:59.509 BoF[71240:2227373] Invalid message sent to event "NSEvent: type=ScrollWheel loc=(371,197) time=80177.1 flags=0x80120 win=0x6180001f8000 winNum=3959 ctxt=0x0 deltaX=0.000000 deltaY=0.400024 deltaZ=0.000000 deviceDeltaX=0.000000 deviceDeltaY=3.000000 deviceDeltaZ=0.000000 count:0 phase=Changed momentumPhase=None"
2018-01-18 11:35:59.643 BoF[71240:2227373] (
    0   CoreFoundation                      0x00007fff90c6e03c __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x00007fff8282176e objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff90c6de1a +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x00007fff8632897b -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   AppKit                              0x00007fff8f96c8c2 -[NSEvent magnification] + 182
    5   AppKit                              0x00007fff8f96c488 -[NSScrollView magnifyWithEvent:] + 395
    6   BoF                      0x000000010002ceb3 -[ComicScrollView scrollWheel:] + 99
    7   AppKit                              0x00007fff8f7c4cfb forwardMethod + 126
    8   AppKit                              0x00007fff8f8f5d00 -[NSView scrollWheel:] + 507
    9   AppKit                              0x00007fff8f7c4cfb forwardMethod + 126
    10  AppKit                              0x00007fff8f8f5d00 -[NSView scrollWheel:] + 507
    11  AppKit                              0x00007fff8fe3b6cc -[NSWindow _reallySendEvent:isDelayedEvent:] + 6941
    12  AppKit                              0x00007fff8f7ccc86 -[NSWindow sendEvent:] + 470
    13  AppKit                              0x00007fff8f7c9137 -[NSApplication sendEvent:] + 2285
    14  AppKit                              0x00007fff8f6f2b68 -[NSApplication run] + 711
    15  AppKit                              0x00007fff8f66f244 NSApplicationMain + 1832
    16  BoF                      0x000000010000f232 main + 34
    17  libdyld.dylib                       0x00007fff848f95c9 start + 1
    18  ???                                 0x0000000000000003 0x0 + 3
)

Solution

  • Well, it's not using the standard magnifyWithEvent: code path, but it works to fake it like so, making assumptions about the multiplier to convert the scroll amount to a magnification amount.

    -(void) scrollWheel:(NSEvent*)event
    {
        if(event.modifierFlags & NSAlternateKeyMask) {
            NSPoint         pt = [self.documentView convertPoint:event.locationInWindow fromView:nil];
            CGFloat         by = event.scrollingDeltaY * 0.001; // The smallest pinch-zoom amount seems to be about 0.002, but that was a bit too coarse.
    
            if(!event.hasPreciseScrollingDeltas)
                by *= self.verticalLineScroll;
    
            [self setMagnification:self.magnification + by centeredAtPoint:pt];
        }
        else
            [super scrollWheel:event];
    }