diff --git a/Sources/LyrebirdShoeGaze/Presets/Factory/FactoryRigLibrary.swift b/Sources/LyrebirdShoeGaze/Presets/Factory/FactoryRigLibrary.swift index 1d1bfed..eaee27a 100644 --- a/Sources/LyrebirdShoeGaze/Presets/Factory/FactoryRigLibrary.swift +++ b/Sources/LyrebirdShoeGaze/Presets/Factory/FactoryRigLibrary.swift @@ -85,6 +85,22 @@ public enum FactoryRigLibrary { factoryIDs.contains(id) } + /// The ``FactoryRigCategory`` of the shipped rig with this id, or `nil` for a user rig. + /// + /// - Parameter id: A ``RigEntry/id`` from the rig library. + /// - Returns: The rig's category, or `nil` if `id` is not a factory rig. + public static func category(for id: UUID) -> FactoryRigCategory? { + all.first(where: { $0.id == id })?.category + } + + /// The shipped rigs of a given category, in catalog order. + /// + /// - Parameter category: The category to filter by. + /// - Returns: The `FactoryRig`s in `category`, in the order they appear in ``all``. + public static func rigs(in category: FactoryRigCategory) -> [FactoryRig] { + all.filter { $0.category == category } + } + /// Every built-in rig, in the order a picker should present them. public static let all: [FactoryRig] = [ glideWall, diff --git a/Sources/LyrebirdShoeGazeUI/RigLibrarySidebarView.swift b/Sources/LyrebirdShoeGazeUI/RigLibrarySidebarView.swift index 653f2af..392b267 100644 --- a/Sources/LyrebirdShoeGazeUI/RigLibrarySidebarView.swift +++ b/Sources/LyrebirdShoeGazeUI/RigLibrarySidebarView.swift @@ -116,17 +116,48 @@ struct RigLibrarySidebarView: View { .foregroundStyle(Color.white.opacity(RigLibrarySidebarView.emptyOpacity)) .padding(.vertical, RigLibrarySidebarView.rowPadding) } - ForEach(store.entries) { entry in - rigRow(entry) + + // Shipped starter rigs, grouped by category (fixed catalog order, not reorderable). + ForEach(factorySections, id: \.category) { section in + sectionHeader(section.category.displayName) + ForEach(section.entries) { entry in + rigRow(entry, reorderable: false) + } + } + + // The user's own rigs, reorderable. Only headed when starter rigs are also shown, so + // a library with no factory rigs (e.g. after Erase All) reads as a plain list. + let userRigs: [RigEntry] = userEntries + if !userRigs.isEmpty { + if !factorySections.isEmpty { + sectionHeader("My Rigs") + } + ForEach(userRigs) { entry in + rigRow(entry, reorderable: true) + } } } } .frame(maxHeight: .infinity) } - private func rigRow(_ entry: RigEntry) -> some View { + /// A small uppercase section header for a group of rigs. + private func sectionHeader(_ title: String) -> some View { + Text(title.uppercased()) + .font(.caption2) + .fontWeight(.semibold) + .foregroundStyle(Color.white.opacity(RigLibrarySidebarView.headerOpacity)) + .padding(.top, RigLibrarySidebarView.rowPadding) + .padding(.horizontal, 2) + .frame(maxWidth: .infinity, alignment: .leading) + } + + /// One rig row. `reorderable` rows (the user's own rigs) accept drag-to-reorder and expose + /// Move Up / Down; starter rigs are fixed and show a "Starter rig" subtitle instead of a date. + @ViewBuilder + private func rigRow(_ entry: RigEntry, reorderable: Bool) -> some View { let isSelected: Bool = entry.id == currentRigID - return Button { + let button = Button { onSelect(entry) } label: { VStack(alignment: .leading, spacing: 2) { @@ -134,7 +165,9 @@ struct RigLibrarySidebarView: View { .font(.subheadline) .foregroundStyle(Color.white) .lineLimit(1) - Text(entry.savedAt.formatted(RigLibrarySidebarView.timestampFormat)) + Text(reorderable + ? entry.savedAt.formatted(RigLibrarySidebarView.timestampFormat) + : "Starter rig") .font(.caption2) .foregroundStyle(Color.white.opacity(RigLibrarySidebarView.subtitleOpacity)) .lineLimit(1) @@ -149,45 +182,81 @@ struct RigLibrarySidebarView: View { .contentShape(Rectangle()) } .buttonStyle(.plain) - // Drag the row onto another to reorder; dropping inserts it just before the target. Works - // on macOS (mouse drag) and iPad (long-press drag); taps still select. - .draggable(entry.id.uuidString) - .dropDestination(for: String.self) { droppedIDs, _ in - guard let first: String = droppedIDs.first else { return false } - moveDragged(first, onto: entry) - return true - } .contextMenu { - Button("Move Up") { store.moveUp(entry) } - .disabled(isFirst(entry)) - Button("Move Down") { store.moveDown(entry) } - .disabled(isLast(entry)) - Divider() - Button("Rename…") { onRename(entry) } - Button("Duplicate…") { onDuplicate(entry) } - // Encoding a fully-concrete RigEntry to JSON does not fail in practice; on the - // unreachable failure path the Share item is simply omitted. - if let shareItem: RigShareItem = try? RigShareItem(rig: entry) { - ShareLink( - item: shareItem, - preview: SharePreview(entry.name) - ) { - Label("Share…", systemImage: "square.and.arrow.up") - } - } - Divider() - Button("Delete", role: .destructive) { onDelete(entry) } + rowContextMenu(entry, reorderable: reorderable) } + + if reorderable { + // Drag the row onto another to reorder; dropping inserts it just before the target. Works + // on macOS (mouse drag) and iPad (long-press drag); taps still select. + button + .draggable(entry.id.uuidString) + .dropDestination(for: String.self) { droppedIDs, _ in + guard let first: String = droppedIDs.first else { return false } + moveDragged(first, onto: entry) + return true + } + } else { + button + } + } + + /// The context menu for a rig row. Move Up / Down appear only for reorderable (user) rigs. + @ViewBuilder + private func rowContextMenu(_ entry: RigEntry, reorderable: Bool) -> some View { + if reorderable { + Button("Move Up") { store.moveUp(entry) } + .disabled(isFirstUser(entry)) + Button("Move Down") { store.moveDown(entry) } + .disabled(isLastUser(entry)) + Divider() + } + Button("Rename…") { onRename(entry) } + Button("Duplicate…") { onDuplicate(entry) } + // Encoding a fully-concrete RigEntry to JSON does not fail in practice; on the + // unreachable failure path the Share item is simply omitted. + if let shareItem: RigShareItem = try? RigShareItem(rig: entry) { + ShareLink( + item: shareItem, + preview: SharePreview(entry.name) + ) { + Label("Share…", systemImage: "square.and.arrow.up") + } + } + Divider() + Button("Delete", role: .destructive) { onDelete(entry) } + } + + // MARK: - Grouping + + /// The starter-rig sections: each non-empty ``FactoryRigCategory`` present in the library, in + /// catalog order, paired with its entries (also in catalog order). + private var factorySections: [(category: FactoryRigCategory, entries: [RigEntry])] { + let byID: [UUID: RigEntry] = Dictionary( + store.entries.map { ($0.id, $0) }, + uniquingKeysWith: { first, _ in first } + ) + return FactoryRigCategory.allCases.compactMap { category in + let entries: [RigEntry] = FactoryRigLibrary.rigs(in: category).compactMap { byID[$0.id] } + return entries.isEmpty ? nil : (category, entries) + } + } + + /// The user's own (non-factory) rigs, in library order. + private var userEntries: [RigEntry] { + store.entries.filter { !FactoryRigLibrary.isFactory($0.id) } } // MARK: - Reorder helpers - private func isFirst(_ entry: RigEntry) -> Bool { - store.entries.first?.id == entry.id + /// Whether `entry` is the first among the user's own rigs (so Move Up is disabled for it). + private func isFirstUser(_ entry: RigEntry) -> Bool { + userEntries.first?.id == entry.id } - private func isLast(_ entry: RigEntry) -> Bool { - store.entries.last?.id == entry.id + /// Whether `entry` is the last among the user's own rigs (so Move Down is disabled for it). + private func isLastUser(_ entry: RigEntry) -> Bool { + userEntries.last?.id == entry.id } /// Moves the dragged entry (by id string) to sit just before `target` in the set list. diff --git a/Tests/LyrebirdShoeGazeTests/FactoryRigLibraryTests.swift b/Tests/LyrebirdShoeGazeTests/FactoryRigLibraryTests.swift index d816962..e23ffe4 100644 --- a/Tests/LyrebirdShoeGazeTests/FactoryRigLibraryTests.swift +++ b/Tests/LyrebirdShoeGazeTests/FactoryRigLibraryTests.swift @@ -61,6 +61,24 @@ struct FactoryRigLibraryTests { #expect(!FactoryRigLibrary.isFactory(UUID())) } + @Test func categoryLookupAndGroupingCoverEveryRigOnce() { + for rig: FactoryRig in FactoryRigLibrary.all { + #expect(FactoryRigLibrary.category(for: rig.id) == rig.category, + "\(rig.name) category lookup mismatch") + } + #expect(FactoryRigLibrary.category(for: UUID()) == nil, "a random id has no category") + + // Each category grouping contains only its own rigs, in catalog order. + for category: FactoryRigCategory in FactoryRigCategory.allCases { + let rigs: [FactoryRig] = FactoryRigLibrary.rigs(in: category) + #expect(rigs.allSatisfy { $0.category == category }) + } + + // Grouping by every category partitions the catalog exactly (no rig lost or duplicated). + let grouped: [FactoryRig] = FactoryRigCategory.allCases.flatMap { FactoryRigLibrary.rigs(in: $0) } + #expect(grouped.count == FactoryRigLibrary.all.count, "grouping dropped or duplicated a rig") + } + // MARK: - Seeding entries @Test func makeEntriesMatchesCatalogAndIsStable() throws {