Skip to content

Commit

Permalink
Scroll wheel scrolls tabs horizontally
Browse files Browse the repository at this point in the history
  • Loading branch information
sfsam committed Jan 18, 2021
1 parent 210631e commit 2d70c5b
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/MacVim/MMTabline/MMTabline.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ @implementation MMTabline
NSInteger _finalDraggedTabIndex;
MMHoverButton *_leftScrollButton;
MMHoverButton *_rightScrollButton;
id _scrollWheelEventMonitor;
}

- (instancetype)initWithFrame:(NSRect)frameRect
Expand Down Expand Up @@ -78,6 +79,30 @@ - (instancetype)initWithFrame:(NSRect)frameRect
[self addConstraint:_addTabButtonTrailingConstraint];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didScroll:) name:NSViewBoundsDidChangeNotification object:_scrollView.contentView];

// Monitor for scroll wheel events so we can scroll the tabline
// horizontally without the user having to hold down SHIFT.
_scrollWheelEventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskScrollWheel handler:^NSEvent * _Nullable(NSEvent * _Nonnull event) {
NSPoint location = [_scrollView convertPoint:event.locationInWindow fromView:nil];
// We want events:
// where the mouse is over the _scrollView
// and where the user is not modifying it with the SHIFT key
// and initiated by the scroll wheel and not the trackpad
if ([_scrollView mouse:location inRect:_scrollView.bounds]
&& !event.modifierFlags
&& !event.hasPreciseScrollingDeltas)
{
// Create a new scroll wheel event based on the original,
// but set the new deltaX to the original's deltaY.
// stackoverflow.com/a/38991946/111418
CGEventRef cgEvent = CGEventCreateCopy(event.CGEvent);
CGEventSetIntegerValueField(cgEvent, kCGScrollWheelEventDeltaAxis2, event.scrollingDeltaY);
NSEvent *newEvent = [NSEvent eventWithCGEvent:cgEvent];
CFRelease(cgEvent);
return newEvent;
}
return event;
}];
}
return self;
}
Expand Down

0 comments on commit 2d70c5b

Please sign in to comment.