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(_ 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]
31 self.isMovableByWindowBackground = false
32 self.isMovable = false
33 self.titlebarAppearsTransparent = true
34 self.setFrame(boundingBox, display: true)
35 self.titleVisibility = .hidden
36 let recordingView = RecordingContentView()
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
44 self.makeKeyAndOrderFront(nil)
47 // MARK: - Window Behavior Overrides
49 override func resetCursorRects() {
50 super.resetCursorRects()
51 let cursor = NSCursor.crosshair
52 self.contentView?.addCursorRect(self.contentView!.bounds, cursor: cursor)
55 override var canBecomeKey: Bool {
59 override var canBecomeMain: Bool {
63 override func resignMain() {
65 if (self.contentView as? RecordingContentView)?.state != .recording {
66 self.ignoresMouseEvents = false
70 override func becomeMain() {
72 if (self.contentView as? RecordingContentView)?.state != .recording {
73 (self.contentView as? RecordingContentView)?.state = .idle
78 enum RecordingWindowState {
79 case passthrough, idle, drawing, moving, resizing, recording;
82 class RecordingContentView: NSView {
84 public var button: NSRect? = nil
85 @Published public var box: NSRect? = nil
86 public var state: RecordingWindowState = .idle
87 private var mouseLocation: NSPoint = NSPoint()
88 private var origin: NSPoint = NSPoint()
89 private var boxOrigin: NSPoint = NSPoint()
91 private var resizeBox: NSRect? {
93 return NSRect(x: box.maxX - 5, y: box.minY - 5, width: 10, height: 10)
98 private var shouldPassthrough: Bool {
99 state == .recording || state == .passthrough
102 // MARK: - State changing API
104 public func startRecording() {
106 window?.ignoresMouseEvents = true
109 public func stopRecording() {
113 public func reset() {
115 window?.ignoresMouseEvents = false
118 public func startPassthrough() {
120 window?.ignoresMouseEvents = true
123 public func stopPassthrough() {
125 window?.ignoresMouseEvents = false
128 // MARK: - View Behavior Overrides
130 override func updateTrackingAreas() {
131 super.updateTrackingAreas()
133 for trackingArea in self.trackingAreas {
134 self.removeTrackingArea(trackingArea)
137 let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeInKeyWindow, .cursorUpdate, .mouseMoved]
138 let trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
139 self.addTrackingArea(trackingArea)
142 override func mouseMoved(with event: NSEvent) {
144 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
146 if shouldPassthrough {
150 if resizeBox!.contains(mouseLocation) {
153 if box.contains(mouseLocation) {
154 NSCursor.openHand.set()
156 NSCursor.crosshair.set()
160 if button.contains(mouseLocation) {
165 NSCursor.crosshair.set()
169 self.setNeedsDisplay(self.bounds)
172 override func mouseDragged(with event: NSEvent) {
173 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
174 if state == .drawing {
176 x: round(min(origin.x, mouseLocation.x)),
177 y: round(min(origin.y, mouseLocation.y)),
178 width: round(abs(mouseLocation.x - origin.x)),
179 height: round(abs(mouseLocation.y - origin.y))
184 if state == .moving {
185 NSCursor.closedHand.set()
186 box!.origin = NSPoint(
187 x: self.boxOrigin.x - self.origin.x + self.mouseLocation.x,
188 y: self.boxOrigin.y - self.origin.y + self.mouseLocation.y)
191 if state == .resizing {
193 x: round(min(origin.x, mouseLocation.x)),
194 y: round(min(origin.y, mouseLocation.y)),
195 width: round(abs(mouseLocation.x - origin.x)),
196 height: round(abs(mouseLocation.y - origin.y))
200 self.setNeedsDisplay(self.bounds)
203 override func cursorUpdate(with event: NSEvent) {
204 NSCursor.crosshair.set()
207 override func hitTest(_ point: NSPoint) -> NSView? {
208 return shouldPassthrough ? nil : self
211 override var acceptsFirstResponder: Bool {
215 override func mouseDown(with event: NSEvent) {
216 self.origin = self.convert(event.locationInWindow, from: nil)
220 if button.contains(origin) {
221 NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil)
226 if resizeBox!.contains(origin) {
227 self.origin = NSPoint(x: box.minX, y: box.maxY)
231 if box.contains(origin) {
233 self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y)
241 override func mouseUp(with event: NSEvent) {
242 if state != .recording {
247 override func keyDown(with event: NSEvent) {
248 switch event.keyCode {
249 case 53: // Escape key
250 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
252 super.keyDown(with: event)
256 override func flagsChanged(with event: NSEvent) {
258 if event.modifierFlags.contains(.shift) {
266 override func draw(_ dirtyRect: NSRect) {
267 if shouldPassthrough {
268 NSColor.clear.setFill()
270 NSColor(white: 1.0, alpha: 0.001).setFill()
274 let dashLength: CGFloat = 5.0
277 if state == .idle && box == nil {
278 let blackLine = NSBezierPath()
279 blackLine.lineWidth = lineWidth
280 blackLine.setLineDash([dashLength, dashLength], count: 2, phase: 0)
282 // Vertical line (Black)
283 blackLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
284 blackLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
286 // Horizontal line (Black)
287 blackLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
288 blackLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
290 NSColor.black.setStroke()
293 let whiteLine = NSBezierPath()
294 whiteLine.lineWidth = lineWidth
295 whiteLine.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
297 // Vertical line (White)
298 whiteLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
299 whiteLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
301 // Horizontal line (White)
302 whiteLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
303 whiteLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
305 NSColor.white.setStroke()
310 let blackBox = NSBezierPath()
311 blackBox.lineWidth = lineWidth
312 blackBox.setLineDash([dashLength, dashLength], count: 2, phase: 0)
313 blackBox.move(to: NSPoint(x: box.minX, y: box.minY))
314 blackBox.line(to: NSPoint(x: box.maxX, y: box.minY))
315 blackBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
316 blackBox.line(to: NSPoint(x: box.minX, y: box.maxY))
317 blackBox.line(to: NSPoint(x: box.minX, y: box.minY))
318 NSColor.black.setStroke()
321 let whiteBox = NSBezierPath()
322 whiteBox.lineWidth = lineWidth
323 whiteBox.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
324 whiteBox.move(to: NSPoint(x: box.minX, y: box.minY))
325 whiteBox.line(to: NSPoint(x: box.maxX, y: box.minY))
326 whiteBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
327 whiteBox.line(to: NSPoint(x: box.minX, y: box.maxY))
328 whiteBox.line(to: NSPoint(x: box.minX, y: box.minY))
329 NSColor.white.setStroke()
332 if state == .recording {
337 let clearBox = NSBezierPath()
338 clearBox.move(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
339 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.minY))
340 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.maxY))
341 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.maxY))
342 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
343 NSColor(white: 0, alpha: 0.2).setFill()
347 if state == .moving {
348 let string = "\(Int(box.minX)), \(Int(box.maxY))" as NSString
349 drawText(string, NSPoint(
355 if state == .resizing {
356 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
357 drawText(string, mouseLocation)
360 if box.contains(mouseLocation) && state != .resizing {
367 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
368 drawText(string, mouseLocation)
373 private func drawText(_ text: NSString, _ location: NSPoint, _ isBottomRight: Bool = false) {
375 let textAttributes = [
376 NSAttributedString.Key.font: NSFont(name: "Hiragino Mincho ProN", size: 12) ?? NSFont.systemFont(ofSize: 12),
377 NSAttributedString.Key.foregroundColor: NSColor.white,
379 let offset = NSPoint(x: 10, y: 10)
380 let padding = NSPoint(x: 5, y: 2)
381 let size = text.size(withAttributes: textAttributes)
382 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)
383 var textRect = NSRect(x: location.x + offset.x + padding.x, y: location.y + offset.y + padding.y, width: size.width, height: size.height)
386 rect = rect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
387 textRect = textRect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
393 text.draw(in: textRect, withAttributes: textAttributes)