PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / StudioPresentation.swift
| 1 | import AppKit |
| 2 | import SwiftUI |
| 3 | |
| 4 | @MainActor |
| 5 | final class StudioPresentationController: NSObject, ObservableObject { |
| 6 | enum Mode { |
| 7 | case studio, visualizer |
| 8 | } |
| 9 | @Published private(set) var isPresented = false |
| 10 | @Published private(set) var mode: Mode = .studio |
| 11 | var followsStudioTimeline: Bool { !isPresented || mode == .studio } |
| 12 | private weak var window: NSWindow? |
| 13 | private var ownsFullScreen = false |
| 14 | private var originalBackground: NSColor? |
| 15 | private var keyboardMonitor: Any? |
| 16 | |
| 17 | var canPresentEditorSheet: Bool { |
| 18 | NSApplication.shared.isActive && window?.isKeyWindow == true |
| 19 | && window?.attachedSheet == nil && NSApplication.shared.modalWindow == nil |
| 20 | } |
| 21 | |
| 22 | override init() { |
| 23 | super.init() |
| 24 | NotificationCenter.default.addObserver(self, selector: #selector(enteredFullScreen(_:)), name: NSWindow.didEnterFullScreenNotification, object: nil) |
| 25 | NotificationCenter.default.addObserver(self, selector: #selector(exitedFullScreen(_:)), name: NSWindow.didExitFullScreenNotification, object: nil) |
| 26 | } |
| 27 | |
| 28 | func attach(_ window: NSWindow?) { self.window = window } |
| 29 | |
| 30 | @discardableResult |
| 31 | func present(audio: AudioController, mode: Mode = .studio) -> Bool { |
| 32 | guard let window, !isPresented else { return false } |
| 33 | originalBackground = window.backgroundColor |
| 34 | window.backgroundColor = .black |
| 35 | ownsFullScreen = !window.styleMask.contains(.fullScreen) |
| 36 | self.mode = mode |
| 37 | isPresented = true |
| 38 | window.makeKeyAndOrderFront(nil) |
| 39 | NSApplication.shared.activate() |
| 40 | keyboardMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self, weak audio] event in |
| 41 | guard let self, self.isPresented, event.modifierFlags.intersection([.command, .control, .option, .shift]).isEmpty else { return event } |
| 42 | if event.isARepeat && (event.keyCode == 53 || event.keyCode == 49) { return nil } |
| 43 | if event.keyCode == 53 { audio?.pause(); self.dismiss(); return nil } |
| 44 | if event.keyCode == 49 { audio?.playPause(); return nil } |
| 45 | return event |
| 46 | } |
| 47 | if ownsFullScreen { window.toggleFullScreen(nil) } |
| 48 | return true |
| 49 | } |
| 50 | |
| 51 | func dismiss() { |
| 52 | guard isPresented else { return } |
| 53 | stopMonitoringKeyboard() |
| 54 | isPresented = false |
| 55 | if ownsFullScreen && window?.styleMask.contains(.fullScreen) == true { window?.toggleFullScreen(nil) } |
| 56 | else if !ownsFullScreen { restoreBackground() } |
| 57 | } |
| 58 | |
| 59 | @objc private func enteredFullScreen(_ notification: Notification) { |
| 60 | guard let source = notification.object as? NSWindow, source === window else { return } |
| 61 | // Escape can arrive while the native entrance animation is still running. |
| 62 | if ownsFullScreen && !isPresented { source.toggleFullScreen(nil) } |
| 63 | } |
| 64 | |
| 65 | @objc private func exitedFullScreen(_ notification: Notification) { |
| 66 | guard let source = notification.object as? NSWindow, source === window else { return } |
| 67 | stopMonitoringKeyboard() |
| 68 | restoreBackground() |
| 69 | ownsFullScreen = false |
| 70 | isPresented = false |
| 71 | } |
| 72 | private func stopMonitoringKeyboard() { |
| 73 | if let keyboardMonitor { NSEvent.removeMonitor(keyboardMonitor) } |
| 74 | keyboardMonitor = nil |
| 75 | } |
| 76 | |
| 77 | private func restoreBackground() { |
| 78 | if let originalBackground { window?.backgroundColor = originalBackground } |
| 79 | originalBackground = nil |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @MainActor |
| 84 | struct StudioWindowReader: NSViewRepresentable { |
| 85 | let attach: (NSWindow?) -> Void |
| 86 | final class WindowView: NSView { |
| 87 | var attach: ((NSWindow?) -> Void)? |
| 88 | override func viewDidMoveToWindow() { super.viewDidMoveToWindow(); attach?(window) } |
| 89 | } |
| 90 | func makeNSView(context: Context) -> WindowView { |
| 91 | let view = WindowView(); view.attach = attach; return view |
| 92 | } |
| 93 | func updateNSView(_ view: WindowView, context: Context) { view.attach = attach } |
| 94 | } |
| 95 | |
| 96 | @MainActor |
| 97 | struct StudioPresentationView: View { |
| 98 | @ObservedObject var render: RenderState |
| 99 | @ObservedObject var audio: AudioController |
| 100 | @ObservedObject var studio: StudioStore |
| 101 | @ObservedObject private var library = ProjectMPresets.shared |
| 102 | var mode: StudioPresentationController.Mode = .studio |
| 103 | var aspect: SocialAspect = .wide |
| 104 | let exit: () -> Void |
| 105 | @AppStorage("PulseCanvas.presentationMatchAspect.v1") private var matchAspectRatio = false |
| 106 | @State private var controlsVisible = true |
| 107 | @State private var hideControls: Task<Void, Never>? |
| 108 | |
| 109 | var body: some View { |
| 110 | GeometryReader { space in |
| 111 | let width = matchAspectRatio ? min(space.size.width, space.size.height * aspect.aspectRatio) : space.size.width |
| 112 | let height = matchAspectRatio ? width / aspect.aspectRatio : space.size.height |
| 113 | ZStack(alignment: .bottom) { |
| 114 | Color.black |
| 115 | VisualizerView(state: render) |
| 116 | .frame(width: width, height: height) |
| 117 | .clipped() |
| 118 | .frame(width: space.size.width, height: space.size.height) |
| 119 | .modifier(VisualizerFavoriteGesture { |
| 120 | ProjectMPresets.shared.favorite(render.scene, appearance: VisualPreset(render: render)) |
| 121 | }) |
| 122 | if controlsVisible || !audio.isPlaying { |
| 123 | HStack(spacing: 16) { |
| 124 | Button { library.toggleFavorite(render.scene, appearance: VisualPreset(render: render)) } label: { |
| 125 | Image(systemName: library.isFavorite(render.scene) ? "heart.fill" : "heart") |
| 126 | }.accessibilityLabel(library.isFavorite(render.scene) ? "Unfavorite visualizer" : "Favorite visualizer") |
| 127 | VStack(alignment: .leading, spacing: 4) { |
| 128 | Text(RenderState.sceneNames[render.scene]).font(.headline) |
| 129 | Text(mode == .studio ? "Studio · Section \(studio.selectedSegment + 1) of \(studio.segments.count) · Esc to exit" : "Visualizer · Esc to exit") |
| 130 | .font(.system(size: 12)).foregroundStyle(.secondary) |
| 131 | } |
| 132 | Spacer() |
| 133 | Toggle("Match aspect ratio", isOn: $matchAspectRatio) |
| 134 | .toggleStyle(.checkbox).controlSize(.large) |
| 135 | .font(.system(size: 12, weight: .semibold)) |
| 136 | .padding(.horizontal, 12).frame(minHeight: 44) |
| 137 | .background(.white.opacity(0.055), in: RoundedRectangle(cornerRadius: 11)) |
| 138 | .overlay(RoundedRectangle(cornerRadius: 11).strokeBorder(.white.opacity(0.13))) |
| 139 | .help("Fit the visualization to the project’s \(aspect.rawValue) aspect ratio, with black bars where needed.") |
| 140 | Button { audio.playPause(); revealControls() } label: { |
| 141 | Label(audio.isPlaying ? "Pause" : "Play", systemImage: audio.isPlaying ? "pause.fill" : "play.fill").frame(width: 68) |
| 142 | }.accessibilityLabel(audio.isPlaying ? "Pause presentation" : "Play presentation") |
| 143 | Button { audio.seek(fraction: 0); audio.playFullSoundtrack(); revealControls() } label: { |
| 144 | Label("Restart", systemImage: "backward.end.fill") |
| 145 | }.accessibilityLabel("Restart presentation") |
| 146 | Button(action: exit) { Label("Exit presentation", systemImage: "arrow.down.right.and.arrow.up.left") } |
| 147 | }.buttonStyle(StudioButtonStyle(compact: true)) |
| 148 | .padding(18).background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 14)) |
| 149 | .padding(24).transition(.opacity) |
| 150 | } |
| 151 | }.contentShape(Rectangle()) |
| 152 | .onContinuousHover { phase in if case .active = phase { revealControls() } } |
| 153 | }.frame(maxWidth: .infinity, maxHeight: .infinity).background(Color.black) |
| 154 | .ignoresSafeArea().preferredColorScheme(.dark) |
| 155 | .onAppear { revealControls() } |
| 156 | .onChange(of: audio.isPlaying) { _, _ in revealControls() } |
| 157 | .onChange(of: matchAspectRatio) { _, _ in revealControls() } |
| 158 | .onDisappear { hideControls?.cancel() } |
| 159 | } |
| 160 | |
| 161 | private func revealControls() { |
| 162 | hideControls?.cancel() |
| 163 | withAnimation(.easeOut(duration: 0.2)) { controlsVisible = true } |
| 164 | hideControls = Task { @MainActor in |
| 165 | do { try await Task.sleep(for: .seconds(3)) } catch { return } |
| 166 | guard audio.isPlaying else { return } |
| 167 | withAnimation(.easeOut(duration: 0.3)) { controlsVisible = false } |
| 168 | } |
| 169 | } |
| 170 | } |