]>
Commit | Line | Data |
---|---|---|
24220348 | 1 | import Cocoa |
a4e80427 | 2 | import Combine |
24220348 RBR |
3 | |
4 | class RecordingWindow: NSWindow { | |
5 | ||
a4e80427 RBR |
6 | var pixelDensity: CGFloat { |
7 | self.screen?.backingScaleFactor ?? 1.0 | |
8 | } | |
9 | ||
c9b9e1d6 RBR |
10 | var recordingContentView: RecordingContentView { |
11 | self.contentView as! RecordingContentView | |
12 | } | |
13 | ||
8e932130 | 14 | init(_ configuration: CaptureSessionConfiguration, _ button: NSRect?) { |
24220348 RBR |
15 | |
16 | let screens = NSScreen.screens | |
17 | var boundingBox = NSZeroRect | |
18 | for screen in screens { | |
19 | boundingBox = NSUnionRect(boundingBox, screen.frame) | |
20 | } | |
21 | ||
22 | super.init( | |
23 | contentRect: boundingBox, | |
24 | styleMask: [.borderless], | |
25 | backing: .buffered, | |
26 | defer: false) | |
27 | ||
e834022c | 28 | self.isReleasedWhenClosed = false |
a4e80427 | 29 | self.collectionBehavior = [.canJoinAllSpaces] |
24220348 RBR |
30 | self.isMovableByWindowBackground = false |
31 | self.isMovable = false | |
533cd932 | 32 | self.canHide = false |
24220348 RBR |
33 | self.titlebarAppearsTransparent = true |
34 | self.setFrame(boundingBox, display: true) | |
35 | self.titleVisibility = .hidden | |
8e932130 | 36 | let recordingView = RecordingContentView(configuration, frame: boundingBox) |
a4e80427 RBR |
37 | recordingView.frame = boundingBox |
38 | recordingView.button = button | |
39 | self.contentView = recordingView | |
24220348 RBR |
40 | self.backgroundColor = NSColor(white: 1.0, alpha: 0.001) |
41 | self.level = .screenSaver | |
42 | self.isOpaque = false | |
43 | self.hasShadow = false | |
24220348 RBR |
44 | } |
45 | ||
a4e80427 RBR |
46 | // MARK: - Window Behavior Overrides |
47 | ||
24220348 RBR |
48 | override func resetCursorRects() { |
49 | super.resetCursorRects() | |
50 | let cursor = NSCursor.crosshair | |
51 | self.contentView?.addCursorRect(self.contentView!.bounds, cursor: cursor) | |
52 | } | |
53 | ||
54 | override var canBecomeKey: Bool { | |
55 | return true | |
56 | } | |
57 | ||
58 | override var canBecomeMain: Bool { | |
59 | return true | |
60 | } | |
e834022c RBR |
61 | |
62 | override func resignMain() { | |
63 | super.resignMain() | |
a4e80427 RBR |
64 | if (self.contentView as? RecordingContentView)?.state != .recording { |
65 | self.ignoresMouseEvents = false | |
66 | } | |
e834022c RBR |
67 | } |
68 | ||
69 | override func becomeMain() { | |
70 | super.becomeMain() | |
a4e80427 RBR |
71 | if (self.contentView as? RecordingContentView)?.state != .recording { |
72 | (self.contentView as? RecordingContentView)?.state = .idle | |
73 | } | |
e834022c RBR |
74 | } |
75 | } | |
76 | ||
77 | enum RecordingWindowState { | |
a4e80427 | 78 | case passthrough, idle, drawing, moving, resizing, recording; |
24220348 RBR |
79 | } |
80 | ||
81 | class RecordingContentView: NSView { | |
82 | ||
8e932130 RBR |
83 | init(_ configuration: CaptureSessionConfiguration, frame: NSRect) { |
84 | super.init(frame: frame) | |
85 | preventResize = configuration.preventResize | |
86 | preventMove = configuration.preventMove | |
87 | autoStart = configuration.autoStart | |
88 | ||
89 | if configuration.x != nil || configuration.y != nil || configuration.width != nil || configuration.height != nil { | |
90 | box = NSRect( | |
91 | x: configuration.x ?? Int(frame.width / 2.0), | |
92 | y: configuration.y ?? Int(frame.height / 2.0), | |
93 | width: configuration.width ?? 400, | |
94 | height: configuration.height ?? 400 | |
95 | ) | |
96 | } | |
97 | ||
98 | if autoStart { | |
99 | DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) { | |
100 | NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil) | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
105 | required init?(coder: NSCoder) { | |
106 | fatalError("init(coder:) has not been implemented") | |
107 | } | |
108 | ||
a4e80427 RBR |
109 | public var button: NSRect? = nil |
110 | @Published public var box: NSRect? = nil | |
111 | public var state: RecordingWindowState = .idle | |
112 | private var mouseLocation: NSPoint = NSPoint() | |
113 | private var origin: NSPoint = NSPoint() | |
114 | private var boxOrigin: NSPoint = NSPoint() | |
8e932130 RBR |
115 | private var preventResize = false |
116 | private var preventMove = false | |
117 | private var autoStart = false | |
24220348 | 118 | |
a4e80427 | 119 | private var resizeBox: NSRect? { |
24220348 RBR |
120 | if let box { |
121 | return NSRect(x: box.maxX - 5, y: box.minY - 5, width: 10, height: 10) | |
122 | } | |
123 | return nil | |
124 | } | |
125 | ||
a4e80427 RBR |
126 | private var shouldPassthrough: Bool { |
127 | state == .recording || state == .passthrough | |
128 | } | |
129 | ||
130 | // MARK: - State changing API | |
131 | ||
132 | public func startRecording() { | |
133 | state = .recording | |
134 | window?.ignoresMouseEvents = true | |
135 | } | |
136 | ||
137 | public func stopRecording() { | |
138 | ||
139 | } | |
140 | ||
141 | public func reset() { | |
142 | state = .idle | |
143 | window?.ignoresMouseEvents = false | |
144 | } | |
145 | ||
146 | public func startPassthrough() { | |
147 | state = .passthrough | |
148 | window?.ignoresMouseEvents = true | |
149 | } | |
150 | ||
151 | public func stopPassthrough() { | |
152 | state = .idle | |
153 | window?.ignoresMouseEvents = false | |
154 | } | |
155 | ||
156 | // MARK: - View Behavior Overrides | |
157 | ||
24220348 RBR |
158 | override func updateTrackingAreas() { |
159 | super.updateTrackingAreas() | |
160 | ||
161 | for trackingArea in self.trackingAreas { | |
162 | self.removeTrackingArea(trackingArea) | |
163 | } | |
164 | ||
165 | let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeInKeyWindow, .cursorUpdate, .mouseMoved] | |
166 | let trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil) | |
167 | self.addTrackingArea(trackingArea) | |
168 | } | |
169 | ||
170 | override func mouseMoved(with event: NSEvent) { | |
171 | ||
172 | self.mouseLocation = self.convert(event.locationInWindow, from: nil) | |
173 | ||
a4e80427 RBR |
174 | if shouldPassthrough { |
175 | NSCursor.arrow.set() | |
176 | } else { | |
177 | if let box { | |
178 | if resizeBox!.contains(mouseLocation) { | |
179 | NSCursor.arrow.set() | |
24220348 | 180 | } else { |
a4e80427 RBR |
181 | if box.contains(mouseLocation) { |
182 | NSCursor.openHand.set() | |
183 | } else { | |
184 | NSCursor.crosshair.set() | |
185 | } | |
24220348 | 186 | } |
a4e80427 RBR |
187 | if let button { |
188 | if button.contains(mouseLocation) { | |
189 | NSCursor.arrow.set() | |
190 | } | |
191 | } | |
192 | } else { | |
193 | NSCursor.crosshair.set() | |
24220348 | 194 | } |
24220348 | 195 | } |
24220348 RBR |
196 | |
197 | self.setNeedsDisplay(self.bounds) | |
198 | } | |
199 | ||
200 | override func mouseDragged(with event: NSEvent) { | |
201 | self.mouseLocation = self.convert(event.locationInWindow, from: nil) | |
e834022c | 202 | if state == .drawing { |
24220348 RBR |
203 | box = NSRect( |
204 | x: round(min(origin.x, mouseLocation.x)), | |
205 | y: round(min(origin.y, mouseLocation.y)), | |
206 | width: round(abs(mouseLocation.x - origin.x)), | |
207 | height: round(abs(mouseLocation.y - origin.y)) | |
208 | ) | |
209 | } | |
210 | ||
e834022c RBR |
211 | if box != nil { |
212 | if state == .moving { | |
213 | NSCursor.closedHand.set() | |
214 | box!.origin = NSPoint( | |
215 | x: self.boxOrigin.x - self.origin.x + self.mouseLocation.x, | |
216 | y: self.boxOrigin.y - self.origin.y + self.mouseLocation.y) | |
217 | } | |
218 | ||
219 | if state == .resizing { | |
220 | box = NSRect( | |
221 | x: round(min(origin.x, mouseLocation.x)), | |
222 | y: round(min(origin.y, mouseLocation.y)), | |
223 | width: round(abs(mouseLocation.x - origin.x)), | |
224 | height: round(abs(mouseLocation.y - origin.y)) | |
225 | ) | |
226 | } | |
24220348 RBR |
227 | } |
228 | self.setNeedsDisplay(self.bounds) | |
229 | } | |
230 | ||
231 | override func cursorUpdate(with event: NSEvent) { | |
232 | NSCursor.crosshair.set() | |
233 | } | |
234 | ||
235 | override func hitTest(_ point: NSPoint) -> NSView? { | |
a4e80427 | 236 | return shouldPassthrough ? nil : self |
24220348 RBR |
237 | } |
238 | ||
239 | override var acceptsFirstResponder: Bool { | |
240 | return true | |
241 | } | |
242 | ||
243 | override func mouseDown(with event: NSEvent) { | |
244 | self.origin = self.convert(event.locationInWindow, from: nil) | |
245 | if let box { | |
a4e80427 RBR |
246 | |
247 | if let button { | |
248 | if button.contains(origin) { | |
249 | NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil) | |
250 | return | |
251 | } | |
252 | } | |
253 | ||
8e932130 | 254 | if resizeBox!.contains(origin) && !preventResize { |
e834022c RBR |
255 | self.origin = NSPoint(x: box.minX, y: box.maxY) |
256 | state = .resizing | |
257 | return | |
258 | } | |
8e932130 | 259 | if box.contains(origin) && !preventMove { |
e834022c | 260 | state = .moving |
24220348 RBR |
261 | self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y) |
262 | return | |
263 | } | |
264 | } | |
265 | ||
8e932130 RBR |
266 | if preventResize || preventMove { |
267 | return | |
268 | } | |
269 | ||
e834022c | 270 | state = .drawing |
24220348 RBR |
271 | } |
272 | ||
273 | override func mouseUp(with event: NSEvent) { | |
a4e80427 RBR |
274 | if state != .recording { |
275 | state = .idle | |
276 | } | |
e834022c RBR |
277 | } |
278 | ||
279 | override func keyDown(with event: NSEvent) { | |
e834022c RBR |
280 | switch event.keyCode { |
281 | case 53: // Escape key | |
282 | NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil) | |
283 | default: | |
284 | super.keyDown(with: event) | |
285 | } | |
286 | } | |
287 | ||
288 | override func flagsChanged(with event: NSEvent) { | |
a4e80427 | 289 | if state == .idle { |
e834022c | 290 | if event.modifierFlags.contains(.shift) { |
a4e80427 | 291 | startPassthrough() |
e834022c | 292 | } else { |
a4e80427 | 293 | stopPassthrough() |
e834022c | 294 | } |
a4e80427 | 295 | } |
24220348 RBR |
296 | } |
297 | ||
298 | override func draw(_ dirtyRect: NSRect) { | |
a4e80427 | 299 | if shouldPassthrough { |
e834022c RBR |
300 | NSColor.clear.setFill() |
301 | } else { | |
302 | NSColor(white: 1.0, alpha: 0.001).setFill() | |
303 | } | |
24220348 RBR |
304 | dirtyRect.fill() |
305 | ||
306 | let dashLength: CGFloat = 5.0 | |
307 | let lineWidth = 0.5 | |
308 | ||
e834022c | 309 | if state == .idle && box == nil { |
24220348 RBR |
310 | let blackLine = NSBezierPath() |
311 | blackLine.lineWidth = lineWidth | |
312 | blackLine.setLineDash([dashLength, dashLength], count: 2, phase: 0) | |
313 | ||
314 | // Vertical line (Black) | |
315 | blackLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds))) | |
316 | blackLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds))) | |
317 | ||
318 | // Horizontal line (Black) | |
319 | blackLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y)) | |
320 | blackLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y)) | |
321 | ||
322 | NSColor.black.setStroke() | |
323 | blackLine.stroke() | |
324 | ||
325 | let whiteLine = NSBezierPath() | |
326 | whiteLine.lineWidth = lineWidth | |
327 | whiteLine.setLineDash([dashLength, dashLength], count: 2, phase: dashLength) | |
328 | ||
329 | // Vertical line (White) | |
330 | whiteLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds))) | |
331 | whiteLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds))) | |
332 | ||
333 | // Horizontal line (White) | |
334 | whiteLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y)) | |
335 | whiteLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y)) | |
336 | ||
337 | NSColor.white.setStroke() | |
338 | whiteLine.stroke() | |
339 | } | |
340 | ||
341 | if let box { | |
342 | let blackBox = NSBezierPath() | |
343 | blackBox.lineWidth = lineWidth | |
344 | blackBox.setLineDash([dashLength, dashLength], count: 2, phase: 0) | |
345 | blackBox.move(to: NSPoint(x: box.minX, y: box.minY)) | |
346 | blackBox.line(to: NSPoint(x: box.maxX, y: box.minY)) | |
347 | blackBox.line(to: NSPoint(x: box.maxX, y: box.maxY)) | |
348 | blackBox.line(to: NSPoint(x: box.minX, y: box.maxY)) | |
349 | blackBox.line(to: NSPoint(x: box.minX, y: box.minY)) | |
350 | NSColor.black.setStroke() | |
351 | blackBox.stroke() | |
352 | ||
353 | let whiteBox = NSBezierPath() | |
354 | whiteBox.lineWidth = lineWidth | |
355 | whiteBox.setLineDash([dashLength, dashLength], count: 2, phase: dashLength) | |
356 | whiteBox.move(to: NSPoint(x: box.minX, y: box.minY)) | |
357 | whiteBox.line(to: NSPoint(x: box.maxX, y: box.minY)) | |
358 | whiteBox.line(to: NSPoint(x: box.maxX, y: box.maxY)) | |
359 | whiteBox.line(to: NSPoint(x: box.minX, y: box.maxY)) | |
360 | whiteBox.line(to: NSPoint(x: box.minX, y: box.minY)) | |
361 | NSColor.white.setStroke() | |
362 | whiteBox.stroke() | |
363 | ||
a4e80427 RBR |
364 | if state == .recording { |
365 | return | |
366 | } | |
367 | ||
24220348 RBR |
368 | if let resizeBox { |
369 | let clearBox = NSBezierPath() | |
370 | clearBox.move(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY)) | |
371 | clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.minY)) | |
372 | clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.maxY)) | |
373 | clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.maxY)) | |
374 | clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY)) | |
375 | NSColor(white: 0, alpha: 0.2).setFill() | |
376 | clearBox.fill() | |
377 | } | |
378 | ||
e834022c RBR |
379 | if state == .moving { |
380 | let string = "\(Int(box.minX)), \(Int(box.maxY))" as NSString | |
381 | drawText(string, NSPoint( | |
382 | x: box.minX, | |
383 | y: box.maxY | |
384 | ), true) | |
385 | } | |
386 | ||
387 | if state == .resizing { | |
388 | let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString | |
389 | drawText(string, mouseLocation) | |
390 | } | |
391 | ||
392 | if box.contains(mouseLocation) && state != .resizing { | |
24220348 RBR |
393 | return; |
394 | } | |
395 | } | |
396 | ||
397 | // Draw text | |
e834022c RBR |
398 | |
399 | let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString | |
400 | drawText(string, mouseLocation) | |
401 | } | |
402 | ||
a4e80427 RBR |
403 | // MARK: - Utilities |
404 | ||
e834022c | 405 | private func drawText(_ text: NSString, _ location: NSPoint, _ isBottomRight: Bool = false) { |
24220348 RBR |
406 | |
407 | let textAttributes = [ | |
408 | NSAttributedString.Key.font: NSFont(name: "Hiragino Mincho ProN", size: 12) ?? NSFont.systemFont(ofSize: 12), | |
409 | NSAttributedString.Key.foregroundColor: NSColor.white, | |
410 | ] | |
e834022c RBR |
411 | let offset = NSPoint(x: 10, y: 10) |
412 | let padding = NSPoint(x: 5, y: 2) | |
413 | let size = text.size(withAttributes: textAttributes) | |
414 | 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) | |
415 | var textRect = NSRect(x: location.x + offset.x + padding.x, y: location.y + offset.y + padding.y, width: size.width, height: size.height) | |
416 | ||
417 | if (isBottomRight) { | |
418 | rect = rect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0) | |
419 | textRect = textRect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0) | |
420 | } | |
24220348 RBR |
421 | |
422 | NSColor.black.set() | |
423 | rect.fill() | |
424 | ||
e834022c | 425 | text.draw(in: textRect, withAttributes: textAttributes) |
24220348 RBR |
426 | } |
427 | } |