Skip to content

Open source CapCut alternative.

TypeScript

feat(ios): the watch's crown scrolls the running script on the phone

David Leff192c80ee32
diff --git a/apps/ios/Donkey/Controllers/WatchLinkController.swift b/apps/ios/Donkey/Controllers/WatchLinkController.swift
index 994d8961..b4b7ecdd 100644
--- a/apps/ios/Donkey/Controllers/WatchLinkController.swift
+++ b/apps/ios/Donkey/Controllers/WatchLinkController.swift
@@ -37,6 +37,7 @@ final class WatchLinkController: NSObject, WCSessionDelegate {
_ = camera.recordingStartedAt
_ = camera.availability
_ = camera.facing
+ _ = camera.teleprompter.isRunning
} onChange: {
Task { @MainActor [weak self] in
guard let self else { return }
@@ -50,7 +51,8 @@ final class WatchLinkController: NSObject, WCSessionDelegate {
CameraRemoteState(
isCameraOpen: camera.availability == .running,
facing: camera.facing,
- recordingElapsed: camera.recordingStartedAt.map { Date.now.timeIntervalSince($0) }
+ recordingElapsed: camera.recordingStartedAt.map { Date.now.timeIntervalSince($0) },
+ isScriptRunning: camera.teleprompter.isRunning
)
}
@@ -82,6 +84,8 @@ final class WatchLinkController: NSObject, WCSessionDelegate {
}
case .preview(let wanted):
setPreview(wanted)
+ case .nudgeScript(let points):
+ camera.nudgeTeleprompter(by: points)
}
}
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraModel.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraModel.swift
index 9793849e..461cc203 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraModel.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraModel.swift
@@ -147,6 +147,15 @@ public final class CameraModel {
teleprompter.isCardShown = false
teleprompter.isRunning = true
teleprompter.runStartedAt = .now
+ // A fresh run puts the script back where the pacing wants it.
+ teleprompter.nudge = 0
+ }
+
+ /// Moves the running script by `points`: down for positive, the way a
+ /// drag down moves it.
+ public func nudgeTeleprompter(by points: Double) {
+ guard teleprompter.isRunning else { return }
+ teleprompter.nudge += points
}
/// Takes the prompter off the picture: the card and the running script
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraRemote.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraRemote.swift
index d984bd2a..8d1a6543 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraRemote.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraRemote.swift
@@ -13,11 +13,20 @@ nonisolated public struct CameraRemoteState: Codable, Equatable, Sendable {
public var facing: CameraFacing
/// Seconds into the take, or nil when the camera is not recording.
public var recordingElapsed: TimeInterval?
+ /// True while the script is running on the phone's picture, which is
+ /// when the watch's crown has something to move.
+ public var isScriptRunning: Bool
- public init(isCameraOpen: Bool = false, facing: CameraFacing = .front, recordingElapsed: TimeInterval? = nil) {
+ public init(
+ isCameraOpen: Bool = false,
+ facing: CameraFacing = .front,
+ recordingElapsed: TimeInterval? = nil,
+ isScriptRunning: Bool = false
+ ) {
self.isCameraOpen = isCameraOpen
self.facing = facing
self.recordingElapsed = recordingElapsed
+ self.isScriptRunning = isScriptRunning
}
public var isRecording: Bool { recordingElapsed != nil }
@@ -31,6 +40,8 @@ nonisolated public enum CameraRemoteCommand: Codable, Equatable, Sendable {
case toggleRecording
/// Whether the watch is looking at the picture and wants frames.
case preview(Bool)
+ /// Move the running script by this many points, down for positive.
+ case nudgeScript(Double)
}
/// The envelope both ends put a state or a command in: one key, one JSON
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Teleprompter.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Teleprompter.swift
index 0ecb3c09..2eb0040e 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Teleprompter.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Teleprompter.swift
@@ -82,6 +82,11 @@ nonisolated public struct TeleprompterState: Equatable, Sendable {
public var settings = TeleprompterSettings()
/// When the run was asked for, so the script starts from the top.
public var runStartedAt: Date?
+ /// How far the reader has moved the script from where the pacing puts
+ /// it, in points. It rides along with the scroll rather than replacing
+ /// it, so a nudge mid-take moves the words and the pace carries on. A
+ /// drag on the phone and the crown on the watch both land here.
+ public var nudge: Double = 0
public init() {}
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift
index ebb639a3..5cb602b7 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift
@@ -690,10 +690,8 @@ struct TeleprompterOverlay: View {
/// Rendered height of the paced script, measured off the Text itself so
/// the scroll rate can pace the exact copy on screen.
@State private var textHeight: Double = 0
- /// How far the reader has dragged the script from where the pacing puts
- /// it. It rides along with the scroll rather than replacing it, so a nudge
- /// mid-take moves the words and the pace carries on.
- @State private var dragged: Double = 0
+ /// The drag in flight. Its end lands on the model's nudge, beside what
+ /// the watch's crown sends.
@GestureState private var dragging: Double = 0
/// Breathing room between one pass of the script and the next, as a share
/// of the prompter's height.
@@ -750,7 +748,7 @@ struct TeleprompterOverlay: View {
overlayHeight: height,
textHeight: textHeight,
gap: gap,
- nudge: dragged + dragging
+ nudge: camera.teleprompter.nudge + dragging
)
VStack(alignment: .leading, spacing: gap) {
ForEach(0..<pass.copies, id: \.self) { copy in
@@ -789,12 +787,10 @@ struct TeleprompterOverlay: View {
.gesture(
DragGesture(minimumDistance: 6)
.updating($dragging) { value, state, _ in state = value.translation.height }
- .onEnded { dragged += $0.translation.height }
+ .onEnded { camera.nudgeTeleprompter(by: $0.translation.height) }
)
}
.ignoresSafeArea()
- // A fresh run puts the script back where the pacing wants it.
- .onChange(of: camera.teleprompter.runStartedAt) { dragged = 0 }
}
/// Seconds into the script, counted from the press of play — or from the
diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
index 0e5f2927..8e971165 100644
--- a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
@@ -40,6 +40,26 @@ import Testing
}
}
+@Suite struct TeleprompterNudgeTests {
+ @Test func nudgesAddUpAndAFreshRunClearsThem() {
+ let camera = CameraModel(defaults: UserDefaults(suiteName: "nudge-\(UUID())")!)
+ camera.loadTeleprompter(script: "One two three")
+ camera.startTeleprompter()
+ camera.nudgeTeleprompter(by: 40)
+ camera.nudgeTeleprompter(by: -15)
+ #expect(camera.teleprompter.nudge == 25)
+ camera.startTeleprompter()
+ #expect(camera.teleprompter.nudge == 0)
+ }
+
+ @Test func aStoppedScriptIgnoresNudges() {
+ let camera = CameraModel(defaults: UserDefaults(suiteName: "nudge-\(UUID())")!)
+ camera.loadTeleprompter(script: "One two three")
+ camera.nudgeTeleprompter(by: 40)
+ #expect(camera.teleprompter.nudge == 0)
+ }
+}
+
@Suite struct SafeZoneTests {
@Test func portraitFrameIsWiderThanThePhone() {
let frame = FrameRect.aspectFill(contentAspect: 9.0 / 16.0, containerWidth: 390, containerHeight: 844)
diff --git a/apps/ios/DonkeyWatch/WatchCameraLink.swift b/apps/ios/DonkeyWatch/WatchCameraLink.swift
index d67d88a0..809b6a09 100644
--- a/apps/ios/DonkeyWatch/WatchCameraLink.swift
+++ b/apps/ios/DonkeyWatch/WatchCameraLink.swift
@@ -23,6 +23,14 @@ final class WatchCameraLink: NSObject, WCSessionDelegate {
private let session = WCSession.default
private var isWatching = false
+ /// Crown turns gathered between sends, in points, so a spin is a few
+ /// messages and the phone moves the script in one motion.
+ private var pendingNudge: Double = 0
+ private var nudgeFlush: Task<Void, Never>?
+ /// Points of script per crown unit. The crown turns "up" to send the
+ /// words up, the way a list scrolls under it.
+ private static let pointsPerCrownUnit: Double = 6
+ private static let nudgeSendInterval: Duration = .milliseconds(60)
override init() {
super.init()
@@ -41,6 +49,25 @@ final class WatchCameraLink: NSObject, WCSessionDelegate {
session.sendMessage(CameraRemoteMessage.encode(.toggleRecording), replyHandler: nil)
}
+ /// A crown turn: `units` is the crown's own count, up for positive.
+ func scrollScript(by units: Double) {
+ guard state.isScriptRunning, session.isReachable else { return }
+ pendingNudge -= units * Self.pointsPerCrownUnit
+ guard nudgeFlush == nil else { return }
+ nudgeFlush = Task { [weak self] in
+ try? await Task.sleep(for: Self.nudgeSendInterval)
+ self?.flushNudge()
+ }
+ }
+
+ private func flushNudge() {
+ nudgeFlush = nil
+ let points = pendingNudge
+ pendingNudge = 0
+ guard points != 0, session.isReachable else { return }
+ session.sendMessage(CameraRemoteMessage.encode(.nudgeScript(points)), replyHandler: nil)
+ }
+
/// Whether the screen is up. On, the phone is asked for its state and
/// starts sending frames; off, the frames stop.
func setWatching(_ watching: Bool) {
diff --git a/apps/ios/DonkeyWatch/WatchCameraView.swift b/apps/ios/DonkeyWatch/WatchCameraView.swift
index f4a10e2c..7f8ab110 100644
--- a/apps/ios/DonkeyWatch/WatchCameraView.swift
+++ b/apps/ios/DonkeyWatch/WatchCameraView.swift
@@ -3,6 +3,10 @@ import SwiftUI
struct WatchCameraView: View {
var link: WatchCameraLink
+ /// The crown's running count. Only the change between turns matters;
+ /// the range is wide so it never hits an end mid-take.
+ @State private var crown: Double = 0
+ @State private var crownRead: Double = 0
var body: some View {
ZStack {
@@ -21,6 +25,22 @@ struct WatchCameraView: View {
.background(.black)
.ignoresSafeArea()
.animation(.spring(duration: 0.3), value: link.recordingStartedAt != nil)
+ // The crown scrolls the script on the phone, so a reader mid-take
+ // can bring the words back without reaching for the screen.
+ .focusable()
+ .digitalCrownRotation(
+ $crown,
+ from: -1_000_000,
+ through: 1_000_000,
+ by: 1,
+ sensitivity: .medium,
+ isContinuous: false,
+ isHapticFeedbackEnabled: false
+ )
+ .onChange(of: crown) { _, value in
+ link.scrollScript(by: value - crownRead)
+ crownRead = value
+ }
}
@ViewBuilder private var picture: some View {
diff --git a/docs/guides/ios-app.md b/docs/guides/ios-app.md
index eae4fa40..6e17bb1d 100644
--- a/docs/guides/ios-app.md
+++ b/docs/guides/ios-app.md
@@ -142,8 +142,9 @@ The watch app is a remote for the phone's camera: a record button, a red
clock while a take runs, and the picture itself, so the shot can be framed
from where the person is standing. It works while the camera tab is up on the
phone — a record command sent from another tab switches to the camera first —
-and the phone streams small frames only while the watch screen is on. The
-phone and the watch talk over Watch Connectivity: the phone sends the camera's
+and the phone streams small frames only while the watch screen is on. While the
+script is running, the crown scrolls it on the phone, the same nudge a drag
+on the picture makes. The phone and the watch talk over Watch Connectivity: the phone sends the camera's
state on every change, and the watch sends its commands and acknowledges each
frame, which is how the stream paces itself to the link.