Skip to content

ios: continuous autofocus for the QR scanner and the capture camera - #349

Merged
GoKubar merged 1 commit into
mainfrom
agent/ipad/camera-autofocus
Sep 14, 2026
Merged

GoKubar merged 1 commit into
mainfrom
agent/ipad/camera-autofocus

Conversation

@GoKubar

@GoKubar GoKubar commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Owner feedback item 4:

iPad doesnt autofocus for the camera (ex. in the QR code), is this fixable.

Yes — with one honest caveat, below.

Cause

Neither camera path ever configured focus, because neither owns its capture session.

  • PairingScanner.swift hands off to VisionKit's DataScannerViewController (the QR path).
  • CaptureView.swift's CameraPicker hands off to UIImagePickerController with .camera.

Both keep their AVCaptureSession and AVCaptureDeviceInput private, and neither exposes any focus API. There was no AVFoundation import anywhere in apps/ios before this change — I checked every capture session in the app, not just the scanner; those two are all of them.

The lever that remains is the AVCaptureDevice itself, which is a process-wide object: focus settings applied to it while a session is running do take effect on that session.

Change — CameraFocus.swift

  • apply(nearRange:) locks the back wide-angle device and sets focusMode = .continuousAutoFocus, .continuousAutoExposure, and isSubjectAreaChangeMonitoringEnabled = true.
  • nearRange additionally sets autoFocusRangeRestriction = .near. This is the one that matters for a QR held at reading distance — without it the lens hunts the full range and tends to settle on the far field.
  • Reapplier re-asserts focus on subjectAreaDidChangeNotification, and restores the device on deinit so other sessions are unaffected.

Both call sites apply it after a short settle delay, because a system controller reconfigures the device as its session starts and anything set before that is simply overwritten. The scanner asks for .near; the photo camera does not, since a capture may be a whiteboard across the room as easily as a page on the desk.

Where it genuinely cannot be fixed

A camera reporting isFocusModeSupported(.continuousAutoFocus) == false is a fixed-focus camera — several iPad front cameras are — and no software setting changes that. That returns .unsupported(deviceName:) and the scanner sheet now says so:

" is a fixed-focus camera — hold the code about 20 cm away and steady."

rather than implying a fix that did not happen.

Verification

⚠️ Not run on any device or simulator, and autofocus cannot be exercised in a simulator regardless — the simulator has no AVCaptureDevice.

Worse, the owner's Mac cannot currently build for the iOS simulator at all: /Library/Developer/PrivateFrameworks/CoreSimulator.framework is missing, so xcodebuild fails to load IDESimulatorFoundation and refuses to run for any destination. xcodebuild -runFirstLaunch needs admin rights not available over SSH.

What I could verify:

  • Sources type-check against the iOS 26.5 simulator SDK via xcrun swiftc -typecheck (bypassing xcodebuild) — 0 errors.
  • apps/ios/scripts/generate-xcodeproj.py re-run so CameraFocus.swift and CameraFocusTests.swift are registered with the app and test targets.

CameraFocusTests pins the part that must stay honest — a fixed-focus camera is reported, not silently ignored. These tests have never been executed here; CI's iPad companion (simulator build + tests) job runs FlashTeXPadTests and will be the first to run them.

The actual autofocus behaviour needs a check on the owner's iPad. If it still will not focus after this, the next thing to know is which camera is in use and what CameraFocus.Outcome it reports.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu

The owner reported the iPad not autofocusing, "ex. in the QR code".

Neither camera path owned a capture session, so neither ever configured
focus: `PairingScannerView` hands off to VisionKit's
`DataScannerViewController` and `CameraPicker` to `UIImagePickerController`,
and both keep their `AVCaptureSession` and `AVCaptureDeviceInput` private.
The remaining lever is the `AVCaptureDevice`, which is process-wide: focus
settings applied to it while a session runs do take effect on that session.

Add `CameraFocus`:
  - `apply(nearRange:)` locks the back wide-angle device and sets
    `focusMode = .continuousAutoFocus`, `.continuousAutoExposure`, and
    `isSubjectAreaChangeMonitoringEnabled = true`.
  - `nearRange` additionally sets `autoFocusRangeRestriction = .near`. This
    is what a QR at reading distance needs; without it the lens hunts the
    whole range and tends to settle on the far field.
  - `Reapplier` re-asserts focus on `subjectAreaDidChangeNotification`, and
    restores the device on deinit so other sessions are unaffected.

Both call sites apply it AFTER a short settle delay, because a system
controller reconfigures the device as its session starts and anything set
before that is overwritten. The scanner asks for `.near`; the photo camera
does not, since a capture may be a whiteboard across the room.

A camera that reports `isFocusModeSupported(.continuousAutoFocus) == false`
is genuinely fixed-focus (several iPad front cameras are). That returns
`.unsupported` and the scanner sheet says so, rather than implying a fix
that did not happen.

NOT verified on hardware: this machine has no simulator runtime (see the
PR body) and autofocus cannot be exercised in a simulator regardless. The
behaviour needs a check on the owner's iPad.

Implementation-Agent: Claude Code iPad companion agent (linux-primary)
Commit-Executor: git via Claude Code
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xd5Hmwh5GHNTiAmHUJ1MZu
@d-q222

d-q222 commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

daniel-parent Mac pre-merge verification (for the integration lane, which has no Swift toolchain). Xcode 26.6, macOS 26.6, merged onto origin/main 98ca402a in a throwaway worktree, PR head d43c542d. Worker: Cursor; results checked by daniel-parent.

PR merge filtered tests full suite verdict
#349 CLEAN iOS CameraFocus 4 / 0 fail iOS 29 / 0 skipped / 0 fail READY

About the 3 full-suite failures: WordCountModelTests.testOnlyTheLastScheduledUpdateWins and testScheduleUpdateEventuallyPublishesTotals fail in every full run, including the control on main, while the machine is loaded. Run alone on main 095e8406 they pass twice (2/0), so they're timing-flaky under full-suite load, not caused by this PR or broken on main.

@GoKubar
GoKubar marked this pull request as ready for review September 14, 2026 16:17
@GoKubar
GoKubar merged commit d90f644 into main Sep 14, 2026
19 of 20 checks passed
@GoKubar
GoKubar deleted the agent/ipad/camera-autofocus branch September 14, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants