Open source CapCut alternative.
TypeScript
feat(app): resizable screen-recording region, dimmed while recording [rebuild]
David Le319de876d807
diff --git a/apps/Donkey/Sources/Donkey/RegionSelectionOverlayController.swift b/apps/Donkey/Sources/Donkey/RegionSelectionOverlayController.swiftindex d3f0b4e5..17941789 100644--- a/apps/Donkey/Sources/Donkey/RegionSelectionOverlayController.swift+++ b/apps/Donkey/Sources/Donkey/RegionSelectionOverlayController.swift@@ -10,8 +10,9 @@ extension NSScreen {}/// The drag-to-select region picker. Puts a dimmed, key-capable panel over every display; the user-/// drags a rectangle on one of them and the controller reports it in display-local, top-left points —-/// the coordinate space `SCStreamConfiguration.sourceRect` expects. Escape cancels. v1 keeps a+/// drags a rectangle on one of them, then resizes or repositions it in place before recording. The+/// controller reports the current rect in display-local, top-left points — the coordinate space+/// `SCStreamConfiguration.sourceRect` expects — after every edit. Escape cancels. v1 keeps a/// selection within the single display it started on.@MainActorfinal class RegionSelectionOverlayController {@@ -76,6 +77,75 @@ final class RegionSelectionOverlayController {}}+/// The dim that stays up while a region is recording. It darkens the display and cuts the recorded rect+/// back to full brightness so the user can see exactly what is being captured. Mouse events pass through+/// (the user keeps working), and the panel is handed to the recorder as an excluded window via its+/// `windowNumber`, so the dim itself never lands in the recording. Region mode only.+@MainActor+final class RecordingRegionDimOverlayController {+ private var panel: NSPanel?++ /// The overlay window's number, for `SCStreamConfiguration` capture exclusion. `nil` while hidden.+ var windowNumber: Int? { panel?.windowNumber }++ /// Show the dim over `displayID`, leaving `region` (display-local, top-left points) clear.+ func show(region: CGRect, displayID: CGDirectDisplayID) {+ close()+ guard let screen = NSScreen.screens.first(where: { $0.donkeyDisplayID == displayID }) else { return }+ let view = RecordingRegionDimView()+ // Flip the top-left region back into the view's bottom-left space.+ view.hole = CGRect(+ x: region.minX,+ y: screen.frame.height - region.maxY,+ width: region.width,+ height: region.height+ )++ let panel = NSPanel(+ contentRect: screen.frame,+ styleMask: [.borderless, .nonactivatingPanel],+ backing: .buffered,+ defer: false+ )+ panel.contentView = view+ panel.backgroundColor = .clear+ panel.isOpaque = false+ panel.hasShadow = false+ panel.hidesOnDeactivate = false+ panel.isReleasedWhenClosed = false+ panel.ignoresMouseEvents = true+ panel.level = DonkeyOverlayWindowLevel.regionSelection+ panel.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary, .ignoresCycle, .stationary]+ panel.orderFrontRegardless()+ self.panel = panel+ }++ func close() {+ panel?.close()+ panel = nil+ }+}++/// Dims its display and cuts the recorded region clear, with a thin frame so the boundary reads.+private final class RecordingRegionDimView: NSView {+ var hole: CGRect = .zero { didSet { needsDisplay = true } }++ override var isFlipped: Bool { false }++ override func draw(_ dirtyRect: NSRect) {+ NSColor.black.withAlphaComponent(0.35).setFill()+ bounds.fill()++ guard hole.width > 0, hole.height > 0 else { return }+ NSColor.clear.set()+ hole.fill(using: .copy)+ NSColor.white.withAlphaComponent(0.5).setStroke()+ let framePath = NSBezierPath(rect: hole.insetBy(dx: 0.5, dy: 0.5))+ framePath.lineWidth = 1+ framePath.stroke()+ }+}+/// A borderless panel that can become key (so it receives `keyDown`) and routes Escape to a handler,/// mirroring the `UserQueryPanel` pattern used elsewhere.private final class RegionSelectionPanel: NSPanel {@@ -93,45 +163,89 @@ private final class RegionSelectionPanel: NSPanel {}}-/// Dims its display and tracks a single drag into a selection rectangle, painting the clear cut-out,-/// a 1px frame, and a live pixel readout. Reports the rect (view coords, bottom-left) on mouse-up.+/// Dims its display and tracks the selection rectangle: the first drag rubber-bands a new box, then the+/// box stays live so it can be resized from any of eight grips or dragged bodily. Paints the clear+/// cut-out, a dashed marquee, corner/edge handles, and a live pixel readout. Reports the current rect+/// (view coords, bottom-left) after every edit so the controller's Record button always reflects it.private final class RegionSelectionView: NSView {var onComplete: ((CGRect) -> Void)?var onCancel: (() -> Void)?- private var anchor: CGPoint?+ /// The eight resize grips: four corners and four edge midpoints.+ private enum Grip: CaseIterable {+ case topLeft, top, topRight, right, bottomRight, bottom, bottomLeft, left+ }++ /// What the current mouse-down is doing.+ private enum DragKind {+ case none+ case creating(anchor: CGPoint)+ case moving(original: CGRect, start: CGPoint)+ case resizing(grip: Grip, original: CGRect)+ }+private var selection: CGRect?+ private var drag: DragKind = .none++ private let minSize: CGFloat = 16+ private let handleSize: CGFloat = 9 // drawn edge length of a grip+ private let handleHitInset: CGFloat = 11 // half the square hit-target around a gripoverride var acceptsFirstResponder: Bool { true }override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }override var isFlipped: Bool { false }+ // MARK: - Mouse+override func mouseDown(with event: NSEvent) {- anchor = convert(event.locationInWindow, from: nil)+ let point = convert(event.locationInWindow, from: nil)+ if let selection {+ if let grip = grip(at: point, in: selection) {+ drag = .resizing(grip: grip, original: selection)+ return+ }+ if selection.contains(point) {+ drag = .moving(original: selection, start: point)+ return+ }+ }+ // Empty space: start a fresh box.+ drag = .creating(anchor: point)selection = nilneedsDisplay = true}override func mouseDragged(with event: NSEvent) {- guard let anchor else { return }- let current = convert(event.locationInWindow, from: nil)- selection = CGRect(- x: min(anchor.x, current.x),- y: min(anchor.y, current.y),- width: abs(current.x - anchor.x),- height: abs(current.y - anchor.y)- ).intersection(bounds)+ let point = convert(event.locationInWindow, from: nil)+ switch drag {+ case .none:+ return+ case .creating(let anchor):+ selection = CGRect(+ x: min(anchor.x, point.x),+ y: min(anchor.y, point.y),+ width: abs(point.x - anchor.x),+ height: abs(point.y - anchor.y)+ ).intersection(bounds)+ case .moving(let original, let start):+ selection = moved(original, by: CGPoint(x: point.x - start.x, y: point.y - start.y))+ case .resizing(let grip, let original):+ selection = resized(original, grip: grip, to: point)+ }needsDisplay = true}override func mouseUp(with event: NSEvent) {- defer { anchor = nil }- guard let selection, selection.width >= 16, selection.height >= 16 else {+ defer { drag = .none }+ guard let selection, selection.width >= minSize, selection.height >= minSize else {self.selection = nilneedsDisplay = true+ window?.invalidateCursorRects(for: self)return}onComplete?(selection)+ needsDisplay = true+ window?.invalidateCursorRects(for: self)}override func keyDown(with event: NSEvent) {@@ -142,23 +256,120 @@ private final class RegionSelectionView: NSView {}}+ // MARK: - Geometry++ private func gripCenters(in rect: CGRect) -> [Grip: CGPoint] {+ [+ .topLeft: CGPoint(x: rect.minX, y: rect.maxY),+ .top: CGPoint(x: rect.midX, y: rect.maxY),+ .topRight: CGPoint(x: rect.maxX, y: rect.maxY),+ .right: CGPoint(x: rect.maxX, y: rect.midY),+ .bottomRight: CGPoint(x: rect.maxX, y: rect.minY),+ .bottom: CGPoint(x: rect.midX, y: rect.minY),+ .bottomLeft: CGPoint(x: rect.minX, y: rect.minY),+ .left: CGPoint(x: rect.minX, y: rect.midY)+ ]+ }++ private func grip(at point: CGPoint, in rect: CGRect) -> Grip? {+ for (grip, center) in gripCenters(in: rect) {+ let hit = CGRect(+ x: center.x - handleHitInset,+ y: center.y - handleHitInset,+ width: handleHitInset * 2,+ height: handleHitInset * 2+ )+ if hit.contains(point) { return grip }+ }+ return nil+ }++ /// Translate the box, clamped so it stays fully on the display.+ private func moved(_ rect: CGRect, by delta: CGPoint) -> CGRect {+ var origin = CGPoint(x: rect.minX + delta.x, y: rect.minY + delta.y)+ origin.x = max(0, min(origin.x, bounds.width - rect.width))+ origin.y = max(0, min(origin.y, bounds.height - rect.height))+ return CGRect(origin: origin, size: rect.size)+ }++ /// Move the grip's edge(s) to the mouse, clamped to the display and never past the minimum size.+ private func resized(_ rect: CGRect, grip: Grip, to point: CGPoint) -> CGRect {+ var minX = rect.minX, maxX = rect.maxX+ var minY = rect.minY, maxY = rect.maxY+ let movesLeft = grip == .topLeft || grip == .left || grip == .bottomLeft+ let movesRight = grip == .topRight || grip == .right || grip == .bottomRight+ let movesTop = grip == .topLeft || grip == .top || grip == .topRight+ let movesBottom = grip == .bottomLeft || grip == .bottom || grip == .bottomRight+ if movesLeft { minX = max(0, min(point.x, maxX - minSize)) }+ if movesRight { maxX = min(bounds.width, max(point.x, minX + minSize)) }+ if movesBottom { minY = max(0, min(point.y, maxY - minSize)) }+ if movesTop { maxY = min(bounds.height, max(point.y, minY + minSize)) }+ return CGRect(x: minX, y: minY, width: maxX - minX, height: maxY - minY)+ }++ // MARK: - Cursors++ override func resetCursorRects() {+ addCursorRect(bounds, cursor: .crosshair) // empty space draws a new box+ guard let selection else { return }+ addCursorRect(selection, cursor: .openHand) // body moves the box+ for (grip, center) in gripCenters(in: selection) {+ let rect = CGRect(+ x: center.x - handleHitInset,+ y: center.y - handleHitInset,+ width: handleHitInset * 2,+ height: handleHitInset * 2+ )+ addCursorRect(rect, cursor: cursor(for: grip))+ }+ }++ private func cursor(for grip: Grip) -> NSCursor {+ switch grip {+ case .left, .right: return .resizeLeftRight+ case .top, .bottom: return .resizeUpDown+ case .topLeft, .topRight, .bottomLeft, .bottomRight: return .crosshair+ }+ }++ // MARK: - Draw+override func draw(_ dirtyRect: NSRect) {NSColor.black.withAlphaComponent(0.35).setFill()bounds.fill()guard let selection, selection.width > 0, selection.height > 0 else { return }- // Punch the selection back out of the dim, then frame it.+ // Punch the selection back out of the dim, then frame it with a dashed marquee.NSColor.clear.set()selection.fill(using: .copy)- NSColor.white.withAlphaComponent(0.9).setStroke()let framePath = NSBezierPath(rect: selection.insetBy(dx: 0.5, dy: 0.5))framePath.lineWidth = 1+ framePath.setLineDash([6, 4], count: 2, phase: 0)+ NSColor.white.withAlphaComponent(0.95).setStroke()framePath.stroke()+ drawHandles(for: selection)drawReadout(for: selection)}+ private func drawHandles(for rect: CGRect) {+ for center in gripCenters(in: rect).values {+ let box = CGRect(+ x: center.x - handleSize / 2,+ y: center.y - handleSize / 2,+ width: handleSize,+ height: handleSize+ )+ let path = NSBezierPath(roundedRect: box, xRadius: 2, yRadius: 2)+ NSColor.white.setFill()+ path.fill()+ NSColor.black.withAlphaComponent(0.55).setStroke()+ path.lineWidth = 1+ path.stroke()+ }+ }+private func drawReadout(for rect: CGRect) {let scale = window?.backingScaleFactor ?? 2let widthPx = Int((rect.width * scale).rounded())diff --git a/apps/Donkey/Sources/Donkey/ScreenRecordingController.swift b/apps/Donkey/Sources/Donkey/ScreenRecordingController.swiftindex f77f9183..c82b540d 100644--- a/apps/Donkey/Sources/Donkey/ScreenRecordingController.swift+++ b/apps/Donkey/Sources/Donkey/ScreenRecordingController.swift@@ -27,6 +27,7 @@ final class ScreenRecordingController {private lazy var controlBar = RecordingControlBarController(model: model)private let regionOverlay = RegionSelectionOverlayController()private let windowPicker = WindowPickerOverlayController()+ private let recordingDim = RecordingRegionDimOverlayController()private var recorder: (any ScreenRecording)?private var phase: Phase = .idle@@ -105,6 +106,7 @@ final class ScreenRecordingController {private func cancel() {teardownOverlays()+ recordingDim.close()controlBar.close()phase = .idleselectedRegion = nil@@ -184,18 +186,27 @@ final class ScreenRecordingController {}}+ // Swap the interactive picker for a non-interactive dim that keeps the un-recorded area darkened+ // while recording. Both the dim and the control bar are excluded from the stream, so the+ // recording captures the region's real content and neither overlay shows up in the file.+ teardownOverlays()+ if case .display(let displayID, let region) = target, let region {+ recordingDim.show(region: region, displayID: displayID)+ }++ var excludedWindowIDs: [CGWindowID] = []+ if let controlBarWindow = controlBar.windowNumber { excludedWindowIDs.append(CGWindowID(controlBarWindow)) }+ if let dimWindow = recordingDim.windowNumber { excludedWindowIDs.append(CGWindowID(dimWindow)) }+let configuration = ScreenRecordingConfiguration(target: target,capturesSystemAudio: audio.capturesSystemAudio,capturesMicrophone: audio.capturesMicrophone,microphoneDeviceID: audio.microphoneDeviceID,- excludedWindowIDs: controlBar.windowNumber.map { [CGWindowID($0)] } ?? [],+ excludedWindowIDs: excludedWindowIDs,outputURL: ScreenRecordingDestination.makeOutputURL())- // The pickers are gone once recording starts; only the (excluded) control bar remains.- teardownOverlays()-let recorder = SCRecordingScreenRecorder()recorder.onUnexpectedStop = { [weak self] _ in self?.handleUnexpectedStop() }self.recorder = recorder@@ -209,10 +220,12 @@ final class ScreenRecordingController {startTimer()} catch ScreenRecordingError.screenRecordingPermissionDenied {self.recorder = nil+ recordingDim.close()model.statusMessage = "Allow Screen Recording in System Settings, then try again."openScreenRecordingSettings()} catch {self.recorder = nil+ recordingDim.close()model.statusMessage = "Couldn't start recording."}}@@ -237,6 +250,7 @@ final class ScreenRecordingController {private func handleUnexpectedStop() {stopTimer()recorder = nil+ recordingDim.close()phase = .idlecontrolBar.close()}@@ -244,6 +258,7 @@ final class ScreenRecordingController {private func finishAfterRecording() {recorder = nilrecordingStart = nil+ recordingDim.close()phase = .idlemodel.isRecording = falsemodel.isBusy = false