Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(mobile): add teleprompter overlay utilities

Richie McIlroya2bae7ed4d20
diff --git a/apps/mobile/src/recording/TeleprompterOverlay.tsx b/apps/mobile/src/recording/TeleprompterOverlay.tsx
new file mode 100644
index 000000000..40e7b848c
--- /dev/null
+++ b/apps/mobile/src/recording/TeleprompterOverlay.tsx
@@ -0,0 +1,232 @@
+import { SymbolView } from "expo-symbols";
+import { useEffect, useMemo, useState } from "react";
+import {
+ type LayoutChangeEvent,
+ Pressable,
+ StyleSheet,
+ Text,
+ View,
+} from "react-native";
+import Animated, {
+ cancelAnimation,
+ Easing,
+ useAnimatedStyle,
+ useSharedValue,
+ withTiming,
+} from "react-native-reanimated";
+import { fonts, radius, squircle } from "@/theme";
+import {
+ calculatePlaybackDurationMs,
+ calculateRemainingPlaybackDurationMs,
+} from "./teleprompter";
+
+type TeleprompterOverlayProps = {
+ script: string;
+ fontSize: number;
+ wordsPerMinute: number;
+ playing: boolean;
+ restartKey: number;
+ onTogglePlayback: () => void;
+};
+
+export function TeleprompterOverlay({
+ script,
+ fontSize,
+ wordsPerMinute,
+ playing,
+ restartKey,
+ onTogglePlayback,
+}: TeleprompterOverlayProps) {
+ const [viewportHeight, setViewportHeight] = useState(0);
+ const [textHeight, setTextHeight] = useState(0);
+ const progress = useSharedValue(0);
+ const lineHeight = Math.round(fontSize * 1.42);
+ const travelDistance = Math.max(0, textHeight - lineHeight);
+ const totalDurationMs = useMemo(
+ () => calculatePlaybackDurationMs(script, wordsPerMinute),
+ [script, wordsPerMinute],
+ );
+
+ useEffect(() => {
+ if (restartKey < 0) return;
+ cancelAnimation(progress);
+ progress.value = 0;
+ }, [progress, restartKey]);
+
+ useEffect(() => {
+ if (
+ restartKey < 0 ||
+ !playing ||
+ travelDistance <= 1 ||
+ totalDurationMs === 0
+ ) {
+ cancelAnimation(progress);
+ return;
+ }
+
+ if (progress.value >= 0.999) progress.value = 0;
+ const duration = calculateRemainingPlaybackDurationMs(
+ totalDurationMs,
+ progress.value,
+ );
+ progress.value = withTiming(1, {
+ duration,
+ easing: Easing.linear,
+ });
+ }, [playing, progress, restartKey, totalDurationMs, travelDistance]);
+
+ const textStyle = useAnimatedStyle(() => ({
+ transform: [{ translateY: -travelDistance * progress.value }],
+ }));
+
+ const onViewportLayout = (event: LayoutChangeEvent) => {
+ setViewportHeight(event.nativeEvent.layout.height);
+ };
+
+ const onTextLayout = (event: LayoutChangeEvent) => {
+ cancelAnimation(progress);
+ progress.value = 0;
+ setTextHeight(event.nativeEvent.layout.height);
+ };
+
+ return (
+ <View
+ accessibilityLabel="Teleprompter"
+ onLayout={onViewportLayout}
+ style={styles.container}
+ >
+ <View pointerEvents="none" style={styles.centerGuide}>
+ <SymbolView
+ name="chevron.right"
+ size={15}
+ tintColor="rgba(255,255,255,0.72)"
+ weight="bold"
+ />
+ <View style={styles.guideLine} />
+ <SymbolView
+ name="chevron.left"
+ size={15}
+ tintColor="rgba(255,255,255,0.72)"
+ weight="bold"
+ />
+ </View>
+ <Animated.View
+ pointerEvents="none"
+ style={[
+ styles.script,
+ {
+ top: Math.max(0, (viewportHeight - lineHeight) / 2),
+ },
+ textStyle,
+ ]}
+ >
+ <Text
+ onLayout={onTextLayout}
+ style={[styles.scriptText, { fontSize, lineHeight }]}
+ >
+ {script.trim()}
+ </Text>
+ </Animated.View>
+ <View pointerEvents="none" style={styles.topFade} />
+ <View pointerEvents="none" style={styles.bottomFade} />
+ <Pressable
+ accessibilityRole="button"
+ accessibilityLabel={
+ playing ? "Pause teleprompter" : "Play teleprompter"
+ }
+ hitSlop={8}
+ onPress={onTogglePlayback}
+ style={({ pressed }) => [
+ styles.playbackButton,
+ pressed ? styles.playbackButtonPressed : null,
+ ]}
+ >
+ <SymbolView
+ name={playing ? "pause.fill" : "play.fill"}
+ size={13}
+ tintColor="rgba(255,255,255,0.9)"
+ weight="semibold"
+ />
+ </Pressable>
+ </View>
+ );
+}
+
+const styles = StyleSheet.create({
+ container: {
+ position: "absolute",
+ top: 106,
+ left: 14,
+ right: 14,
+ height: "43%",
+ borderRadius: radius.xl,
+ borderWidth: StyleSheet.hairlineWidth,
+ borderColor: "rgba(255,255,255,0.16)",
+ backgroundColor: "rgba(8,10,14,0.28)",
+ overflow: "hidden",
+ ...squircle,
+ },
+ centerGuide: {
+ position: "absolute",
+ zIndex: 2,
+ left: 9,
+ right: 9,
+ top: "50%",
+ flexDirection: "row",
+ alignItems: "center",
+ gap: 7,
+ transform: [{ translateY: -8 }],
+ },
+ guideLine: {
+ flex: 1,
+ height: StyleSheet.hairlineWidth,
+ backgroundColor: "rgba(255,255,255,0.18)",
+ },
+ script: {
+ position: "absolute",
+ left: 28,
+ right: 28,
+ },
+ scriptText: {
+ fontFamily: fonts.medium,
+ color: "rgba(255,255,255,0.9)",
+ textAlign: "center",
+ letterSpacing: -0.6,
+ textShadowColor: "rgba(0,0,0,0.34)",
+ textShadowOffset: { width: 0, height: 1 },
+ textShadowRadius: 4,
+ },
+ topFade: {
+ position: "absolute",
+ top: 0,
+ left: 0,
+ right: 0,
+ height: 36,
+ backgroundColor: "rgba(8,10,14,0.34)",
+ },
+ bottomFade: {
+ position: "absolute",
+ bottom: 0,
+ left: 0,
+ right: 0,
+ height: 42,
+ backgroundColor: "rgba(8,10,14,0.34)",
+ },
+ playbackButton: {
+ position: "absolute",
+ right: 10,
+ bottom: 9,
+ zIndex: 3,
+ width: 34,
+ height: 34,
+ borderRadius: radius.full,
+ alignItems: "center",
+ justifyContent: "center",
+ backgroundColor: "rgba(0,0,0,0.42)",
+ borderWidth: StyleSheet.hairlineWidth,
+ borderColor: "rgba(255,255,255,0.18)",
+ },
+ playbackButtonPressed: {
+ opacity: 0.72,
+ },
+});
diff --git a/apps/mobile/src/recording/cap-recorder-native.test.ts b/apps/mobile/src/recording/cap-recorder-native.test.ts
new file mode 100644
index 000000000..0a7c4d39c
--- /dev/null
+++ b/apps/mobile/src/recording/cap-recorder-native.test.ts
@@ -0,0 +1,29 @@
+import { readFileSync } from "node:fs";
+import { join } from "node:path";
+import { describe, expect, it } from "vitest";
+
+describe("CapRecorder native capture session", () => {
+ it("commits configuration before starting the capture session", () => {
+ const source = readFileSync(
+ join(process.cwd(), "modules/cap-recorder/ios/CapRecorderView.swift"),
+ "utf8",
+ );
+ const configureSession = source.slice(
+ source.indexOf("private func configureSession()"),
+ source.indexOf("private func configureAudioSession()"),
+ );
+ const startRunningIndex = configureSession.indexOf(
+ "self.session.startRunning()",
+ );
+ const commitConfigurationIndex = configureSession.lastIndexOf(
+ "self.session.commitConfiguration()",
+ startRunningIndex,
+ );
+
+ expect(configureSession).not.toMatch(
+ /defer\s*\{[^}]*commitConfiguration\(\)/s,
+ );
+ expect(commitConfigurationIndex).toBeGreaterThan(-1);
+ expect(startRunningIndex).toBeGreaterThan(commitConfigurationIndex);
+ });
+});
diff --git a/apps/mobile/src/recording/teleprompter.test.ts b/apps/mobile/src/recording/teleprompter.test.ts
new file mode 100644
index 000000000..989e64350
--- /dev/null
+++ b/apps/mobile/src/recording/teleprompter.test.ts
@@ -0,0 +1,37 @@
+import { describe, expect, it } from "vitest";
+import {
+ calculatePlaybackDurationMs,
+ calculateRemainingPlaybackDurationMs,
+ clamp,
+ countWords,
+ formatRecordingDuration,
+} from "./teleprompter";
+
+describe("mobile teleprompter", () => {
+ it("counts pasted scripts consistently", () => {
+ expect(countWords(" One two\nthree ")).toBe(3);
+ expect(countWords(" ")).toBe(0);
+ });
+
+ it("derives playback duration from reading speed", () => {
+ expect(calculatePlaybackDurationMs("one two three", 180)).toBe(1000);
+ expect(calculatePlaybackDurationMs("one two three", 60)).toBe(3000);
+ expect(calculatePlaybackDurationMs("", 150)).toBe(0);
+ });
+
+ it("keeps resumed playback proportional to the unread script", () => {
+ expect(calculateRemainingPlaybackDurationMs(10_000, 0.25)).toBe(7500);
+ expect(calculateRemainingPlaybackDurationMs(10_000, 2)).toBe(0);
+ });
+
+ it("clamps teleprompter settings", () => {
+ expect(clamp(5, 10, 20)).toBe(10);
+ expect(clamp(25, 10, 20)).toBe(20);
+ });
+
+ it("formats a native recording timer", () => {
+ expect(formatRecordingDuration(0)).toBe("0:00");
+ expect(formatRecordingDuration(65.9)).toBe("1:05");
+ expect(formatRecordingDuration(3661)).toBe("1:01:01");
+ });
+});
diff --git a/apps/mobile/src/recording/teleprompter.ts b/apps/mobile/src/recording/teleprompter.ts
new file mode 100644
index 000000000..71b2561cc
--- /dev/null
+++ b/apps/mobile/src/recording/teleprompter.ts
@@ -0,0 +1,39 @@
+export const teleprompterDefaults = {
+ fontSize: 30,
+ wordsPerMinute: 150,
+} as const;
+
+export const teleprompterLimits = {
+ fontSize: { minimum: 24, maximum: 44, step: 2 },
+ wordsPerMinute: { minimum: 60, maximum: 350, step: 10 },
+} as const;
+
+export const clamp = (value: number, minimum: number, maximum: number) =>
+ Math.min(maximum, Math.max(minimum, value));
+
+export const countWords = (text: string) =>
+ text.trim() ? text.trim().split(/\s+/u).length : 0;
+
+export const calculatePlaybackDurationMs = (
+ script: string,
+ wordsPerMinute: number,
+) => {
+ const wordCount = countWords(script);
+ if (wordCount === 0) return 0;
+ return Math.max(1000, (wordCount / Math.max(1, wordsPerMinute)) * 60_000);
+};
+
+export const calculateRemainingPlaybackDurationMs = (
+ totalDurationMs: number,
+ progress: number,
+) => Math.max(0, totalDurationMs * (1 - clamp(progress, 0, 1)));
+
+export const formatRecordingDuration = (elapsedSeconds: number) => {
+ const safeSeconds = Math.max(0, Math.floor(elapsedSeconds));
+ const hours = Math.floor(safeSeconds / 3600);
+ const minutes = Math.floor((safeSeconds % 3600) / 60);
+ const seconds = safeSeconds % 60;
+ const minuteText = hours > 0 ? String(minutes).padStart(2, "0") : minutes;
+ const time = `${minuteText}:${String(seconds).padStart(2, "0")}`;
+ return hours > 0 ? `${hours}:${time}` : time;
+};