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]
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()
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 public var button: NSRect? = nil
84 @Published public var box: NSRect? = nil
85 public var state: RecordingWindowState = .idle
86 private var mouseLocation: NSPoint = NSPoint()
87 private var origin: NSPoint = NSPoint()
88 private var boxOrigin: NSPoint = NSPoint()
90 private var resizeBox: NSRect? {
92 return NSRect(x: box.maxX - 5, y: box.minY - 5, width: 10, height: 10)
97 private var shouldPassthrough: Bool {
98 state == .recording || state == .passthrough
101 // MARK: - State changing API
103 public func startRecording() {
105 window?.ignoresMouseEvents = true
108 public func stopRecording() {
112 public func reset() {
114 window?.ignoresMouseEvents = false
117 public func startPassthrough() {
119 window?.ignoresMouseEvents = true
122 public func stopPassthrough() {
124 window?.ignoresMouseEvents = false
127 // MARK: - View Behavior Overrides
129 override func updateTrackingAreas() {
130 super.updateTrackingAreas()
132 for trackingArea in self.trackingAreas {
133 self.removeTrackingArea(trackingArea)
136 let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeInKeyWindow, .cursorUpdate, .mouseMoved]
137 let trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil)
138 self.addTrackingArea(trackingArea)
141 override func mouseMoved(with event: NSEvent) {
143 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
145 if shouldPassthrough {
149 if resizeBox!.contains(mouseLocation) {
152 if box.contains(mouseLocation) {
153 NSCursor.openHand.set()
155 NSCursor.crosshair.set()
159 if button.contains(mouseLocation) {
164 NSCursor.crosshair.set()
168 self.setNeedsDisplay(self.bounds)
171 override func mouseDragged(with event: NSEvent) {
172 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
173 if state == .drawing {
175 x: round(min(origin.x, mouseLocation.x)),
176 y: round(min(origin.y, mouseLocation.y)),
177 width: round(abs(mouseLocation.x - origin.x)),
178 height: round(abs(mouseLocation.y - origin.y))
183 if state == .moving {
184 NSCursor.closedHand.set()
185 box!.origin = NSPoint(
186 x: self.boxOrigin.x - self.origin.x + self.mouseLocation.x,
187 y: self.boxOrigin.y - self.origin.y + self.mouseLocation.y)
190 if state == .resizing {
192 x: round(min(origin.x, mouseLocation.x)),
193 y: round(min(origin.y, mouseLocation.y)),
194 width: round(abs(mouseLocation.x - origin.x)),
195 height: round(abs(mouseLocation.y - origin.y))
199 self.setNeedsDisplay(self.bounds)
202 override func cursorUpdate(with event: NSEvent) {
203 NSCursor.crosshair.set()
206 override func hitTest(_ point: NSPoint) -> NSView? {
207 return shouldPassthrough ? nil : self
210 override var acceptsFirstResponder: Bool {
214 override func mouseDown(with event: NSEvent) {
215 self.origin = self.convert(event.locationInWindow, from: nil)
219 if button.contains(origin) {
220 NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil)
225 if resizeBox!.contains(origin) {
226 self.origin = NSPoint(x: box.minX, y: box.maxY)
230 if box.contains(origin) {
232 self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y)
240 override func mouseUp(with event: NSEvent) {
241 if state != .recording {
246 override func keyDown(with event: NSEvent) {
247 switch event.keyCode {
248 case 53: // Escape key
249 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
251 super.keyDown(with: event)
255 override func flagsChanged(with event: NSEvent) {
257 if event.modifierFlags.contains(.shift) {
265 override func draw(_ dirtyRect: NSRect) {
266 if shouldPassthrough {
267 NSColor.clear.setFill()
269 NSColor(white: 1.0, alpha: 0.001).setFill()
273 let dashLength: CGFloat = 5.0
276 if state == .idle && box == nil {
277 let blackLine = NSBezierPath()
278 blackLine.lineWidth = lineWidth
279 blackLine.setLineDash([dashLength, dashLength], count: 2, phase: 0)
281 // Vertical line (Black)
282 blackLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
283 blackLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
285 // Horizontal line (Black)
286 blackLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
287 blackLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
289 NSColor.black.setStroke()
292 let whiteLine = NSBezierPath()
293 whiteLine.lineWidth = lineWidth
294 whiteLine.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
296 // Vertical line (White)
297 whiteLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds)))
298 whiteLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds)))
300 // Horizontal line (White)
301 whiteLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y))
302 whiteLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y))
304 NSColor.white.setStroke()
309 let blackBox = NSBezierPath()
310 blackBox.lineWidth = lineWidth
311 blackBox.setLineDash([dashLength, dashLength], count: 2, phase: 0)
312 blackBox.move(to: NSPoint(x: box.minX, y: box.minY))
313 blackBox.line(to: NSPoint(x: box.maxX, y: box.minY))
314 blackBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
315 blackBox.line(to: NSPoint(x: box.minX, y: box.maxY))
316 blackBox.line(to: NSPoint(x: box.minX, y: box.minY))
317 NSColor.black.setStroke()
320 let whiteBox = NSBezierPath()
321 whiteBox.lineWidth = lineWidth
322 whiteBox.setLineDash([dashLength, dashLength], count: 2, phase: dashLength)
323 whiteBox.move(to: NSPoint(x: box.minX, y: box.minY))
324 whiteBox.line(to: NSPoint(x: box.maxX, y: box.minY))
325 whiteBox.line(to: NSPoint(x: box.maxX, y: box.maxY))
326 whiteBox.line(to: NSPoint(x: box.minX, y: box.maxY))
327 whiteBox.line(to: NSPoint(x: box.minX, y: box.minY))
328 NSColor.white.setStroke()
331 if state == .recording {
336 let clearBox = NSBezierPath()
337 clearBox.move(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
338 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.minY))
339 clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.maxY))
340 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.maxY))
341 clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY))
342 NSColor(white: 0, alpha: 0.2).setFill()
346 if state == .moving {
347 let string = "\(Int(box.minX)), \(Int(box.maxY))" as NSString
348 drawText(string, NSPoint(
354 if state == .resizing {
355 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
356 drawText(string, mouseLocation)
359 if box.contains(mouseLocation) && state != .resizing {
366 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
367 drawText(string, mouseLocation)
372 private func drawText(_ text: NSString, _ location: NSPoint, _ isBottomRight: Bool = false) {
374 let textAttributes = [
375 NSAttributedString.Key.font: NSFont(name: "Hiragino Mincho ProN", size: 12) ?? NSFont.systemFont(ofSize: 12),
376 NSAttributedString.Key.foregroundColor: NSColor.white,
378 let offset = NSPoint(x: 10, y: 10)
379 let padding = NSPoint(x: 5, y: 2)
380 let size = text.size(withAttributes: textAttributes)
381 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)
382 var textRect = NSRect(x: location.x + offset.x + padding.x, y: location.y + offset.y + padding.y, width: size.width, height: size.height)
385 rect = rect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
386 textRect = textRect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0)
392 text.draw(in: textRect, withAttributes: textAttributes)