Add macOS New/Save Rig menu items and save-on-switch prompt

- macOS File menu gains New Rig (⌘N) and Save Rig (⌘S), which forward to
  the in-window builder via shoeGazeNewRig / shoeGazeSaveRig notifications
  so the naming prompt and current-rig highlight update correctly.
- Selecting a different rig while the current one has unsaved changes now
  prompts Save / Don't Save / Cancel (both platforms). Dirtiness is
  captureState() != the loaded entry's state; a backing-less working rig
  with boards counts as dirty.
- Generalize saveCurrentRig to take a completion (used by Save & Clear and
  by save-then-switch).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-27 18:04:55 -07:00
parent dc42db0c59
commit e8e134499d
4 changed files with 99 additions and 12 deletions
@@ -0,0 +1,19 @@
// RigLibraryCommands.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
/// Notifications the macOS app shell posts from its menu bar so the in-window
/// ``ShoeGazeBuilderView`` — which owns the current-rig state — performs the action with the
/// correct UI (naming prompts, highlight updates). iOS has no menu bar and simply never posts them.
public extension Notification.Name {
/// Save the current rig to the library (overwrites the loaded entry, or prompts for a name).
static let shoeGazeSaveRig: Notification.Name = Notification.Name("net.realizedsound.shoegaze.saveRig")
/// Create a new, named, empty rig and make it current.
static let shoeGazeNewRig: Notification.Name = Notification.Name("net.realizedsound.shoegaze.newRig")
}
@@ -111,6 +111,11 @@ public struct ShoeGazeBuilderView: View {
/// Confirmation dialog for the "Erase All Rigs" library action.
@State private var showEraseAllDialog: Bool = false
/// Save-before-switch prompt: the rig the user asked to switch to while the current rig has
/// unsaved changes, and the flag presenting the dialog.
@State private var pendingSwitchEntry: RigEntry?
@State private var showSwitchSaveDialog: Bool = false
/// Archive export state. Cross-platform: backs the sidebar's Export Archive and the
/// "Save & Clear" flow is separate (it saves to the library, not a file).
@State private var showExporter: Bool = false
@@ -197,13 +202,37 @@ public struct ShoeGazeBuilderView: View {
Text("This permanently removes every saved rig from the library"
+ (library.isICloud ? " on all your devices." : "."))
}
.confirmationDialog(
"Save changes to the current rig?",
isPresented: $showSwitchSaveDialog,
titleVisibility: .visible,
presenting: pendingSwitchEntry
) { target in
Button("Save") {
pendingSwitchEntry = nil
saveCurrentRig(then: { loadEntry(target) })
}
Button("Don't Save", role: .destructive) {
pendingSwitchEntry = nil
loadEntry(target)
}
Button("Cancel", role: .cancel) { pendingSwitchEntry = nil }
} message: { target in
Text("Switching to \"\(target.name)\" will discard unsaved changes to the current rig.")
}
.onReceive(NotificationCenter.default.publisher(for: .shoeGazeSaveRig)) { _ in
saveCurrentRig()
}
.onReceive(NotificationCenter.default.publisher(for: .shoeGazeNewRig)) { _ in
promptNewRig()
}
.confirmationDialog(
"Clear all boards?",
isPresented: $showClearDialog,
titleVisibility: .visible
) {
Button("Save & Clear") {
saveCurrentRig(thenClear: true)
saveCurrentRig(then: { performClear() })
}
Button("Clear Without Saving", role: .destructive) {
performClear()
@@ -229,7 +258,7 @@ public struct ShoeGazeBuilderView: View {
RigLibrarySidebarView(
store: library,
currentRigID: $currentRigID,
onSelect: { entry in loadEntry(entry) },
onSelect: { entry in requestLoad(entry) },
onNew: { promptNewRig() },
onSave: { saveCurrentRig() },
onSaveAs: { promptSaveAs() },
@@ -700,16 +729,38 @@ public struct ShoeGazeBuilderView: View {
transportTick.toggle()
}
/// Switches to `entry`, prompting to save first when the current rig has unsaved changes.
/// No-op when `entry` is already the current rig.
private func requestLoad(_ entry: RigEntry) {
guard entry.id != currentRigID else { return }
if isCurrentRigDirty() {
pendingSwitchEntry = entry
showSwitchSaveDialog = true
} else {
loadEntry(entry)
}
}
/// Whether the working rig differs from the library entry it was loaded from. A working rig with
/// no backing entry counts as dirty when it has any boards.
private func isCurrentRigDirty() -> Bool {
guard let id: UUID = currentRigID,
let entry: RigEntry = library.entries.first(where: { $0.id == id }) else {
return !shoeGaze.boards.isEmpty
}
return shoeGaze.captureState() != entry.state
}
/// Saves the current session over the loaded library entry, or prompts for a name when no entry
/// is loaded (a new, never-saved rig).
///
/// - Parameter thenClear: When `true`, clears the boards after a successful save (the
/// "Save & Clear" flow). Cancelling the name prompt cancels the clear too — no save, no clear.
private func saveCurrentRig(thenClear: Bool = false) {
let completion: () -> Void = thenClear ? { performClear() } : { }
/// - Parameter completion: Runs only after a successful save (e.g. clear the boards, or load the
/// rig the user was switching to). Cancelling the name prompt cancels it too — no save, no
/// follow-up.
private func saveCurrentRig(then completion: @escaping () -> Void = { }) {
guard let id: UUID = currentRigID,
let existing: RigEntry = library.entries.first(where: { $0.id == id }) else {
// No current entry — prompt for a name; the clear (if any) runs only on commit.
// No current entry — prompt for a name; the follow-up runs only on commit.
namePrompt = RigNamePrompt(
title: "Save Rig As",
initialText: currentRigName() ?? defaultNewRigName(),