]>
Commit | Line | Data |
---|---|---|
1 | import Cocoa | |
2 | import Combine | |
3 | ||
4 | class RecordingWindow: NSWindow { | |
5 | ||
6 | var pixelDensity: CGFloat { | |
7 | self.screen?.backingScaleFactor ?? 1.0 | |
8 | } | |
9 | ||
10 | var recordingContentView: RecordingContentView { | |
11 | self.contentView as! RecordingContentView | |
12 | } | |
13 | ||
14 | init(_ configuration: CaptureSessionConfiguration, _ button: NSRect?) { | |
15 | ||
16 | let boundingBox = NSScreen.screenWithMouse?.frame ?? NSZeroRect | |
17 | ||
18 | super.init( | |
19 | contentRect: boundingBox, | |
20 | styleMask: [.borderless], | |
21 | backing: .buffered, | |
22 | defer: false) | |
23 | ||
24 | ||
25 | self.isReleasedWhenClosed = false | |
26 | self.collectionBehavior = [.canJoinAllSpaces] | |
27 | self.isMovableByWindowBackground = false | |
28 | self.isMovable = false | |
29 | self.canHide = 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 | |
40 | self.isOpaque = false | |
41 | self.hasShadow = false | |
42 | } | |
43 | ||
44 | // MARK: - Window Behavior Overrides | |
45 | ||
46 | override func resetCursorRects() { | |
47 | super.resetCursorRects() | |
48 | let cursor = NSCursor.crosshair | |
49 | self.contentView?.addCursorRect(self.contentView!.bounds, cursor: cursor) | |
50 | } | |
51 | ||
52 | override var canBecomeKey: Bool { | |
53 | return true | |
54 | } | |
55 | ||
56 | override var canBecomeMain: Bool { | |
57 | return true | |
58 | } | |
59 | ||
60 | override func resignMain() { | |
61 | super.resignMain() | |
62 | if (self.contentView as? RecordingContentView)?.state != .recording { | |
63 | self.ignoresMouseEvents = false | |
64 | } | |
65 | } | |
66 | ||
67 | override func becomeMain() { | |
68 | super.becomeMain() | |
69 | if (self.contentView as? RecordingContentView)?.state != .recording { | |
70 | (self.contentView as? RecordingContentView)?.state = .idle | |
71 | } | |
72 | } | |
73 | } | |
74 | ||
75 | enum RecordingWindowState { | |
76 | case passthrough, idle, drawing, moving, resizing, recording; | |
77 | } | |
78 | ||
79 | class RecordingContentView: NSView { | |
80 | ||
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) | |
89 | } | |
90 | } | |
91 | self.buttonOffset = buttonOffset | |
92 | super.init(frame: frame) | |
93 | preventResize = configuration.preventResize | |
94 | preventMove = configuration.preventMove | |
95 | autoStart = configuration.autoStart | |
96 | ||
97 | self.bounds = frame | |
98 | self.button = NSRect(x: frame.maxX - buttonOffset.x, y: frame.maxY - buttonOffset.y, width: buttonSize.width, height: buttonSize.height) | |
99 | ||
100 | if configuration.x != nil || configuration.y != nil || configuration.width != nil || configuration.height != nil { | |
101 | box = NSRect( | |
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 | |
106 | ) | |
107 | } | |
108 | ||
109 | if autoStart { | |
110 | DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) { | |
111 | NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil) | |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | required init?(coder: NSCoder) { | |
117 | fatalError("init(coder:) has not been implemented") | |
118 | } | |
119 | ||
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 | |
131 | ||
132 | private var resizeBox: NSRect? { | |
133 | if let box { | |
134 | return NSRect(x: box.maxX - 5, y: box.minY - 5, width: 10, height: 10) | |
135 | } | |
136 | return nil | |
137 | } | |
138 | ||
139 | private var shouldPassthrough: Bool { | |
140 | state == .recording || state == .passthrough | |
141 | } | |
142 | ||
143 | // MARK: - State changing API | |
144 | ||
145 | public func startRecording() { | |
146 | state = .recording | |
147 | window?.ignoresMouseEvents = true | |
148 | } | |
149 | ||
150 | public func stopRecording() { | |
151 | ||
152 | } | |
153 | ||
154 | public func reset() { | |
155 | state = .idle | |
156 | window?.ignoresMouseEvents = false | |
157 | } | |
158 | ||
159 | public func startPassthrough() { | |
160 | state = .passthrough | |
161 | window?.ignoresMouseEvents = true | |
162 | } | |
163 | ||
164 | public func stopPassthrough() { | |
165 | state = .idle | |
166 | window?.ignoresMouseEvents = false | |
167 | } | |
168 | ||
169 | // MARK: - View Behavior Overrides | |
170 | ||
171 | override func updateTrackingAreas() { | |
172 | super.updateTrackingAreas() | |
173 | ||
174 | for trackingArea in self.trackingAreas { | |
175 | self.removeTrackingArea(trackingArea) | |
176 | } | |
177 | ||
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) | |
181 | } | |
182 | ||
183 | override func mouseExited(with event: NSEvent) { | |
184 | if state == .idle && box == nil { | |
185 | self.moveWindow() | |
186 | } | |
187 | } | |
188 | ||
189 | override func mouseMoved(with event: NSEvent) { | |
190 | ||
191 | self.mouseLocation = self.convert(event.locationInWindow, from: nil) | |
192 | ||
193 | if shouldPassthrough { | |
194 | NSCursor.arrow.set() | |
195 | } else { | |
196 | if let box { | |
197 | if resizeBox!.contains(mouseLocation) { | |
198 | NSCursor.arrow.set() | |
199 | } else { | |
200 | if box.contains(mouseLocation) { | |
201 | NSCursor.openHand.set() | |
202 | } else { | |
203 | NSCursor.crosshair.set() | |
204 | } | |
205 | } | |
206 | if let button { | |
207 | if button.contains(mouseLocation) { | |
208 | NSCursor.arrow.set() | |
209 | } | |
210 | } | |
211 | } else { | |
212 | NSCursor.crosshair.set() | |
213 | } | |
214 | } | |
215 | ||
216 | self.setNeedsDisplay(self.bounds) | |
217 | } | |
218 | ||
219 | override func mouseDragged(with event: NSEvent) { | |
220 | self.mouseLocation = self.convert(event.locationInWindow, from: nil) | |
221 | if state == .drawing { | |
222 | box = NSRect( | |
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)) | |
227 | ) | |
228 | } | |
229 | ||
230 | if box != nil { | |
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) | |
236 | } | |
237 | ||
238 | if state == .resizing { | |
239 | box = NSRect( | |
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)) | |
244 | ) | |
245 | } | |
246 | } | |
247 | self.setNeedsDisplay(self.bounds) | |
248 | } | |
249 | ||
250 | override func cursorUpdate(with event: NSEvent) { | |
251 | NSCursor.crosshair.set() | |
252 | } | |
253 | ||
254 | override func hitTest(_ point: NSPoint) -> NSView? { | |
255 | return shouldPassthrough ? nil : self | |
256 | } | |
257 | ||
258 | override var acceptsFirstResponder: Bool { | |
259 | return true | |
260 | } | |
261 | ||
262 | override func mouseDown(with event: NSEvent) { | |
263 | self.origin = self.convert(event.locationInWindow, from: nil) | |
264 | if let box { | |
265 | ||
266 | if let button { | |
267 | if button.contains(origin) { | |
268 | NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil) | |
269 | return | |
270 | } | |
271 | } | |
272 | ||
273 | if resizeBox!.contains(origin) && !preventResize { | |
274 | self.origin = NSPoint(x: box.minX, y: box.maxY) | |
275 | state = .resizing | |
276 | return | |
277 | } | |
278 | if box.contains(origin) && !preventMove { | |
279 | state = .moving | |
280 | self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y) | |
281 | return | |
282 | } | |
283 | } | |
284 | ||
285 | if preventResize || preventMove { | |
286 | return | |
287 | } | |
288 | ||
289 | state = .drawing | |
290 | } | |
291 | ||
292 | override func mouseUp(with event: NSEvent) { | |
293 | if state != .recording { | |
294 | state = .idle | |
295 | } | |
296 | } | |
297 | ||
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) | |
302 | default: | |
303 | super.keyDown(with: event) | |
304 | } | |
305 | } | |
306 | ||
307 | override func flagsChanged(with event: NSEvent) { | |
308 | if state == .idle { | |
309 | if event.modifierFlags.contains(.shift) { | |
310 | startPassthrough() | |
311 | } else { | |
312 | stopPassthrough() | |
313 | } | |
314 | } | |
315 | } | |
316 | ||
317 | override func draw(_ dirtyRect: NSRect) { | |
318 | if shouldPassthrough { | |
319 | NSColor.clear.setFill() | |
320 | } else { | |
321 | NSColor(white: 1.0, alpha: 0.001).setFill() | |
322 | } | |
323 | dirtyRect.fill() | |
324 | ||
325 | let dashLength: CGFloat = 5.0 | |
326 | let lineWidth = 0.5 | |
327 | ||
328 | // Uncomment below to debug button placement visually | |
329 | // if let button { | |
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() | |
337 | // buttonPath.fill() | |
338 | // } | |
339 | ||
340 | if state == .idle && box == nil { | |
341 | let blackLine = NSBezierPath() | |
342 | blackLine.lineWidth = lineWidth | |
343 | blackLine.setLineDash([dashLength, dashLength], count: 2, phase: 0) | |
344 | ||
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))) | |
348 | ||
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)) | |
352 | ||
353 | NSColor.black.setStroke() | |
354 | blackLine.stroke() | |
355 | ||
356 | let whiteLine = NSBezierPath() | |
357 | whiteLine.lineWidth = lineWidth | |
358 | whiteLine.setLineDash([dashLength, dashLength], count: 2, phase: dashLength) | |
359 | ||
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))) | |
363 | ||
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)) | |
367 | ||
368 | NSColor.white.setStroke() | |
369 | whiteLine.stroke() | |
370 | } | |
371 | ||
372 | if let box { | |
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() | |
382 | blackBox.stroke() | |
383 | ||
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() | |
393 | whiteBox.stroke() | |
394 | ||
395 | if state == .recording { | |
396 | return | |
397 | } | |
398 | ||
399 | if let resizeBox { | |
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() | |
407 | clearBox.fill() | |
408 | } | |
409 | ||
410 | if state == .moving { | |
411 | let string = "\(Int(box.minX)), \(Int(box.maxY))" as NSString | |
412 | drawText(string, NSPoint( | |
413 | x: box.minX, | |
414 | y: box.maxY | |
415 | ), true) | |
416 | } | |
417 | ||
418 | if state == .resizing { | |
419 | let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString | |
420 | drawText(string, mouseLocation) | |
421 | } | |
422 | ||
423 | if box.contains(mouseLocation) && state != .resizing { | |
424 | return; | |
425 | } | |
426 | } | |
427 | ||
428 | // Draw text | |
429 | ||
430 | let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString | |
431 | drawText(string, mouseLocation) | |
432 | } | |
433 | ||
434 | // MARK: - Utilities | |
435 | ||
436 | private func drawText(_ text: NSString, _ location: NSPoint, _ isBottomRight: Bool = false) { | |
437 | ||
438 | let textAttributes = [ | |
439 | NSAttributedString.Key.font: NSFont(name: "Hiragino Mincho ProN", size: 12) ?? NSFont.systemFont(ofSize: 12), | |
440 | NSAttributedString.Key.foregroundColor: NSColor.white, | |
441 | ] | |
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) | |
447 | ||
448 | if (isBottomRight) { | |
449 | rect = rect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0) | |
450 | textRect = textRect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0) | |
451 | } | |
452 | ||
453 | NSColor.black.set() | |
454 | rect.fill() | |
455 | ||
456 | text.draw(in: textRect, withAttributes: textAttributes) | |
457 | } | |
458 | ||
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() | |
467 | ||
468 | if let window = self.window { | |
469 | self.bounds = frame | |
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() | |
474 | } | |
475 | } | |
476 | } | |
477 | } | |
478 | } |