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 boundingBox = NSScreen.screenWithMouse?.frame ?? NSZeroRect
19 contentRect: boundingBox,
20 styleMask: [.borderless],
25 self.isReleasedWhenClosed = false
26 self.collectionBehavior = [.canJoinAllSpaces]
27 self.isMovableByWindowBackground = false
28 self.isMovable = false
30 self.titlebarAppearsTransparent = true
31 self.setFrame(boundingBox, display: true)
32 self.titleVisibility = .hidden
33 let recordingView = RecordingContentView(configuration, frame: boundingBox, button: button ?? NSZeroRect)
34 recordingView.frame = boundingBox
35 self.contentView = recordingView
36 self.backgroundColor = NSColor(white: 1.0, alpha: 0.001)
37 // uncomment below to debug window placement visually
38 // self.backgroundColor = NSColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 0.5)
39 self.level = .screenSaver
41 self.hasShadow = false
44 // MARK: - Window Behavior Overrides
46 override func resetCursorRects() {
47 super.resetCursorRects()
48 let cursor = NSCursor.crosshair
49 self.contentView?.addCursorRect(self.contentView!.bounds, cursor: cursor)
52 override var canBecomeKey: Bool {
56 override var canBecomeMain: Bool {
60 override func resignMain() {
62 if (self.contentView as? RecordingContentView)?.state != .recording {
63 self.ignoresMouseEvents = false
67 override func becomeMain() {
69 if (self.contentView as? RecordingContentView)?.state != .recording {
70 (self.contentView as? RecordingContentView)?.state = .idle
75 enum RecordingWindowState {
76 case passthrough, idle, drawing, moving, resizing, recording;
79 class RecordingContentView: NSView {
81 init(_ configuration: CaptureSessionConfiguration, frame: NSRect, button: NSRect) {
82 self.buttonSize = button.size
83 var buttonOffset = NSPoint()
84 for screen in NSScreen.screens {
85 if screen.frame.intersects(button) {
86 let relativeY = screen.frame.height - (button.minY - screen.frame.minY)
87 let relativeX = screen.frame.width - (button.minX - screen.frame.minX)
88 buttonOffset = NSPoint(x: relativeX, y: relativeY)
91 self.buttonOffset = buttonOffset
92 super.init(frame: frame)
93 preventResize = configuration.preventResize
94 preventMove = configuration.preventMove
95 autoStart = configuration.autoStart
98 self.button = NSRect(x: frame.maxX - buttonOffset.x, y: frame.maxY - buttonOffset.y, width: buttonSize.width, height: buttonSize.height)
100 if configuration.x != nil || configuration.y != nil || configuration.width != nil || configuration.height != nil {
102 x: configuration.x ?? Int(frame.width / 2.0),
103 y: configuration.y ?? Int(frame.height / 2.0),
104 width: configuration.width ?? 400,
105 height: configuration.height ?? 400
110 DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
111 NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil)
116 required init?(coder: NSCoder) {
117 fatalError("init(coder:) has not been implemented")
120 private let buttonSize: NSSize
121 private let buttonOffset: NSPoint
122 public var button: NSRect? = nil
123 @Published public var box: NSRect? = nil
124 public var state: RecordingWindowState = .idle
125 private var mouseLocation: NSPoint = NSPoint()
126 private var origin: NSPoint = NSPoint()
127 private var boxOrigin: NSPoint = NSPoint()
128 private var preventResize = false
129 private var preventMove = false
130 private var autoStart = false
132 private var resizeBox: NSRect? {
134 return NSRect(x: box.maxX - 5, y: box.minY - 5, width: 10, height: 10)
139 private var shouldPassthrough: Bool {
140 state == .recording || state == .passthrough
143 // MARK: - State changing API
145 public func startRecording() {
147 window?.ignoresMouseEvents = true
150 public func stopRecording() {
154 public func reset() {
156 window?.ignoresMouseEvents = false
159 public func startPassthrough() {
161 window?.ignoresMouseEvents = true
164 public func stopPassthrough() {
166 window?.ignoresMouseEvents = false
169 // MARK: - View Behavior Overrides
171 override func updateTrackingAreas() {
172 super.updateTrackingAreas()
174 for trackingArea in self.trackingAreas {
175 self.removeTrackingArea(trackingArea)
178 let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeInKeyWindow, .cursorUpdate, .mouseMoved]
179 let trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
180 self.addTrackingArea(trackingArea)
183 override func mouseExited(with event: NSEvent) {
184 if state == .idle && box == nil {
189 override func mouseMoved(with event: NSEvent) {
191 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
193 if shouldPassthrough {
197 if resizeBox!.contains(mouseLocation) {
200 if box.contains(mouseLocation) {
201 NSCursor.openHand.set()
203 NSCursor.crosshair.set()
207 if button.contains(mouseLocation) {
212 NSCursor.crosshair.set()
216 self.setNeedsDisplay(self.bounds)
219 override func mouseDragged(with event: NSEvent) {
220 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
221 if state == .drawing {
223 x: round(min(origin.x, mouseLocation.x)),
224 y: round(min(origin.y, mouseLocation.y)),
225 width: round(abs(mouseLocation.x - origin.x)),
226 height: round(abs(mouseLocation.y - origin.y))
231 if state == .moving {
232 NSCursor.closedHand.set()
233 box!.origin = NSPoint(
234 x: self.boxOrigin.x - self.origin.x + self.mouseLocation.x,
235 y: self.boxOrigin.y - self.origin.y + self.mouseLocation.y)
238 if state == .resizing {
240 x: round(min(origin.x, mouseLocation.x)),
241 y: round(min(origin.y, mouseLocation.y)),
242 width: round(abs(mouseLocation.x - origin.x)),
243 height: round(abs(mouseLocation.y - origin.y))
247 self.setNeedsDisplay(self.bounds)
250 override func cursorUpdate(with event: NSEvent) {
251 NSCursor.crosshair.set()
254 override func hitTest(_ point: NSPoint) -> NSView? {
255 return shouldPassthrough ? nil : self
258 override var acceptsFirstResponder: Bool {
262 override func mouseDown(with event: NSEvent) {
263 self.origin = self.convert(event.locationInWindow, from: nil)
267 if button.contains(origin) {
268 NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil)
273 if resizeBox!.contains(origin) && !preventResize {
274 self.origin = NSPoint(x: box.minX, y: box.maxY)
278 if box.contains(origin) && !preventMove {
280 self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y)
285 if preventResize || preventMove {
292 override func mouseUp(with event: NSEvent) {
293 if state != .recording {
298 override func keyDown(with event: NSEvent) {
299 switch event.keyCode {
300 case 53: // Escape key
301 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
303 super.keyDown(with: event)
307 override func flagsChanged(with event: NSEvent) {
309 if event.modifierFlags.contains(.shift) {
317 override func draw(_ dirtyRect: NSRect) {
318 if shouldPassthrough {
319 NSColor.clear.setFill()
321 NSColor(white: 1.0, alpha: 0.001).setFill()
325 let dashLength: CGFloat = 5.0
328 // Uncomment below to debug button placement visually
330 // let buttonPath = NSBezierPath()
331 // buttonPath.move(to: NSPoint(x: button.minX, y: button.minY))
332 // buttonPath.line(to: NSPoint(x: button.maxX, y: button.minY))
333 // buttonPath.line(to: NSPoint(x: button.maxX, y: button.maxY))
334 // buttonPath.line(to: NSPoint(x: button.minX, y: button.maxY))
335 // buttonPath.line(to: NSPoint(x: button.minX, y: button.minY))
336 // NSColor(red: 1, green: 0, blue: 1, alpha: 1).setFill()
340 if state == .idle && box == nil {
341 let blackLine = NSBezierPath()
342 blackLine.lineWidth = lineWidth
343 blackLine.setLineDash([dashLength, dashLength], count: 2, phase: 0)
345 // Vertical line (Black)
346 blackLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
347 blackLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
349 // Horizontal line (Black)
350 blackLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
351 blackLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
353 NSColor.black.setStroke()
356 let whiteLine = NSBezierPath()
357 whiteLine.lineWidth = lineWidth
358 whiteLine.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
360 // Vertical line (White)
361 whiteLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
362 whiteLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
364 // Horizontal line (White)
365 whiteLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
366 whiteLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
368 NSColor.white.setStroke()
373 let blackBox = NSBezierPath()
374 blackBox.lineWidth = lineWidth
375 blackBox.setLineDash([dashLength, dashLength], count: 2, phase: 0)
376 blackBox.move(to: NSPoint(x: box.minX, y: box.minY))
377 blackBox.line(to: NSPoint(x: box.maxX, y: box.minY))
378 blackBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
379 blackBox.line(to: NSPoint(x: box.minX, y: box.maxY))
380 blackBox.line(to: NSPoint(x: box.minX, y: box.minY))
381 NSColor.black.setStroke()
384 let whiteBox = NSBezierPath()
385 whiteBox.lineWidth = lineWidth
386 whiteBox.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
387 whiteBox.move(to: NSPoint(x: box.minX, y: box.minY))
388 whiteBox.line(to: NSPoint(x: box.maxX, y: box.minY))
389 whiteBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
390 whiteBox.line(to: NSPoint(x: box.minX, y: box.maxY))
391 whiteBox.line(to: NSPoint(x: box.minX, y: box.minY))
392 NSColor.white.setStroke()
395 if state == .recording {
400 let clearBox = NSBezierPath()
401 clearBox.move(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
402 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.minY))
403 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.maxY))
404 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.maxY))
405 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
406 NSColor(white: 0, alpha: 0.2).setFill()
410 if state == .moving {
411 let string = "\(Int(box.minX)), \(Int(box.maxY))" as NSString
412 drawText(string, NSPoint(
418 if state == .resizing {
419 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
420 drawText(string, mouseLocation)
423 if box.contains(mouseLocation) && state != .resizing {
430 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
431 drawText(string, mouseLocation)
436 private func drawText(_ text: NSString, _ location: NSPoint, _ isBottomRight: Bool = false) {
438 let textAttributes = [
439 NSAttributedString.Key.font: NSFont(name: "Hiragino Mincho ProN", size: 12) ?? NSFont.systemFont(ofSize: 12),
440 NSAttributedString.Key.foregroundColor: NSColor.white,
442 let offset = NSPoint(x: 10, y: 10)
443 let padding = NSPoint(x: 5, y: 2)
444 let size = text.size(withAttributes: textAttributes)
445 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)
446 var textRect = NSRect(x: location.x + offset.x + padding.x, y: location.y + offset.y + padding.y, width: size.width, height: size.height)
449 rect = rect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
450 textRect = textRect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
456 text.draw(in: textRect, withAttributes: textAttributes)
459 private func moveWindow() {
460 let screen = NSScreen.screenWithMouse
461 if let currentScreen = self.window?.screen {
462 if currentScreen != screen {
463 let frame = screen?.frame ?? NSZeroRect
464 self.frame = CGRect(origin: NSZeroPoint, size: frame.size)
465 self.bounds = CGRect(origin: NSZeroPoint, size: frame.size)
466 self.updateTrackingAreas()
468 if let window = self.window {
470 self.button = NSRect(x: frame.maxX - buttonOffset.x, y: frame.maxY - buttonOffset.y, width: buttonSize.width, height: buttonSize.height)
471 window.setFrame(frame, display: true, animate: false)
472 window.makeKeyAndOrderFront(nil)
473 window.orderFrontRegardless()