PulseCanvas / app / Sources / PulseCanvas / VisualizerPoolControls.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | /// One shared choice pool for the strip, Library, Studio and randomizers. |
| 4 | struct VisualizerPoolButton: View { |
| 5 | @ObservedObject private var library = ProjectMPresets.shared |
| 6 | var compact = false |
| 7 | @State private var showing = false |
| 8 | var body: some View { |
| 9 | Button { showing = true } label: { |
| 10 | Label(library.pool.enabled ? "Pool · \(library.eligibleScenes.count)" : "Visualizer pool", |
| 11 | systemImage: library.pool.enabled ? "line.3.horizontal.decrease.circle.fill" : "line.3.horizontal.decrease.circle") |
| 12 | } |
| 13 | #if os(macOS) |
| 14 | .buttonStyle(StudioButtonStyle(treatment: library.pool.enabled ? .accent : .secondary, compact: compact)) |
| 15 | #else |
| 16 | .buttonStyle(.bordered) |
| 17 | #endif |
| 18 | .accessibilityLabel("Visualizer pool") |
| 19 | .accessibilityValue(library.pool.enabled ? library.poolSummary : "Off; all visualizers") |
| 20 | .help("Choose categories or favorites for browsing, Studio and randomizers") |
| 21 | .popover(isPresented: $showing) { VisualizerPoolSettings { showing = false } } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /// The selection is shared by the editor, Studio randomizers and Radio. |
| 26 | struct VisualizerRandomizerPicker: View { |
| 27 | @ObservedObject private var library = ProjectMPresets.shared |
| 28 | var compact = false |
| 29 | var fillsWidth = false |
| 30 | @State private var showSettings = false |
| 31 | var body: some View { |
| 32 | Menu { |
| 33 | Picker("Randomize visualizers", selection: Binding(get: { library.randomizerGroup }, set: { library.setRandomizerGroup($0) })) { |
| 34 | Text("Randomize all").tag(VisualizerRandomizerGroup.all) |
| 35 | Text("Randomize favorites").tag(VisualizerRandomizerGroup.favorites) |
| 36 | Divider() |
| 37 | ForEach(library.categories, id: \.self) { category in |
| 38 | Text("Randomize \(Bundle.main.localizedString(forKey: category, value: category, table: nil))").tag(VisualizerRandomizerGroup.category(category)) |
| 39 | } |
| 40 | if library.randomizerGroup == .custom { |
| 41 | Text("Randomize \(library.poolSummary)").tag(VisualizerRandomizerGroup.custom) |
| 42 | } |
| 43 | }.labelsHidden().pickerStyle(.inline) |
| 44 | Divider() |
| 45 | Button("Customize visualizer pool…") { showSettings = true } |
| 46 | } label: { |
| 47 | HStack(spacing: 10) { |
| 48 | Text(library.randomizerGroup == .custom ? String(localized: "Randomize \(library.poolSummary)") : library.randomizerGroup.title) |
| 49 | .lineLimit(fillsWidth ? 2 : 1) |
| 50 | if fillsWidth { Spacer(minLength: 0) } |
| 51 | Image(systemName: "chevron.down").font(.caption.weight(.semibold)) |
| 52 | } |
| 53 | #if os(iOS) |
| 54 | .font(.body.weight(.semibold)) |
| 55 | .padding(.horizontal, 16).frame(minHeight: 52) |
| 56 | .frame(maxWidth: fillsWidth ? .infinity : nil, alignment: .leading) |
| 57 | .background(Color.white.opacity(0.08), in: RoundedRectangle(cornerRadius: 14)) |
| 58 | .overlay(RoundedRectangle(cornerRadius: 14).strokeBorder(Color.white.opacity(0.16))) |
| 59 | .contentShape(RoundedRectangle(cornerRadius: 14)) |
| 60 | #endif |
| 61 | } |
| 62 | #if os(macOS) |
| 63 | .menuStyle(.borderlessButton).menuIndicator(.hidden) |
| 64 | .buttonStyle(StudioButtonStyle(treatment: library.pool.enabled ? .accent : .secondary, compact: compact)) |
| 65 | #elseif os(iOS) |
| 66 | .buttonStyle(.plain) |
| 67 | #else |
| 68 | .buttonStyle(.bordered) |
| 69 | #endif |
| 70 | .fixedSize(horizontal: !fillsWidth, vertical: false) |
| 71 | .frame(maxWidth: fillsWidth ? .infinity : nil, alignment: .leading) |
| 72 | .accessibilityLabel("Randomize visualizer group") |
| 73 | .accessibilityValue(library.randomizerGroup.title) |
| 74 | .help("Choose all visualizers, favorites, or a category for browsing, Studio and Radio. New choices get fresh looks; favorites restore their saved settings.") |
| 75 | .popover(isPresented: $showSettings) { VisualizerPoolSettings { showSettings = false } } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private struct VisualizerPoolSettings: View { |
| 80 | @ObservedObject private var library = ProjectMPresets.shared |
| 81 | let close: () -> Void |
| 82 | @State private var draft = VisualizerPool() |
| 83 | private let accent = Color(red: 0.95, green: 0.68, blue: 0.55) |
| 84 | var body: some View { |
| 85 | VStack(alignment: .leading, spacing: 18) { |
| 86 | Label("Visualizer pool", systemImage: "line.3.horizontal.decrease.circle") |
| 87 | .font(.title2.bold()) |
| 88 | Text("Choose the visualizers available throughout the app.") |
| 89 | .foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) |
| 90 | Toggle("Limit visualizers", isOn: $draft.enabled) |
| 91 | #if os(macOS) |
| 92 | .toggleStyle(StudioSwitchStyle()) |
| 93 | #else |
| 94 | .toggleStyle(.switch) |
| 95 | #endif |
| 96 | VStack(spacing: 14) { |
| 97 | choice("Collection", value: $draft.collection, options: library.collections, all: "All collections") |
| 98 | choice("Category", value: $draft.category, options: library.categories(in: draft.collection), all: "All categories") |
| 99 | Toggle(isOn: $draft.favoritesOnly) { Label("Favorites only", systemImage: "heart.fill") } |
| 100 | #if os(macOS) |
| 101 | .toggleStyle(StudioSwitchStyle()) |
| 102 | #else |
| 103 | .toggleStyle(.switch) |
| 104 | #endif |
| 105 | }.disabled(!draft.enabled).opacity(draft.enabled ? 1 : 0.5) |
| 106 | let count = library.scenes(in: draft).count |
| 107 | Label("\(count.formatted()) matching visualizers", systemImage: count == 0 ? "exclamationmark.circle" : "checkmark.circle") |
| 108 | .foregroundStyle(count == 0 ? Color.orange : accent) |
| 109 | if count == 0 { |
| 110 | Text("Choose another category, add favorites, or turn the limit off. Randomizers stay idle when the pool is empty.") |
| 111 | .font(.callout).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) |
| 112 | } |
| 113 | Text("Applies to the visualizer strip, Library, Studio pickers and randomizers. Saved scenes keep their assignments until you replace or randomize them. Small pools repeat to fill a timeline.") |
| 114 | .font(.caption).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true) |
| 115 | HStack { |
| 116 | Button("Cancel", action: close) |
| 117 | Spacer() |
| 118 | Button(draft.enabled ? "Use this pool" : "Show all visualizers") { library.setPool(draft); close() } |
| 119 | #if os(macOS) |
| 120 | .buttonStyle(StudioButtonStyle(treatment: .primary)) |
| 121 | #else |
| 122 | .buttonStyle(.borderedProminent) |
| 123 | #endif |
| 124 | } |
| 125 | #if os(macOS) |
| 126 | .buttonStyle(StudioButtonStyle()) |
| 127 | #endif |
| 128 | }.padding(24).frame(width: 420).tint(accent) |
| 129 | .onAppear { draft = library.pool } |
| 130 | .onChange(of: draft.collection) { _, _ in draft.category = "" } |
| 131 | } |
| 132 | private func choice(_ title: LocalizedStringKey, value: Binding<String>, options: [String], all: LocalizedStringKey) -> some View { |
| 133 | HStack { |
| 134 | Text(title).frame(width: 85, alignment: .leading) |
| 135 | Picker(title, selection: value) { |
| 136 | Text(all).tag("") |
| 137 | ForEach(options, id: \.self) { Text(LocalizedStringKey($0)).tag($0) } |
| 138 | }.labelsHidden().frame(maxWidth: .infinity).controlSize(.large) |
| 139 | } |
| 140 | } |
| 141 | } |