PulseCanvas / app / Sources / PulseCanvas / VisualizerBrowser.swift
| 1 | import SwiftUI |
| 2 | import AppKit |
| 3 | import UniformTypeIdentifiers |
| 4 | |
| 5 | @MainActor |
| 6 | struct VisualizerBrowser: View { |
| 7 | @ObservedObject private var library = ProjectMPresets.shared |
| 8 | let selected: Int |
| 9 | var heading: String? = nil |
| 10 | var appearance: ((Int) -> VisualPreset)? = nil |
| 11 | let choose: (Int) -> Void |
| 12 | @State private var query = "" |
| 13 | @State private var collection = "" |
| 14 | @State private var category = "" |
| 15 | @State private var folder = "" |
| 16 | @State private var favoritesOnly = false |
| 17 | @State private var backupMessage = "" |
| 18 | @State private var backupFailed = false |
| 19 | @FocusState private var searchFocused: Bool |
| 20 | private let accent = Color(red: 0.95, green: 0.68, blue: 0.55) |
| 21 | private var matches: [Int] { |
| 22 | library.filteredScenes(query: query, collection: collection, category: category, |
| 23 | folder: folder, favoritesOnly: favoritesOnly) |
| 24 | } |
| 25 | private var folders: [String] { |
| 26 | library.folders(in: category, collection: collection) |
| 27 | } |
| 28 | var body: some View { |
| 29 | VStack(alignment: .leading, spacing: 12) { |
| 30 | HStack { |
| 31 | Text(heading ?? "Library").font(.headline) |
| 32 | Spacer() |
| 33 | VisualizerPoolButton(compact: true) |
| 34 | Text("\(matches.count) effects").font(.caption).foregroundStyle(.secondary) |
| 35 | } |
| 36 | HStack(spacing: 8) { |
| 37 | HStack(spacing: 10) { |
| 38 | Image(systemName: "magnifyingglass").foregroundStyle(.secondary) |
| 39 | .accessibilityHidden(true) |
| 40 | TextField("Search visualizers", text: $query) |
| 41 | .textFieldStyle(.plain).font(.system(size: 15)) |
| 42 | .focused($searchFocused).accessibilityLabel("Search visualizers") |
| 43 | }.padding(.horizontal, 12).frame(height: 44) |
| 44 | .background(.white.opacity(0.055), in: RoundedRectangle(cornerRadius: 11)) |
| 45 | .overlay(RoundedRectangle(cornerRadius: 11).strokeBorder(searchFocused ? accent : .white.opacity(0.13), lineWidth: searchFocused ? 2 : 1)) |
| 46 | .contentShape(RoundedRectangle(cornerRadius: 11)) |
| 47 | .onTapGesture { searchFocused = true } |
| 48 | Button {favoritesOnly.toggle()} label: { |
| 49 | Label("Favorites", systemImage: favoritesOnly ? "heart.fill" : "heart") |
| 50 | }.buttonStyle(StudioButtonStyle(treatment: favoritesOnly ? .accent : .secondary, compact: true)) |
| 51 | .accessibilityLabel(favoritesOnly ? "Show all effects" : "Show favorite effects") |
| 52 | } |
| 53 | HStack { |
| 54 | filterMenu("Collection", selection: $collection, options: library.collections, emptyTitle: "All collections") |
| 55 | filterMenu("Category", selection: $category, options: library.categories(in: collection), emptyTitle: "All categories") |
| 56 | } |
| 57 | if !folders.isEmpty { |
| 58 | HStack { |
| 59 | filterMenu("Folder", selection: $folder, options: folders, emptyTitle: "All folders", prefix: category + "/") |
| 60 | } |
| 61 | } |
| 62 | ScrollView { |
| 63 | LazyVStack(spacing: 4) { |
| 64 | ForEach(matches, id: \.self) { scene in |
| 65 | HStack(spacing: 0) { |
| 66 | Button { choose(scene) } label: { |
| 67 | HStack(spacing: 10) { |
| 68 | Image(systemName: library.icon(for:scene)).font(.system(size:18, weight:.light)).frame(width: 28, height:32).foregroundStyle(.orange.opacity(0.85)) |
| 69 | VStack(alignment: .leading, spacing: 3) { |
| 70 | Text(RenderState.sceneNames[scene]).font(.system(size: 12, weight: .medium)).lineLimit(1) |
| 71 | Text(library.preset(for: scene)?.category ?? "Native Metal · \(library.category(for: scene))").font(.system(size: 10)).foregroundStyle(.secondary).lineLimit(1) |
| 72 | } |
| 73 | Spacer() |
| 74 | if selected == scene { Image(systemName: "checkmark").foregroundStyle(.orange) } |
| 75 | }.padding(.horizontal, 12).frame(maxWidth: .infinity, minHeight: 52) |
| 76 | .contentShape(Rectangle()) |
| 77 | }.buttonStyle(.plain).accessibilityLabel("Use \(RenderState.sceneNames[scene])") |
| 78 | .help("Select visualizer") |
| 79 | Button {library.toggleFavorite(scene, appearance: appearance?(scene))} label: { |
| 80 | Image(systemName: library.isFavorite(scene) ? "heart.fill" : "heart") |
| 81 | .foregroundStyle(library.isFavorite(scene) ? Color.orange : .secondary) |
| 82 | .frame(width: 44, height: 52).contentShape(Rectangle()) |
| 83 | }.buttonStyle(.plain) |
| 84 | .accessibilityLabel(library.isFavorite(scene) ? "Unfavorite \(RenderState.sceneNames[scene])" : "Favorite \(RenderState.sceneNames[scene])") |
| 85 | }.background(selected == scene ? Color.orange.opacity(0.12) : .white.opacity(0.04), in: RoundedRectangle(cornerRadius: 8)) |
| 86 | .help(library.preset(for:scene)?.originalTitle ?? RenderState.sceneNames[scene]) |
| 87 | } |
| 88 | } |
| 89 | }.frame(height: 430) |
| 90 | if matches.isEmpty {Text(favoritesOnly ? "Tap an effect’s heart to add it to Favorites." : (library.pool.enabled ? "No matches in your visualizer pool. Change the pool or turn its limit off." : "No matching visualizers")).foregroundStyle(.secondary).font(.caption)} |
| 91 | HStack { |
| 92 | Menu { |
| 93 | Button("Export favorites…", systemImage: "square.and.arrow.up", action: exportFavorites) |
| 94 | Button("Import favorites…", systemImage: "square.and.arrow.down", action: importFavorites) |
| 95 | if let directory = library.favoritesBackupDirectory { |
| 96 | Divider() |
| 97 | Button("Show automatic backups") { NSWorkspace.shared.open(directory) } |
| 98 | } |
| 99 | } label: { |
| 100 | Label("Back up favorites", systemImage: "heart.circle") |
| 101 | }.menuStyle(.borderlessButton).fixedSize() |
| 102 | .help("Export or restore favorites across builds and Macs. Imports keep your existing favorites.") |
| 103 | Spacer() |
| 104 | Button {if let scene = matches.randomElement() {choose(scene)}} label: {Label("Surprise me", systemImage: "shuffle")} |
| 105 | .buttonStyle(StudioButtonStyle(compact: true)).disabled(matches.isEmpty) |
| 106 | .help("Choose randomly from the current search, category, or favorites") |
| 107 | } |
| 108 | if !backupMessage.isEmpty || library.favoritesBackupError != nil { |
| 109 | Text(library.favoritesBackupError ?? backupMessage).font(.caption) |
| 110 | .foregroundStyle(backupFailed || library.favoritesBackupError != nil ? Color.red : .secondary) |
| 111 | .fixedSize(horizontal: false, vertical: true) |
| 112 | } |
| 113 | }.padding(18).frame(width: 680) |
| 114 | .onChange(of: category) {_,_ in folder = ""} |
| 115 | .onChange(of: collection) {_,_ in category = ""; folder = ""} |
| 116 | } |
| 117 | |
| 118 | private func exportFavorites() { |
| 119 | let panel = NSSavePanel() |
| 120 | panel.allowedContentTypes = [.json] |
| 121 | panel.nameFieldStringValue = "PulseCanvas-Favorites.json" |
| 122 | panel.title = "Back up favorites" |
| 123 | panel.message = String(localized: "Save a portable backup to restore in another build or on another Mac.") |
| 124 | present(panel) { response in |
| 125 | guard response == .OK, let url = panel.url else { return } |
| 126 | do { |
| 127 | try library.exportFavorites().write(to: url, options: .atomic) |
| 128 | backupFailed = false; backupMessage = String(localized: "Favorites backup saved.") |
| 129 | } catch { backupFailed = true; backupMessage = String(localized: "Couldn't save favorites: \(error.localizedDescription)") } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private func importFavorites() { |
| 134 | let panel = NSOpenPanel() |
| 135 | panel.allowedContentTypes = [.json] |
| 136 | panel.allowsMultipleSelection = false; panel.canChooseDirectories = false |
| 137 | panel.title = "Restore favorites" |
| 138 | panel.message = String(localized: "Import a PulseCanvas favorites backup. Your current favorites will be kept.") |
| 139 | present(panel) { response in |
| 140 | guard response == .OK, let url = panel.url else { return } |
| 141 | let scoped = url.startAccessingSecurityScopedResource() |
| 142 | defer { if scoped { url.stopAccessingSecurityScopedResource() } } |
| 143 | do { |
| 144 | let restored = try library.importFavorites(Data(contentsOf: url)) |
| 145 | backupFailed = false |
| 146 | backupMessage = String(localized: "Added \(restored.added) favorites.") |
| 147 | if restored.unavailable > 0 { backupMessage += " Skipped \(restored.unavailable) effects that aren't in this edition’s gallery." } |
| 148 | } catch { backupFailed = true; backupMessage = String(localized: "Couldn't import favorites: \(error.localizedDescription)") } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | private func present(_ panel: NSSavePanel, completion: @escaping (NSApplication.ModalResponse) -> Void) { |
| 153 | if let window = NSApp.keyWindow { panel.beginSheetModal(for: window, completionHandler: completion) } |
| 154 | else { panel.begin(completionHandler: completion) } |
| 155 | } |
| 156 | |
| 157 | private func filterMenu(_ title: LocalizedStringKey, selection: Binding<String>, options: [String], emptyTitle: LocalizedStringKey, prefix: String = "") -> some View { |
| 158 | HStack(spacing: 8) { |
| 159 | Text(title).font(.system(size: 13)) |
| 160 | Menu { |
| 161 | Button { selection.wrappedValue = "" } label: { |
| 162 | if selection.wrappedValue.isEmpty { Label(emptyTitle, systemImage: "checkmark") } |
| 163 | else { Text(emptyTitle) } |
| 164 | } |
| 165 | ForEach(options, id: \.self) { option in |
| 166 | Button { selection.wrappedValue = option } label: { |
| 167 | if selection.wrappedValue == option { Label(LocalizedStringKey(option.replacingOccurrences(of: prefix, with: "")), systemImage: "checkmark") } |
| 168 | else { Text(LocalizedStringKey(option.replacingOccurrences(of: prefix, with: ""))) } |
| 169 | } |
| 170 | } |
| 171 | } label: { |
| 172 | HStack(spacing: 10) { |
| 173 | Group { |
| 174 | if selection.wrappedValue.isEmpty { Text(emptyTitle) } |
| 175 | else { Text(LocalizedStringKey(selection.wrappedValue.replacingOccurrences(of: prefix, with: ""))) } |
| 176 | } |
| 177 | .font(.system(size: 14)).lineLimit(1) |
| 178 | Spacer(minLength: 0) |
| 179 | Image(systemName: "chevron.down").font(.system(size: 11, weight: .semibold)).foregroundStyle(.secondary) |
| 180 | .accessibilityHidden(true) |
| 181 | }.contentShape(Rectangle()) |
| 182 | }.menuStyle(.button).menuIndicator(.hidden) |
| 183 | .buttonStyle(StudioButtonStyle(compact: true)) |
| 184 | .accessibilityLabel(Text(title)) |
| 185 | .accessibilityValue(Text(selection.wrappedValue.isEmpty ? emptyTitle : LocalizedStringKey(selection.wrappedValue.replacingOccurrences(of: prefix, with: "")))) |
| 186 | } |
| 187 | } |
| 188 | } |