SPB Git

Repository Public

OrbitAndFocusViews.swift 9.9 KB 196 lines swift

1import SwiftUI23/// AURA Command Orbit: the handful of commands that fit what is on screen, arranged around the point of action.4/// It is a second face of the command palette, not a second palette: the commands come from the same catalog and5/// run through the same dispatcher; typing hands over to the full palette.6struct OrbitView: View {7    let commands: [CommandID]8    var isEnabled: (CommandID) -> Bool9    var shortcut: (CommandID) -> String = { _ in "" }10    var onRun: (CommandID) -> Void11    /// The user started typing: open the full palette with this text.12    var onType: (String) -> Void13    var onDismiss: () -> Void1415    @Environment(\.auraPolicy) private var policy16    @Environment(\.horizontalSizeClass) private var sizeClass17    @State private var appeared = false18    @State private var focusIndex: Int?19    @FocusState private var keyboard: Bool2021    private var radius: CGFloat { commands.count > 6 ? 112 : 98 }2223    var body: some View {24        Group {25            if sizeClass == .compact { grid } else { ring }26        }27        .onAppear { keyboard = true; AuraMotion.perform(.spring) { appeared = true } }28        .accessibilityElement(children: .contain)29        .accessibilityLabel(L("Command Orbit"))30        .accessibilityAddTraits(.isModal)31        .accessibilityIdentifier("orbit")32    }3334    // MARK: Ring (Mac, iPad)3536    private var ring: some View {37        ZStack {38            Circle().fill(.ultraThinMaterial).frame(width: radius * 2 + 86, height: radius * 2 + 86)39                .overlay(Circle().strokeBorder(LinearGradient(colors: [Aura.Palette.rimLight, Aura.Palette.rimShade], startPoint: .top, endPoint: .bottom), lineWidth: 1))40                .shadow(color: .black.opacity(0.32), radius: 34, y: 14)41                .scaleEffect(appeared ? 1 : 0.5).opacity(appeared ? 1 : 0)42            Circle().strokeBorder(Aura.gradient.opacity(0.35), lineWidth: 1).frame(width: radius * 2, height: radius * 2)43                .scaleEffect(appeared ? 1 : 0.3).opacity(appeared ? 1 : 0)44            ForEach(Array(commands.enumerated()), id: \.element) { index, command in45                let angle = Double(index) / Double(max(commands.count, 1)) * 2 * .pi - .pi / 246                OrbitButton(command: command, isFocused: focusIndex == index, enabled: isEnabled(command), shortcut: shortcut(command)) { run(command) }47                    .offset(x: appeared || !policy.allowsSpatial ? radius * CGFloat(cos(angle)) : 0, y: appeared || !policy.allowsSpatial ? radius * CGFloat(sin(angle)) : 0)48                    .opacity(appeared ? 1 : 0)49                    .animation(AuraMotion.animation(.spring, policy: policy)?.delay(Double(index) * 0.022), value: appeared)50            }51            Button { onType("") } label: {52                VStack(spacing: 2) {53                    Image(systemName: "command").font(.system(size: 18, weight: .medium))54                    Text(L("All")).font(.system(size: 9.5, weight: .semibold))55                }56                .foregroundStyle(.white).frame(width: 58, height: 58).background(Circle().fill(Aura.gradient))57                .shadow(color: Aura.Palette.violet.opacity(0.5), radius: 12)58            }59            .buttonStyle(AuraPressStyle(scale: 0.9, haptic: .tap))60            .help(L("All commands"))61            .accessibilityLabel(L("All commands"))62            .accessibilityIdentifier("orbit-all")63        }64        .frame(width: radius * 2 + 110, height: radius * 2 + 110)65        .focusable().focusEffectDisabled().focused($keyboard)66        .onKeyPress(.rightArrow) { step(1) }67        .onKeyPress(.downArrow) { step(1) }68        .onKeyPress(.leftArrow) { step(-1) }69        .onKeyPress(.upArrow) { step(-1) }70        .onKeyPress(.tab) { step(1) }71        .onKeyPress(.return) { if let focusIndex, commands.indices.contains(focusIndex) { run(commands[focusIndex]) } else { onType("") }; return .handled }72        .onKeyPress(.escape) { onDismiss(); return .handled }73        .onKeyPress(characters: .alphanumerics) { press in onType(press.characters); return .handled }74    }7576    private func step(_ delta: Int) -> KeyPress.Result {77        guard !commands.isEmpty else { return .ignored }78        focusIndex = ((focusIndex ?? (delta > 0 ? -1 : 0)) + delta + commands.count) % commands.count79        AuraHaptics.play(.selection)80        return .handled81    }8283    private func run(_ command: CommandID) {84        guard isEnabled(command) else { return }85        onRun(command)86    }8788    // MARK: Grid (iPhone): within thumb reach, no precision needed8990    private var grid: some View {91        VStack(spacing: 12) {92            Capsule().fill(Color.secondary.opacity(0.35)).frame(width: 36, height: 4).padding(.top, 8).accessibilityHidden(true)93            LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 10), count: 4), spacing: 12) {94                ForEach(Array(commands.enumerated()), id: \.element) { index, command in95                    OrbitButton(command: command, isFocused: false, enabled: isEnabled(command), shortcut: "") { run(command) }96                        .scaleEffect(appeared ? 1 : 0.6).opacity(appeared ? 1 : 0)97                        .animation(AuraMotion.animation(.spring, policy: policy)?.delay(Double(index) * 0.018), value: appeared)98                }99            }100            Button { onType("") } label: {101                Label(L("All commands"), systemImage: "command").font(.callout.weight(.medium)).frame(maxWidth: .infinity).frame(height: 44)102                    .background(RoundedRectangle(cornerRadius: 14, style: .continuous).fill(Aura.Palette.field))103            }104            .buttonStyle(AuraPressStyle(scale: 0.97, haptic: .tap))105            .accessibilityIdentifier("orbit-all")106        }107        .padding(.horizontal, 16).padding(.bottom, 14)108    }109}110111private struct OrbitButton: View {112    let command: CommandID113    let isFocused: Bool114    let enabled: Bool115    let shortcut: String116    let action: () -> Void117    @State private var hovering = false118119    var body: some View {120        Button(action: action) {121            VStack(spacing: 4) {122                Image(systemName: OrbitCatalog.symbol(command)).font(.system(size: 17, weight: .medium))123                    .frame(width: 46, height: 46)124                    .background(Circle().fill(hovering || isFocused ? AnyShapeStyle(Aura.Palette.accent.opacity(0.22)) : AnyShapeStyle(Aura.Palette.field)))125                    .overlay(Circle().strokeBorder(hovering || isFocused ? AnyShapeStyle(Aura.horizontalGradient) : AnyShapeStyle(Aura.Palette.hairline), lineWidth: hovering || isFocused ? 1.5 : 1))126                Text(OrbitCatalog.shortTitle(command)).font(.system(size: 10.5, weight: .medium)).lineLimit(1).minimumScaleFactor(0.75).frame(width: 74)127            }128            .contentShape(Rectangle())129        }130        .buttonStyle(AuraPressStyle(scale: 0.88, haptic: .tap))131        .scaleEffect(hovering || isFocused ? 1.08 : 1)132        .animation(AuraMotion.animation(.quick), value: hovering || isFocused)133        .onHover { hovering = $0 }134        .disabled(!enabled).opacity(enabled ? 1 : 0.35)135        .help(shortcut.isEmpty ? command.title : "\(command.title) (\(shortcut))")136        .accessibilityLabel(command.title)137        .accessibilityIdentifier("orbit-" + command.rawValue)138    }139}140141// MARK: - Focus142143/// Focus Trail: a thin luminous line showing how far the reader is in the page. Decorative to assistive technologies144/// except for its value, which is exposed as a progress.145struct FocusTrail: View {146    /// 0…1147    let progress: Double148    var thickness: CGFloat = 2.5149    @Environment(\.auraPolicy) private var policy150151    var body: some View {152        GeometryReader { proxy in153            ZStack(alignment: .leading) {154                Rectangle().fill(Color.primary.opacity(0.06))155                Capsule().fill(Aura.horizontalGradient)156                    .frame(width: max(proxy.size.width * progress.clamped(0, 1), progress > 0.001 ? thickness * 2 : 0))157                    .shadow(color: Aura.Palette.cyan.opacity(0.6), radius: 4)158                    .animation(AuraMotion.animation(.interactive, policy: policy), value: progress)159            }160        }161        .frame(height: thickness)162        .accessibilityElement()163        .accessibilityLabel(L("Reading progress"))164        .accessibilityValue("\(Int((progress.clamped(0, 1) * 100).rounded())) %")165        .accessibilityIdentifier("focus-trail")166    }167}168169/// The discreet control shown in Focus Mode: page title, progress, and the way out. It fades to almost nothing until approached.170struct FocusPill: View {171    let title: String172    let progress: Double?173    var minutesLeft: Int?174    var onExit: () -> Void175    @State private var hovering = false176    @Environment(\.auraPolicy) private var policy177178    var body: some View {179        HStack(spacing: 10) {180            Image(systemName: "eye").font(.system(size: 11, weight: .semibold)).foregroundStyle(Aura.gradient)181            Text(title).font(.system(size: 12, weight: .medium)).lineLimit(1).frame(maxWidth: 320)182            if let progress { Text("\(Int((progress.clamped(0, 1) * 100).rounded())) %").font(.system(size: 11, design: .rounded).monospacedDigit()).foregroundStyle(.secondary) }183            if let minutesLeft, minutesLeft > 0 { Text(L("%d min left", minutesLeft)).font(.system(size: 11)).foregroundStyle(.secondary) }184            Button(action: onExit) { Image(systemName: "xmark").font(.system(size: 9, weight: .bold)).frame(width: 20, height: 20).background(Circle().fill(Aura.Palette.hover)) }185                .buttonStyle(AuraPressStyle(scale: 0.85))186                .help(L("Exit Focus Mode")).accessibilityLabel(L("Exit Focus Mode")).accessibilityIdentifier("focus-exit")187        }188        .padding(.leading, 12).padding(.trailing, 6).frame(height: 32)189        .auraSurface(.floating, radius: 16)190        .opacity(hovering ? 1 : 0.82)191        .animation(AuraMotion.animation(.standard, policy: policy), value: hovering)192        .onHover { hovering = $0 }193        .accessibilityElement(children: .contain)194    }195}196
Files main
Menu

Theme