Open source CapCut alternative.
TypeScript
fix(ios): the prompter loops without an empty screen
David Le4a28fca92f49
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Teleprompter.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Teleprompter.swiftindex 9fd90745..0eb46d54 100644--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Teleprompter.swift+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Teleprompter.swift@@ -22,6 +22,7 @@ nonisolated public struct TeleprompterSettings: Equatable, Codable, Sendable {/// so the eye has a rhythm to follow. Nobody has to format a note to read it.nonisolated public func pacedScript(_ raw: String) -> String {let paragraphs = raw+ .trimmingCharacters(in: .whitespacesAndNewlines).split(separator: "\n", omittingEmptySubsequences: true).map { pacedParagraph(String($0)) }.filter { !$0.isEmpty }@@ -79,16 +80,25 @@ nonisolated public struct TeleprompterState: Equatable, Sendable {/// of the prompter's height.public static let leadShare = 0.5- /// Vertical offset of the script at `elapsed` seconds into a recording.- /// The text starts halfway down the prompter and travels its own rendered- /// height plus that lead-in over the read duration, so the last words- /// leave the top exactly when the speaker should be finishing them.- /// Paragraph gaps are part of the height, so they read as natural pauses.- public func scrollOffset(elapsed: TimeInterval, overlayHeight: Double, textHeight: Double) -> Double {+ /// Vertical offset of the script at `elapsed` seconds in. The script+ /// starts halfway down the prompter and rises at the reading pace — its+ /// own rendered height per read duration, so the words pass the reader at+ /// the set words per minute, paragraph gaps included as natural pauses.+ ///+ /// The travel wraps at the script's height plus `gap`, which is the copy+ /// the screen draws twice: the next pass comes up from below as the last+ /// one leaves, so the loop never runs through an empty screen.+ public func scrollOffset(+ elapsed: TimeInterval,+ overlayHeight: Double,+ textHeight: Double,+ gap: Double+ ) -> Double {let lead = overlayHeight * Self.leadSharelet total = duration- guard total > 0 else { return lead }- let speed = (textHeight + lead) / total- return lead - speed * elapsed+ guard total > 0, textHeight > 0 else { return lead }+ let speed = textHeight / total+ let cycle = textHeight + gap+ return lead - (speed * elapsed).truncatingRemainder(dividingBy: cycle)}}diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swiftindex 857f526a..97ccf991 100644--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift@@ -512,37 +512,49 @@ struct TeleprompterOverlay: View {/// mid-take moves the words and the pace carries on.@State private var dragged: Double = 0@GestureState private var dragging: Double = 0- /// Beat between the end of a preview pass and the top of the next one.- private static var previewGap: TimeInterval { 1.5 }+ /// Breathing room between one pass of the script and the next, as a share+ /// of the prompter's height.+ private static var gapShare: Double { 0.3 }/// Room kept at the foot of the screen for the record button and the tabs.private static var controlsInset: Double { 190 }+ private var scriptText: some View {+ Text(camera.teleprompter.displayScript)+ .font(.system(size: camera.teleprompter.settings.textSize, weight: .heavy))+ .foregroundStyle(.white)+ .lineSpacing(4)+ .shadow(color: .black.opacity(0.7), radius: 6, y: 1)+ // The window the script scrolls through is a fraction of the+ // screen, and a Text offered that height lays out only what fits+ // and ends the last line in an ellipsis. The script is laid out+ // whole and scrolled past the window.+ .fixedSize(horizontal: false, vertical: true)+ .frame(maxWidth: .infinity, alignment: .leading)+ }+var body: some View {GeometryReader { geometry in// The script gets the screen, stopping short of the controls.let height = max(geometry.size.height - Self.controlsInset, 0)+ let gap = height * Self.gapShareTimelineView(.animation) { context inlet elapsed = elapsed(at: context.date)- Text(camera.teleprompter.displayScript)- .font(.system(size: camera.teleprompter.settings.textSize, weight: .heavy))- .foregroundStyle(.white)- .lineSpacing(4)- .shadow(color: .black.opacity(0.7), radius: 6, y: 1)- // The window the script scrolls through is a fraction of- // the screen, and a Text offered that height lays out only- // what fits and ends the last line in an ellipsis. The- // script is laid out whole and scrolled past the window.- .fixedSize(horizontal: false, vertical: true)- .frame(maxWidth: .infinity, alignment: .leading)- .onGeometryChange(for: Double.self, of: { $0.size.height }) { textHeight = $0 }- .offset(- y: camera.teleprompter.scrollOffset(- elapsed: elapsed,- overlayHeight: height,- textHeight: textHeight- ) + dragged + dragging- )- .padding(.horizontal, 24)+ // Two passes, one behind the other: the script runs without+ // ever leaving the screen empty, in a take as in a preview.+ VStack(alignment: .leading, spacing: gap) {+ scriptText+ .onGeometryChange(for: Double.self, of: { $0.size.height }) { textHeight = $0 }+ scriptText+ }+ .offset(+ y: camera.teleprompter.scrollOffset(+ elapsed: elapsed,+ overlayHeight: height,+ textHeight: textHeight,+ gap: gap+ ) + dragged + dragging+ )+ .padding(.horizontal, 24)}.frame(height: height, alignment: .top).clipped()@@ -575,16 +587,13 @@ struct TeleprompterOverlay: View {}/// Seconds into the script. A take counts from the moment recording- /// started; idle, the script runs the same pass on a loop, from the top- /// again each time the test button is pressed.+ /// started; idle, from when the prompter came up, or from the last press+ /// of the test button. The loop itself lives in the offset.private func elapsed(at now: Date) -> TimeInterval {if let startedAt = camera.recordingStartedAt {return now.timeIntervalSince(startedAt)}- let anchor = camera.teleprompter.testStartedAt ?? previewStart- let pass = camera.teleprompter.duration + Self.previewGap- guard pass > 0 else { return 0 }- return now.timeIntervalSince(anchor).truncatingRemainder(dividingBy: pass)+ return now.timeIntervalSince(camera.teleprompter.testStartedAt ?? previewStart)}}diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swiftindex fb197442..c1451236 100644--- a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift@@ -65,20 +65,31 @@ import Testingvar state = TeleprompterState()state.settings.wordsPerMinute = 120state.script = Array(repeating: "word", count: 120).joined(separator: " ")- // 120 words at 120 wpm = 60s. The script starts halfway down the- // prompter and travels its own height plus that lead over the minute.+ // 120 words at 120 wpm = 60s, so 420pt of script passes in a minute.+ // The script starts halfway down the prompter and rises at that pace.#expect(state.duration == 60)- let start = state.scrollOffset(elapsed: 0, overlayHeight: 300, textHeight: 420)+ let start = state.scrollOffset(elapsed: 0, overlayHeight: 300, textHeight: 420, gap: 100)#expect(start == 150)- let mid = state.scrollOffset(elapsed: 30, overlayHeight: 300, textHeight: 420)- #expect(mid == 150 - (420.0 + 150.0) / 2)- let end = state.scrollOffset(elapsed: 60, overlayHeight: 300, textHeight: 420)- #expect(end == -420)+ let mid = state.scrollOffset(elapsed: 30, overlayHeight: 300, textHeight: 420, gap: 100)+ #expect(mid == 150 - 210)+ let end = state.scrollOffset(elapsed: 60, overlayHeight: 300, textHeight: 420, gap: 100)+ #expect(end == 150 - 420)+ }++ @Test func scrollWrapsAtTheGapWithoutAnEmptyScreen() {+ var state = TeleprompterState()+ state.settings.wordsPerMinute = 120+ state.script = Array(repeating: "word", count: 120).joined(separator: " ")+ // 7pt a second: the second copy of the script reaches the first one's+ // starting place exactly when the travel wraps.+ let cycle = (420.0 + 100.0) / 7+ let wrapped = state.scrollOffset(elapsed: cycle, overlayHeight: 300, textHeight: 420, gap: 100)+ #expect(abs(wrapped - 150) < 0.000_001)}@Test func emptyScriptHoldsAtLead() {let state = TeleprompterState()- #expect(state.scrollOffset(elapsed: 5, overlayHeight: 300, textHeight: 0) == 150)+ #expect(state.scrollOffset(elapsed: 5, overlayHeight: 300, textHeight: 0, gap: 100) == 150)}@Test func hasScriptIgnoresWhitespace() {diff --git a/docs/guides/ios-app.md b/docs/guides/ios-app.mdindex 13e2a506..7a29a951 100644--- a/docs/guides/ios-app.md+++ b/docs/guides/ios-app.md@@ -69,10 +69,11 @@ A note is read for what is written in it — its body, or its title when that isall it has. The script is laid out whole and scrolled past the screen, solength is never a reason for a note to arrive half-read.-Closing the card hands the screen to the prompter, which runs the script on a-loop until the take starts, and the card's test button restarts that run from-the top. A speed and a size are something a person watches before they commit-to them.+Closing the card hands the screen to the prompter. The script runs on a loop+the same way in a preview as in a take: the screen draws it twice, so the next+pass rises as the last one leaves and the loop never runs through an empty+screen. The card's test button starts a run from the top, which is how a speed+and a size get watched before anyone commits to them.The script starts halfway down the screen, and a drag moves it: the nudgerides along with the pacing, so the words land where the reader wants them and