4 class RecordingWindow: NSWindow {
6 var pixelDensity: CGFloat {
7 self.screen?.backingScaleFactor ?? 1.0
10 init(_ button: NSRect?) {
12 let screens = NSScreen.screens
13 var boundingBox = NSZeroRect
14 for screen in screens {
15 boundingBox = NSUnionRect(boundingBox, screen.frame)
19 contentRect: boundingBox,
20 styleMask: [.borderless],
24 self.isReleasedWhenClosed = false
25 self.collectionBehavior = [.canJoinAllSpaces]
27 self.isMovableByWindowBackground = false
28 self.isMovable = false
29 self.titlebarAppearsTransparent = true
30 self.setFrame(boundingBox, display: true)
31 self.titleVisibility = .hidden
32 let recordingView = RecordingContentView()
33 recordingView.frame = boundingBox
34 recordingView.button = button
35 self.contentView = recordingView
36 self.backgroundColor = NSColor(white: 1.0, alpha: 0.001)
37 self.level = .screenSaver
39 self.hasShadow = false
40 self.makeKeyAndOrderFront(nil)
43 // MARK: - Window Behavior Overrides
45 override func resetCursorRects() {
46 super.resetCursorRects()
47 let cursor = NSCursor.crosshair
48 self.contentView?.addCursorRect(self.contentView!.bounds, cursor: cursor)
51 override var canBecomeKey: Bool {
55 override var canBecomeMain: Bool {
59 override func resignMain() {
61 if (self.contentView as? RecordingContentView)?.state != .recording {
62 self.ignoresMouseEvents = false
66 override func becomeMain() {
68 if (self.contentView as? RecordingContentView)?.state != .recording {
69 (self.contentView as? RecordingContentView)?.state = .idle
74 enum RecordingWindowState {
75 case passthrough, idle, drawing, moving, resizing, recording;
78 class RecordingContentView: NSView {
80 public var button: NSRect? = nil
81 @Published public var box: NSRect? = nil
82 public var state: RecordingWindowState = .idle
83 private var mouseLocation: NSPoint = NSPoint()
84 private var origin: NSPoint = NSPoint()
85 private var boxOrigin: NSPoint = NSPoint()
87 private var resizeBox: NSRect? {
89 return NSRect(x: box.maxX - 5, y: box.minY - 5, width: 10, height: 10)
94 private var shouldPassthrough: Bool {
95 state == .recording || state == .passthrough
98 // MARK: - State changing API
100 public func startRecording() {
102 window?.ignoresMouseEvents = true
105 public func stopRecording() {
109 public func reset() {
111 window?.ignoresMouseEvents = false
114 public func startPassthrough() {
116 window?.ignoresMouseEvents = true
119 public func stopPassthrough() {
121 window?.ignoresMouseEvents = false
124 // MARK: - View Behavior Overrides
126 override func updateTrackingAreas() {
127 super.updateTrackingAreas()
129 for trackingArea in self.trackingAreas {
130 self.removeTrackingArea(trackingArea)
133 let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeInKeyWindow, .cursorUpdate, .mouseMoved]
134 let trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
135 self.addTrackingArea(trackingArea)
138 override func mouseMoved(with event: NSEvent) {
140 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
142 if shouldPassthrough {
146 if resizeBox!.contains(mouseLocation) {
149 if box.contains(mouseLocation) {
150 NSCursor.openHand.set()
152 NSCursor.crosshair.set()
156 if button.contains(mouseLocation) {
161 NSCursor.crosshair.set()
165 self.setNeedsDisplay(self.bounds)
168 override func mouseDragged(with event: NSEvent) {
169 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
170 if state == .drawing {
172 x: round(min(origin.x, mouseLocation.x)),
173 y: round(min(origin.y, mouseLocation.y)),
174 width: round(abs(mouseLocation.x - origin.x)),
175 height: round(abs(mouseLocation.y - origin.y))
180 if state == .moving {
181 NSCursor.closedHand.set()
182 box!.origin = NSPoint(
183 x: self.boxOrigin.x - self.origin.x + self.mouseLocation.x,
184 y: self.boxOrigin.y - self.origin.y + self.mouseLocation.y)
187 if state == .resizing {
189 x: round(min(origin.x, mouseLocation.x)),
190 y: round(min(origin.y, mouseLocation.y)),
191 width: round(abs(mouseLocation.x - origin.x)),
192 height: round(abs(mouseLocation.y - origin.y))
196 self.setNeedsDisplay(self.bounds)
199 override func cursorUpdate(with event: NSEvent) {
200 NSCursor.crosshair.set()
203 override func hitTest(_ point: NSPoint) -> NSView? {
204 return shouldPassthrough ? nil : self
207 override var acceptsFirstResponder: Bool {
211 override func mouseDown(with event: NSEvent) {
212 self.origin = self.convert(event.locationInWindow, from: nil)
216 if button.contains(origin) {
217 NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil)
222 if resizeBox!.contains(origin) {
223 self.origin = NSPoint(x: box.minX, y: box.maxY)
227 if box.contains(origin) {
229 self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y)
237 override func mouseUp(with event: NSEvent) {
238 if state != .recording {
243 override func keyDown(with event: NSEvent) {
244 switch event.keyCode {
245 case 53: // Escape key
246 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
248 super.keyDown(with: event)
252 override func flagsChanged(with event: NSEvent) {
254 if event.modifierFlags.contains(.shift) {
262 override func draw(_ dirtyRect: NSRect) {
263 if shouldPassthrough {
264 NSColor.clear.setFill()
266 NSColor(white: 1.0, alpha: 0.001).setFill()
270 let dashLength: CGFloat = 5.0
273 if state == .idle && box == nil {
274 let blackLine = NSBezierPath()
275 blackLine.lineWidth = lineWidth
276 blackLine.setLineDash([dashLength, dashLength], count: 2, phase: 0)
278 // Vertical line (Black)
279 blackLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
280 blackLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
282 // Horizontal line (Black)
283 blackLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
284 blackLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
286 NSColor.black.setStroke()
289 let whiteLine = NSBezierPath()
290 whiteLine.lineWidth = lineWidth
291 whiteLine.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
293 // Vertical line (White)
294 whiteLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
295 whiteLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
297 // Horizontal line (White)
298 whiteLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
299 whiteLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
301 NSColor.white.setStroke()
306 let blackBox = NSBezierPath()
307 blackBox.lineWidth = lineWidth
308 blackBox.setLineDash([dashLength, dashLength], count: 2, phase: 0)
309 blackBox.move(to: NSPoint(x: box.minX, y: box.minY))
310 blackBox.line(to: NSPoint(x: box.maxX, y: box.minY))
311 blackBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
312 blackBox.line(to: NSPoint(x: box.minX, y: box.maxY))
313 blackBox.line(to: NSPoint(x: box.minX, y: box.minY))
314 NSColor.black.setStroke()
317 let whiteBox = NSBezierPath()
318 whiteBox.lineWidth = lineWidth
319 whiteBox.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
320 whiteBox.move(to: NSPoint(x: box.minX, y: box.minY))
321 whiteBox.line(to: NSPoint(x: box.maxX, y: box.minY))
322 whiteBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
323 whiteBox.line(to: NSPoint(x: box.minX, y: box.maxY))
324 whiteBox.line(to: NSPoint(x: box.minX, y: box.minY))
325 NSColor.white.setStroke()
328 if state == .recording {
333 let clearBox = NSBezierPath()
334 clearBox.move(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
335 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.minY))
336 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.maxY))
337 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.maxY))
338 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
339 NSColor(white: 0, alpha: 0.2).setFill()
343 if state == .moving {
344 let string = "\(Int(box.minX)), \(Int(box.maxY))" as NSString
345 drawText(string, NSPoint(
351 if state == .resizing {
352 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
353 drawText(string, mouseLocation)
356 if box.contains(mouseLocation) && state != .resizing {
363 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
364 drawText(string, mouseLocation)
369 private func drawText(_ text: NSString, _ location: NSPoint, _ isBottomRight: Bool = false) {
371 let textAttributes = [
372 NSAttributedString.Key.font: NSFont(name: "Hiragino Mincho ProN", size: 12) ?? NSFont.systemFont(ofSize: 12),
373 NSAttributedString.Key.foregroundColor: NSColor.white,
375 let offset = NSPoint(x: 10, y: 10)
376 let padding = NSPoint(x: 5, y: 2)
377 let size = text.size(withAttributes: textAttributes)
378 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)
379 var textRect = NSRect(x: location.x + offset.x + padding.x, y: location.y + offset.y + padding.y, width: size.width, height: size.height)
382 rect = rect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
383 textRect = textRect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
389 text.draw(in: textRect, withAttributes: textAttributes)