Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 67 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,73 @@
# Agent guide for Swift and SwiftUI
# AGENT.md

This repository contains an Xcode project written with Swift and SwiftUI. Please follow the guidelines below so that the development experience is built on modern, safe API usage.
Behavioral guidelines to reduce common LLM coding mistakes. Merge with project-specific instructions as needed.

## Role
**Tradeoff:** These guidelines bias toward caution over speed. For trivial tasks, use judgment.

You are a **Senior iOS Engineer**, specializing in SwiftUI, SwiftData, and related frameworks. Your code must always adhere to Apple's Human Interface Guidelines and App Review guidelines.
## 1. Think Before Coding

**Don't assume. Don't hide confusion. Surface tradeoffs.**

Before implementing:

- State your assumptions explicitly. If uncertain, ask.
- If multiple interpretations exist, present them - don't pick silently.
- If a simpler approach exists, say so. Push back when warranted.
- If something is unclear, stop. Name what's confusing. Ask.

## 2. Simplicity First

**Minimum code that solves the problem. Nothing speculative.**

- No features beyond what was asked.
- No abstractions for single-use code.
- No "flexibility" or "configurability" that wasn't requested.
- No error handling for impossible scenarios.
- If you write 200 lines and it could be 50, rewrite it.

Ask yourself: "Would a senior engineer say this is overcomplicated?" If yes, simplify.

## 3. Surgical Changes

**Touch only what you must. Clean up only your own mess.**

When editing existing code:

- Don't "improve" adjacent code, comments, or formatting.
- Don't refactor things that aren't broken.
- Match existing style, even if you'd do it differently.
- If you notice unrelated dead code, mention it - don't delete it.

When your changes create orphans:

- Remove imports/variables/functions that YOUR changes made unused.
- Don't remove pre-existing dead code unless asked.

The test: Every changed line should trace directly to the user's request.

## 4. Goal-Driven Execution

**Define success criteria. Loop until verified.**

Transform tasks into verifiable goals:

- "Add validation" → "Write tests for invalid inputs, then make them pass"
- "Fix the bug" → "Write a test that reproduces it, then make it pass"
- "Refactor X" → "Ensure tests pass before and after"

For multi-step tasks, state a brief plan:

```
1. [Step] → verify: [check]
2. [Step] → verify: [check]
3. [Step] → verify: [check]
```

Strong success criteria let you loop independently. Weak criteria ("make it work") require constant clarification.

---

**These guidelines are working if:** fewer unnecessary changes in diffs, fewer rewrites due to overcomplication, and clarifying questions come before implementation rather than after mistakes.

## Core instructions

Expand Down
17 changes: 14 additions & 3 deletions BetterCapture/Service/CaptureEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ final class CaptureEngine: NSObject {
/// - Parameters:
/// - settings: The settings store containing capture configuration
/// - videoSize: The dimensions for the captured video
func startCapture(with settings: SettingsStore, videoSize: CGSize) async throws {
/// - sourceRect: Optional rectangle for area selection (display points, top-left origin)
func startCapture(with settings: SettingsStore, videoSize: CGSize, sourceRect: CGRect? = nil) async throws {
guard let filter = contentFilter else {
throw CaptureError.noContentFilterSelected
}
Expand Down Expand Up @@ -128,7 +129,7 @@ final class CaptureEngine: NSObject {
let filteredContent = try await contentFilterService.applySettings(to: filter, settings: settings)
logger.info("Content filter applied, creating stream...")

let streamConfig = createStreamConfiguration(from: settings, contentSize: videoSize)
let streamConfig = createStreamConfiguration(from: settings, contentSize: videoSize, sourceRect: sourceRect)

stream = SCStream(filter: filteredContent, configuration: streamConfig, delegate: self)

Expand Down Expand Up @@ -182,13 +183,23 @@ final class CaptureEngine: NSObject {
// MARK: - Configuration

/// Creates an SCStreamConfiguration from user settings
private func createStreamConfiguration(from settings: SettingsStore, contentSize: CGSize) -> SCStreamConfiguration {
/// - Parameters:
/// - settings: The settings store containing capture configuration
/// - contentSize: The output dimensions for the captured video
/// - sourceRect: Optional rectangle for area selection (display points, top-left origin)
private func createStreamConfiguration(from settings: SettingsStore, contentSize: CGSize, sourceRect: CGRect? = nil) -> SCStreamConfiguration {
let config = SCStreamConfiguration()

// Set output dimensions - required for proper capture
config.width = Int(contentSize.width)
config.height = Int(contentSize.height)

// Set source rect for area selection (only works with display captures)
if let sourceRect {
config.sourceRect = sourceRect
logger.info("Source rect set: \(sourceRect.origin.x),\(sourceRect.origin.y) \(sourceRect.width)x\(sourceRect.height)")
}

// Frame rate - native uses display sync (1/120 timescale)
if settings.frameRate == .native {
config.minimumFrameInterval = CMTime(value: 1, timescale: 120)
Expand Down
46 changes: 20 additions & 26 deletions BetterCapture/Service/PreviewService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final class PreviewService: NSObject {

private var stream: SCStream?
private var currentFilter: SCContentFilter?
private var currentSourceRect: CGRect?
private let previewQueue = DispatchQueue(label: "com.bettercapture.previewQueue", qos: .userInteractive)

private let logger = Logger(
Expand All @@ -43,20 +44,30 @@ final class PreviewService: NSObject {
// MARK: - Public Methods

/// Updates the content filter and captures a static thumbnail
/// - Parameter filter: The content filter to use
func setContentFilter(_ filter: SCContentFilter) async {
/// - Parameters:
/// - filter: The content filter to use
/// - sourceRect: Optional rectangle for area selection (display points, top-left origin)
func setContentFilter(_ filter: SCContentFilter, sourceRect: CGRect? = nil) async {
currentFilter = filter
await captureStaticThumbnail(for: filter)
currentSourceRect = sourceRect
await captureStaticThumbnail(for: filter, sourceRect: sourceRect)
}

/// Captures a single static frame as a thumbnail (no continuous streaming)
private func captureStaticThumbnail(for filter: SCContentFilter) async {
/// - Parameters:
/// - filter: The content filter to capture
/// - sourceRect: Optional rectangle for area selection (display points, top-left origin)
private func captureStaticThumbnail(for filter: SCContentFilter, sourceRect: CGRect? = nil) async {
let config = SCStreamConfiguration()
config.width = previewWidth
config.height = previewHeight
config.pixelFormat = kCVPixelFormatType_32BGRA
config.showsCursor = true

if let sourceRect {
config.sourceRect = sourceRect
}

do {
let image = try await SCScreenshotManager.captureImage(
contentFilter: filter,
Expand Down Expand Up @@ -98,28 +109,6 @@ final class PreviewService: NSObject {
await stopStream()
}

/// Starts or updates the preview stream for the given content filter
/// - Parameter filter: The content filter to capture
func captureSnapshot(for filter: SCContentFilter) async {
currentFilter = filter

// If already streaming, update the filter
if let stream, isCapturing {
do {
try await stream.updateContentFilter(filter)
logger.info("Updated preview stream filter")
} catch {
logger.error("Failed to update preview filter: \(error.localizedDescription)")
await stopStream()
await startStream(with: filter)
}
return
}

// Otherwise start a new stream
await startStream(with: filter)
}

/// Starts the preview stream
private func startStream(with filter: SCContentFilter) async {
guard !isCapturing else { return }
Expand Down Expand Up @@ -198,6 +187,11 @@ final class PreviewService: NSObject {
// Show cursor in preview
config.showsCursor = true

// Apply source rect for area selection
if let sourceRect = currentSourceRect {
config.sourceRect = sourceRect
}

return config
}

Expand Down
Loading
Loading