9e09eb284a
Introduce a single shareable document type for rigs and boards: - ShoeGazeDocument: versioned JSON envelope wrapping either a RigArchive (one or many rigs) or a BoardVersion, with a tolerant decoder that also reads the legacy bare-RigArchive and bare-ShoeGazeState shapes. - Register the .shoegaze UTType (net.realizedsound.shoegaze, conforms to public.json) in all four demo app Info.plists. - Migrate library export/import onto the envelope: exportDocumentData() and a decoder-tolerant importArchive(); RigFileDocument and the Builder exporter/importer now use .shoegaze. Internal per-rig and board-version files stay app-private JSON. - Add OS share-sheet entries via SwiftUI ShareLink: Share per rig in the library sidebar, Share per version in the board editor, backed by the Transferable wrappers RigShareItem / BoardShareItem. Tests: ShoeGazeDocument round-trip and legacy decode, library .shoegaze export/import round-trip, and a board-document-imports-no-rigs case. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
309 lines
12 KiB
Swift
309 lines
12 KiB
Swift
// BoardEditorView.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import SwiftUI
|
|
import LyrebirdUI
|
|
import LyrebirdShoeGaze
|
|
|
|
/// An interactive editor for a single `PedalBoard`.
|
|
///
|
|
/// `BoardEditorView` binds to `ShoeGaze` (an `@Observable` class) and reads the live
|
|
/// board via `shoeGaze.boards[boardIndex]`. Because `ShoeGaze.boards` is an `@Observable`
|
|
/// tracked property, loading a version — which calls `shoeGaze.rebuildBoard(_:from:)` and
|
|
/// mutates `boards` in-place at the same index — triggers an automatic SwiftUI re-render
|
|
/// with the new board and its new pedal list without any manual refresh.
|
|
///
|
|
/// ## Board-load re-binding
|
|
///
|
|
/// `rebuildBoard(_:from:)` tears down the old board, calls `addBoard()`, and moves the
|
|
/// result back to `oldIndex`. The `boards` array is mutated on the main actor, so
|
|
/// `shoeGaze.boards[boardIndex]` — read fresh in `body` — always resolves to the current
|
|
/// board after a load. `BoardEditorView` never caches the `PedalBoard` reference itself;
|
|
/// the index is the stable anchor.
|
|
///
|
|
/// ## Usage
|
|
///
|
|
/// ```swift
|
|
/// BoardEditorView(
|
|
/// shoeGaze: shoeGaze,
|
|
/// boardIndex: 0,
|
|
/// versionStore: versionStore
|
|
/// )
|
|
/// ```
|
|
///
|
|
/// - Parameters:
|
|
/// - shoeGaze: The `ShoeGaze` manager that owns the board.
|
|
/// - boardIndex: Index into `shoeGaze.boards`. If out of range, a small placeholder
|
|
/// is shown instead.
|
|
/// - versionStore: The `BoardVersionStore` that manages named snapshots.
|
|
public struct BoardEditorView: View {
|
|
|
|
// MARK: - Layout constants
|
|
|
|
private static let outerPadding: CGFloat = 16
|
|
private static let headerSpacing: CGFloat = 10
|
|
private static let sectionSpacing: CGFloat = 14
|
|
private static let boardBackgroundRed: Double = 0.12
|
|
private static let boardBackgroundGreen: Double = 0.10
|
|
private static let boardBackgroundBlue: Double = 0.08
|
|
private static let boardCornerRadius: CGFloat = 10
|
|
private static let boardBorderBrightness: Double = 0.30
|
|
private static let boardBorderOpacity: Double = 0.50
|
|
private static let boardBorderLineWidth: CGFloat = 1
|
|
private static let boardPedalAreaPadding: CGFloat = 16
|
|
private static let labelFontSize: CGFloat = 17
|
|
private static let placeholderOpacity: Double = 0.55
|
|
private static let versionMenuMaxWidth: CGFloat = 280
|
|
private static let versionRowSpacing: CGFloat = 6
|
|
private static let versionDateOpacity: Double = 0.55
|
|
private static let versionDateFontSize: CGFloat = 11
|
|
private static let toolbarButtonSpacing: CGFloat = 8
|
|
private static let textFieldMinWidth: CGFloat = 160
|
|
private static let dividerOpacity: Double = 0.20
|
|
|
|
// MARK: - Properties
|
|
|
|
private let shoeGaze: ShoeGaze
|
|
private let boardIndex: Int
|
|
private let versionStore: BoardVersionStore
|
|
|
|
/// The text typed into the version-name field before saving.
|
|
@State private var newVersionName: String = ""
|
|
|
|
// MARK: - Date formatting
|
|
|
|
/// Short date + time formatter used on version menu rows.
|
|
private static let savedAtStyle: Date.FormatStyle = Date.FormatStyle()
|
|
.month(.abbreviated)
|
|
.day(.defaultDigits)
|
|
.year(.defaultDigits)
|
|
.hour(.defaultDigits(amPM: .abbreviated))
|
|
.minute(.twoDigits)
|
|
|
|
// MARK: - Init
|
|
|
|
/// Creates a board editor view.
|
|
///
|
|
/// - Parameters:
|
|
/// - shoeGaze: The `ShoeGaze` manager that owns the board at `boardIndex`.
|
|
/// - boardIndex: Index into `shoeGaze.boards`. Renders a placeholder when out of range.
|
|
/// - versionStore: The `BoardVersionStore` that manages named snapshots for this board.
|
|
public init(
|
|
shoeGaze: ShoeGaze,
|
|
boardIndex: Int,
|
|
versionStore: BoardVersionStore
|
|
) {
|
|
self.shoeGaze = shoeGaze
|
|
self.boardIndex = boardIndex
|
|
self.versionStore = versionStore
|
|
}
|
|
|
|
// MARK: - Body
|
|
|
|
public var body: some View {
|
|
if boardIndex < 0 || boardIndex >= shoeGaze.boards.count {
|
|
outOfRangePlaceholder
|
|
} else {
|
|
let board: PedalBoard = shoeGaze.boards[boardIndex]
|
|
editorContent(board: board)
|
|
}
|
|
}
|
|
|
|
// MARK: - Main editor layout
|
|
|
|
/// The primary editor layout when `boardIndex` is in range.
|
|
private func editorContent(board: PedalBoard) -> some View {
|
|
VStack(alignment: .leading, spacing: BoardEditorView.sectionSpacing) {
|
|
header(board: board)
|
|
Divider()
|
|
.overlay(Color.white.opacity(BoardEditorView.dividerOpacity))
|
|
pedalArea(board: board)
|
|
}
|
|
.padding(BoardEditorView.outerPadding)
|
|
}
|
|
|
|
// MARK: - Header
|
|
|
|
/// Toolbar row: board label on the left, version controls on the right.
|
|
private func header(board: PedalBoard) -> some View {
|
|
VStack(alignment: .leading, spacing: BoardEditorView.headerSpacing) {
|
|
HStack(spacing: BoardEditorView.headerSpacing) {
|
|
Text(board.label)
|
|
.font(.system(size: BoardEditorView.labelFontSize, weight: .semibold))
|
|
.foregroundStyle(Color.white)
|
|
Spacer()
|
|
diskButtons
|
|
}
|
|
HStack(spacing: BoardEditorView.toolbarButtonSpacing) {
|
|
versionNameField(board: board)
|
|
versionsMenu
|
|
}
|
|
}
|
|
}
|
|
|
|
/// "Save to Disk" and "Load from Disk" buttons. Cross-platform; no file picker used.
|
|
private var diskButtons: some View {
|
|
HStack(spacing: BoardEditorView.toolbarButtonSpacing) {
|
|
Button("Save to Disk") {
|
|
try? versionStore.writeAllToDisk(
|
|
in: BoardVersionStore.defaultDirectory()
|
|
)
|
|
}
|
|
.buttonStyle(.bordered)
|
|
|
|
Button("Load from Disk") {
|
|
try? versionStore.loadAllFromDisk(
|
|
in: BoardVersionStore.defaultDirectory()
|
|
)
|
|
}
|
|
.buttonStyle(.bordered)
|
|
}
|
|
}
|
|
|
|
/// Version-name `TextField` + "Save Version" button.
|
|
private func versionNameField(board: PedalBoard) -> some View {
|
|
HStack(spacing: BoardEditorView.toolbarButtonSpacing) {
|
|
TextField("Version name…", text: $newVersionName)
|
|
.textFieldStyle(.roundedBorder)
|
|
.frame(minWidth: BoardEditorView.textFieldMinWidth)
|
|
|
|
Button("Save Version") {
|
|
let effectiveName: String = newVersionName.isEmpty
|
|
? "Version \(versionStore.versions.count + 1)"
|
|
: newVersionName
|
|
versionStore.capture(board, name: effectiveName)
|
|
newVersionName = ""
|
|
}
|
|
.buttonStyle(.borderedProminent)
|
|
.tint(.orange)
|
|
}
|
|
}
|
|
|
|
/// A `Menu` listing all saved versions.
|
|
///
|
|
/// Each version is represented by a nested `Menu` whose title is the version name.
|
|
/// Opening a version's sub-menu exposes two reliably-reachable actions:
|
|
/// - **Load** — calls `shoeGaze.rebuildBoard(_:from:)`, which tears down the old board,
|
|
/// builds a fresh one from the saved `BoardState`, and moves it back to `boardIndex`.
|
|
/// Because `ShoeGaze.boards` is `@Observable`, every view reading `shoeGaze.boards`
|
|
/// re-renders automatically with the replacement board.
|
|
/// - **Delete** — removes the version from memory AND from disk so that the deletion
|
|
/// survives the next `loadAllFromDisk` call.
|
|
private var versionsMenu: some View {
|
|
Menu {
|
|
if versionStore.versions.isEmpty {
|
|
Text("No saved versions")
|
|
} else {
|
|
ForEach(versionStore.versions) { version in
|
|
versionSubmenu(version: version)
|
|
}
|
|
}
|
|
} label: {
|
|
Label("Versions", systemImage: "clock.arrow.circlepath")
|
|
}
|
|
.menuStyle(.automatic)
|
|
.frame(maxWidth: BoardEditorView.versionMenuMaxWidth, alignment: .leading)
|
|
}
|
|
|
|
/// A nested `Menu` for a single version with Load and Delete actions.
|
|
///
|
|
/// Using a sub-menu rather than an `HStack` inside a single menu row ensures that
|
|
/// both buttons are independently hittable on every platform.
|
|
private func versionSubmenu(version: BoardVersion) -> some View {
|
|
let dateString: String = version.savedAt.formatted(BoardEditorView.savedAtStyle)
|
|
return Menu {
|
|
Button("Load") {
|
|
loadVersion(version)
|
|
}
|
|
// Encoding a fully-concrete BoardVersion to JSON does not fail in practice; on the
|
|
// unreachable failure path the Share item is simply omitted.
|
|
if let shareItem: BoardShareItem = try? BoardShareItem(board: version) {
|
|
ShareLink(
|
|
item: shareItem,
|
|
preview: SharePreview(version.name)
|
|
) {
|
|
Label("Share…", systemImage: "square.and.arrow.up")
|
|
}
|
|
}
|
|
Button("Delete", role: .destructive) {
|
|
versionStore.remove(version)
|
|
try? versionStore.deleteFromDisk(version, in: BoardVersionStore.defaultDirectory())
|
|
}
|
|
} label: {
|
|
VStack(alignment: .leading, spacing: BoardEditorView.versionRowSpacing) {
|
|
Text(version.name)
|
|
Text(dateString)
|
|
.font(.system(size: BoardEditorView.versionDateFontSize))
|
|
.foregroundStyle(Color.secondary.opacity(BoardEditorView.versionDateOpacity))
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Pedal area
|
|
|
|
/// Scrollable wrap-to-fit pedal area backed by `FlowLayout`.
|
|
private func pedalArea(board: PedalBoard) -> some View {
|
|
ScrollView(.vertical, showsIndicators: true) {
|
|
FlowLayout(
|
|
horizontalSpacing: FlowLayout.defaultHorizontalSpacing,
|
|
verticalSpacing: FlowLayout.defaultVerticalSpacing
|
|
) {
|
|
ForEach(Array(board.pedals.enumerated()), id: \.offset) { pair in
|
|
let pedal: Pedal = pair.element
|
|
PedalView(pedal: pedal)
|
|
}
|
|
}
|
|
.padding(BoardEditorView.boardPedalAreaPadding)
|
|
}
|
|
.background(
|
|
RoundedRectangle(cornerRadius: BoardEditorView.boardCornerRadius)
|
|
.fill(
|
|
Color(
|
|
red: BoardEditorView.boardBackgroundRed,
|
|
green: BoardEditorView.boardBackgroundGreen,
|
|
blue: BoardEditorView.boardBackgroundBlue
|
|
)
|
|
)
|
|
.overlay(
|
|
RoundedRectangle(cornerRadius: BoardEditorView.boardCornerRadius)
|
|
.stroke(
|
|
Color(white: BoardEditorView.boardBorderBrightness)
|
|
.opacity(BoardEditorView.boardBorderOpacity),
|
|
lineWidth: BoardEditorView.boardBorderLineWidth
|
|
)
|
|
)
|
|
)
|
|
}
|
|
|
|
// MARK: - Out-of-range placeholder
|
|
|
|
/// Shown when `boardIndex` is outside `shoeGaze.boards`.
|
|
private var outOfRangePlaceholder: some View {
|
|
Text("No board at index \(boardIndex).")
|
|
.foregroundStyle(Color.secondary.opacity(BoardEditorView.placeholderOpacity))
|
|
.padding(BoardEditorView.outerPadding)
|
|
}
|
|
|
|
// MARK: - Version loading
|
|
|
|
/// Loads `version` back into the board at `boardIndex`.
|
|
///
|
|
/// `rebuildBoard(_:from:)` tears down the old board, allocates a new one populated
|
|
/// from `version.state`, finalises it, and moves it back to `boardIndex`. Because
|
|
/// `ShoeGaze` is `@Observable` and `boards` is a tracked property, the mutation
|
|
/// causes this view to re-render immediately with the replacement board — no manual
|
|
/// state refresh needed.
|
|
private func loadVersion(_ version: BoardVersion) {
|
|
guard boardIndex >= 0, boardIndex < shoeGaze.boards.count else { return }
|
|
let board: PedalBoard = shoeGaze.boards[boardIndex]
|
|
do {
|
|
_ = try shoeGaze.rebuildBoard(board, from: version.state)
|
|
} catch {
|
|
print("[BoardEditorView] rebuildBoard failed: \(error)")
|
|
}
|
|
}
|
|
}
|