Skip to content

Open source CapCut alternative.

TypeScript

fix(ios): every player draws its own controls

David Lebe6564d9c987
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift
index f798c24b..167f43f2 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift
@@ -538,6 +538,8 @@ struct InspirationCard: View {
var ideas: IdeasModel
let onOpen: () -> Void
+ @State private var confirmingDelete = false
+
/// The shape a card holds while nothing has measured its media yet.
static let defaultRatio = 9.0 / 14
@@ -548,6 +550,18 @@ struct InspirationCard: View {
InspirationPoster(item: item, ideas: ideas)
.contentShape(.rect)
.onTapGesture(perform: onOpen)
+ .contextMenu {
+ InspirationActions(item: item, shareURL: item.link) { confirmingDelete = true }
+ }
+ .confirmationDialog(
+ "Delete this?",
+ isPresented: $confirmingDelete,
+ titleVisibility: .visible
+ ) {
+ Button("Delete", role: .destructive) { ideas.deleteInspiration(id: item.id) }
+ } message: {
+ Text("It goes from this phone and from your cloud library.")
+ }
} else if let link = item.link {
LinkCard(url: link, state: item.importState, text: item.sourceText) {
ideas.retryInspiration(id: item.id)
@@ -622,9 +636,30 @@ struct InspirationPoster: View {
}
}
-/// One inspiration item full screen: the video plays, a photo fills the
-/// screen, and the close and share controls ride the same glass chrome the
-/// Library player wears.
+/// The actions an item carries wherever it shows: the card's long press and
+/// the viewer's menu offer the same ones.
+struct InspirationActions: View {
+ let item: InspirationItem
+ let shareURL: URL?
+ let onDelete: () -> Void
+
+ @Environment(\.openURL) private var openURL
+
+ var body: some View {
+ if let shared = shareURL {
+ ShareLink(item: shared) { Label("Share", systemImage: "square.and.arrow.up") }
+ }
+ if let link = item.link {
+ Button { openURL(link) } label: { Label("Open original", systemImage: "safari") }
+ }
+ Divider()
+ Button(role: .destructive, action: onDelete) { Label("Delete", systemImage: "trash") }
+ }
+}
+
+/// One inspiration item full screen: the video plays under chrome this view
+/// draws itself, a photo fills the screen, and close, share, open and delete
+/// ride the same glass the Library player wears.
struct InspirationViewer: View {
let item: InspirationItem
var ideas: IdeasModel
@@ -632,65 +667,23 @@ struct InspirationViewer: View {
@State private var player: AVPlayer?
@State private var url: URL?
@State private var unplayable = false
+ @State private var confirmingDelete = false
@Environment(\.dismiss) private var dismiss
var body: some View {
- ZStack(alignment: .topLeading) {
+ ZStack {
Color.black.ignoresSafeArea()
- if unplayable {
- Text("This one's format won't play on iPhone. Delete the card and paste the link again.")
- .font(.callout)
- .foregroundStyle(.white.opacity(0.8))
- .multilineTextAlignment(.center)
- .padding(32)
- .frame(maxWidth: .infinity, maxHeight: .infinity)
- } else if let player {
- VideoPlayer(player: player)
- .ignoresSafeArea()
- } else if let url, !item.isVideo {
- AsyncImage(url: url) { image in
- image.resizable().scaledToFit()
- } placeholder: {
- ProgressView().tint(.white)
- }
- .ignoresSafeArea()
- } else {
- ProgressView().tint(.white)
- .frame(maxWidth: .infinity, maxHeight: .infinity)
- }
- if let text = item.sourceText, !text.isEmpty {
- ScrollView {
- Text(text)
- .font(.footnote)
- .foregroundStyle(.white)
- .frame(maxWidth: .infinity, alignment: .leading)
- .padding(16)
- }
- .frame(maxWidth: .infinity, maxHeight: 160, alignment: .bottom)
- .background(.black.opacity(0.55))
- .frame(maxHeight: .infinity, alignment: .bottom)
- .ignoresSafeArea(edges: .horizontal)
- }
- GlassEffectContainer {
- HStack(spacing: 10) {
- Button { dismiss() } label: {
- Image(systemName: "xmark")
- .font(.title3.weight(.bold))
- .frame(width: 40, height: 40)
- }
- .glassEffect(.regular.interactive())
- Spacer()
- if let shared = item.link ?? url {
- ShareLink(item: shared) {
- Image(systemName: "square.and.arrow.up")
- .font(.title3.weight(.bold))
- .frame(width: 40, height: 40)
- }
- .glassEffect(.regular.interactive())
- }
- }
+ media
+ chrome
+ }
+ .statusBarHidden()
+ .confirmationDialog("Delete this?", isPresented: $confirmingDelete, titleVisibility: .visible) {
+ Button("Delete", role: .destructive) {
+ ideas.deleteInspiration(id: item.id)
+ dismiss()
}
- .padding(16)
+ } message: {
+ Text("It goes from this phone and from your cloud library.")
}
.task {
guard let url = await source() else { return }
@@ -713,6 +706,75 @@ struct InspirationViewer: View {
.onDisappear { player?.pause() }
}
+ @ViewBuilder private var media: some View {
+ if unplayable {
+ Text("This one's format won't play on iPhone. Delete the card and paste the link again.")
+ .font(.callout)
+ .foregroundStyle(.white.opacity(0.8))
+ .multilineTextAlignment(.center)
+ .padding(32)
+ } else if let player {
+ PlayerSurface(player: player)
+ .ignoresSafeArea()
+ .onTapGesture {
+ if player.timeControlStatus == .paused { player.play() } else { player.pause() }
+ }
+ } else if let url, !item.isVideo {
+ AsyncImage(url: url) { image in
+ image.resizable().scaledToFit()
+ } placeholder: {
+ ProgressView().tint(.white)
+ }
+ .ignoresSafeArea()
+ } else {
+ ProgressView().tint(.white)
+ }
+ }
+
+ private var chrome: some View {
+ VStack(spacing: 0) {
+ GlassEffectContainer {
+ HStack(spacing: 10) {
+ Button { dismiss() } label: {
+ Image(systemName: "xmark")
+ .font(.title3.weight(.bold))
+ .frame(width: 40, height: 40)
+ }
+ .glassEffect(.regular.interactive())
+ Spacer()
+ Menu {
+ InspirationActions(item: item, shareURL: item.link ?? url) {
+ confirmingDelete = true
+ }
+ } label: {
+ Image(systemName: "ellipsis")
+ .font(.title3.weight(.bold))
+ .frame(width: 40, height: 40)
+ }
+ .glassEffect(.regular.interactive())
+ }
+ }
+ .padding(16)
+
+ Spacer(minLength: 0)
+
+ if let text = item.sourceText, !text.isEmpty {
+ ScrollView {
+ Text(text)
+ .font(.footnote)
+ .foregroundStyle(.white)
+ .frame(maxWidth: .infinity, alignment: .leading)
+ .padding(16)
+ }
+ .frame(maxHeight: 160)
+ .background(.black.opacity(0.55))
+ }
+ if let player {
+ PlaybackBar(player: player)
+ }
+ }
+ }
+
/// Where the bytes are: on this phone for an import, on the account's
/// shelf for a link the cloud fetched.
private func source() async -> URL? {
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/MediaScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/MediaScreen.swift
index 42d0626b..e60cf0a2 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/MediaScreen.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/MediaScreen.swift
@@ -277,8 +277,15 @@ struct RecordingPlayerView: View {
ZStack(alignment: .topLeading) {
Color.black.ignoresSafeArea()
if let player {
- VideoPlayer(player: player)
+ PlayerSurface(player: player)
.ignoresSafeArea()
+ .onTapGesture {
+ if player.timeControlStatus == .paused { player.play() } else { player.pause() }
+ }
+ VStack(spacing: 0) {
+ Spacer(minLength: 0)
+ PlaybackBar(player: player)
+ }
}
GlassEffectContainer {
HStack(spacing: 10) {
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/ProjectsScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/ProjectsScreen.swift
index 08d22862..9e88abf1 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/ProjectsScreen.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/ProjectsScreen.swift
@@ -162,7 +162,6 @@ struct ProjectPlayerView: View {
Color.black.ignoresSafeArea()
video
chrome
- exportControl
}
.task { await load() }
.onDisappear { player?.pause() }
@@ -186,14 +185,12 @@ struct ProjectPlayerView: View {
@ViewBuilder
private var video: some View {
if let player {
- VideoPlayer(player: player)
+ PlayerSurface(player: player)
.ignoresSafeArea()
- // The player's own controls answer the same tap; watching it
- // alongside them keeps this chrome on their clock.
- .simultaneousGesture(TapGesture().onEnded {
+ .onTapGesture {
chromeShown.toggle()
chromeTick += 1
- })
+ }
} else if failed {
VStack(spacing: 14) {
Text("Couldn't load this project's video")
@@ -215,10 +212,34 @@ struct ProjectPlayerView: View {
}
}
- /// The close button and the project's name, on the same row as the
- /// player's own controls — which own the corner, so the row stops short
- /// of it.
+ /// Every control the player has: the close button and the project's name
+ /// on top, the export control beside them, and the transport along the
+ /// bottom. They fade together.
private var chrome: some View {
+ VStack(spacing: 0) {
+ topRow
+ Spacer(minLength: 0)
+ if let player {
+ PlaybackBar(player: player)
+ }
+ }
+ .opacity(chromeShown ? 1 : 0)
+ .allowsHitTesting(chromeShown)
+ .animation(.easeInOut(duration: 0.2), value: chromeShown)
+ // Fades out the way a video player's controls do, and stays while the
+ // video is paused.
+ .task(id: chromeTick) {
+ while !Task.isCancelled {
+ try? await Task.sleep(for: .seconds(4))
+ guard !Task.isCancelled else { return }
+ guard player?.timeControlStatus == .playing else { continue }
+ chromeShown = false
+ return
+ }
+ }
+ }
+
+ private var topRow: some View {
HStack(alignment: .top, spacing: 12) {
Button {
dismiss()
@@ -234,35 +255,16 @@ struct ProjectPlayerView: View {
.foregroundStyle(.white)
.lineLimit(1)
Spacer(minLength: 8)
+ exportControl
}
.padding(16)
- .padding(.trailing, 52)
- .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
- .opacity(chromeShown ? 1 : 0)
- .allowsHitTesting(chromeShown)
- .animation(.easeInOut(duration: 0.2), value: chromeShown)
- // Fades out the way the player's controls do, and stays while the
- // video is paused.
- .task(id: chromeTick) {
- while !Task.isCancelled {
- try? await Task.sleep(for: .seconds(4))
- guard !Task.isCancelled else { return }
- guard player?.timeControlStatus == .playing else { continue }
- chromeShown = false
- return
- }
- }
}
- /// The export control, directly under the player's sound button and lined
- /// up with it: same corner, same inset, same round glass, and on screen
- /// exactly when the sound button is. A render in flight shows its progress
- /// on the button itself, so what it is doing reads without a second panel.
+ /// The export control, in the top row's trailing corner. A render in
+ /// flight shows its progress on the button itself, so what it is doing
+ /// reads without a second panel.
private var exportControl: some View {
VStack(alignment: .trailing, spacing: 8) {
- // The player's own top row owns this much of the corner; the
- // control sits under it.
- Color.clear.frame(width: 40, height: 40)
Button {
showingExport = true
} label: {
@@ -300,11 +302,8 @@ struct ProjectPlayerView: View {
.frame(maxWidth: 190, alignment: .trailing)
}
}
- .padding(16)
- .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topTrailing)
- .opacity(chromeShown && player != nil ? 1 : 0)
- .allowsHitTesting(chromeShown && player != nil)
- .animation(.easeInOut(duration: 0.2), value: chromeShown)
+ .opacity(player == nil ? 0 : 1)
+ .allowsHitTesting(player != nil)
.animation(.snappy, value: phase)
}
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/VideoSurface.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/VideoSurface.swift
new file mode 100644
index 00000000..5f1b68bc
--- /dev/null
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/VideoSurface.swift
@@ -0,0 +1,87 @@
+#if os(iOS)
+import AVFoundation
+import SwiftUI
+
+// The video surface every full-screen player in the app shares. AVKit's
+// VideoPlayer draws its own buttons in the top corners and its own scrubber
+// along the bottom, which land on top of the chrome each screen puts there;
+// these two carry the picture and the transport, and the screen owns the rest.
+
+/// The picture alone.
+struct PlayerSurface: UIViewRepresentable {
+ let player: AVPlayer
+
+ func makeUIView(context: Context) -> PlayerView {
+ let view = PlayerView()
+ view.playerLayer.videoGravity = .resizeAspect
+ view.playerLayer.player = player
+ return view
+ }
+
+ func updateUIView(_ view: PlayerView, context: Context) {
+ if view.playerLayer.player !== player { view.playerLayer.player = player }
+ }
+
+ final class PlayerView: UIView {
+ override public class var layerClass: AnyClass { AVPlayerLayer.self }
+ var playerLayer: AVPlayerLayer { layer as! AVPlayerLayer }
+ }
+}
+
+/// Play, position, and how much is left, in the viewer's own hand.
+struct PlaybackBar: View {
+ let player: AVPlayer
+
+ @State private var time = 0.0
+ @State private var duration = 0.0
+ @State private var scrubbing = false
+ @State private var playing = true
+
+ var body: some View {
+ HStack(spacing: 12) {
+ Button {
+ if playing { player.pause() } else { player.play() }
+ } label: {
+ Image(systemName: playing ? "pause.fill" : "play.fill")
+ .font(.body.weight(.bold))
+ .frame(width: 24, height: 24)
+ }
+ Text(clock(time))
+ Slider(value: $time, in: 0...max(duration, 0.1)) { editing in
+ scrubbing = editing
+ if !editing {
+ player.seek(
+ to: CMTime(seconds: time, preferredTimescale: 600),
+ toleranceBefore: .zero,
+ toleranceAfter: .zero
+ )
+ }
+ }
+ Text("−" + clock(max(duration - time, 0)))
+ }
+ .font(.caption.monospacedDigit())
+ .foregroundStyle(.white)
+ .tint(.white)
+ .padding(.horizontal, 16)
+ .padding(.vertical, 10)
+ .background(.black.opacity(0.55))
+ .task {
+ if let asset = player.currentItem?.asset,
+ let length = try? await asset.load(.duration).seconds, length.isFinite {
+ duration = length
+ }
+ while !Task.isCancelled {
+ let now = player.currentTime().seconds
+ if !scrubbing, now.isFinite { time = now }
+ playing = player.timeControlStatus != .paused
+ try? await Task.sleep(for: .milliseconds(200))
+ }
+ }
+ }
+
+ private func clock(_ seconds: Double) -> String {
+ let whole = Int(seconds.isFinite ? seconds : 0)
+ return String(format: "%d:%02d", whole / 60, whole % 60)
+ }
+}
+#endif
diff --git a/docs/guides/ios-app.md b/docs/guides/ios-app.md
index fdd2cd25..8ddc8bd1 100644
--- a/docs/guides/ios-app.md
+++ b/docs/guides/ios-app.md
@@ -32,7 +32,9 @@ words stays a link and keeps what it said.
An inspiration card is the media itself, the way a Library clip and a project
card are: a poster tile that opens full screen on a tap, with the source's own
-words alongside it there. A card wears its media's own shape — the worker
+words alongside it there. The same actions sit under a long press on the card
+and behind the viewer's menu — share it, open the original where it came from,
+or delete it from the phone and the cloud shelf together. A card wears its media's own shape — the worker
probes what it fetched and sends the pixel size along, an import is measured
as it lands — so a reel stands tall beside a landscape clip, and the grid
deals cards into whichever of its two columns is shorter. A card always says where its link stands: waiting to be handed over,
@@ -74,9 +76,13 @@ cloud behind it, every time the app comes forward and whichever tab is
showing. Signing out drops it, so the next account never opens on someone
else's projects.
-The phone exports the whole cut. A control under the player's own sound
-button, on screen exactly when that button is, opens a sheet of the sizes the
-editor's export dialog offers. Picking one queues a cloud render of the
+Every full-screen player in the app draws its own controls: the picture rides
+a plain player layer, and each screen puts its close button, its actions and
+its transport bar where it wants them. The system player places buttons in
+those same corners itself, which is why none of these screens use one.
+
+The phone exports the whole cut. A control in the player's top row opens a
+sheet of the sizes the editor's export dialog offers. Picking one queues a cloud render of the
project's timeline: the worker opens the stored document and builds the render
spec itself, so what comes back is the file the editor's own export produces —
overlays, captions, soundtrack and all — rather than whatever happened to be