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