From 6ec45262732f1a01b4cec1856e1d3820b4f52461 Mon Sep 17 00:00:00 2001 From: Viktor Sukochev Date: Wed, 23 Aug 2017 13:09:46 +0700 Subject: [PATCH] Minor refactoring --- Source/platform/macOS/MView_macOS.swift | 109 +++++++----------------- Source/views/MacawView.swift | 1 - 2 files changed, 33 insertions(+), 77 deletions(-) diff --git a/Source/platform/macOS/MView_macOS.swift b/Source/platform/macOS/MView_macOS.swift index fc0cbb25..6afc6155 100644 --- a/Source/platform/macOS/MView_macOS.swift +++ b/Source/platform/macOS/MView_macOS.swift @@ -143,30 +143,9 @@ import Foundation private func subscribeForMouseDown() { NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown, handler: { [weak self] event -> NSEvent? in - guard let weakSelf = self else { - return event - } - - // Touch pad - guard event.subtype == .touch else { - let touchPoints = event.touches(matching: .any, in: self).map { touch -> MTouchEvent in - let location = touch.location(in: weakSelf) - let id = Int(bitPattern: Unmanaged.passUnretained(touch).toOpaque()) - - return MTouchEvent(x: Double(location.x), y: Double(location.y), id: id) - } - - weakSelf.mTouchesBegan(touchPoints) - return event - } - - // Mouse - let location = weakSelf.convert(event.locationInWindow, to: .none) - let id = 0 - let touchPoint = MTouchEvent(x: Double(location.x), y: Double(location.y), id: id) - - - weakSelf.mTouchesBegan([touchPoint]) + self?.handleInput(event: event, handler: { touches in + self?.mTouchesBegan(touches) + }) return event }) @@ -174,67 +153,45 @@ import Foundation private func subscribeForMouseUp() { NSEvent.addLocalMonitorForEvents(matching: .leftMouseUp, handler: { [weak self] event -> NSEvent? in - guard let weakSelf = self else { - return event - } + self?.handleInput(event: event, handler: { touches in + self?.mTouchesEnded(touches) + }) + + return event + }) + } - // Touch pad - guard event.subtype == .touch else { - let touchPoints = event.touches(matching: .any, in: self).map { touch -> MTouchEvent in - let location = touch.location(in: weakSelf) - let id = Int(bitPattern: Unmanaged.passUnretained(touch).toOpaque()) - - return MTouchEvent(x: Double(location.x), y: Double(location.y), id: id) - } - - weakSelf.mTouchesEnded(touchPoints) - return event - } - - // Mouse - let location = weakSelf.convert(event.locationInWindow, to: .none) - let id = 0 - - let touchPoint = MTouchEvent(x: Double(location.x), y: Double(location.y), id: id) - - - weakSelf.mTouchesEnded([touchPoint]) + private func subscribeForMouseDragged() { + NSEvent.addLocalMonitorForEvents(matching: .leftMouseDragged, handler: { [weak self] event -> NSEvent? in + self?.handleInput(event: event, handler: { touches in + self?.mTouchesMoved(touches) + }) return event }) } - - private func subscribeForMouseDragged() { - NSEvent.addLocalMonitorForEvents(matching: .leftMouseDragged, handler: { [weak self] event -> NSEvent? in - guard let weakSelf = self else { - return event - } - - // Touch pad - guard event.subtype == .touch else { - let touchPoints = event.touches(matching: .any, in: self).map { touch -> MTouchEvent in - let location = touch.location(in: weakSelf) - let id = Int(bitPattern: Unmanaged.passUnretained(touch).toOpaque()) - - return MTouchEvent(x: Double(location.x), y: Double(location.y), id: id) - } + private func handleInput(event: NSEvent, handler: (_ touches: [MTouchEvent]) -> Void ) { + // Touch pad + guard event.subtype == .touch else { + let touchPoints = event.touches(matching: .any, in: self).map { touch -> MTouchEvent in + let location = touch.location(in: self) + let id = Int(bitPattern: Unmanaged.passUnretained(touch).toOpaque()) - weakSelf.mTouchesMoved(touchPoints) - return event + return MTouchEvent(x: Double(location.x), y: Double(location.y), id: id) } - // Mouse - let location = weakSelf.convert(event.locationInWindow, to: .none) - let id = 0 - - let touchPoint = MTouchEvent(x: Double(location.x), y: Double(location.y), id: 0) - - - weakSelf.mTouchesMoved([touchPoint]) - - return event - }) + handler(touchPoints) + return + } + + // Mouse + let location = self.convert(event.locationInWindow, to: .none) + let touchPoint = MTouchEvent(x: Double(location.x), y: Double(location.y), id: 0) + + handler([touchPoint]) + + return } // MARK: - Touchable diff --git a/Source/views/MacawView.swift b/Source/views/MacawView.swift index 10167cfd..cc877237 100644 --- a/Source/views/MacawView.swift +++ b/Source/views/MacawView.swift @@ -254,7 +254,6 @@ override func mTouchesBegan(_ touches: [MTouchEvent]) { for touch in touches { let location = CGPoint(x: touch.x, y: touch.y) - NSLog("\(location)") var foundNode: Node? = .none localContext { ctx in foundNode = renderer.findNodeAt(location: location, ctx: ctx)