mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38:38 +00:00
action has unified source too
This commit is contained in:
parent
d0cfa45b5d
commit
ea5fbf0ee8
@ -1,27 +1,20 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
class AppleScriptTouchBarItem: NSCustomTouchBarItem {
|
class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
|
||||||
private var script: NSAppleScript!
|
private var script: NSAppleScript!
|
||||||
private let button = NSButton(title: "", target: nil, action: nil)
|
|
||||||
private let interval: TimeInterval
|
private let interval: TimeInterval
|
||||||
private var forceHideConstraint: NSLayoutConstraint!
|
private var forceHideConstraint: NSLayoutConstraint!
|
||||||
|
|
||||||
init?(identifier: NSTouchBarItem.Identifier, source: Source, interval: TimeInterval) {
|
init?(identifier: NSTouchBarItem.Identifier, source: Source, interval: TimeInterval, onTap: @escaping ()->()) {
|
||||||
self.interval = interval
|
self.interval = interval
|
||||||
super.init(identifier: identifier)
|
super.init(identifier: identifier, title: "compile", onTap: onTap)
|
||||||
self.view = button
|
|
||||||
self.forceHideConstraint = self.view.widthAnchor.constraint(equalToConstant: 0)
|
self.forceHideConstraint = self.view.widthAnchor.constraint(equalToConstant: 0)
|
||||||
guard let source = source.string else {
|
guard let script = source.appleScript else {
|
||||||
button.title = "no script"
|
button.title = "no script"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
guard let script = NSAppleScript(source: source) else {
|
|
||||||
button.title = "isn't a script"
|
|
||||||
return
|
|
||||||
}
|
|
||||||
self.script = script
|
self.script = script
|
||||||
button.bezelColor = .clear
|
button.bezelColor = .clear
|
||||||
button.title = "compile"
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
var error: NSDictionary?
|
var error: NSDictionary?
|
||||||
guard script.compileAndReturnError(&error) else {
|
guard script.compileAndReturnError(&error) else {
|
||||||
@ -51,8 +44,6 @@ class AppleScriptTouchBarItem: NSCustomTouchBarItem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func execute() -> String {
|
func execute() -> String {
|
||||||
var error: NSDictionary?
|
var error: NSDictionary?
|
||||||
let output = script.executeAndReturnError(&error)
|
let output = script.executeAndReturnError(&error)
|
||||||
@ -64,3 +55,10 @@ class AppleScriptTouchBarItem: NSCustomTouchBarItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension Source {
|
||||||
|
var appleScript: NSAppleScript? {
|
||||||
|
guard let source = self.string else { return nil }
|
||||||
|
return NSAppleScript(source: source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -145,7 +145,7 @@ enum ActionType: Decodable {
|
|||||||
case none
|
case none
|
||||||
case hidKey(keycode: Int)
|
case hidKey(keycode: Int)
|
||||||
case keyPress(keycode: Int)
|
case keyPress(keycode: Int)
|
||||||
case appleSctipt(source: String)
|
case appleSctipt(source: Source)
|
||||||
case shellScript(executable: String, parameters: [String])
|
case shellScript(executable: String, parameters: [String])
|
||||||
case custom(closure: ()->())
|
case custom(closure: ()->())
|
||||||
|
|
||||||
@ -175,7 +175,7 @@ enum ActionType: Decodable {
|
|||||||
let keycode = try container.decode(Int.self, forKey: .keycode)
|
let keycode = try container.decode(Int.self, forKey: .keycode)
|
||||||
self = .keyPress(keycode: keycode)
|
self = .keyPress(keycode: keycode)
|
||||||
case .some(.appleScript):
|
case .some(.appleScript):
|
||||||
let source = try container.decode(String.self, forKey: .actionAppleScript)
|
let source = try container.decode(Source.self, forKey: .actionAppleScript)
|
||||||
self = .appleSctipt(source: source)
|
self = .appleSctipt(source: source)
|
||||||
case .some(.shellScript):
|
case .some(.shellScript):
|
||||||
let executable = try container.decode(String.self, forKey: .executablePath)
|
let executable = try container.decode(String.self, forKey: .executablePath)
|
||||||
|
|||||||
@ -105,7 +105,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
case .staticImageButton(title: let title, image: let image):
|
case .staticImageButton(title: let title, image: let image):
|
||||||
barItem = CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action, image: image)
|
barItem = CustomButtonTouchBarItem(identifier: identifier, title: title, onTap: action, image: image)
|
||||||
case .appleScriptTitledButton(source: let source, refreshInterval: let interval):
|
case .appleScriptTitledButton(source: let source, refreshInterval: let interval):
|
||||||
barItem = AppleScriptTouchBarItem(identifier: identifier, source: source, interval: interval)
|
barItem = AppleScriptTouchBarItem(identifier: identifier, source: source, interval: interval, onTap: action)
|
||||||
case .timeButton(formatTemplate: let template):
|
case .timeButton(formatTemplate: let template):
|
||||||
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template)
|
barItem = TimeTouchBarItem(identifier: identifier, formatTemplate: template)
|
||||||
case .flexSpace:
|
case .flexSpace:
|
||||||
@ -127,7 +127,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
case .keyPress(keycode: let keycode):
|
case .keyPress(keycode: let keycode):
|
||||||
return { GenericKeyPress(keyCode: CGKeyCode(keycode)).send() }
|
return { GenericKeyPress(keyCode: CGKeyCode(keycode)).send() }
|
||||||
case .appleSctipt(source: let source):
|
case .appleSctipt(source: let source):
|
||||||
guard let appleScript = NSAppleScript(source: source) else {
|
guard let appleScript = source.appleScript else {
|
||||||
print("cannot create apple script for item \(item)")
|
print("cannot create apple script for item \(item)")
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,29 +14,20 @@ extension NSTouchBarItem.Identifier {
|
|||||||
|
|
||||||
class CustomButtonTouchBarItem: NSCustomTouchBarItem {
|
class CustomButtonTouchBarItem: NSCustomTouchBarItem {
|
||||||
let tapClosure: () -> ()
|
let tapClosure: () -> ()
|
||||||
|
private(set) var button: NSButton!
|
||||||
|
|
||||||
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> ()) {
|
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> (), image: NSImage? = nil) {
|
||||||
self.tapClosure = callback
|
self.tapClosure = callback
|
||||||
super.init(identifier: identifier)
|
super.init(identifier: identifier)
|
||||||
var button = NSButton(title: "", target: nil, action: nil)
|
if let image = image {
|
||||||
button = NSButton(title: title, target: self, action: #selector(didTapped))
|
button = NSButton(title: title, image: image, target: self, action: #selector(didTapped))
|
||||||
self.view = button
|
button.bezelColor = .clear
|
||||||
}
|
|
||||||
|
|
||||||
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> (), image: NSImage?) {
|
|
||||||
self.tapClosure = callback
|
|
||||||
super.init(identifier: identifier)
|
|
||||||
var button = NSButton(title: "", target: nil, action: nil)
|
|
||||||
if ((image) != nil) {
|
|
||||||
button = NSButton(title: title, image: image!, target: self, action: #selector(didTapped))
|
|
||||||
} else {
|
} else {
|
||||||
button = NSButton(title: title, target: self, action: #selector(didTapped))
|
button = NSButton(title: title, target: self, action: #selector(didTapped))
|
||||||
}
|
}
|
||||||
self.view = button
|
self.view = button
|
||||||
button.bezelColor = .clear
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user