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)
34 recordingView.frame = boundingBox
35 recordingView.button = button
36 self.contentView = recordingView
37 //self.backgroundColor = NSColor(white: 1.0, alpha: 0.001)
38 self.backgroundColor = NSColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 0.5)
39 self.level = .screenSaver
41 self.hasShadow = false
43 print("AAAAH INIT CHANGE")
44 print("AAAAH FRAME X: \(recordingView.frame.minX) \(recordingView.frame.maxX) // Y: \(recordingView.frame.minY) \(recordingView.frame.maxY)")
45 print("AAAAH BOUNDS X: \(recordingView.bounds.minX) \(recordingView.bounds.maxX) // Y: \(recordingView.bounds.minY) \(recordingView.bounds.maxY)")
46 print("AAAAH WIN F X: \(self.frame.minX) \(self.frame.maxX) // Y: \(self.frame.minY) \(self.frame.maxY)")
49 // MARK: - Window Behavior Overrides
51 override func resetCursorRects() {
52 super.resetCursorRects()
53 let cursor = NSCursor.crosshair
54 self.contentView?.addCursorRect(self.contentView!.bounds, cursor: cursor)
57 override var canBecomeKey: Bool {
61 override var canBecomeMain: Bool {
65 override func resignMain() {
67 if (self.contentView as? RecordingContentView)?.state != .recording {
68 self.ignoresMouseEvents = false
72 override func becomeMain() {
74 if (self.contentView as? RecordingContentView)?.state != .recording {
75 (self.contentView as? RecordingContentView)?.state = .idle
80 enum RecordingWindowState {
81 case passthrough, idle, drawing, moving, resizing, recording;
84 class RecordingContentView: NSView {
86 init(_ configuration: CaptureSessionConfiguration, frame: NSRect) {
87 super.init(frame: frame)
88 preventResize = configuration.preventResize
89 preventMove = configuration.preventMove
90 autoStart = configuration.autoStart
92 if configuration.x != nil || configuration.y != nil || configuration.width != nil || configuration.height != nil {
94 x: configuration.x ?? Int(frame.width / 2.0),
95 y: configuration.y ?? Int(frame.height / 2.0),
96 width: configuration.width ?? 400,
97 height: configuration.height ?? 400
102 DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
103 NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil)
108 required init?(coder: NSCoder) {
109 fatalError("init(coder:) has not been implemented")
112 public var button: NSRect? = nil
113 @Published public var box: NSRect? = nil
114 public var state: RecordingWindowState = .idle
115 private var mouseLocation: NSPoint = NSPoint()
116 private var origin: NSPoint = NSPoint()
117 private var boxOrigin: NSPoint = NSPoint()
118 private var preventResize = false
119 private var preventMove = false
120 private var autoStart = false
122 private var resizeBox: NSRect? {
124 return NSRect(x: box.maxX - 5, y: box.minY - 5, width: 10, height: 10)
129 private var shouldPassthrough: Bool {
130 state == .recording || state == .passthrough
133 // MARK: - State changing API
135 public func startRecording() {
137 window?.ignoresMouseEvents = true
140 public func stopRecording() {
144 public func reset() {
146 window?.ignoresMouseEvents = false
149 public func startPassthrough() {
151 window?.ignoresMouseEvents = true
154 public func stopPassthrough() {
156 window?.ignoresMouseEvents = false
159 // MARK: - View Behavior Overrides
161 override func updateTrackingAreas() {
162 super.updateTrackingAreas()
164 for trackingArea in self.trackingAreas {
165 self.removeTrackingArea(trackingArea)
168 let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeInKeyWindow, .cursorUpdate, .mouseMoved]
169 let trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
170 self.addTrackingArea(trackingArea)
173 override func mouseExited(with event: NSEvent) {
174 if state == .idle && box == nil {
179 override func mouseMoved(with event: NSEvent) {
181 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
183 if shouldPassthrough {
187 if resizeBox!.contains(mouseLocation) {
190 if box.contains(mouseLocation) {
191 NSCursor.openHand.set()
193 NSCursor.crosshair.set()
197 if button.contains(mouseLocation) {
202 NSCursor.crosshair.set()
206 self.setNeedsDisplay(self.bounds)
209 override func mouseDragged(with event: NSEvent) {
210 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
211 if state == .drawing {
213 x: round(min(origin.x, mouseLocation.x)),
214 y: round(min(origin.y, mouseLocation.y)),
215 width: round(abs(mouseLocation.x - origin.x)),
216 height: round(abs(mouseLocation.y - origin.y))
221 if state == .moving {
222 NSCursor.closedHand.set()
223 box!.origin = NSPoint(
224 x: self.boxOrigin.x - self.origin.x + self.mouseLocation.x,
225 y: self.boxOrigin.y - self.origin.y + self.mouseLocation.y)
228 if state == .resizing {
230 x: round(min(origin.x, mouseLocation.x)),
231 y: round(min(origin.y, mouseLocation.y)),
232 width: round(abs(mouseLocation.x - origin.x)),
233 height: round(abs(mouseLocation.y - origin.y))
237 self.setNeedsDisplay(self.bounds)
240 override func cursorUpdate(with event: NSEvent) {
241 NSCursor.crosshair.set()
244 override func hitTest(_ point: NSPoint) -> NSView? {
245 return shouldPassthrough ? nil : self
248 override var acceptsFirstResponder: Bool {
252 override func mouseDown(with event: NSEvent) {
253 self.origin = self.convert(event.locationInWindow, from: nil)
257 if button.contains(origin) {
258 NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil)
263 if resizeBox!.contains(origin) && !preventResize {
264 self.origin = NSPoint(x: box.minX, y: box.maxY)
268 if box.contains(origin) && !preventMove {
270 self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y)
275 if preventResize || preventMove {
282 override func mouseUp(with event: NSEvent) {
283 if state != .recording {
288 override func keyDown(with event: NSEvent) {
289 switch event.keyCode {
290 case 53: // Escape key
291 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
293 super.keyDown(with: event)
297 override func flagsChanged(with event: NSEvent) {
299 if event.modifierFlags.contains(.shift) {
307 override func draw(_ dirtyRect: NSRect) {
308 if shouldPassthrough {
309 NSColor.clear.setFill()
311 NSColor(white: 1.0, alpha: 0.001).setFill()
315 let dashLength: CGFloat = 5.0
319 let buttonPath = NSBezierPath()
320 buttonPath.move(to: NSPoint(x: button.minX, y: button.minY))
321 buttonPath.line(to: NSPoint(x: button.maxX, y: button.minY))
322 buttonPath.line(to: NSPoint(x: button.maxX, y: button.maxY))
323 buttonPath.line(to: NSPoint(x: button.minX, y: button.maxY))
324 buttonPath.line(to: NSPoint(x: button.minX, y: button.minY))
325 NSColor(red: 1, green: 0, blue: 1, alpha: 1).setFill()
329 if state == .idle && box == nil {
330 let blackLine = NSBezierPath()
331 blackLine.lineWidth = lineWidth
332 blackLine.setLineDash([dashLength, dashLength], count: 2, phase: 0)
334 // Vertical line (Black)
335 blackLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
336 blackLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
338 // Horizontal line (Black)
339 blackLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
340 blackLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
342 NSColor.black.setStroke()
345 let whiteLine = NSBezierPath()
346 whiteLine.lineWidth = lineWidth
347 whiteLine.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
349 // Vertical line (White)
350 whiteLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
351 whiteLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
353 // Horizontal line (White)
354 whiteLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
355 whiteLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
357 NSColor.white.setStroke()
362 let blackBox = NSBezierPath()
363 blackBox.lineWidth = lineWidth
364 blackBox.setLineDash([dashLength, dashLength], count: 2, phase: 0)
365 blackBox.move(to: NSPoint(x: box.minX, y: box.minY))
366 blackBox.line(to: NSPoint(x: box.maxX, y: box.minY))
367 blackBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
368 blackBox.line(to: NSPoint(x: box.minX, y: box.maxY))
369 blackBox.line(to: NSPoint(x: box.minX, y: box.minY))
370 NSColor.black.setStroke()
373 let whiteBox = NSBezierPath()
374 whiteBox.lineWidth = lineWidth
375 whiteBox.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
376 whiteBox.move(to: NSPoint(x: box.minX, y: box.minY))
377 whiteBox.line(to: NSPoint(x: box.maxX, y: box.minY))
378 whiteBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
379 whiteBox.line(to: NSPoint(x: box.minX, y: box.maxY))
380 whiteBox.line(to: NSPoint(x: box.minX, y: box.minY))
381 NSColor.white.setStroke()
384 if state == .recording {
389 let clearBox = NSBezierPath()
390 clearBox.move(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
391 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.minY))
392 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.maxY))
393 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.maxY))
394 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
395 NSColor(white: 0, alpha: 0.2).setFill()
399 if state == .moving {
400 let string = "\(Int(box.minX)), \(Int(box.maxY))" as NSString
401 drawText(string, NSPoint(
407 if state == .resizing {
408 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
409 drawText(string, mouseLocation)
412 if box.contains(mouseLocation) && state != .resizing {
419 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
420 drawText(string, mouseLocation)
425 private func drawText(_ text: NSString, _ location: NSPoint, _ isBottomRight: Bool = false) {
427 let textAttributes = [
428 NSAttributedString.Key.font: NSFont(name: "Hiragino Mincho ProN", size: 12) ?? NSFont.systemFont(ofSize: 12),
429 NSAttributedString.Key.foregroundColor: NSColor.white,
431 let offset = NSPoint(x: 10, y: 10)
432 let padding = NSPoint(x: 5, y: 2)
433 let size = text.size(withAttributes: textAttributes)
434 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)
435 var textRect = NSRect(x: location.x + offset.x + padding.x, y: location.y + offset.y + padding.y, width: size.width, height: size.height)
438 rect = rect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
439 textRect = textRect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
445 text.draw(in: textRect, withAttributes: textAttributes)
448 private func moveWindow() {
449 print("AAAAH BEFORE WE CHANGE")
450 print("AAAAH FRAME X: \(self.frame.minX) \(self.frame.maxX) // Y: \(self.frame.minY) \(self.frame.maxY)")
451 print("AAAAH BOUNDS X: \(self.bounds.minX) \(self.bounds.maxX) // Y: \(self.bounds.minY) \(self.bounds.maxY)")
452 print("AAAAH WIN F X: \(self.window?.frame.minX) \(self.window?.frame.maxX) // Y: \(self.window?.frame.minY) \(self.window?.frame.maxY)")
453 let screen = NSScreen.screenWithMouse
454 if let currentScreen = self.window?.screen {
455 if currentScreen != screen {
456 let frame = screen?.frame ?? NSZeroRect
459 self.updateTrackingAreas()
461 if let window = self.window {
463 window.setFrame(frame, display: true, animate: false)
464 window.makeKeyAndOrderFront(nil)
465 window.orderFrontRegardless()
466 print("AAAAH AFTER CHANGE")
467 print("AAAAH FRAME X: \(self.frame.minX) \(self.frame.maxX) // Y: \(self.frame.minY) \(self.frame.maxY)")
468 print("AAAAH BOUNDS X: \(self.bounds.minX) \(self.bounds.maxX) // Y: \(self.bounds.minY) \(self.bounds.maxY)")
469 print("AAAAH WIN F X: \(self.window?.frame.minX) \(self.window?.frame.maxX) // Y: \(self.window?.frame.minY) \(self.window?.frame.maxY)")