touch-bar-simulator/Touch Bar Simulator/TouchBarView.swift
Sindre Sorhus de1c9145d4 Rewrite the app
Fixes #11
Fixes #28
Fixes #29
2018-09-25 14:52:09 +07:00

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)
}
}