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