Branchwell

PulseCanvas / app / mobile / MobileWorkspace.swift

1import SwiftUI
2import UniformTypeIdentifiers
3import UIKit
4
5private enum MobileSheet: String, Identifiable {
6 case library, style, export, about, apply, music
7 var id: String { rawValue }
8}
9
10enum MobileTheme {
11 static let background = Color(red: 0.12, green: 0.10, blue: 0.14)
12 static let panel = Color(red: 0.19, green: 0.16, blue: 0.21)
13 static let accent = Color(red: 0.95, green: 0.68, blue: 0.55)
14}
15
16struct MobileButton: ButtonStyle {
17 var prominent = false
18 func makeBody(configuration: Configuration) -> some View {
19 configuration.label.font(.subheadline.weight(.semibold))
20 .padding(.horizontal, 14).frame(minHeight: 44)
21 .foregroundStyle(prominent ? MobileTheme.background : .white)
22 .background(prominent ? MobileTheme.accent : MobileTheme.panel, in: RoundedRectangle(cornerRadius: 13))
23 .overlay(RoundedRectangle(cornerRadius: 13).strokeBorder(.white.opacity(prominent ? 0 : 0.1)))
24 .contentShape(RoundedRectangle(cornerRadius: 13))
25 .opacity(configuration.isPressed ? 0.7 : 1)
26 }
27}
28
29struct MobileWorkspace: View {
30 @ObservedObject var editor: MobileEditor
31 @ObservedObject var studio: StudioStore
32 @ObservedObject var audio: AudioController
33 @ObservedObject var radio: RadioController
34 @ObservedObject private var library = ProjectMPresets.shared
35 @Environment(\.scenePhase) private var phase
36 @State private var sheet: MobileSheet?
37 @State private var audioPicker = false
38 @State private var projectPicker = false
39 @State private var savingProject = false
40 @State private var projectDocument = MobileProjectDocument(data: Data())
41 @State private var randomizePrompt = false
42
43 init(editor: MobileEditor) {
44 self.editor = editor; studio = editor.studio; audio = editor.audio
45 radio = editor.radio
46 }
47 var body: some View {
48 GeometryReader { geometry in
49 // Duo's large phone windows have a corner status cluster, rather than
50 // a full-width status bar. Reuse the clear part of that top region.
51 let cornerHeader = UIDevice.current.userInterfaceIdiom == .phone && geometry.size.width >= 460
52 let controlInset = cornerHeader ? max(116, geometry.safeAreaInsets.trailing) : geometry.safeAreaInsets.trailing
53 VStack(spacing: 0) {
54 header(compact: geometry.size.width < 700, edgeInset: geometry.size.width < 600 ? 12 : 16)
55 .padding(.top, cornerHeader ? 16 : 0)
56 .padding(.leading, geometry.safeAreaInsets.leading)
57 .padding(.trailing, controlInset)
58 Divider().overlay(.white.opacity(0.08))
59 if editor.selectedTab == 1 {
60 MobileRadio(editor: editor, trailingControlInset: geometry.safeAreaInsets.trailing)
61 } else {
62 HStack(spacing: 0) {
63 if geometry.size.width >= 520 && geometry.size.width > geometry.size.height
64 || geometry.size.width >= 350 && geometry.size.height >= 600 {
65 studioContent(size: geometry.size, bounded: true, trailingControlInset: controlInset)
66 .accessibilityElement(children: .contain)
67 .accessibilityIdentifier("mobileStudioLandscape")
68 } else {
69 ScrollView { studioContent(size: geometry.size, bounded: false, trailingControlInset: controlInset) }
70 }
71 }
72 }
73 }
74 // Media and the bottom timeline use the whole Duo display in either
75 // rotation. Only the header reserves side insets around the camera.
76 .ignoresSafeArea(.container, edges: cornerHeader ? .all : [.leading, .trailing, .bottom])
77 .background(MobileTheme.background).tint(MobileTheme.accent)
78 }
79 .task { await editor.radio.refreshFavoriteSongs() }
80 .onChange(of: phase) { _, value in editor.setForeground(value == .active, inBackground: value == .background) }
81 .sheet(item: $sheet) { item in
82 NavigationStack {
83 Group {
84 switch item {
85 case .library: MobileLibrary(editor: editor, close: { sheet = nil })
86 case .style: MobileStyle(editor: editor)
87 case .export: MobileExport(editor: editor)
88 case .about: MobileAbout(editor: editor)
89 case .apply: MobileApply(editor: editor)
90 case .music:
91 StudioMusicPicker(source: radio.studioMusicSource, folderBookmark: radio.studioMusicFolderBookmark,
92 library: radio.studioMusicLibrary, favoriteIDs: Set(radio.favoriteSongs.map(\.id)), useSong: editor.importLibrarySong, close: { sheet = nil })
93 .toolbar(.hidden, for: .navigationBar)
94 }
95 }.navigationTitle(title(item)).navigationBarTitleDisplayMode(.inline)
96 .toolbar { ToolbarItem(placement: .confirmationAction) { Button("Done") { sheet = nil }.disabled(editor.exporter.isExporting).accessibilityIdentifier("mobileSheetDone") } }
97 .background(MobileTheme.background)
98 }.tint(MobileTheme.accent).preferredColorScheme(.dark)
99 }
100 .fileImporter(isPresented: $audioPicker, allowedContentTypes: [.audio]) { result in
101 switch result {
102 case .success(let url): Task { await editor.importAudio(url) }
103 case .failure(let error): editor.message = error.localizedDescription
104 }
105 }
106 .fileImporter(isPresented: $projectPicker, allowedContentTypes: [.pulseCanvasProject, .json]) { result in
107 switch result {
108 case .success(let url):
109 let access = SecurityScopedAccess(url: url)
110 do { let data = try Data(contentsOf: url); Task { await editor.openProject(data); withExtendedLifetime(access) {} } }
111 catch { editor.message = error.localizedDescription }
112 case .failure(let error): editor.message = error.localizedDescription
113 }
114 }
115 .onOpenURL { url in
116 let access = SecurityScopedAccess(url: url)
117 do {
118 let data = try Data(contentsOf: url)
119 Task { await editor.openProject(data); withExtendedLifetime(access) {} }
120 } catch { editor.message = error.localizedDescription }
121 }
122 .fileExporter(isPresented: $savingProject, document: projectDocument, contentType: .pulseCanvasProject,
123 defaultFilename: "PulseCanvas Studio.pulsecanvas") { result in
124 if case .failure(let error) = result { editor.message = error.localizedDescription }
125 if case .success(let url) = result {
126 do { try UserBackup.rememberProject(projectDocument.data, name: url.path) }
127 catch { editor.message = String(localized: "Project saved, but its backup copy wasn't updated: \(error.localizedDescription)") }
128 }
129 }
130 .alert("PulseCanvas Studio", isPresented: Binding(get: { editor.message != nil }, set: { if !$0 { editor.message = nil } })) {
131 Button("OK") { editor.message = nil }
132 } message: { Text(editor.message ?? "") }
133 .confirmationDialog("Randomize this timeline?", isPresented: $randomizePrompt, titleVisibility: .visible) {
134 Button("Randomize visualizers") { _ = editor.studio.randomizeTimeline(editor.render); editor.saveSession() }
135 Button("Randomize bar lengths") {
136 if let grid = editor.audio.segmentPlan(beatsPerSection: 4) { _ = editor.studio.randomizeBars(grid: grid, render: editor.render); editor.saveSession() }
137 }
138 } message: { Text("This replaces the chosen visualizers or scene lengths throughout your Studio timeline.") }
139 .accessibilityHidden(editor.presenting)
140 .fullScreenCover(isPresented: $editor.presenting) { MobilePresentation(editor: editor) }
141 }
142 @ViewBuilder private func studioContent(size: CGSize, bounded: Bool, trailingControlInset: CGFloat) -> some View {
143 if bounded && size.height < 500 {
144 // The closed display in landscape is too short to stack a preview
145 // above full scene cards. Give each its own column instead.
146 let preview = MobilePreview(editor: editor, fillsAvailableSpace: bounded, trailingControlInset: trailingControlInset, style: { sheet = .style })
147 let edgeInset: CGFloat = size.width < 600 ? 12 : 16
148 VStack(spacing: 6) {
149 preview.controls
150 HStack(spacing: 16) {
151 preview.canvas.frame(maxWidth: .infinity, maxHeight: .infinity)
152 VStack(spacing: 4) {
153 MobileTransport(audio: editor.audio, apply: { sheet = .apply }, randomize: { randomizePrompt = true })
154 timeline(edgeInset: edgeInset)
155 }.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
156 }
157 }.padding(.horizontal, edgeInset).padding(.top, 8).padding(.bottom, 8)
158 } else {
159 VStack(spacing: bounded ? 12 : 16) {
160 MobilePreview(editor: editor, fillsAvailableSpace: bounded, trailingControlInset: trailingControlInset, style: { sheet = .style })
161 .frame(maxHeight: bounded ? .infinity : max(220, size.height * 0.52))
162 MobileTransport(audio: editor.audio, apply: { sheet = .apply }, randomize: { randomizePrompt = true })
163 timeline(edgeInset: size.width < 600 ? 12 : 16)
164 }.padding(.horizontal, size.width < 600 ? 12 : 16)
165 .padding(.top, bounded ? 12 : 20).padding(.bottom, 8)
166 .frame(maxWidth: .infinity, maxHeight: bounded ? .infinity : nil, alignment: .bottom)
167 }
168 }
169 private func title(_ item: MobileSheet) -> LocalizedStringKey {
170 switch item {
171 case .library: "Visualizers"
172 case .style: "Visual style"
173 case .export: "Export video"
174 case .about: "Settings"
175 case .apply: "Apply to all"
176 case .music: "Choose music"
177 }
178 }
179 private var windowControlClearance: CGFloat {
180 guard UIDevice.current.userInterfaceIdiom == .pad else { return 0 }
181 if #available(iOS 26, *) { return 28 }
182 return 0
183 }
184 private func header(compact: Bool, edgeInset: CGFloat) -> some View {
185 HStack(spacing: 8) {
186 Picker("Workspace", selection: $editor.selectedTab) { Text("Studio").tag(0); Text("Radio").tag(1) }
187 .pickerStyle(.segmented).frame(maxWidth: compact ? 175 : 225)
188 .disabled(editor.loading || editor.exporter.isExporting)
189 .onChange(of: editor.selectedTab) { _, tab in if tab == 0 { editor.radio.leave() } else { editor.audio.pause() } }
190 Spacer(minLength: 0)
191 Button { sheet = .library } label: { Image(systemName: "square.grid.2x2").frame(width: 20) }
192 .accessibilityLabel("Open visualizer library").accessibilityIdentifier("mobileLibraryButton")
193 .disabled(editor.loading || editor.exporter.isExporting)
194 Button { editor.presenting = true } label: { Image(systemName: "arrow.up.left.and.arrow.down.right").frame(width: 20) }
195 .accessibilityLabel(editor.selectedTab == 1 ? "Present radio" : "Present visualizer")
196 .accessibilityIdentifier("mobilePresentationButton")
197 .disabled(editor.loading || editor.exporter.isExporting)
198 Menu {
199 Group {
200 Button("Choose music", systemImage: "music.note.list") { sheet = .music }
201 Button("Import audio", systemImage: "waveform.badge.plus") { audioPicker = true }
202 Button("Open project", systemImage: "folder") { projectPicker = true }
203 Button("Save project", systemImage: "internaldrive") {
204 if editor.studio.cloudProjectID == nil { editor.studio.cloudProjectID = UUID().uuidString }
205 do { projectDocument = MobileProjectDocument(data: try editor.projectData()); savingProject = true }
206 catch { editor.message = error.localizedDescription }
207 }.disabled(radio.isActive && radio.isPersonalMusic)
208 Button("Export MP4", systemImage: "film") { sheet = .export }
209 .disabled(audio.selectedURL == nil || radio.isActive && (!radio.canSeek || radio.isPersonalMusic))
210 }.disabled(editor.loading || editor.exporter.isExporting)
211 Divider()
212 Button("Settings", systemImage: "gearshape") { sheet = .about }
213 } label: { Image(systemName: "ellipsis").frame(width: 20) }.accessibilityLabel("Project actions")
214 }.buttonStyle(MobileButton()).padding(.horizontal, edgeInset).padding(.vertical, 10)
215 // Keep the tabs leading, below iPadOS's window-control strip.
216 .padding(.top, windowControlClearance)
217 .accessibilityElement(children: .contain).accessibilityIdentifier("mobileWorkspaceHeader")
218 }
219 private func timeline(edgeInset: CGFloat) -> some View {
220 VStack(alignment: .leading, spacing: 12) {
221 if editor.needsSoundtrack {
222 Button("Locate soundtrack", systemImage: "waveform.badge.plus") { audioPicker = true }.buttonStyle(MobileButton(prominent: true))
223 }
224 ScrollViewReader { proxy in
225 ScrollView(.horizontal) {
226 HStack(spacing: 10) {
227 ForEach(Array(editor.studio.segments.enumerated()), id: \.offset) { index, clip in
228 MobileClip(editor: editor, index: index, clip: clip, choose: { editor.selectClip(index); sheet = .library }).id(index)
229 }
230 }.padding(.vertical, 3).padding(.horizontal, edgeInset)
231 }.frame(height: 154).padding(.horizontal, -edgeInset)
232 .accessibilityIdentifier("mobileStudioTimeline")
233 .onChange(of: studio.selectedSegment) { _, index in
234 guard audio.isPlaying, studio.segments.indices.contains(index) else { return }
235 withAnimation(.easeInOut(duration: 0.25)) { proxy.scrollTo(index, anchor: .center) }
236 }
237 }
238 }
239 }
240}
241
242private struct MobilePreview: View {
243 @ObservedObject var editor: MobileEditor
244 @ObservedObject var render: RenderState
245 @ObservedObject private var library = ProjectMPresets.shared
246 let fillsAvailableSpace: Bool
247 let trailingControlInset: CGFloat
248 let style: () -> Void
249 init(editor: MobileEditor, fillsAvailableSpace: Bool = false, trailingControlInset: CGFloat = 0, style: @escaping () -> Void) {
250 self.editor = editor; self.render = editor.render; self.fillsAvailableSpace = fillsAvailableSpace; self.trailingControlInset = trailingControlInset; self.style = style
251 }
252 var body: some View {
253 VStack(spacing: 10) {
254 controls
255 canvas
256 }
257 }
258 var controls: some View {
259 HStack(spacing: 8) {
260 Button { library.toggleFavorite(render.scene, appearance: VisualPreset(render: render)) } label: {
261 Image(systemName: library.isFavorite(render.scene) ? "heart.fill" : "heart").frame(width: 16)
262 }.accessibilityLabel("Favorite visualizer")
263 .accessibilityValue(library.isFavorite(render.scene) ? "Favorited" : "Not favorited")
264 Text(library.sceneNames[render.scene]).font(.headline).lineLimit(1).accessibilityIdentifier("mobileVisualizerName")
265
266 Spacer(minLength: 0)
267 Button { editor.randomizePreviewVisualizer() } label: { Image(systemName: "shuffle").frame(width: 20) }.accessibilityLabel("Random visualizer")
268 Button { editor.studio.randomize(render); editor.saveSession() } label: { Image(systemName: "dice.fill").frame(width: 20) }
269 .accessibilityLabel("Randomize visual style").accessibilityIdentifier("mobileRandomizeStyleButton")
270 Button(action: style) { Image(systemName: "slider.horizontal.3").frame(width: 20) }.accessibilityLabel("Visual style")
271 }.buttonStyle(MobileButton()).padding(.trailing, trailingControlInset)
272 }
273 var canvas: some View {
274 VStack(spacing: 10) {
275 Group {
276 if fillsAvailableSpace {
277 GeometryReader { geometry in
278 VisualizerView(state: render).frame(width: geometry.size.width, height: geometry.size.height)
279 }.frame(minHeight: 80, maxHeight: .infinity)
280 } else {
281 VisualizerView(state: render).aspectRatio(16 / 9.0, contentMode: .fit)
282 }
283 }.clipShape(RoundedRectangle(cornerRadius: 16))
284 .accessibilityElement(children: .ignore).accessibilityLabel("Studio visualization")
285 .accessibilityValue(library.sceneNames[render.scene])
286 .accessibilityIdentifier("mobileVisualizerPreview")
287 .modifier(MobileVisualizerSwipe { editor.randomizePreviewVisualizer() })
288 .modifier(VisualizerFavoriteGesture(favorite: editor.favoritePreviewVisualizer))
289 if editor.loading {
290 ProgressView("Preparing your soundtrack…").font(.callout)
291 .accessibilityIdentifier("mobileSoundtrackPreparing")
292 }
293 if let error = render.errorMessage { Text(error).foregroundStyle(.red).accessibilityIdentifier("mobileRenderError") }
294 }
295 }
296}
297
298/// Listen alongside scrolling and taps; only a deliberate horizontal gesture
299/// changes the visualizer. The full-screen tap still reveals/hides controls.
300struct MobileVisualizerSwipe: ViewModifier {
301 let changeVisualizer: () -> Void
302 func body(content: Content) -> some View {
303 content.contentShape(Rectangle())
304 .simultaneousGesture(DragGesture(minimumDistance: 24).onEnded { value in
305 let travel = value.translation
306 let projected = value.predictedEndTranslation
307 guard abs(travel.width) >= 24,
308 abs(travel.width) > abs(travel.height) * 1.25,
309 abs(travel.width) >= 44 || (travel.width * projected.width > 0 && abs(projected.width) >= 80) else { return }
310 changeVisualizer()
311 })
312 .accessibilityHint("Swipe left or right to change the visualizer while keeping the current song. Double-tap to favorite the visualizer.")
313 .accessibilityAction(named: Text("Change visualizer"), changeVisualizer)
314 }
315}
316
317struct MobileTransport: View {
318 @ObservedObject var audio: AudioController
319 var apply: (() -> Void)? = nil
320 var randomize: (() -> Void)? = nil
321 @State private var fraction = 0.0
322 @State private var seconds = 0.0
323 @State private var seeking = false
324 var body: some View {
325 VStack(alignment: .leading, spacing: 8) {
326 HStack(spacing: 8) {
327 Text(audio.title).font(.subheadline.weight(.medium)).lineLimit(1)
328 Spacer(minLength: 0)
329 if let apply { Button(action: apply) { Label("Apply to all", systemImage: "square.stack.3d.up").lineLimit(1) }.accessibilityLabel("Apply to all") }
330 if let randomize { Button(action: randomize) { Label("Shuffle scenes", systemImage: "rectangle.3.group").lineLimit(1) }.accessibilityLabel("Randomize timeline") }
331 }.buttonStyle(MobileButton())
332 HStack(spacing: 12) {
333 Button { audio.playPause() } label: { Image(systemName: audio.isPlaying ? "pause.fill" : "play.fill").frame(width: 30) }
334 .buttonStyle(MobileButton(prominent: true)).accessibilityLabel(audio.isPlaying ? "Pause" : "Play").accessibilityIdentifier("mobilePlaybackButton")
335 Text(Self.time(seconds)).monospacedDigit().font(.caption)
336 Slider(value: $fraction, in: 0...1, onEditingChanged: { editing in
337 seeking = editing
338 if !editing { audio.seek(fraction: fraction) }
339 }).accessibilityLabel("Soundtrack position")
340 Text(Self.time(audio.duration)).monospacedDigit().font(.caption)
341 Toggle(isOn: $audio.loopEnabled) { Image(systemName: "repeat") }.toggleStyle(.button).accessibilityLabel("Loop soundtrack")
342 }
343 }.onReceive(audio.visualizationUpdated) { _ in if !seeking { fraction = audio.progress; seconds = audio.playbackSeconds } }
344 }
345 static func time(_ seconds: Double) -> String { let value = Int(max(0, seconds.isFinite ? seconds : 0)); return String(format: "%d:%02d", value / 60, value % 60) }
346}
347
348private struct MobileClip: View {
349 @ObservedObject var editor: MobileEditor
350 @ObservedObject var studio: StudioStore
351 let index: Int
352 let clip: StudioSegment
353 let choose: () -> Void
354 init(editor: MobileEditor, index: Int, clip: StudioSegment, choose: @escaping () -> Void) {
355 self.editor = editor; studio = editor.studio; self.index = index; self.clip = clip; self.choose = choose
356 }
357 var body: some View {
358 ZStack {
359 Button { editor.selectClip(index) } label: { RoundedRectangle(cornerRadius: 15).fill(MobileTheme.panel) }
360 .buttonStyle(.plain).accessibilityLabel("Select scene \(index + 1)")
361 VStack(alignment: .leading, spacing: 12) {
362 HStack {
363 Text("\(MobileTransport.time(clip.start)) – \(MobileTransport.time(clip.end))").font(.caption).allowsHitTesting(false)
364 Spacer()
365 Button { editor.selectClip(index, play: true) } label: { Image(systemName: "play.fill").frame(width: 36, height: 36) }
366 .accessibilityLabel("Play scene \(index + 1)")
367 }
368 Button(action: choose) { HStack { Text(editor.library.sceneNames[clip.scene]).lineLimit(1); Spacer(); Image(systemName: "chevron.down") } }
369 .accessibilityLabel("Choose visualizer for scene \(index + 1)")
370 Stepper("\(studio.barCount(for: clip)) bars", value: Binding(get: { studio.barCount(for: clip) }, set: { editor.setBars($0, at: index) }), in: 1...64)
371 .font(.caption).accessibilityIdentifier("sceneBars-\(index)")
372 }.padding(14)
373 }.frame(width: 260, height: 148)
374 .overlay(RoundedRectangle(cornerRadius: 15).strokeBorder(studio.selectedSegment == index ? MobileTheme.accent : .white.opacity(0.12), lineWidth: 1.5))
375 .accessibilityElement(children: .contain)
376 .accessibilityIdentifier(studio.selectedSegment == index ? "currentStudioClip" : "studioClip-\(index)")
377 }
378}
379
380struct MobileLibrary: View {
381 @ObservedObject var editor: MobileEditor
382 @ObservedObject private var library = ProjectMPresets.shared
383 let close: () -> Void
384 @State private var query = ""
385 @FocusState private var searchFocused: Bool
386 var body: some View {
387 VStack(spacing: 12) {
388 TextField("Search visualizers", text: $query).textFieldStyle(.plain).padding(.horizontal, 12).frame(height: 48)
389 .background(MobileTheme.panel, in: RoundedRectangle(cornerRadius: 12))
390 .focused($searchFocused)
391 .submitLabel(.search).onSubmit { searchFocused = false }
392 .accessibilityIdentifier("modalVisualizerSearch")
393 VisualizerRandomizerPicker(fillsWidth: true)
394 List(library.filteredScenes(query: query, category: "", favoritesOnly: false), id: \.self) { scene in
395 if library.eligibleScenes.contains(scene) {
396 HStack {
397 Button {
398 if editor.radio.isActive { editor.radio.leave(); editor.selectedTab = 0 }
399 searchFocused = false
400 editor.choose(scene); close()
401 } label: {
402 HStack(spacing: 12) {
403 Image(systemName: library.icon(for: scene)).foregroundStyle(MobileTheme.accent).frame(width: 28)
404 Text(library.sceneNames[scene]).lineLimit(1).frame(maxWidth: .infinity, alignment: .leading)
405 }.frame(minHeight: 44).contentShape(Rectangle())
406 }.buttonStyle(.plain)
407 .accessibilityIdentifier("libraryVisualizer-\(scene)")
408 Button { library.toggleFavorite(scene, appearance: editor.studio.presets[scene]) } label: {
409 Image(systemName: library.isFavorite(scene) ? "heart.fill" : "heart").frame(width: 44, height: 44)
410 }.buttonStyle(.borderless).accessibilityLabel("Favorite \(library.sceneNames[scene])")
411 }.listRowBackground(MobileTheme.panel)
412 }
413 }.listStyle(.plain).scrollContentBackground(.hidden)
414 .scrollDismissesKeyboard(.interactively)
415 .contentMargins(.bottom, 0, for: .scrollContent)
416 .frame(maxHeight: .infinity)
417 .accessibilityIdentifier("modalVisualizerList")
418 }.padding(.horizontal, 12).padding(.top, 12)
419 .frame(maxWidth: .infinity, maxHeight: .infinity)
420 .ignoresSafeArea(.container, edges: .bottom)
421 .onDisappear { searchFocused = false }
422 }
423}
424
425struct MobileStyle: View {
426 @ObservedObject var editor: MobileEditor
427 @ObservedObject var render: RenderState
428 init(editor: MobileEditor) { self.editor = editor; render = editor.render }
429 var body: some View {
430 ScrollView {
431 VStack(spacing: 22) {
432 HStack {
433 Text(editor.library.sceneNames[render.scene]).font(.headline).lineLimit(1)
434 Spacer()
435 Button("Reset") { editor.studio.reset(render) }
436 Button { editor.studio.randomize(render) } label: { Image(systemName: "dice.fill").font(.title2) }.accessibilityLabel("Randomize visual style")
437 }.buttonStyle(MobileButton())
438 Picker("Palette", selection: $render.palette) { ForEach(VisualPalette.allCases, id: \.rawValue) { Text($0.title).tag($0.rawValue) } }
439 .pickerStyle(.menu).accessibilityIdentifier("mobilePalettePicker")
440 LazyVGrid(columns: [GridItem(.adaptive(minimum: 230), spacing: 24)], spacing: 24) {
441 control("Chaos", value: $render.chaos, range: 0...1)
442 control("Motion speed", value: $render.speed, range: 0.05...3)
443 control("Zoom speed", value: $render.zoomSpeed, range: 0...3)
444 control("Brightness", value: $render.brightness, range: 0.3...2)
445 control("Element size", value: $render.particleSize, range: 0.2...4)
446 control("Depth", value: $render.fog, range: 0...1)
447 control("Liquid detail", value: $render.liquidDetail, range: 0...1)
448 control("Content blur", value: $render.motionBlur, range: 0...1)
449 }
450 Toggle("Hi-hat strobe", isOn: $render.strobeEnabled).accessibilityIdentifier("mobileStrobeToggle")
451 if render.strobeEnabled {
452 Text("Adds rapid flashes in time with high-frequency percussion.").font(.caption).foregroundStyle(.secondary)
453 control("Strobe sensitivity", value: $render.strobeSensitivity, range: 0.25...4)
454 control("Strobe strength", value: $render.strobeStrength, range: 0.1...1)
455 }
456 }.padding(24)
457 }.accessibilityIdentifier("mobileStyleScroll")
458 .onDisappear { editor.studio.save(render); editor.saveSession() }
459 }
460 private func control(_ title: LocalizedStringKey, value: Binding<Float>, range: ClosedRange<Float>) -> some View {
461 VStack(alignment: .leading, spacing: 8) {
462 HStack { Text(title); Spacer(); Text(value.wrappedValue, format: .number.precision(.fractionLength(2))).foregroundStyle(.secondary).monospacedDigit() }
463 Slider(value: value, in: range).accessibilityLabel(title)
464 }
465 }
466}
467
468struct MobileApply: View {
469 @ObservedObject var editor: MobileEditor
470 @State private var bars = 4
471 @State private var chaos: Float = 0.6
472 @State private var zoom: Float = 1.5
473 @State private var speed: Float = 1
474 @State private var useBars = false
475 @State private var useChaos = false
476 @State private var useZoom = false
477 @State private var useSpeed = false
478 @Environment(\.dismiss) private var dismiss
479 var body: some View {
480 Form {
481 Section("Change selected settings across the timeline") {
482 Toggle("Bars per scene", isOn: $useBars)
483 if useBars { Stepper("\(bars) bars", value: $bars, in: 1...64).accessibilityIdentifier("mobileCommonBars") }
484 Toggle("Chaos", isOn: $useChaos)
485 if useChaos { Slider(value: $chaos, in: 0...1).accessibilityLabel("Common chaos") }
486 Toggle("Zoom speed", isOn: $useZoom)
487 if useZoom { Slider(value: $zoom, in: 0...3).accessibilityLabel("Common zoom speed") }
488 Toggle("Motion speed", isOn: $useSpeed)
489 if useSpeed { Slider(value: $speed, in: 0.05...3).accessibilityLabel("Common motion speed") }
490 }
491 Button("Apply to all scenes") {
492 editor.studio.applyCommonSettings(chaos: useChaos ? chaos : nil, zoomSpeed: useZoom ? zoom : nil, motionSpeed: useSpeed ? speed : nil, render: editor.render)
493 if useBars, let grid = editor.audio.segmentPlan(beatsPerSection: 4) { _ = editor.studio.setBars(bars, grid: grid, render: editor.render) }
494 editor.saveSession(); dismiss()
495 }.disabled(!useBars && !useChaos && !useZoom && !useSpeed)
496 }
497 }
498}