-
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updated README Added explanation for missing parameters (background, title and image) * Implemented changable icons for AppleScriptTouchBarItem AppleScriptTouchBarItem now allow to specify any number of icons which can be changed from the script. You cannot change icon from touch event. To change icon, you need to return array from your script with 2 values - title and icn name. More info in readme * Implemented custom swipe actions Co-authored-by: Fedor Zaitsev <lobster@Fedors-MacBook-Pro.local>
- Loading branch information
1 parent
42ce95b
commit a0fc0b3
Showing
9 changed files
with
252 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// | ||
// BasicView.swift | ||
// MTMR | ||
// | ||
// Created by Fedor Zaitsev on 3/29/20. | ||
// Copyright © 2020 Anton Palgunov. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
class BasicView: NSCustomTouchBarItem, NSGestureRecognizerDelegate { | ||
var twofingers: NSPanGestureRecognizer! | ||
var threefingers: NSPanGestureRecognizer! | ||
var fourfingers: NSPanGestureRecognizer! | ||
var swipeItems: [SwipeItem] = [] | ||
var prevPositions: [Int: CGFloat] = [2:0, 3:0, 4:0] | ||
|
||
// legacy gesture positions | ||
// by legacy I mean gestures to increse/decrease volume/brigtness which can be checked from app menu | ||
var legacyPrevPositions: [Int: CGFloat] = [2:0, 3:0, 4:0] | ||
var legacyGesturesEnabled = false | ||
|
||
init(identifier: NSTouchBarItem.Identifier, items: [NSTouchBarItem], swipeItems: [SwipeItem]) { | ||
super.init(identifier: identifier) | ||
self.swipeItems = swipeItems | ||
let views = items.compactMap { $0.view } | ||
let stackView = NSStackView(views: views) | ||
stackView.spacing = 1 | ||
stackView.orientation = .horizontal | ||
view = stackView | ||
|
||
twofingers = NSPanGestureRecognizer(target: self, action: #selector(twofingersHandler(_:))) | ||
twofingers.numberOfTouchesRequired = 2 | ||
twofingers.allowedTouchTypes = .direct | ||
view.addGestureRecognizer(twofingers) | ||
|
||
threefingers = NSPanGestureRecognizer(target: self, action: #selector(threefingersHandler(_:))) | ||
threefingers.numberOfTouchesRequired = 3 | ||
threefingers.allowedTouchTypes = .direct | ||
view.addGestureRecognizer(threefingers) | ||
|
||
fourfingers = NSPanGestureRecognizer(target: self, action: #selector(fourfingersHandler(_:))) | ||
fourfingers.numberOfTouchesRequired = 4 | ||
fourfingers.allowedTouchTypes = .direct | ||
view.addGestureRecognizer(fourfingers) | ||
} | ||
|
||
required init?(coder _: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
func gestureHandler(position: CGFloat, fingers: Int, state: NSGestureRecognizer.State) { | ||
switch state { | ||
case .began: | ||
prevPositions[fingers] = position | ||
legacyPrevPositions[fingers] = position | ||
case .changed: | ||
if self.legacyGesturesEnabled { | ||
if fingers == 2 { | ||
let prevPos = legacyPrevPositions[fingers]! | ||
if ((position - prevPos) > 10) || ((prevPos - position) > 10) { | ||
if position > prevPos { | ||
HIDPostAuxKey(NX_KEYTYPE_SOUND_UP) | ||
} else if position < prevPos { | ||
HIDPostAuxKey(NX_KEYTYPE_SOUND_DOWN) | ||
} | ||
legacyPrevPositions[fingers] = position | ||
} | ||
} | ||
if fingers == 3 { | ||
let prevPos = legacyPrevPositions[fingers]! | ||
if ((position - prevPos) > 15) || ((prevPos - position) > 15) { | ||
if position > prevPos { | ||
GenericKeyPress(keyCode: CGKeyCode(144)).send() | ||
} else if position < prevPos { | ||
GenericKeyPress(keyCode: CGKeyCode(145)).send() | ||
} | ||
legacyPrevPositions[fingers] = position | ||
} | ||
} | ||
} | ||
case .ended: | ||
print("gesture ended \(position - prevPositions[fingers]!) \(fingers)") | ||
for item in swipeItems { | ||
item.processEvent(offset: position - prevPositions[fingers]!, fingers: fingers) | ||
} | ||
default: | ||
break | ||
} | ||
} | ||
|
||
@objc func twofingersHandler(_ sender: NSGestureRecognizer?) { | ||
let position = (sender?.location(in: sender?.view).x)! | ||
self.gestureHandler(position: position, fingers: 2, state: sender!.state) | ||
} | ||
|
||
@objc func threefingersHandler(_ sender: NSGestureRecognizer?) { | ||
let position = (sender?.location(in: sender?.view).x)! | ||
self.gestureHandler(position: position, fingers: 3, state: sender!.state) | ||
} | ||
|
||
@objc func fourfingersHandler(_ sender: NSGestureRecognizer?) { | ||
let position = (sender?.location(in: sender?.view).x)! | ||
self.gestureHandler(position: position, fingers: 4, state: sender!.state) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// SwipeItem.swift | ||
// MTMR | ||
// | ||
// Created by Fedor Zaitsev on 3/29/20. | ||
// Copyright © 2020 Anton Palgunov. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Foundation | ||
|
||
class SwipeItem: NSCustomTouchBarItem { | ||
private var scriptApple: NSAppleScript? | ||
private var scriptBash: String? | ||
private var direction: String | ||
private var fingers: Int | ||
private var minOffset: Float | ||
init?(identifier: NSTouchBarItem.Identifier, direction: String, fingers: Int, minOffset: Float, sourceApple: SourceProtocol?, sourceBash: SourceProtocol?) { | ||
self.direction = direction | ||
self.fingers = fingers | ||
self.scriptBash = sourceBash?.string | ||
self.scriptApple = sourceApple?.appleScript | ||
self.minOffset = minOffset | ||
super.init(identifier: identifier) | ||
} | ||
|
||
required init?(coder _: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
func processEvent(offset: CGFloat, fingers: Int) { | ||
if direction == "right" && Float(offset) > self.minOffset && self.fingers == fingers { | ||
self.execute() | ||
} | ||
if direction == "left" && Float(offset) < -self.minOffset && self.fingers == fingers { | ||
self.execute() | ||
} | ||
} | ||
|
||
func execute() { | ||
if scriptApple != nil { | ||
DispatchQueue.appleScriptQueue.async { | ||
var error: NSDictionary? | ||
self.scriptApple?.executeAndReturnError(&error) | ||
if let error = error { | ||
print("SwipeItem apple script error: \(error)") | ||
return | ||
} | ||
} | ||
} | ||
if scriptBash != nil { | ||
DispatchQueue.shellScriptQueue.async { | ||
let task = Process() | ||
task.launchPath = "/bin/bash" | ||
task.arguments = ["-c", self.scriptBash!] | ||
task.launch() | ||
task.waitUntilExit() | ||
|
||
|
||
if (task.terminationStatus != 0) { | ||
print("SwipeItem bash script error. Status: \(task.terminationStatus)") | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.