1
0
mirror of https://github.com/mpv-player/mpv synced 2025-01-20 22:40:52 +00:00

mac/input: move mouse event handling into input helper

This commit is contained in:
der richter 2024-03-13 23:34:29 +01:00
parent ab419a6660
commit db4eac140d
2 changed files with 40 additions and 47 deletions

View File

@ -115,6 +115,16 @@ class InputHelper: NSObject {
}
}
func processMouse(event: NSEvent) {
if !mouseEnabled() { return }
lock.withLock {
putKey(map(button: event.buttonNumber), modifiers: event.modifierFlags, type: event.type)
if event.clickCount > 1 {
putKey(map(button: event.buttonNumber), modifiers: event.modifierFlags, type: .keyUp)
}
}
}
func draggable(at pos: NSPoint) -> Bool {
lock.withLock {
guard let input = input else { return false }
@ -158,6 +168,12 @@ class InputHelper: NSObject {
let typeMapping: [NSEvent.EventType:UInt32] = [
.keyDown: MP_KEY_STATE_DOWN,
.keyUp: MP_KEY_STATE_UP,
.leftMouseDown: MP_KEY_STATE_DOWN,
.leftMouseUp: MP_KEY_STATE_UP,
.rightMouseDown: MP_KEY_STATE_DOWN,
.rightMouseUp: MP_KEY_STATE_UP,
.otherMouseDown: MP_KEY_STATE_DOWN,
.otherMouseUp: MP_KEY_STATE_UP,
]
return Int32(typeMapping[type] ?? 0);
@ -182,6 +198,18 @@ class InputHelper: NSObject {
return Int32(mask)
}
private func map(button: Int) -> Int32 {
let buttonMapping: [Int:Int32] = [
0: SWIFT_MBTN_LEFT,
1: SWIFT_MBTN_RIGHT,
2: SWIFT_MBTN_MID,
3: SWIFT_MBTN_BACK,
4: SWIFT_MBTN_FORWARD,
]
return Int32(buttonMapping[button] ?? SWIFT_MBTN9 + Int32(button - 5));
}
@objc func open(files: [String]) {
lock.withLock {
guard let input = input else { return }

View File

@ -145,40 +145,34 @@ class View: NSView, CALayerDelegate {
}
override func mouseDown(with event: NSEvent) {
if input?.mouseEnabled() ?? true {
signalMouseDown(event)
}
hasMouseDown = true
input?.processMouse(event: event)
}
override func mouseUp(with event: NSEvent) {
if input?.mouseEnabled() ?? true {
signalMouseUp(event)
}
hasMouseDown = false
common.window?.isMoving = false
input?.processMouse(event: event)
}
override func rightMouseDown(with event: NSEvent) {
if input?.mouseEnabled() ?? true {
signalMouseDown(event)
}
hasMouseDown = true
input?.processMouse(event: event)
}
override func rightMouseUp(with event: NSEvent) {
if input?.mouseEnabled() ?? true {
signalMouseUp(event)
}
hasMouseDown = false
input?.processMouse(event: event)
}
override func otherMouseDown(with event: NSEvent) {
if input?.mouseEnabled() ?? true {
signalMouseDown(event)
}
hasMouseDown = true
input?.processMouse(event: event)
}
override func otherMouseUp(with event: NSEvent) {
if input?.mouseEnabled() ?? true {
signalMouseUp(event)
}
hasMouseDown = false
input?.processMouse(event: event)
}
override func magnify(with event: NSEvent) {
@ -188,23 +182,6 @@ class View: NSView, CALayerDelegate {
common.window?.addWindowScale(Double(event.magnification))
}
func signalMouseDown(_ event: NSEvent) {
signalMouseEvent(event, MP_KEY_STATE_DOWN)
if event.clickCount > 1 {
signalMouseEvent(event, MP_KEY_STATE_UP)
}
}
func signalMouseUp(_ event: NSEvent) {
signalMouseEvent(event, MP_KEY_STATE_UP)
}
func signalMouseEvent(_ event: NSEvent, _ state: UInt32) {
hasMouseDown = state == MP_KEY_STATE_DOWN
let mpkey = getMpvButton(event)
input?.put(key: (mpkey | Int32(state)), modifiers: event.modifierFlags)
}
func signalMouseMovement(_ event: NSEvent) {
var point = convert(event.locationInWindow, from: nil)
point = convertToBacking(point)
@ -283,16 +260,4 @@ class View: NSView, CALayerDelegate {
guard let window = common.window else { return false }
return !hasMouseDown && containsMouseLocation() && window.isKeyWindow
}
func getMpvButton(_ event: NSEvent) -> Int32 {
let buttonNumber = event.buttonNumber
switch (buttonNumber) {
case 0: return SWIFT_MBTN_LEFT
case 1: return SWIFT_MBTN_RIGHT
case 2: return SWIFT_MBTN_MID
case 3: return SWIFT_MBTN_BACK
case 4: return SWIFT_MBTN_FORWARD
default: return SWIFT_MBTN9 + Int32(buttonNumber - 5)
}
}
}