]> git.r.bdr.sh - rbdr/captura/blame - Captura/Presentation/Windows/RecordingWindow.swift
Add clipboard
[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
a4e80427 14 init(_ button: NSRect?) {
24220348
RBR
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
e834022c 28 self.isReleasedWhenClosed = false
a4e80427 29 self.collectionBehavior = [.canJoinAllSpaces]
24220348
RBR
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
a4e80427
RBR
36 let recordingView = RecordingContentView()
37 recordingView.frame = boundingBox
38 recordingView.button = button
39 self.contentView = recordingView
24220348
RBR
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)
24220348
RBR
45 }
46
a4e80427
RBR
47 // MARK: - Window Behavior Overrides
48
24220348
RBR
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 }
e834022c
RBR
62
63 override func resignMain() {
64 super.resignMain()
a4e80427
RBR
65 if (self.contentView as? RecordingContentView)?.state != .recording {
66 self.ignoresMouseEvents = false
67 }
e834022c
RBR
68 }
69
70 override func becomeMain() {
71 super.becomeMain()
a4e80427
RBR
72 if (self.contentView as? RecordingContentView)?.state != .recording {
73 (self.contentView as? RecordingContentView)?.state = .idle
74 }
e834022c
RBR
75 }
76}
77
78enum RecordingWindowState {
a4e80427 79 case passthrough, idle, drawing, moving, resizing, recording;
24220348
RBR
80}
81
82class RecordingContentView: NSView {
83
a4e80427
RBR
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()
24220348 90
a4e80427 91 private var resizeBox: NSRect? {
24220348
RBR
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
a4e80427
RBR
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
24220348
RBR
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
a4e80427
RBR
146 if shouldPassthrough {
147 NSCursor.arrow.set()
148 } else {
149 if let box {
150 if resizeBox!.contains(mouseLocation) {
151 NSCursor.arrow.set()
24220348 152 } else {
a4e80427
RBR
153 if box.contains(mouseLocation) {
154 NSCursor.openHand.set()
155 } else {
156 NSCursor.crosshair.set()
157 }
24220348 158 }
a4e80427
RBR
159 if let button {
160 if button.contains(mouseLocation) {
161 NSCursor.arrow.set()
162 }
163 }
164 } else {
165 NSCursor.crosshair.set()
24220348 166 }
24220348 167 }
24220348
RBR
168
169 self.setNeedsDisplay(self.bounds)
170 }
171
172 override func mouseDragged(with event: NSEvent) {
173 self.mouseLocation = self.convert(event.locationInWindow, from: nil)
e834022c 174 if state == .drawing {
24220348
RBR
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
e834022c
RBR
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 }
24220348
RBR
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? {
a4e80427 208 return shouldPassthrough ? nil : self
24220348
RBR
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 {
a4e80427
RBR
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
e834022c
RBR
226 if resizeBox!.contains(origin) {
227 self.origin = NSPoint(x: box.minX, y: box.maxY)
228 state = .resizing
229 return
230 }
24220348 231 if box.contains(origin) {
e834022c 232 state = .moving
24220348
RBR
233 self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y)
234 return
235 }
236 }
237
e834022c 238 state = .drawing
24220348
RBR
239 }
240
241 override func mouseUp(with event: NSEvent) {
a4e80427
RBR
242 if state != .recording {
243 state = .idle
244 }
e834022c
RBR
245 }
246
247 override func keyDown(with event: NSEvent) {
e834022c
RBR
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) {
a4e80427 257 if state == .idle {
e834022c 258 if event.modifierFlags.contains(.shift) {
a4e80427 259 startPassthrough()
e834022c 260 } else {
a4e80427 261 stopPassthrough()
e834022c 262 }
a4e80427 263 }
24220348
RBR
264 }
265
266 override func draw(_ dirtyRect: NSRect) {
a4e80427 267 if shouldPassthrough {
e834022c
RBR
268 NSColor.clear.setFill()
269 } else {
270 NSColor(white: 1.0, alpha: 0.001).setFill()
271 }
24220348
RBR
272 dirtyRect.fill()
273
274 let dashLength: CGFloat = 5.0
275 let lineWidth = 0.5
276
e834022c 277 if state == .idle && box == nil {
24220348
RBR
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
a4e80427
RBR
332 if state == .recording {
333 return
334 }
335
24220348
RBR
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
e834022c
RBR
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 {
24220348
RBR
361 return;
362 }
363 }
364
365 // Draw text
e834022c
RBR
366
367 let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString
368 drawText(string, mouseLocation)
369 }
370
a4e80427
RBR
371 // MARK: - Utilities
372
e834022c 373 private func drawText(_ text: NSString, _ location: NSPoint, _ isBottomRight: Bool = false) {
24220348
RBR
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 ]
e834022c
RBR
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 }
24220348
RBR
389
390 NSColor.black.set()
391 rect.fill()
392
e834022c 393 text.draw(in: textRect, withAttributes: textAttributes)
24220348
RBR
394 }
395}