diff --git a/Demo/ShoeGazeBuilder/AppDelegate.swift b/Demo/ShoeGazeBuilder/AppDelegate.swift index b8ec574..3cb2c2d 100644 --- a/Demo/ShoeGazeBuilder/AppDelegate.swift +++ b/Demo/ShoeGazeBuilder/AppDelegate.swift @@ -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] diff --git a/Sources/LyrebirdShoeGazeUI/RigLibraryCommands.swift b/Sources/LyrebirdShoeGazeUI/RigLibraryCommands.swift new file mode 100644 index 0000000..a8a7c7a --- /dev/null +++ b/Sources/LyrebirdShoeGazeUI/RigLibraryCommands.swift @@ -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") +} diff --git a/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift b/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift index 47d2358..b699dbd 100644 --- a/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift +++ b/Sources/LyrebirdShoeGazeUI/ShoeGazeBuilderView.swift @@ -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(), diff --git a/docs/plans/1.2-rig-library.md b/docs/plans/1.2-rig-library.md index c68b62e..09cde92 100644 --- a/docs/plans/1.2-rig-library.md +++ b/docs/plans/1.2-rig-library.md @@ -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.