mirror of
https://github.com/sindresorhus/touch-bar-simulator
synced 2024-12-05 01:59:09 +00:00
de1c9145d4
Fixes #11 Fixes #28 Fixes #29
66 lines
1.2 KiB
Swift
66 lines
1.2 KiB
Swift
import Cocoa
|
|
|
|
final class TouchBarView: NSView {
|
|
private var stream: CGDisplayStream?
|
|
private let displayView = NSView()
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: .zero)
|
|
wantsLayer = true
|
|
start()
|
|
setFrameSize(DFRGetScreenSize())
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
deinit {
|
|
stop()
|
|
}
|
|
|
|
override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
|
|
return true
|
|
}
|
|
|
|
func start() {
|
|
stream = SLSDFRDisplayStreamCreate(0, .main) { status, _, frameSurface, _ in
|
|
guard status == .frameComplete else {
|
|
return
|
|
}
|
|
|
|
self.layer!.contents = frameSurface
|
|
} .takeUnretainedValue()
|
|
|
|
DFRSetStatus(2)
|
|
stream?.start()
|
|
}
|
|
|
|
func stop() {
|
|
guard let stream = stream else {
|
|
return
|
|
}
|
|
|
|
DFRSetStatus(0)
|
|
stream.stop()
|
|
self.stream = nil
|
|
}
|
|
|
|
private func mouseEvent(_ event: NSEvent) {
|
|
let location = convert(event.locationInWindow, from: nil)
|
|
DFRFoundationPostEventWithMouseActivity(event.type, location)
|
|
}
|
|
|
|
override func mouseDown(with event: NSEvent) {
|
|
mouseEvent(event)
|
|
}
|
|
|
|
override func mouseUp(with event: NSEvent) {
|
|
mouseEvent(event)
|
|
}
|
|
|
|
override func mouseDragged(with event: NSEvent) {
|
|
mouseEvent(event)
|
|
}
|
|
}
|