4 class RecordingWindow: NSWindow {
6 var pixelDensity: CGFloat {
7 self.screen?.backingScaleFactor ?? 1.0
10 var recordingContentView: RecordingContentView {
11 self.contentView as! RecordingContentView
14 init(_ configuration: CaptureSessionConfiguration, _ button: NSRect?) {
16 let screens = NSScreen.screens
17 var boundingBox = NSZeroRect
18 for screen in screens {
19 boundingBox = NSUnionRect(boundingBox, screen.frame)
23 contentRect: boundingBox,
24 styleMask: [.borderless],
28 self.isReleasedWhenClosed = false
29 self.collectionBehavior = [.canJoinAllSpaces]
30 self.isMovableByWindowBackground = false
31 self.isMovable = false
33 self.titlebarAppearsTransparent = true
34 self.setFrame(boundingBox, display: true)
35 self.titleVisibility = .hidden
36 let recordingView = RecordingContentView(configuration, frame: boundingBox)
37 recordingView.frame = boundingBox
38 recordingView.button = button
39 self.contentView = recordingView
40 self.backgroundColor = NSColor(white: 1.0, alpha: 0.001)
41 self.level = .screenSaver
43 self.hasShadow = false
46 // MARK: - Window Behavior Overrides
48 override func resetCursorRects() {
49 super.resetCursorRects()
50 let cursor = NSCursor.crosshair
51 self.contentView?.addCursorRect(self.contentView!.bounds, cursor: cursor)
54 override var canBecomeKey: Bool {
58 override var canBecomeMain: Bool {
62 override func resignMain() {
64 if (self.contentView as? RecordingContentView)?.state != .recording {
65 self.ignoresMouseEvents = false
69 override func becomeMain() {
71 if (self.contentView as? RecordingContentView)?.state != .recording {
72 (self.contentView as? RecordingContentView)?.state = .idle
77 enum RecordingWindowState {
78 case passthrough, idle, drawing, moving, resizing, recording;
81 class RecordingContentView: NSView {
83 init(_ configuration: CaptureSessionConfiguration, frame: NSRect) {
84 super.init(frame: frame)
85 preventResize = configuration.preventResize
86 preventMove = configuration.preventMove
87 autoStart = configuration.autoStart
89 if configuration.x != nil || configuration.y != nil || configuration.width != nil || configuration.height != nil {
91 x: configuration.x ?? Int(frame.width / 2.0),
92 y: configuration.y ?? Int(frame.height / 2.0),
93 width: configuration.width ?? 400,
94 height: configuration.height ?? 400
99 DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
100 NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil)
105 required init?(coder: NSCoder) {
106 fatalError("init(coder:) has not been implemented")
109 public var button: NSRect? = nil
110 @Published public var box: NSRect? = nil
111 public var state: RecordingWindowState = .idle
112 private var mouseLocation: NSPoint = NSPoint()
113 private var origin: NSPoint = NSPoint()
114 private var boxOrigin: NSPoint = NSPoint()
115 private var preventResize = false
116 private var preventMove = false
117 private var autoStart = false
119 private var resizeBox: NSRect? {
121 return NSRect(x: box.maxX - 5, y: box.minY - 5, width: 10, height: 10)
126 private var shouldPassthrough: Bool {
127 state == .recording || state == .passthrough
130 // MARK: - State changing API
132 public func startRecording() {
134 window?.ignoresMouseEvents = true
137 public func stopRecording() {
141 public func reset() {
143 window?.ignoresMouseEvents = false
146 public func startPassthrough() {
148 window?.ignoresMouseEvents = true
151 public func stopPassthrough() {
153 window?.ignoresMouseEvents = false
156 // MARK: - View Behavior Overrides
158 override func updateTrackingAreas() {
159 super.updateTrackingAreas()
161 for trackingArea in self.trackingAreas {
162 self.removeTrackingArea(trackingArea)
165 let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeInKeyWindow, .cursorUpdate, .mouseMoved]
166 let trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
167 self.addTrackingArea(trackingArea)
170 override func mouseMoved(with event: NSEvent) {
172 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
174 if shouldPassthrough {
178 if resizeBox!.contains(mouseLocation) {
181 if box.contains(mouseLocation) {
182 NSCursor.openHand.set()
184 NSCursor.crosshair.set()
188 if button.contains(mouseLocation) {
193 NSCursor.crosshair.set()
197 self.setNeedsDisplay(self.bounds)
200 override func mouseDragged(with event: NSEvent) {
201 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
202 if state == .drawing {
204 x: round(min(origin.x, mouseLocation.x)),
205 y: round(min(origin.y, mouseLocation.y)),
206 width: round(abs(mouseLocation.x - origin.x)),
207 height: round(abs(mouseLocation.y - origin.y))
212 if state == .moving {
213 NSCursor.closedHand.set()
214 box!.origin = NSPoint(
215 x: self.boxOrigin.x - self.origin.x + self.mouseLocation.x,
216 y: self.boxOrigin.y - self.origin.y + self.mouseLocation.y)
219 if state == .resizing {
221 x: round(min(origin.x, mouseLocation.x)),
222 y: round(min(origin.y, mouseLocation.y)),
223 width: round(abs(mouseLocation.x - origin.x)),
224 height: round(abs(mouseLocation.y - origin.y))
228 self.setNeedsDisplay(self.bounds)
231 override func cursorUpdate(with event: NSEvent) {
232 NSCursor.crosshair.set()
235 override func hitTest(_ point: NSPoint) -> NSView? {
236 return shouldPassthrough ? nil : self
239 override var acceptsFirstResponder: Bool {
243 override func mouseDown(with event: NSEvent) {
244 self.origin = self.convert(event.locationInWindow, from: nil)
248 if button.contains(origin) {
249 NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil)
254 if resizeBox!.contains(origin) && !preventResize {
255 self.origin = NSPoint(x: box.minX, y: box.maxY)
259 if box.contains(origin) && !preventMove {
261 self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y)
266 if preventResize || preventMove {
273 override func mouseUp(with event: NSEvent) {
274 if state != .recording {
279 override func keyDown(with event: NSEvent) {
280 switch event.keyCode {
281 case 53: // Escape key
282 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
284 super.keyDown(with: event)
288 override func flagsChanged(with event: NSEvent) {
290 if event.modifierFlags.contains(.shift) {
298 override func draw(_ dirtyRect: NSRect) {
299 if shouldPassthrough {
300 NSColor.clear.setFill()
302 NSColor(white: 1.0, alpha: 0.001).setFill()
306 let dashLength: CGFloat = 5.0
309 if state == .idle && box == nil {
310 let blackLine = NSBezierPath()
311 blackLine.lineWidth = lineWidth
312 blackLine.setLineDash([dashLength, dashLength], count: 2, phase: 0)
314 // Vertical line (Black)
315 blackLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
316 blackLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
318 // Horizontal line (Black)
319 blackLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
320 blackLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
322 NSColor.black.setStroke()
325 let whiteLine = NSBezierPath()
326 whiteLine.lineWidth = lineWidth
327 whiteLine.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
329 // Vertical line (White)
330 whiteLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
331 whiteLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
333 // Horizontal line (White)
334 whiteLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
335 whiteLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
337 NSColor.white.setStroke()
342 let blackBox = NSBezierPath()
343 blackBox.lineWidth = lineWidth
344 blackBox.setLineDash([dashLength, dashLength], count: 2, phase: 0)
345 blackBox.move(to: NSPoint(x: box.minX, y: box.minY))
346 blackBox.line(to: NSPoint(x: box.maxX, y: box.minY))
347 blackBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
348 blackBox.line(to: NSPoint(x: box.minX, y: box.maxY))
349 blackBox.line(to: NSPoint(x: box.minX, y: box.minY))
350 NSColor.black.setStroke()
353 let whiteBox = NSBezierPath()
354 whiteBox.lineWidth = lineWidth
355 whiteBox.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
356 whiteBox.move(to: NSPoint(x: box.minX, y: box.minY))
357 whiteBox.line(to: NSPoint(x: box.maxX, y: box.minY))
358 whiteBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
359 whiteBox.line(to: NSPoint(x: box.minX, y: box.maxY))
360 whiteBox.line(to: NSPoint(x: box.minX, y: box.minY))
361 NSColor.white.setStroke()
364 if state == .recording {
369 let clearBox = NSBezierPath()
370 clearBox.move(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
371 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.minY))
372 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.maxY))
373 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.maxY))
374 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
375 NSColor(white: 0, alpha: 0.2).setFill()
379 if state == .moving {
380 let string = "\(Int(box.minX)), \(Int(box.maxY))" as NSString
381 drawText(string, NSPoint(
387 if state == .resizing {
388 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
389 drawText(string, mouseLocation)
392 if box.contains(mouseLocation) && state != .resizing {
399 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
400 drawText(string, mouseLocation)
405 private func drawText(_ text: NSString, _ location: NSPoint, _ isBottomRight: Bool = false) {
407 let textAttributes = [
408 NSAttributedString.Key.font: NSFont(name: "Hiragino Mincho ProN", size: 12) ?? NSFont.systemFont(ofSize: 12),
409 NSAttributedString.Key.foregroundColor: NSColor.white,
411 let offset = NSPoint(x: 10, y: 10)
412 let padding = NSPoint(x: 5, y: 2)
413 let size = text.size(withAttributes: textAttributes)
414 var rect = NSRect(x: location.x + offset.x, y: location.y + offset.y, width: size.width + 2 * padding.x, height: size.height + 2 * padding.y)
415 var textRect = NSRect(x: location.x + offset.x + padding.x, y: location.y + offset.y + padding.y, width: size.width, height: size.height)
418 rect = rect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
419 textRect = textRect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
425 text.draw(in: textRect, withAttributes: textAttributes)