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
+15 -2
View File
@@ -198,8 +198,12 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
mainMenu.addItem(fileMenuItem)
let fileMenu: NSMenu = NSMenu(title: "File")
fileMenuItem.submenu = fileMenu
// The rig library (saved rigs) lives in the in-window sidebar; the File menu handles the
// whole-library archive: a single file holding every rig, for backup or transfer.
// Per-rig commands forward to the in-window builder (which owns the current-rig state and
// the naming prompts) via notifications.
addItem(to: fileMenu, title: "New Rig", action: #selector(newRigAction(_:)), key: "n")
addItem(to: fileMenu, title: "Save Rig", action: #selector(saveRigAction(_:)), key: "s")
fileMenu.addItem(NSMenuItem.separator())
// The whole-library archive: a single file holding every rig, for backup or transfer.
addItem(to: fileMenu, title: "Export Archive…", action: #selector(exportArchiveAction(_:)), key: "e")
addItem(to: fileMenu, title: "Import Archive…", action: #selector(importArchiveAction(_:)), key: "i")
@@ -250,6 +254,15 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
// MARK: - Menu actions
@objc private func newRigAction(_ sender: Any?) {
// Forward to the builder view, which owns the current-rig state and naming prompt.
NotificationCenter.default.post(name: .shoeGazeNewRig, object: nil)
}
@objc private func saveRigAction(_ sender: Any?) {
NotificationCenter.default.post(name: .shoeGazeSaveRig, object: nil)
}
@objc private func exportArchiveAction(_ sender: Any?) {
let panel: NSSavePanel = NSSavePanel()
panel.allowedContentTypes = [.json]
@@ -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(),
+7 -3
View File
@@ -27,12 +27,16 @@ session (`ShoeGazeState`) and may contain multiple boards.
- `RigLibraryBootstrap` — one-call shell setup: resolve location, start the store, seed it from the
working rig on first run, resolve the launch highlight.
- `ShoeGazeBuilderView` gains the sidebar; selecting a rig **replaces** the current session
(engine stopped/restarted around the rebuild). The old single-rig Save/Load is gone; the File
menu / sidebar now do whole-library **Archive** import/export.
(engine stopped/restarted around the rebuild). When the current rig has unsaved changes
(`captureState()` differs from its library entry), switching first prompts **Save /
Don't Save / Cancel** (both platforms). The old single-rig Save/Load is gone; the File menu /
sidebar now do whole-library **Archive** import/export.
**Shells / entitlements**
- macOS `AppDelegate` and iOS app model own + start the store and pass it to the view; macOS File
menu = Export/Import Archive.
menu = New Rig (⌘N), Save Rig (⌘S), Export/Import Archive (⌘E/⌘I). The per-rig menu items forward
to the builder view via `Notification.Name` (`shoeGazeNewRig` / `shoeGazeSaveRig`) so naming
prompts and the current-rig highlight stay in the view that owns that state.
- Both builder targets carry the iCloud entitlement for container
`iCloud.net.realizedsound.ShoeGazeData`; `NSUbiquitousContainers` exposes `Documents/Rigs` in the
Files app.