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