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