Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Hosting/CommandCenterHost.swift3// Description: The Universal Command Center of the window: the shared SPNotesFeatures command center over the one command registry, fed with this app's catalogue (rebuilt for the window in front each time it opens) and the library's own commands; it also finds notes (⌘O).4import SwiftUI5import SPNotesCore6import SPNotesSync7import SPNotesDesign8import SPNotesFeatures910/// The single command registry of the application (recents are kept in the preferences).11@MainActor final class FeatureServices {12 let registry = CommandRegistry(recents: UserDefaultsCommandRecents(defaults: .standard, key: "commandRecents"))13 private var standardToken: CommandRegistry.Token?1415 /// Replaces the commands of the window in front (they depend on its note, its selection, its destination).16 func registerWindowCommands(env: AppEnvironment, window: WindowModel) {17 registry.unregister(owner: "window")18 _ = registry.register(AppCommandCatalog.commands(env: env, window: window).map(Self.feature), owner: "window")19 }20 /// What the library can do by itself (open each database, synchronise…), once.21 func registerStandard(env: AppEnvironment, window: WindowModel) async {22 guard standardToken == nil, env.account.phase != .local else { return }23 let own = Set(registry.commands.map(\.id))24 standardToken = registry.register(await FeatureCommands.standard(repositories: env.repositories, router: window).filter { !own.contains($0.id) && $0.id != "sync.now" }, owner: "features")25 }26 static func feature(_ c: AppCommand) -> FeatureCommand {27 FeatureCommand(id: c.id, title: c.title, keywords: c.keywords, systemImage: c.symbol, section: c.group.rawValue, shortcut: c.shortcut.isEmpty ? nil : c.shortcut, action: { c.run() })28 }29}3031struct CommandCenter: View {32 @Environment(AppEnvironment.self) private var env33 @Environment(WindowModel.self) private var window34 @State private var model: CommandCenterModel?3536 var body: some View {37 ZStack(alignment: .top) {38 Color.black.opacity(0.22).ignoresSafeArea().onTapGesture { close() }39 if let model {40 CommandCenterView(model: model)41 .frame(width: 640).frame(maxHeight: 520)42 .nvPop(radius: NVRadius.lg).padding(.top, NVSpace.huge + NVSpace.lg)43 }44 }45 .onAppear {46 env.featureServices.registerWindowCommands(env: env, window: window)47 model = CommandCenterModel(registry: env.featureServices.registry, repositories: env.repositories, router: window, onDismiss: { close() })48 }49 .task { await env.featureServices.registerStandard(env: env, window: window) }50 .onExitCommand { close() }51 .accessibilityIdentifier("command-palette")52 }53 private func close() { window.showPalette = false; window.session?.editor.focus() }54}55