mac/input: move scroll wheel event handling into input helper

This commit is contained in:
der richter 2024-03-14 23:58:10 +01:00
parent 9a4c673dd7
commit a4eddf742d
2 changed files with 41 additions and 40 deletions

View File

@ -125,6 +125,43 @@ class InputHelper: NSObject {
} }
} }
private func preciseScroll(_ event: NSEvent) {
var delta: Double
var cmd: Int32
if abs(event.deltaY) >= abs(event.deltaX) {
delta = Double(event.deltaY) * 0.1
cmd = delta > 0 ? SWIFT_WHEEL_UP : SWIFT_WHEEL_DOWN
} else {
delta = Double(event.deltaX) * 0.1
cmd = delta > 0 ? SWIFT_WHEEL_LEFT : SWIFT_WHEEL_RIGHT
}
putAxis(cmd, modifiers: event.modifierFlags, delta: abs(delta))
}
func processWheel(event: NSEvent) {
if !mouseEnabled() { return }
lock.withLock {
if event.hasPreciseScrollingDeltas {
preciseScroll(event)
} else {
let modifiers = event.modifierFlags
let deltaX = modifiers.contains(.shift) ? event.scrollingDeltaY : event.scrollingDeltaX
let deltaY = modifiers.contains(.shift) ? event.scrollingDeltaX : event.scrollingDeltaY
var mpkey: Int32
if abs(deltaY) >= abs(deltaX) {
mpkey = deltaY > 0 ? SWIFT_WHEEL_UP : SWIFT_WHEEL_DOWN
} else {
mpkey = deltaX > 0 ? SWIFT_WHEEL_LEFT : SWIFT_WHEEL_RIGHT
}
putKey(mpkey, modifiers: modifiers)
}
}
}
func draggable(at pos: NSPoint) -> Bool { func draggable(at pos: NSPoint) -> Bool {
lock.withLock { lock.withLock {
guard let input = input else { return false } guard let input = input else { return false }
@ -147,12 +184,10 @@ class InputHelper: NSObject {
} }
} }
func putAxis(_ mpkey: Int32, modifiers: NSEvent.ModifierFlags, delta: Double) { private func putAxis(_ mpkey: Int32, modifiers: NSEvent.ModifierFlags, delta: Double) {
lock.withLock {
guard let input = input else { return } guard let input = input else { return }
mp_input_put_wheel(input, mpkey | mapModifier(modifiers), delta) mp_input_put_wheel(input, mpkey | mapModifier(modifiers), delta)
} }
}
@discardableResult @objc func command(_ cmd: String) -> Bool { @discardableResult @objc func command(_ cmd: String) -> Bool {
lock.withLock { lock.withLock {

View File

@ -189,42 +189,8 @@ class View: NSView, CALayerDelegate {
} }
} }
func preciseScroll(_ event: NSEvent) {
var delta: Double
var cmd: Int32
if abs(event.deltaY) >= abs(event.deltaX) {
delta = Double(event.deltaY) * 0.1
cmd = delta > 0 ? SWIFT_WHEEL_UP : SWIFT_WHEEL_DOWN
} else {
delta = Double(event.deltaX) * 0.1
cmd = delta > 0 ? SWIFT_WHEEL_LEFT : SWIFT_WHEEL_RIGHT
}
input?.putAxis(cmd, modifiers: event.modifierFlags, delta: abs(delta))
}
override func scrollWheel(with event: NSEvent) { override func scrollWheel(with event: NSEvent) {
if !(input?.mouseEnabled() ?? true) { input?.processWheel(event: event)
return
}
if event.hasPreciseScrollingDeltas {
preciseScroll(event)
} else {
let modifiers = event.modifierFlags
let deltaX = modifiers.contains(.shift) ? event.scrollingDeltaY : event.scrollingDeltaX
let deltaY = modifiers.contains(.shift) ? event.scrollingDeltaX : event.scrollingDeltaY
var mpkey: Int32
if abs(deltaY) >= abs(deltaX) {
mpkey = deltaY > 0 ? SWIFT_WHEEL_UP : SWIFT_WHEEL_DOWN
} else {
mpkey = deltaX > 0 ? SWIFT_WHEEL_LEFT : SWIFT_WHEEL_RIGHT
}
input?.put(key: mpkey, modifiers: modifiers)
}
} }
func containsMouseLocation() -> Bool { func containsMouseLocation() -> Bool {