]> git.r.bdr.sh - rbdr/captura/blame - Captura/Presentation/Windows/RecordingWindow.swift
Add about and sandbox sparkle
[rbdr/captura] / Captura / Presentation / Windows / RecordingWindow.swift
CommitLineData
24220348 1import Cocoa
a4e80427 2import Combine
24220348
RBR
3
4class 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 15
7ee43fb8 16 let boundingBox = NSScreen.screenWithMouse?.frame ?? NSZeroRect
24220348
RBR
17
18 super.init(
19 contentRect: boundingBox,
20 styleMask: [.borderless],
21 backing: .buffered,
22 defer: false)
23
7ee43fb8 24
e834022c 25 self.isReleasedWhenClosed = false
a4e80427 26 self.collectionBehavior = [.canJoinAllSpaces]
24220348
RBR
27 self.isMovableByWindowBackground = false
28 self.isMovable = false
533cd932 29 self.canHide = false
24220348
RBR
30 self.titlebarAppearsTransparent = true
31 self.setFrame(boundingBox, display: true)
32 self.titleVisibility = .hidden
9431168d 33 let recordingView = RecordingContentView(configuration, frame: boundingBox, button: button ?? NSZeroRect)
a4e80427 34 recordingView.frame = boundingBox
a4e80427 35 self.contentView = recordingView
082b61f3
RBR
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)
24220348
RBR
39 self.level = .screenSaver
40 self.isOpaque = false
41 self.hasShadow = false
24220348
RBR
42 }
43
a4e80427
RBR
44 // MARK: - Window Behavior Overrides
45
24220348
RBR
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 }
e834022c
RBR
59
60 override func resignMain() {
61 super.resignMain()
a4e80427
RBR
62 if (self.contentView as? RecordingContentView)?.state != .recording {
63 self.ignoresMouseEvents = false
64 }
e834022c
RBR
65 }
66
67 override func becomeMain() {
68 super.becomeMain()
a4e80427
RBR
69 if (self.contentView as? RecordingContentView)?.state != .recording {
70 (self.contentView as? RecordingContentView)?.state = .idle
71 }
e834022c
RBR
72 }
73}
74
75enum RecordingWindowState {
a4e80427 76 case passthrough, idle, drawing, moving, resizing, recording;
24220348
RBR
77}
78
79class RecordingContentView: NSView {
80
9431168d 81 init(_ configuration: CaptureSessionConfiguration, frame: NSRect, button: NSRect) {
082b61f3
RBR
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
8e932130
RBR
92 super.init(frame: frame)
93 preventResize = configuration.preventResize
94 preventMove = configuration.preventMove
95 autoStart = configuration.autoStart
9431168d 96
082b61f3
RBR
97 self.bounds = frame
98 self.button = NSRect(x: frame.maxX - buttonOffset.x, y: frame.maxY - buttonOffset.y, width: buttonSize.width, height: buttonSize.height)
8e932130
RBR
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
082b61f3
RBR
120 private let buttonSize: NSSize
121 private let buttonOffset: NSPoint
a4e80427
RBR
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()
8e932130
RBR
128 private var preventResize = false
129 private var preventMove = false
130 private var autoStart = false
24220348 131
a4e80427 132 private var resizeBox: NSRect? {
24220348
RBR
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
a4e80427
RBR
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
24220348
RBR
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
7ee43fb8
RBR
183 override func mouseExited(with event: NSEvent) {
184 if state == .idle && box == nil {
185 self.moveWindow()
186 }
187 }
188
24220348
RBR
189 override func mouseMoved(with event: NSEvent) {
190
191 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
192
a4e80427
RBR
193 if shouldPassthrough {
194 NSCursor.arrow.set()
195 } else {
196 if let box {
197 if resizeBox!.contains(mouseLocation) {
198 NSCursor.arrow.set()
24220348 199 } else {
a4e80427
RBR
200 if box.contains(mouseLocation) {
201 NSCursor.openHand.set()
202 } else {
203 NSCursor.crosshair.set()
204 }
24220348 205 }
a4e80427
RBR
206 if let button {
207 if button.contains(mouseLocation) {
208 NSCursor.arrow.set()
209 }
210 }
211 } else {
212 NSCursor.crosshair.set()
24220348 213 }
24220348 214 }
24220348
RBR
215
216 self.setNeedsDisplay(self.bounds)
217 }
218
219 override func mouseDragged(with event: NSEvent) {
220 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
e834022c 221 if state == .drawing {
24220348
RBR
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
e834022c
RBR
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 }
24220348
RBR
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? {
a4e80427 255 return shouldPassthrough ? nil : self
24220348
RBR
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 {
a4e80427
RBR
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
8e932130 273 if resizeBox!.contains(origin) && !preventResize {
e834022c
RBR
274 self.origin = NSPoint(x: box.minX, y: box.maxY)
275 state = .resizing
276 return
277 }
8e932130 278 if box.contains(origin) && !preventMove {
e834022c 279 state = .moving
24220348
RBR
280 self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y)
281 return
282 }
283 }
284
8e932130
RBR
285 if preventResize || preventMove {
286 return
287 }
288
e834022c 289 state = .drawing
24220348
RBR
290 }
291
292 override func mouseUp(with event: NSEvent) {
a4e80427
RBR
293 if state != .recording {
294 state = .idle
295 }
e834022c
RBR
296 }
297
298 override func keyDown(with event: NSEvent) {
e834022c
RBR
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) {
a4e80427 308 if state == .idle {
e834022c 309 if event.modifierFlags.contains(.shift) {
a4e80427 310 startPassthrough()
e834022c 311 } else {
a4e80427 312 stopPassthrough()
e834022c 313 }
a4e80427 314 }
24220348
RBR
315 }
316
317 override func draw(_ dirtyRect: NSRect) {
a4e80427 318 if shouldPassthrough {
e834022c
RBR
319 NSColor.clear.setFill()
320 } else {
321 NSColor(white: 1.0, alpha: 0.001).setFill()
322 }
24220348
RBR
323 dirtyRect.fill()
324
325 let dashLength: CGFloat = 5.0
326 let lineWidth = 0.5
082b61f3
RBR
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// }
24220348 339
e834022c 340 if state == .idle && box == nil {
24220348
RBR
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
a4e80427
RBR
395 if state == .recording {
396 return
397 }
398
24220348
RBR
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
e834022c
RBR
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 {
24220348
RBR
424 return;
425 }
426 }
427
428 // Draw text
e834022c
RBR
429
430 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
431 drawText(string, mouseLocation)
432 }
433
a4e80427
RBR
434 // MARK: - Utilities
435
e834022c 436 private func drawText(_ text: NSString, _ location: NSPoint, _ isBottomRight: Bool = false) {
24220348
RBR
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 ]
e834022c
RBR
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 }
24220348
RBR
452
453 NSColor.black.set()
454 rect.fill()
455
e834022c 456 text.draw(in: textRect, withAttributes: textAttributes)
24220348 457 }
7ee43fb8
RBR
458
459 private func moveWindow() {
7ee43fb8
RBR
460 let screen = NSScreen.screenWithMouse
461 if let currentScreen = self.window?.screen {
462 if currentScreen != screen {
463 let frame = screen?.frame ?? NSZeroRect
9431168d
RBR
464 self.frame = CGRect(origin: NSZeroPoint, size: frame.size)
465 self.bounds = CGRect(origin: NSZeroPoint, size: frame.size)
7ee43fb8
RBR
466 self.updateTrackingAreas()
467
468 if let window = self.window {
469 self.bounds = frame
082b61f3 470 self.button = NSRect(x: frame.maxX - buttonOffset.x, y: frame.maxY - buttonOffset.y, width: buttonSize.width, height: buttonSize.height)
7ee43fb8
RBR
471 window.setFrame(frame, display: true, animate: false)
472 window.makeKeyAndOrderFront(nil)
473 window.orderFrontRegardless()
7ee43fb8
RBR
474 }
475 }
476 }
477 }
24220348 478}