Skip to content

Open source CapCut alternative.

TypeScript

feat(ios): safe-zone shading for short-form platforms on the camera

David Le0b17084e5f33
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraModel.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraModel.swift
index d8b6c48b..9793849e 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraModel.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraModel.swift
@@ -41,12 +41,24 @@ public final class CameraModel {
}
}
+ /// The short-form platforms whose covered areas shade the picture. Kept
+ /// across sessions: a person who posts to one place frames for it every
+ /// time.
+ public private(set) var safeZones: Set<SafeZonePlatform> = [] {
+ didSet {
+ if safeZones != oldValue {
+ defaults.set(safeZones.map(\.rawValue).sorted(), forKey: Self.safeZonesKey)
+ }
+ }
+ }
+
public var isRecording: Bool { recordingStartedAt != nil }
public var controller: (any CameraControlling)?
private let defaults: UserDefaults
private static let teleprompterKey = "teleprompterSettings"
+ private static let safeZonesKey = "safeZonePlatforms"
public init(defaults: UserDefaults = .standard) {
self.defaults = defaults
@@ -54,6 +66,17 @@ public final class CameraModel {
let settings = try? JSONDecoder().decode(TeleprompterSettings.self, from: data) {
teleprompter.settings = settings
}
+ if let stored = defaults.stringArray(forKey: Self.safeZonesKey) {
+ safeZones = Set(stored.compactMap(SafeZonePlatform.init(rawValue:)))
+ }
+ }
+
+ public func toggleSafeZone(_ platform: SafeZonePlatform) {
+ if safeZones.contains(platform) {
+ safeZones.remove(platform)
+ } else {
+ safeZones.insert(platform)
+ }
}
private func persistTeleprompterSettings() {
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SafeZones.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SafeZones.swift
new file mode 100644
index 00000000..3f3bc106
--- /dev/null
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SafeZones.swift
@@ -0,0 +1,84 @@
+import Foundation
+
+/// A short-form platform whose own chrome covers part of a vertical frame:
+/// the bar across the top, the caption block along the bottom, the action
+/// column down the right edge. Whatever a shot needs stays out of them.
+nonisolated public enum SafeZonePlatform: String, CaseIterable, Codable, Sendable, Identifiable {
+ case tiktok, reels, shorts
+
+ public var id: String { rawValue }
+
+ public var title: String {
+ switch self {
+ case .tiktok: "TikTok"
+ case .reels: "Reels"
+ case .shorts: "Shorts"
+ }
+ }
+
+ /// The covered parts of a 9:16 frame, as fractions of its width and
+ /// height. The numbers approximate each platform's layout on a phone;
+ /// the platforms move their chrome now and then, so these are tuned by
+ /// eye against a current phone.
+ public var covered: [SafeZoneRegion] {
+ switch self {
+ case .tiktok: [
+ SafeZoneRegion(x: 0, y: 0, width: 1, height: 0.11),
+ SafeZoneRegion(x: 0, y: 0.76, width: 1, height: 0.24),
+ SafeZoneRegion(x: 0.87, y: 0.36, width: 0.13, height: 0.40),
+ ]
+ case .reels: [
+ SafeZoneRegion(x: 0, y: 0, width: 1, height: 0.12),
+ SafeZoneRegion(x: 0, y: 0.74, width: 1, height: 0.26),
+ SafeZoneRegion(x: 0.86, y: 0.40, width: 0.14, height: 0.34),
+ ]
+ case .shorts: [
+ SafeZoneRegion(x: 0, y: 0, width: 1, height: 0.09),
+ SafeZoneRegion(x: 0, y: 0.78, width: 1, height: 0.22),
+ SafeZoneRegion(x: 0.87, y: 0.44, width: 0.13, height: 0.34),
+ ]
+ }
+ }
+}
+
+/// A rectangle in a frame's own units: every value is a fraction of the
+/// frame's width or height.
+nonisolated public struct SafeZoneRegion: Equatable, Sendable {
+ public var x: Double
+ public var y: Double
+ public var width: Double
+ public var height: Double
+
+ public init(x: Double, y: Double, width: Double, height: Double) {
+ self.x = x
+ self.y = y
+ self.width = width
+ self.height = height
+ }
+}
+
+/// Where a picture lands on a screen it fills: scaled until it covers the
+/// screen, centered, with the overflow off the edges.
+nonisolated public struct FrameRect: Equatable, Sendable {
+ public var x: Double
+ public var y: Double
+ public var width: Double
+ public var height: Double
+
+ /// `contentAspect` is the picture's width over its height; the frame it
+ /// returns is in the container's coordinates and may start off screen.
+ public static func aspectFill(contentAspect: Double, containerWidth: Double, containerHeight: Double) -> FrameRect {
+ guard contentAspect > 0, containerWidth > 0, containerHeight > 0 else {
+ return FrameRect(x: 0, y: 0, width: containerWidth, height: containerHeight)
+ }
+ let containerAspect = containerWidth / containerHeight
+ if contentAspect > containerAspect {
+ // Wider than the screen: full height, sides cropped.
+ let width = containerHeight * contentAspect
+ return FrameRect(x: (containerWidth - width) / 2, y: 0, width: width, height: containerHeight)
+ }
+ // Taller than the screen: full width, top and bottom cropped.
+ let height = containerWidth / contentAspect
+ return FrameRect(x: 0, y: (containerHeight - height) / 2, width: containerWidth, height: height)
+ }
+}
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift
index b4b4ac34..ebb639a3 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift
@@ -22,6 +22,7 @@ struct CameraScreen<CameraPreview: View>: View {
@State private var showsZoomPicker = false
@State private var showsQualityPopover = false
@State private var showsTeleSettings = false
+ @State private var showsSafeZones = false
@State private var showsNotePicker = false
@State private var playingRecording: Recording?
/// The take that just finished, docked in the corner until it is watched
@@ -59,6 +60,11 @@ struct CameraScreen<CameraPreview: View>: View {
var body: some View {
ZStack {
stage
+ // Short-form chrome is shaded on the picture itself, under the
+ // script and the controls, so a take is framed against it too.
+ if !camera.safeZones.isEmpty, !isSideways, camera.availability == .running {
+ SafeZoneOverlay(platforms: camera.safeZones)
+ }
if camera.isFillLightOn, camera.availability == .running {
FillLightOverlay()
}
@@ -291,6 +297,22 @@ struct CameraScreen<CameraPreview: View>: View {
}
.glassEffect(camera.teleprompter.isCardShown || camera.teleprompter.isRunning ? .regular.tint(.white.opacity(0.25)).interactive() : .regular.interactive())
+ Button {
+ showsSafeZones.toggle()
+ showsZoomPicker = false
+ showsQualityPopover = false
+ } label: {
+ Image(systemName: "rectangle.dashed")
+ .frame(width: 40, height: 40)
+ }
+ .glassEffect(camera.safeZones.isEmpty ? .regular.interactive() : .regular.tint(.white.opacity(0.25)).interactive())
+ .popover(isPresented: $showsSafeZones, arrowEdge: .leading) {
+ safeZonePopover
+ .presentationCompactAdaptation(.popover)
+ .preferredColorScheme(.dark)
+ .presentationBackground(Color.black.opacity(0.78))
+ }
+
Spacer().frame(height: 10)
Button {
@@ -389,6 +411,22 @@ struct CameraScreen<CameraPreview: View>: View {
.frame(minWidth: 280)
}
+ private var safeZonePopover: some View {
+ VStack(alignment: .leading, spacing: 12) {
+ Text("Safe Zones")
+ .font(.footnote)
+ .foregroundStyle(.secondary)
+ ForEach(SafeZonePlatform.allCases) { platform in
+ Toggle(platform.title, isOn: Binding(
+ get: { camera.safeZones.contains(platform) },
+ set: { _ in camera.toggleSafeZone(platform) }
+ ))
+ }
+ }
+ .padding(16)
+ .frame(minWidth: 220)
+ }
+
private var resolutionBinding: Binding<CaptureResolution> {
Binding(
get: { camera.settings.resolution },
@@ -466,6 +504,54 @@ private struct CaptureFlight: View {
/// A bright frame around the preview: the screen fill light for cameras
/// without a torch.
+/// Shades the parts of the recorded frame a platform's chrome will cover.
+/// The picture fills the screen, so the frame is wider than the screen in
+/// portrait and the regions are placed against the frame, not the screen.
+struct SafeZoneOverlay: View {
+ let platforms: Set<SafeZonePlatform>
+
+ private static let verticalAspect = 9.0 / 16.0
+
+ var body: some View {
+ GeometryReader { geometry in
+ let frame = FrameRect.aspectFill(
+ contentAspect: Self.verticalAspect,
+ containerWidth: geometry.size.width,
+ containerHeight: geometry.size.height
+ )
+ let shown = SafeZonePlatform.allCases.filter(platforms.contains)
+ ZStack(alignment: .topLeading) {
+ ForEach(Array(shown.enumerated()), id: \.element) { index, platform in
+ ForEach(Array(platform.covered.enumerated()), id: \.offset) { _, region in
+ Rectangle()
+ .fill(.black.opacity(0.22))
+ .overlay(Rectangle().strokeBorder(.white.opacity(0.4), lineWidth: 1))
+ .frame(width: region.width * frame.width, height: region.height * frame.height)
+ .offset(x: frame.x + region.x * frame.width, y: frame.y + region.y * frame.height)
+ }
+ // The name sits at the top of the caption block, one row
+ // per platform, so overlapping shades still read.
+ if let bottom = platform.covered.dropFirst().first {
+ Text(platform.title)
+ .font(.caption2.weight(.semibold))
+ .foregroundStyle(.white.opacity(0.8))
+ .padding(.horizontal, 6)
+ .padding(.vertical, 2)
+ .background(.black.opacity(0.4), in: Capsule())
+ .offset(
+ x: max(frame.x, 0) + 8,
+ y: frame.y + bottom.y * frame.height + 6 + CGFloat(index) * 22
+ )
+ }
+ }
+ }
+ .frame(width: geometry.size.width, height: geometry.size.height, alignment: .topLeading)
+ }
+ .ignoresSafeArea()
+ .allowsHitTesting(false)
+ }
+}
+
struct FillLightOverlay: View {
var body: some View {
Rectangle()
diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
index 7720e9a2..0e5f2927 100644
--- a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
@@ -40,6 +40,31 @@ import Testing
}
}
+@Suite struct SafeZoneTests {
+ @Test func portraitFrameIsWiderThanThePhone() {
+ let frame = FrameRect.aspectFill(contentAspect: 9.0 / 16.0, containerWidth: 390, containerHeight: 844)
+ #expect(frame.height == 844)
+ #expect(abs(frame.width - 474.75) < 0.01)
+ #expect(frame.x < 0)
+ #expect(abs(frame.x * 2 + frame.width - 390) < 0.01)
+ }
+
+ @Test func landscapeFrameIsTallerThanTheScreen() {
+ let frame = FrameRect.aspectFill(contentAspect: 16.0 / 9.0, containerWidth: 844, containerHeight: 390)
+ #expect(frame.width == 844)
+ #expect(frame.y < 0)
+ }
+
+ @Test func everyPlatformKeepsRegionsInsideTheFrame() {
+ for platform in SafeZonePlatform.allCases {
+ for region in platform.covered {
+ #expect(region.x >= 0 && region.x + region.width <= 1)
+ #expect(region.y >= 0 && region.y + region.height <= 1)
+ }
+ }
+ }
+}
+
@Suite struct ZoomTests {
@Test func tripleCameraOffersUltraWide() {
let mapping = ZoomMapping(wideBase: 2, minDisplay: 0.5, maxDisplay: 8)
diff --git a/docs/guides/ios-app.md b/docs/guides/ios-app.md
index 1abffce7..eae4fa40 100644
--- a/docs/guides/ios-app.md
+++ b/docs/guides/ios-app.md
@@ -131,6 +131,11 @@ viewfinder.
The camera shoots whichever way the phone is held: turn it sideways and the
take is a landscape file, with the shutter across on the trailing edge.
+A rail button shades the parts of a vertical frame that TikTok, Reels or
+Shorts cover with their own chrome, placed against the recorded frame rather
+than the screen, kept across sessions, and drawn under the script so a take
+is framed against them too.
+
## The Watch
The watch app is a remote for the phone's camera: a record button, a red