diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-07-27 22:32:10 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-07-27 22:32:10 +0200 |
| commit | a4e804275517af683afa1733e2db7c383c306f2b (patch) | |
| tree | 643eff7b3648aad15ce569eec0e1d7c9abddc00d /Captura/Presentation/Windows/RecordingWindow.swift | |
| parent | e834022c9b363804d36045892a305204f2019216 (diff) | |
Use framerate, control stop
Diffstat (limited to 'Captura/Presentation/Windows/RecordingWindow.swift')
| -rw-r--r-- | Captura/Presentation/Windows/RecordingWindow.swift | 391 |
1 files changed, 391 insertions, 0 deletions
diff --git a/Captura/Presentation/Windows/RecordingWindow.swift b/Captura/Presentation/Windows/RecordingWindow.swift new file mode 100644 index 0000000..2bb9928 --- /dev/null +++ b/Captura/Presentation/Windows/RecordingWindow.swift @@ -0,0 +1,391 @@ +import Cocoa +import Combine + +class RecordingWindow: NSWindow { + + var pixelDensity: CGFloat { + self.screen?.backingScaleFactor ?? 1.0 + } + + init(_ button: NSRect?) { + + let screens = NSScreen.screens + var boundingBox = NSZeroRect + for screen in screens { + boundingBox = NSUnionRect(boundingBox, screen.frame) + } + + super.init( + contentRect: boundingBox, + styleMask: [.borderless], + backing: .buffered, + defer: false) + + self.isReleasedWhenClosed = false + self.collectionBehavior = [.canJoinAllSpaces] + self.center() + self.isMovableByWindowBackground = false + self.isMovable = false + self.titlebarAppearsTransparent = true + self.setFrame(boundingBox, display: true) + self.titleVisibility = .hidden + let recordingView = RecordingContentView() + recordingView.frame = boundingBox + recordingView.button = button + self.contentView = recordingView + self.backgroundColor = NSColor(white: 1.0, alpha: 0.001) + self.level = .screenSaver + self.isOpaque = false + self.hasShadow = false + self.makeKeyAndOrderFront(nil) + } + + // MARK: - Window Behavior Overrides + + override func resetCursorRects() { + super.resetCursorRects() + let cursor = NSCursor.crosshair + self.contentView?.addCursorRect(self.contentView!.bounds, cursor: cursor) + } + + override var canBecomeKey: Bool { + return true + } + + override var canBecomeMain: Bool { + return true + } + + override func resignMain() { + super.resignMain() + if (self.contentView as? RecordingContentView)?.state != .recording { + self.ignoresMouseEvents = false + } + } + + override func becomeMain() { + super.becomeMain() + if (self.contentView as? RecordingContentView)?.state != .recording { + (self.contentView as? RecordingContentView)?.state = .idle + } + } +} + +enum RecordingWindowState { + case passthrough, idle, drawing, moving, resizing, recording; +} + +class RecordingContentView: NSView { + + public var button: NSRect? = nil + @Published public var box: NSRect? = nil + public var state: RecordingWindowState = .idle + private var mouseLocation: NSPoint = NSPoint() + private var origin: NSPoint = NSPoint() + private var boxOrigin: NSPoint = NSPoint() + + private var resizeBox: NSRect? { + if let box { + return NSRect(x: box.maxX - 5, y: box.minY - 5, width: 10, height: 10) + } + return nil + } + + private var shouldPassthrough: Bool { + state == .recording || state == .passthrough + } + + // MARK: - State changing API + + public func startRecording() { + state = .recording + window?.ignoresMouseEvents = true + } + + public func stopRecording() { + + } + + public func reset() { + state = .idle + window?.ignoresMouseEvents = false + } + + public func startPassthrough() { + state = .passthrough + window?.ignoresMouseEvents = true + } + + public func stopPassthrough() { + state = .idle + window?.ignoresMouseEvents = false + } + + // MARK: - View Behavior Overrides + + override func updateTrackingAreas() { + super.updateTrackingAreas() + + for trackingArea in self.trackingAreas { + self.removeTrackingArea(trackingArea) + } + + let options: NSTrackingArea.Options = [.mouseEnteredAndExited, .activeInKeyWindow, .cursorUpdate, .mouseMoved] + let trackingArea = NSTrackingArea(rect: self.bounds, options: options, owner: self, userInfo: nil) + self.addTrackingArea(trackingArea) + } + + override func mouseMoved(with event: NSEvent) { + + self.mouseLocation = self.convert(event.locationInWindow, from: nil) + + if shouldPassthrough { + NSCursor.arrow.set() + } else { + if let box { + if resizeBox!.contains(mouseLocation) { + NSCursor.arrow.set() + } else { + if box.contains(mouseLocation) { + NSCursor.openHand.set() + } else { + NSCursor.crosshair.set() + } + } + if let button { + if button.contains(mouseLocation) { + NSCursor.arrow.set() + } + } + } else { + NSCursor.crosshair.set() + } + } + + self.setNeedsDisplay(self.bounds) + } + + override func mouseDragged(with event: NSEvent) { + self.mouseLocation = self.convert(event.locationInWindow, from: nil) + if state == .drawing { + box = NSRect( + x: round(min(origin.x, mouseLocation.x)), + y: round(min(origin.y, mouseLocation.y)), + width: round(abs(mouseLocation.x - origin.x)), + height: round(abs(mouseLocation.y - origin.y)) + ) + } + + if box != nil { + if state == .moving { + NSCursor.closedHand.set() + box!.origin = NSPoint( + x: self.boxOrigin.x - self.origin.x + self.mouseLocation.x, + y: self.boxOrigin.y - self.origin.y + self.mouseLocation.y) + } + + if state == .resizing { + box = NSRect( + x: round(min(origin.x, mouseLocation.x)), + y: round(min(origin.y, mouseLocation.y)), + width: round(abs(mouseLocation.x - origin.x)), + height: round(abs(mouseLocation.y - origin.y)) + ) + } + } + self.setNeedsDisplay(self.bounds) + } + + override func cursorUpdate(with event: NSEvent) { + NSCursor.crosshair.set() + } + + override func hitTest(_ point: NSPoint) -> NSView? { + return shouldPassthrough ? nil : self + } + + override var acceptsFirstResponder: Bool { + return true + } + + override func mouseDown(with event: NSEvent) { + self.origin = self.convert(event.locationInWindow, from: nil) + if let box { + + if let button { + if button.contains(origin) { + NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil) + return + } + } + + if resizeBox!.contains(origin) { + self.origin = NSPoint(x: box.minX, y: box.maxY) + state = .resizing + return + } + if box.contains(origin) { + state = .moving + self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y) + return + } + } + + state = .drawing + } + + override func mouseUp(with event: NSEvent) { + if state != .recording { + state = .idle + } + } + + override func keyDown(with event: NSEvent) { + switch event.keyCode { + case 53: // Escape key + NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil) + default: + super.keyDown(with: event) + } + } + + override func flagsChanged(with event: NSEvent) { + if state == .idle { + if event.modifierFlags.contains(.shift) { + startPassthrough() + } else { + stopPassthrough() + } + } + } + + override func draw(_ dirtyRect: NSRect) { + if shouldPassthrough { + NSColor.clear.setFill() + } else { + NSColor(white: 1.0, alpha: 0.001).setFill() + } + dirtyRect.fill() + + let dashLength: CGFloat = 5.0 + let lineWidth = 0.5 + + if state == .idle && box == nil { + let blackLine = NSBezierPath() + blackLine.lineWidth = lineWidth + blackLine.setLineDash([dashLength, dashLength], count: 2, phase: 0) + + // Vertical line (Black) + blackLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds))) + blackLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds))) + + // Horizontal line (Black) + blackLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y)) + blackLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y)) + + NSColor.black.setStroke() + blackLine.stroke() + + let whiteLine = NSBezierPath() + whiteLine.lineWidth = lineWidth + whiteLine.setLineDash([dashLength, dashLength], count: 2, phase: dashLength) + + // Vertical line (White) + whiteLine.move(to: NSPoint(x: self.mouseLocation.x, y: NSMinY(self.bounds))) + whiteLine.line(to: NSPoint(x: self.mouseLocation.x, y: NSMaxY(self.bounds))) + + // Horizontal line (White) + whiteLine.move(to: NSPoint(x: NSMinX(self.bounds), y: self.mouseLocation.y)) + whiteLine.line(to: NSPoint(x: NSMaxX(self.bounds), y: self.mouseLocation.y)) + + NSColor.white.setStroke() + whiteLine.stroke() + } + + if let box { + let blackBox = NSBezierPath() + blackBox.lineWidth = lineWidth + blackBox.setLineDash([dashLength, dashLength], count: 2, phase: 0) + blackBox.move(to: NSPoint(x: box.minX, y: box.minY)) + blackBox.line(to: NSPoint(x: box.maxX, y: box.minY)) + blackBox.line(to: NSPoint(x: box.maxX, y: box.maxY)) + blackBox.line(to: NSPoint(x: box.minX, y: box.maxY)) + blackBox.line(to: NSPoint(x: box.minX, y: box.minY)) + NSColor.black.setStroke() + blackBox.stroke() + + let whiteBox = NSBezierPath() + whiteBox.lineWidth = lineWidth + whiteBox.setLineDash([dashLength, dashLength], count: 2, phase: dashLength) + whiteBox.move(to: NSPoint(x: box.minX, y: box.minY)) + whiteBox.line(to: NSPoint(x: box.maxX, y: box.minY)) + whiteBox.line(to: NSPoint(x: box.maxX, y: box.maxY)) + whiteBox.line(to: NSPoint(x: box.minX, y: box.maxY)) + whiteBox.line(to: NSPoint(x: box.minX, y: box.minY)) + NSColor.white.setStroke() + whiteBox.stroke() + + if state == .recording { + return + } + + if let resizeBox { + let clearBox = NSBezierPath() + clearBox.move(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY)) + clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.minY)) + clearBox.line(to: NSPoint(x: resizeBox.maxX, y: resizeBox.maxY)) + clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.maxY)) + clearBox.line(to: NSPoint(x: resizeBox.minX, y: resizeBox.minY)) + NSColor(white: 0, alpha: 0.2).setFill() + clearBox.fill() + } + + if state == .moving { + let string = "\(Int(box.minX)), \(Int(box.maxY))" as NSString + drawText(string, NSPoint( + x: box.minX, + y: box.maxY + ), true) + } + + if state == .resizing { + let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString + drawText(string, mouseLocation) + } + + if box.contains(mouseLocation) && state != .resizing { + return; + } + } + + // Draw text + + let string = "\(Int(mouseLocation.x)), \(Int(mouseLocation.y))" as NSString + drawText(string, mouseLocation) + } + + // MARK: - Utilities + + private func drawText(_ text: NSString, _ location: NSPoint, _ isBottomRight: Bool = false) { + + let textAttributes = [ + NSAttributedString.Key.font: NSFont(name: "Hiragino Mincho ProN", size: 12) ?? NSFont.systemFont(ofSize: 12), + NSAttributedString.Key.foregroundColor: NSColor.white, + ] + let offset = NSPoint(x: 10, y: 10) + let padding = NSPoint(x: 5, y: 2) + let size = text.size(withAttributes: textAttributes) + 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) + var textRect = NSRect(x: location.x + offset.x + padding.x, y: location.y + offset.y + padding.y, width: size.width, height: size.height) + + if (isBottomRight) { + rect = rect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0) + textRect = textRect.offsetBy(dx: -size.width - 2 * offset.x, dy: 0) + } + + NSColor.black.set() + rect.fill() + + text.draw(in: textRect, withAttributes: textAttributes) + } +} |