PulseCanvas / app / Sources / PulseCanvas / StudioControls.swift
| 1 | import SwiftUI |
| 2 | import AppKit |
| 3 | |
| 4 | extension View { |
| 5 | /// Consume the outside click so closing a sheet never activates the editor. |
| 6 | func dismissOnOutsideClick(_ dismiss: @escaping () -> Void) -> some View { |
| 7 | background(SheetOutsideClickHandler(dismiss: dismiss).frame(width: 0, height: 0)) |
| 8 | } |
| 9 | } |
| 10 | |
| 11 | private struct SheetOutsideClickHandler: NSViewRepresentable { |
| 12 | let dismiss: () -> Void |
| 13 | func makeNSView(context: Context) -> SheetClickView { |
| 14 | let view = SheetClickView() |
| 15 | view.dismiss = dismiss |
| 16 | return view |
| 17 | } |
| 18 | func updateNSView(_ view: SheetClickView, context: Context) { view.dismiss = dismiss } |
| 19 | static func dismantleNSView(_ view: SheetClickView, coordinator: ()) { view.stopMonitoring() } |
| 20 | } |
| 21 | |
| 22 | private final class SheetClickView: NSView { |
| 23 | var dismiss: (() -> Void)? |
| 24 | private var monitor: Any? |
| 25 | |
| 26 | override func viewDidMoveToWindow() { |
| 27 | super.viewDidMoveToWindow() |
| 28 | stopMonitoring() |
| 29 | guard window != nil else { return } |
| 30 | monitor = NSEvent.addLocalMonitorForEvents(matching: .leftMouseDown) { [weak self] event in |
| 31 | let dismissed = MainActor.assumeIsolated { |
| 32 | guard let self, let sheet = self.window, let parent = sheet.sheetParent, |
| 33 | sheet.attachedSheet == nil, |
| 34 | event.window === parent else { return false } |
| 35 | self.dismiss?() |
| 36 | return true |
| 37 | } |
| 38 | return dismissed ? nil : event |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func stopMonitoring() { |
| 43 | if let monitor { NSEvent.removeMonitor(monitor) } |
| 44 | monitor = nil |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// Shared controls keep transport, project actions, and export visually related. |
| 49 | struct StudioButtonStyle: ButtonStyle { |
| 50 | enum Treatment { case secondary, accent, primary, quiet } |
| 51 | var treatment: Treatment = .secondary |
| 52 | var compact = false |
| 53 | var circular = false |
| 54 | @Environment(\.isEnabled) private var isEnabled |
| 55 | @Environment(\.isFocused) private var isFocused |
| 56 | @State private var hovering = false |
| 57 | private let peach = Color(red: 0.95, green: 0.68, blue: 0.55) |
| 58 | |
| 59 | func makeBody(configuration: Configuration) -> some View { |
| 60 | configuration.label |
| 61 | .font(.system(size: 12, weight: .semibold)) |
| 62 | .padding(.horizontal, circular ? 0 : (compact ? 11 : 16)) |
| 63 | .frame(width: circular ? 44 : nil, height: circular ? 44 : nil) |
| 64 | .frame(minHeight: 44) |
| 65 | .foregroundStyle(treatment == .primary ? Color(red: 0.22, green: 0.13, blue: 0.15) : .white.opacity(0.92)) |
| 66 | .background(fill(pressed: configuration.isPressed), in: RoundedRectangle(cornerRadius: circular ? 22 : 11)) |
| 67 | .overlay(RoundedRectangle(cornerRadius: circular ? 22 : 11).strokeBorder(border, lineWidth: isFocused ? 2 : 1)) |
| 68 | .opacity(isEnabled ? 1 : 0.4) |
| 69 | .contentShape(RoundedRectangle(cornerRadius: circular ? 22 : 11)) |
| 70 | .onHover { hovering = $0 } |
| 71 | } |
| 72 | |
| 73 | private func fill(pressed: Bool) -> Color { |
| 74 | if treatment == .primary { return peach.opacity(pressed ? 0.7 : (hovering && isEnabled ? 1 : 0.92)) } |
| 75 | if pressed { return peach.opacity(0.23) } |
| 76 | if hovering && isEnabled { return Color.white.opacity(treatment == .quiet ? 0.09 : 0.13) } |
| 77 | switch treatment { |
| 78 | case .accent: return peach.opacity(0.14) |
| 79 | case .quiet: return .clear |
| 80 | default: return Color.white.opacity(0.055) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private var border: Color { |
| 85 | if isFocused { return peach } |
| 86 | if treatment == .primary { return peach } |
| 87 | if treatment == .accent { return peach.opacity(hovering ? 0.65 : 0.38) } |
| 88 | return Color.white.opacity(hovering && isEnabled ? 0.25 : (treatment == .quiet ? 0 : 0.13)) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /// A 44-point hit area includes both the label and a clearly visible switch. |
| 93 | struct StudioSwitchStyle: ToggleStyle { |
| 94 | var icon: String? = nil |
| 95 | @Environment(\.isEnabled) private var isEnabled |
| 96 | @Environment(\.isFocused) private var isFocused |
| 97 | private let peach = Color(red: 0.95, green: 0.68, blue: 0.55) |
| 98 | |
| 99 | func makeBody(configuration: Configuration) -> some View { |
| 100 | Button { configuration.isOn.toggle() } label: { |
| 101 | HStack(spacing: 9) { |
| 102 | if let icon { Image(systemName: icon).foregroundStyle(configuration.isOn ? peach : .white.opacity(0.55)) } |
| 103 | configuration.label.font(.system(size: 12, weight: .medium)) |
| 104 | ZStack(alignment: configuration.isOn ? .trailing : .leading) { |
| 105 | Capsule().fill(configuration.isOn ? peach : Color.white.opacity(0.15)) |
| 106 | Capsule().strokeBorder(configuration.isOn ? peach : Color.white.opacity(0.22), lineWidth: 1) |
| 107 | Circle().fill(configuration.isOn ? Color(red: 0.23, green: 0.14, blue: 0.16) : .white.opacity(0.85)) |
| 108 | .frame(width: 22, height: 22).padding(3) |
| 109 | }.frame(width: 48, height: 28) |
| 110 | }.padding(.horizontal, 10).frame(minHeight: 44) |
| 111 | .background(configuration.isOn ? peach.opacity(0.08) : Color.white.opacity(0.025), in: RoundedRectangle(cornerRadius: 9)) |
| 112 | .overlay(RoundedRectangle(cornerRadius: 9).strokeBorder(isFocused ? peach : .white.opacity(configuration.isOn ? 0.13 : 0.07), lineWidth: isFocused ? 2 : 1)) |
| 113 | .contentShape(Rectangle()) |
| 114 | } |
| 115 | .buttonStyle(.plain) |
| 116 | .opacity(isEnabled ? 1 : 0.4) |
| 117 | .accessibilityRepresentation { |
| 118 | Toggle(isOn: configuration.$isOn) { configuration.label }.toggleStyle(.switch) |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /// Equal-width mode tabs share a visible track and one selected indicator. |
| 124 | struct StudioTabStyle: ButtonStyle { |
| 125 | let selected: Bool |
| 126 | @Environment(\.isEnabled) private var isEnabled |
| 127 | @Environment(\.isFocused) private var isFocused |
| 128 | @State private var hovering = false |
| 129 | private let peach = Color(red: 0.95, green: 0.68, blue: 0.55) |
| 130 | |
| 131 | func makeBody(configuration: Configuration) -> some View { |
| 132 | configuration.label |
| 133 | .font(.system(size: 12, weight: .semibold)) |
| 134 | .frame(maxWidth: .infinity).frame(height: 44) |
| 135 | .foregroundStyle(selected ? peach : .white.opacity(0.8)) |
| 136 | .background(selected ? peach.opacity(configuration.isPressed ? 0.25 : 0.16) : .white.opacity(hovering || configuration.isPressed ? 0.09 : 0.035), in: RoundedRectangle(cornerRadius: 6)) |
| 137 | .overlay(alignment: .leading) { |
| 138 | Capsule().fill(peach).frame(width: 3, height: 20).padding(.leading, 4).opacity(selected ? 1 : 0) |
| 139 | } |
| 140 | .overlay(RoundedRectangle(cornerRadius: 6).strokeBorder(isFocused ? peach : .clear, lineWidth: 2)) |
| 141 | .contentShape(Rectangle()) |
| 142 | .opacity(isEnabled ? 1 : 0.4) |
| 143 | .accessibilityAddTraits(selected ? .isSelected : []) |
| 144 | .onHover { hovering = $0 } |
| 145 | } |
| 146 | } |