Skip to content

Scrollwheel/trackpad scrolling for MacOS #5012

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions Source/Charts/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,104 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
}
#endif

#if os(macOS)
open override func scrollWheel(with event: NSEvent) {
let wheelTranslation = CGPoint(x: event.scrollingDeltaX, y: event.scrollingDeltaY)

let movementPhase: NSEvent.Phase
if event.phase != [] {
movementPhase = event.phase
} else if isDragDecelerationEnabled {
movementPhase = event.momentumPhase
} else {
movementPhase = []
}

if movementPhase == .began
{
stopDeceleration()

if data === nil || !self.isDragEnabled
{ // If we have no data, we have nothing to pan and no data to highlight
return
}

// If drag is enabled and we are in a position where there's something to drag:
// * If we're zoomed in, then obviously we have something to drag.
// * If we have a drag offset - we always have something to drag
if !self.hasNoDragOffset || !self.isFullyZoomedOut
{

var translation = wheelTranslation
if !self.dragXEnabled
{
translation.x = 0.0
}
else if !self.dragYEnabled
{
translation.y = 0.0
}

let didUserDrag = translation.x != 0.0 || translation.y != 0.0

// Check to see if user dragged at all and if so, can the chart be dragged by the given amount
if didUserDrag && !performPanChange(translation: translation)
{
if _outerScrollView !== nil
{
// We can stop dragging right now, and let the scroll view take control
_outerScrollView = nil
}
}
else
{
if _outerScrollView !== nil
{
// Prevent the parent scroll view from scrolling
_outerScrollView?.nsuiIsScrollEnabled = false
}
}
}
}
else if movementPhase == .changed
{
var translation = wheelTranslation

if !self.dragXEnabled
{
translation.x = 0.0
}
else if !self.dragYEnabled
{
translation.y = 0.0
}

let _ = performPanChange(translation: translation)
}
else if movementPhase == .ended || movementPhase == .cancelled
{
if isDragDecelerationEnabled
{
stopDeceleration()

_decelerationLastTime = CACurrentMediaTime()
_decelerationVelocity = .zero

_decelerationDisplayLink = NSUIDisplayLink(target: self, selector: #selector(BarLineChartViewBase.decelerationLoop))
_decelerationDisplayLink.add(to: RunLoop.main, forMode: RunLoop.Mode.common)
}

if _outerScrollView !== nil
{
_outerScrollView?.nsuiIsScrollEnabled = true
_outerScrollView = nil
}

delegate?.chartViewDidEndPanning?(self)
}
}
#endif

@objc private func panGestureRecognized(_ recognizer: NSUIPanGestureRecognizer)
{
if recognizer.state == NSUIGestureRecognizerState.began && recognizer.nsuiNumberOfTouches() > 0
Expand Down