mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 09:28:38 +00:00
Rewrote presets parsing. Instead of parsing presets in a separate file using bunch of huge enums it is now parsed inside each object. To implement a new class: 1. Derive your class from CustomTouchBarItem (for static) or CustomButtonTouchBarItem (for buttons) 2. Override class var typeIdentifier with your object identificator 3. Override init(from decoder: Decoder) and read all custom json params you need 4. Don't forget to call super.init(identifier: CustomTouchBarItem.createIdentifier(type)) in the init() function 5. Add your new class to BarItemDefinition.types in ItemParsing.swift Good example is PomodoroBarItem If you want to inherid from some other NS class (NSSlider or NSPopoverTouchBarItem or other) then look into GroupBarItem and BrightnessViewController
93 lines
3.0 KiB
Swift
93 lines
3.0 KiB
Swift
//
|
|
// SwipeItem.swift
|
|
// MTMR
|
|
//
|
|
// Created by Fedor Zaitsev on 3/29/20.
|
|
// Copyright © 2020 Anton Palgunov. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import Foundation
|
|
|
|
class SwipeItem: CustomTouchBarItem {
|
|
private var scriptApple: NSAppleScript?
|
|
private var scriptBash: String?
|
|
private var direction: String
|
|
private var fingers: Int
|
|
private var minOffset: Float
|
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
case sourceApple
|
|
case sourceBash
|
|
case direction
|
|
case fingers
|
|
case minOffset
|
|
}
|
|
|
|
override class var typeIdentifier: String {
|
|
return "swipe"
|
|
}
|
|
|
|
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")
|
|
}
|
|
|
|
required init(from decoder: Decoder) throws {
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
self.scriptApple = try container.decodeIfPresent(Source.self, forKey: .sourceApple)?.appleScript
|
|
self.scriptBash = try container.decodeIfPresent(Source.self, forKey: .sourceBash)?.string
|
|
self.direction = try container.decode(String.self, forKey: .direction)
|
|
self.fingers = try container.decode(Int.self, forKey: .fingers)
|
|
self.minOffset = try container.decodeIfPresent(Float.self, forKey: .minOffset) ?? 0.0
|
|
|
|
|
|
try super.init(from: decoder)
|
|
}
|
|
|
|
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)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|