Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Browser9// File : SPBrowser/Shared/Aura/AuraComponents.swift10// Module : SPBrowser11// Repository : https://git.spboucher.ai/sp-browser12// Created : 2026-11-1913// Last Modified : 2026-11-1914//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author : Simon-Pierre Boucher20// Position : Professor of Finance21// Institution : Université du Québec en Outaouais (UQO)22// Department : Département des sciences administratives23// Organization : SP Boucher24//25// Website : https://spboucher.ai26// Git : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General : info@spboucher.ai33// Professional : contact@spboucher.ai34// Direct : simon@spboucher.ai35// Research : research@spboucher.ai36// Development : dev@spboucher.ai37// Support : support@spboucher.ai38// Security/Admin : admin@spboucher.ai39// Notifications : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification : Public66// Distribution : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License : None specified (no license file in this repository)80// Maintainer : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import SwiftUI8889// MARK: - Buttons9091/// Press feedback shared by every AURA control: the control gives slightly under the finger or the pointer.92struct AuraPressStyle: ButtonStyle {93 var scale: CGFloat = 0.9494 var haptic: AuraHaptics.Kind?95 @Environment(\.auraPolicy) private var policy9697 func makeBody(configuration: Configuration) -> some View {98 configuration.label99 .scaleEffect(configuration.isPressed && policy.allowsSpatial ? scale : 1)100 .opacity(configuration.isPressed ? 0.82 : 1)101 .animation(AuraMotion.animation(.interactive, policy: policy), value: configuration.isPressed)102 .onChange(of: configuration.isPressed) { _, pressed in if pressed, let haptic { AuraHaptics.play(haptic) } }103 }104}105106/// The standard icon control: an SF Symbol with hover highlight, press feedback, tooltip and accessibility label.107struct AuraIconButton: View {108 let symbol: String109 let label: String110 var isActive = false111 var tint: Color?112 var size: CGFloat = 28113 let action: () -> Void114 @State private var hovering = false115 @Environment(\.isEnabled) private var isEnabled116117 var body: some View {118 Button(action: action) {119 Image(systemName: symbol)120 .font(.system(size: size * 0.48, weight: .medium))121 .contentTransition(.symbolEffect(.replace))122 .foregroundStyle(isActive ? AnyShapeStyle(tint ?? Aura.Palette.accent) : AnyShapeStyle(tint ?? Color.primary.opacity(isEnabled ? 0.78 : 0.3)))123 .frame(width: size, height: size)124 .background(RoundedRectangle(cornerRadius: Aura.Radius.s + 1, style: .continuous)125 .fill(isActive ? (tint ?? Aura.Palette.accent).opacity(0.16) : (hovering && isEnabled ? Aura.Palette.hover : .clear)))126 .contentShape(Rectangle())127 }128 .buttonStyle(AuraPressStyle(scale: 0.9))129 .onHover { hovering = $0 }130 .help(label)131 .accessibilityLabel(label)132 .accessibilityAddTraits(isActive ? .isSelected : [])133 }134}135136// MARK: - Toolbar and sidebar containers137138/// A row of controls with AURA density. `floating` gives it its own glass surface (iPhone bar, Focus Mode controls).139struct AuraToolbar<Content: View>: View {140 var density: Aura.Density = .regular141 var floating = false142 var radius: CGFloat = Aura.Radius.xl143 @ViewBuilder var content: () -> Content144145 var body: some View {146 let row = HStack(spacing: density.spacing) { content() }.environment(\.auraDensity, density)147 if floating {148 row.padding(.horizontal, Aura.Space.s).padding(.vertical, Aura.Space.xs + 2).auraSurface(.floating, radius: radius)149 } else {150 row151 }152 }153}154155/// Sidebar container: chrome material and a hairline on its trailing edge. Content changes animate spatially.156struct AuraSidebar<Content: View>: View {157 var edge: Edge = .leading158 @ViewBuilder var content: () -> Content159 @Environment(\.auraPolicy) private var policy160161 var body: some View {162 content()163 .frame(maxHeight: .infinity, alignment: .top)164 .overlay(alignment: edge == .leading ? .trailing : .leading) { Rectangle().fill(Aura.Palette.hairline).frame(width: 1) }165 .transition(AuraTransition.rise(edge, policy))166 .accessibilityElement(children: .contain)167 }168}169170struct AuraSectionHeader: View {171 let text: String172 var body: some View {173 Text(text.uppercased()).font(.system(size: 10.5, weight: .semibold)).kerning(0.6).foregroundStyle(.secondary)174 .frame(maxWidth: .infinity, alignment: .leading).accessibilityAddTraits(.isHeader)175 }176}177178// MARK: - Tab179180/// A tab in a strip. The selection is one shared shape that glides from tab to tab (`matchedGeometryEffect`).181struct AuraTab<Icon: View>: View {182 let title: String183 var isSelected = false184 var isPinned = false185 var accent: Color?186 var badge: String?187 var namespace: Namespace.ID?188 var showsClose = true189 var closeLabel = ""190 var onClose: (() -> Void)?191 @ViewBuilder var icon: () -> Icon192 @State private var hovering = false193194 var body: some View {195 HStack(spacing: 6) {196 icon().frame(width: 16, height: 16)197 if !isPinned {198 Text(title).font(.system(size: 12.5, weight: isSelected ? .medium : .regular)).lineLimit(1)199 .foregroundStyle(isSelected ? .primary : .secondary)200 Spacer(minLength: 0)201 if let badge { Image(systemName: badge).font(.system(size: 9)).foregroundStyle(.secondary) }202 if showsClose, let onClose, hovering || isSelected {203 Button(action: onClose) {204 Image(systemName: "xmark").font(.system(size: 8.5, weight: .bold)).frame(width: 16, height: 16)205 .background(Circle().fill(hovering ? Aura.Palette.hover : .clear))206 }207 .buttonStyle(AuraPressStyle(scale: 0.8)).accessibilityLabel(closeLabel)208 .transition(.opacity)209 }210 }211 }212 .padding(.horizontal, isPinned ? 9 : 10)213 .frame(height: 28)214 .frame(minWidth: isPinned ? 36 : 92, maxWidth: isPinned ? 36 : 210)215 .background {216 if isSelected {217 let pill = RoundedRectangle(cornerRadius: 9, style: .continuous).fill(Aura.Palette.selected)218 .overlay(RoundedRectangle(cornerRadius: 9, style: .continuous).strokeBorder(LinearGradient(colors: [Aura.Palette.rimLight, Aura.Palette.rimShade], startPoint: .top, endPoint: .bottom), lineWidth: 1))219 .shadow(color: .black.opacity(0.12), radius: 3, y: 1)220 if let namespace { pill.matchedGeometryEffect(id: "aura.tab.selection", in: namespace) } else { pill }221 } else if hovering {222 RoundedRectangle(cornerRadius: 9, style: .continuous).fill(Aura.Palette.hover)223 }224 }225 .overlay(alignment: .bottom) {226 if let accent { Capsule().fill(accent).frame(height: 2.5).padding(.horizontal, 9).offset(y: 3) }227 }228 .contentShape(Rectangle())229 .onHover { hovering = $0 }230 }231}232233// MARK: - Search field234235/// The AURA search field: glass field, luminous focus ring, clear button. Owns its focus.236struct AuraSearchField<Accessory: View>: View {237 @Binding var text: String238 var placeholder: String239 var symbol = "magnifyingglass"240 var height: CGFloat = 36241 var fontSize: CGFloat = 14242 var radius: CGFloat?243 /// Changing this value moves keyboard focus into the field.244 var focusRequest = 0245 var focusOnAppear = false246 var identifier: String?247 var onSubmit: () -> Void = {}248 /// Up / down arrows; return true when handled.249 var onMove: ((Int) -> Bool)?250 var onEscape: (() -> Bool)?251 var onFocusChange: ((Bool) -> Void)?252 @ViewBuilder var accessory: () -> Accessory253 @FocusState private var focused: Bool254255 var body: some View {256 HStack(spacing: 9) {257 Image(systemName: symbol).font(.system(size: fontSize, weight: .medium)).foregroundStyle(focused ? AnyShapeStyle(Aura.Palette.accent) : AnyShapeStyle(.secondary))258 .accessibilityHidden(true)259 TextField(placeholder, text: $text)260 .textFieldStyle(.plain).font(.system(size: fontSize)).focused($focused)261 #if os(iOS)262 .textInputAutocapitalization(.never).autocorrectionDisabled()263 #endif264 .onSubmit(onSubmit)265 .onKeyPress(.downArrow) { onMove?(1) == true ? .handled : .ignored }266 .onKeyPress(.upArrow) { onMove?(-1) == true ? .handled : .ignored }267 .onKeyPress(.escape) { onEscape?() == true ? .handled : .ignored }268 .accessibilityLabel(placeholder)269 .accessibilityIdentifier(identifier ?? "aura-search-field")270 if !text.isEmpty {271 Button { text = "" } label: { Image(systemName: "xmark.circle.fill").foregroundStyle(.tertiary) }272 .buttonStyle(AuraPressStyle(scale: 0.85)).accessibilityLabel(L("Clear")).transition(.opacity.combined(with: .scale(scale: 0.6)))273 }274 accessory()275 }276 .padding(.horizontal, height * 0.34)277 .frame(height: height)278 .auraField(focused: focused, radius: radius ?? height / 2)279 .animation(AuraMotion.animation(.quick), value: text.isEmpty)280 .onAppear { if focusOnAppear { focused = true } }281 .onChange(of: focusRequest) { focused = true }282 .onChange(of: focused) { _, value in onFocusChange?(value) }283 }284}285286extension AuraSearchField where Accessory == EmptyView {287 init(text: Binding<String>, placeholder: String, symbol: String = "magnifyingglass", height: CGFloat = 36, fontSize: CGFloat = 14, radius: CGFloat? = nil,288 focusRequest: Int = 0, focusOnAppear: Bool = false, identifier: String? = nil, onSubmit: @escaping () -> Void = {},289 onMove: ((Int) -> Bool)? = nil, onEscape: (() -> Bool)? = nil, onFocusChange: ((Bool) -> Void)? = nil) {290 self.init(text: text, placeholder: placeholder, symbol: symbol, height: height, fontSize: fontSize, radius: radius, focusRequest: focusRequest,291 focusOnAppear: focusOnAppear, identifier: identifier, onSubmit: onSubmit, onMove: onMove, onEscape: onEscape, onFocusChange: onFocusChange) { EmptyView() }292 }293}294295// MARK: - Progress296297/// Loading and progress, never intrusive: a thin luminous line (determinate or not) or a small ring.298struct AuraProgressIndicator: View {299 enum Style { case line, ring }300 /// 0…1, or nil while the amount is unknown.301 var progress: Double?302 var style: Style = .line303 var thickness: CGFloat = 2304 var label = ""305 @Environment(\.auraPolicy) private var policy306307 var body: some View {308 Group {309 switch style {310 case .line: line311 case .ring: ring312 }313 }314 .accessibilityElement()315 .accessibilityLabel(label.isEmpty ? L("Loading") : label)316 .accessibilityValue(progress.map { "\(Int($0 * 100)) %" } ?? "")317 }318319 private var line: some View {320 GeometryReader { proxy in321 if let progress {322 Capsule().fill(Aura.horizontalGradient)323 .frame(width: max(proxy.size.width * min(max(progress, 0), 1), thickness * 6), height: thickness)324 .shadow(color: Aura.Palette.cyan.opacity(0.5), radius: 3)325 .animation(AuraMotion.animation(.standard, policy: policy), value: progress)326 } else if policy.allowsDecorative {327 TimelineView(.animation(minimumInterval: 1.0 / 30.0)) { context in328 let t = context.date.timeIntervalSinceReferenceDate.truncatingRemainder(dividingBy: 1.4) / 1.4329 Capsule().fill(Aura.horizontalGradient).frame(width: proxy.size.width * 0.3, height: thickness)330 .offset(x: (proxy.size.width * 1.3) * t - proxy.size.width * 0.3)331 }332 } else {333 Capsule().fill(Aura.horizontalGradient.opacity(0.6)).frame(height: thickness)334 }335 }336 .frame(height: thickness)337 .clipped()338 }339340 private var ring: some View {341 ZStack {342 Circle().stroke(Aura.Palette.hairline, lineWidth: thickness)343 Circle().trim(from: 0, to: progress.map { min(max($0, 0.02), 1) } ?? 0.25)344 .stroke(Aura.gradient, style: StrokeStyle(lineWidth: thickness, lineCap: .round))345 .rotationEffect(.degrees(-90))346 .animation(AuraMotion.animation(.standard, policy: policy), value: progress)347 }348 }349}350351// MARK: - Toast352353/// A short confirmation that rises, stays a moment and leaves. It is announced to assistive technologies.354struct AuraToast: View {355 @Binding var text: String?356 var edge: Edge = .bottom357 var duration: Double = 3.5358 @Environment(\.auraPolicy) private var policy359360 var body: some View {361 ZStack {362 if let message = text {363 HStack(spacing: 8) {364 Image(systemName: Self.symbol(for: message)).font(.system(size: 12, weight: .semibold)).foregroundStyle(Aura.gradient)365 .accessibilityHidden(true)366 Text(message).font(.callout).lineLimit(2)367 }368 .padding(.horizontal, 16).padding(.vertical, 10)369 .auraSurface(.floating, radius: 22)370 .transition(AuraTransition.rise(edge, policy))371 .task(id: message) {372 AuraAccessibility.announce(message)373 try? await Task.sleep(for: .seconds(duration))374 if text == message { AuraMotion.perform(.standard) { text = nil } }375 }376 .onTapGesture { AuraMotion.perform(.quick) { text = nil } }377 .accessibilityAddTraits(.isStaticText)378 .accessibilityIdentifier("aura-toast")379 }380 }381 .animation(AuraMotion.animation(.spring, policy: policy), value: text)382 }383384 /// The icon confirms the kind of outcome without the caller having to say so.385 static func symbol(for message: String) -> String {386 let lower = message.lowercased()387 if lower.contains("could not") || lower.contains("impossible") || lower.contains("n’a pas") || lower.contains("blocked") || lower.contains("bloqué") { return "exclamationmark.circle" }388 return "checkmark.circle"389 }390}391392@MainActor393enum AuraAccessibility {394 static func announce(_ message: String) {395 #if canImport(UIKit)396 UIAccessibility.post(notification: .announcement, argument: message)397 #else398 NSAccessibility.post(element: NSApp as Any, notification: .announcementRequested,399 userInfo: [.announcement: message, .priority: NSAccessibilityPriorityLevel.medium.rawValue])400 #endif401 }402}403404// MARK: - Context menu (floating action list)405406struct AuraMenuItem: Identifiable {407 var id: String408 var title: String409 var symbol: String410 var isDestructive = false411 var isEnabled = true412 var action: () -> Void413}414415/// A compact floating list of actions, used where a system menu cannot be attached (Peek, Orbit, canvas cards, constellations).416struct AuraContextMenu: View {417 let items: [AuraMenuItem]418 var width: CGFloat = 230419 @State private var hovered: String?420421 var body: some View {422 VStack(spacing: 1) {423 ForEach(items) { item in424 Button(action: item.action) {425 HStack(spacing: 10) {426 Image(systemName: item.symbol).font(.system(size: 12.5)).frame(width: 18)427 Text(item.title).font(.system(size: 13)).lineLimit(1)428 Spacer(minLength: 0)429 }430 .foregroundStyle(item.isDestructive ? AnyShapeStyle(Color.red) : AnyShapeStyle(.primary))431 .padding(.horizontal, 10).frame(height: 32)432 .background(RoundedRectangle(cornerRadius: 8, style: .continuous).fill(hovered == item.id ? Aura.Palette.hover : .clear))433 .contentShape(Rectangle())434 }435 .buttonStyle(AuraPressStyle(scale: 0.97))436 .disabled(!item.isEnabled).opacity(item.isEnabled ? 1 : 0.4)437 .onHover { hovered = $0 ? item.id : (hovered == item.id ? nil : hovered) }438 .accessibilityIdentifier("aura-menu-" + item.id)439 }440 }441 .padding(5)442 .frame(width: width)443 }444}445446// MARK: - Modal447448private struct AuraModalModifier<Modal: View>: ViewModifier {449 @Binding var isPresented: Bool450 var maxWidth: CGFloat451 var alignment: Alignment452 @ViewBuilder var modal: () -> Modal453 @Environment(\.auraPolicy) private var policy454455 func body(content: Content) -> some View {456 content.overlay {457 ZStack(alignment: alignment) {458 if isPresented {459 Color.black.opacity(0.28).ignoresSafeArea().transition(.opacity)460 .onTapGesture { isPresented = false }461 .accessibilityLabel(L("Close")).accessibilityAddTraits(.isButton)462 AuraFloatingPanel(anchor: alignment == .bottom ? .bottom : .top) { modal() }463 .frame(maxWidth: maxWidth)464 .padding(Aura.Space.l)465 .accessibilityAddTraits(.isModal)466 }467 }468 .animation(AuraMotion.animation(.expressive, policy: policy), value: isPresented)469 }470 }471}472473extension View {474 /// Presents content in an AURA modal: dimmed backdrop, floating glass panel, depth transition. Tapping outside dismisses it.475 func auraModal<Modal: View>(isPresented: Binding<Bool>, maxWidth: CGFloat = 560, alignment: Alignment = .center, @ViewBuilder content: @escaping () -> Modal) -> some View {476 modifier(AuraModalModifier(isPresented: isPresented, maxWidth: maxWidth, alignment: alignment, modal: content))477 }478}479480// MARK: - Command palette shell481482struct AuraCommandItem: Identifiable, Equatable {483 var id: String484 var symbol: String485 var title: String486 var subtitle = ""487 var trailing = ""488}489490/// The palette surface shared by ⌘K on the Mac and the compact palette on iPhone and iPad:491/// one field, a keyboard-navigable list, a hint line. It owns no commands — callers supply the items.492struct AuraCommandPalette<Header: View>: View {493 @Binding var query: String494 var placeholder: String495 var items: [AuraCommandItem]496 @Binding var selection: Int497 var hint = ""498 var maxListHeight: CGFloat = 380499 var onRun: (AuraCommandItem) -> Void500 var onDismiss: () -> Void501 @ViewBuilder var header: () -> Header502 @Environment(\.auraPolicy) private var policy503504 var body: some View {505 VStack(spacing: 0) {506 AuraSearchField(text: $query, placeholder: placeholder, symbol: "command", height: 50, fontSize: 17, radius: 0, focusOnAppear: true,507 identifier: "aura-palette-field",508 onSubmit: { if items.indices.contains(selection) { onRun(items[selection]) } },509 onMove: { delta in selection = min(max(selection + delta, 0), max(items.count - 1, 0)); return true },510 onEscape: { onDismiss(); return true })511 .background(Color.clear)512 header()513 Rectangle().fill(Aura.Palette.hairline).frame(height: 1)514 if items.isEmpty {515 VStack(spacing: 6) {516 Image(systemName: "magnifyingglass").font(.title2).foregroundStyle(.tertiary)517 Text(L("No result")).font(.callout).foregroundStyle(.secondary)518 }519 .frame(maxWidth: .infinity).frame(height: 110)520 } else {521 ScrollViewReader { proxy in522 ScrollView {523 LazyVStack(spacing: 1) {524 ForEach(Array(items.enumerated()), id: \.element.id) { index, item in525 Button { onRun(item) } label: { row(item, selected: index == selection) }526 .buttonStyle(AuraPressStyle(scale: 0.985)).id(index)527 .accessibilityLabel(item.subtitle.isEmpty ? item.title : "\(item.title), \(item.subtitle)")528 .accessibilityAddTraits(index == selection ? .isSelected : [])529 }530 }531 .padding(6)532 }533 .frame(maxHeight: maxListHeight)534 .onChange(of: selection) { _, value in proxy.scrollTo(value) }535 }536 }537 if !hint.isEmpty {538 Rectangle().fill(Aura.Palette.hairline).frame(height: 1)539 Text(hint).font(.caption).foregroundStyle(.tertiary).frame(maxWidth: .infinity, alignment: .leading).padding(.horizontal, 14).padding(.vertical, 7)540 }541 }542 .onChange(of: query) { selection = 0 }543 }544545 private func row(_ item: AuraCommandItem, selected: Bool) -> some View {546 HStack(spacing: 10) {547 Image(systemName: item.symbol).frame(width: 20).foregroundStyle(selected ? AnyShapeStyle(Aura.Palette.accent) : AnyShapeStyle(.secondary))548 Text(item.title).lineLimit(1)549 Text(item.subtitle).foregroundStyle(.secondary).lineLimit(1).truncationMode(.middle)550 Spacer(minLength: 0)551 if !item.trailing.isEmpty {552 Text(item.trailing).font(.system(size: 11, design: .rounded)).foregroundStyle(.secondary)553 .padding(.horizontal, 6).padding(.vertical, 2).background(Aura.Palette.hover, in: RoundedRectangle(cornerRadius: 5))554 }555 }556 .font(.system(size: 13.5)).padding(.horizontal, 10).frame(height: 36)557 .frame(maxWidth: .infinity, alignment: .leading)558 .background(RoundedRectangle(cornerRadius: 9, style: .continuous).fill(selected ? Aura.Palette.selected : .clear)559 .shadow(color: .black.opacity(selected ? 0.08 : 0), radius: 2, y: 1))560 .contentShape(Rectangle())561 }562}563564extension AuraCommandPalette where Header == EmptyView {565 init(query: Binding<String>, placeholder: String, items: [AuraCommandItem], selection: Binding<Int>, hint: String = "", maxListHeight: CGFloat = 380,566 onRun: @escaping (AuraCommandItem) -> Void, onDismiss: @escaping () -> Void) {567 self.init(query: query, placeholder: placeholder, items: items, selection: selection, hint: hint, maxListHeight: maxListHeight, onRun: onRun, onDismiss: onDismiss) { EmptyView() }568 }569}570571// MARK: - Small shared pieces572573/// A device, group or workspace chip.574struct AuraChip: View {575 let text: String576 var symbol: String?577 var tint: Color = .secondary578 var isSelected = false579580 var body: some View {581 HStack(spacing: 5) {582 if let symbol { Image(systemName: symbol).font(.system(size: 10.5, weight: .semibold)) }583 Text(text).font(.system(size: 11.5, weight: .medium)).lineLimit(1)584 }585 .foregroundStyle(isSelected ? AnyShapeStyle(Color.white) : AnyShapeStyle(tint))586 .padding(.horizontal, 9).padding(.vertical, 4)587 .background(Capsule().fill(isSelected ? AnyShapeStyle(tint) : AnyShapeStyle(tint.opacity(0.14))))588 }589}590591/// An empty or error state in AURA style, used by the new surfaces.592struct AuraEmptyState: View {593 let symbol: String594 let title: String595 var message = ""596 var actionTitle: String?597 var action: (() -> Void)?598599 var body: some View {600 VStack(spacing: 10) {601 Image(systemName: symbol).font(.system(size: 38, weight: .light)).foregroundStyle(Aura.gradient).accessibilityHidden(true)602 Text(title).font(.title3.weight(.semibold)).multilineTextAlignment(.center)603 if !message.isEmpty {604 Text(message).font(.callout).foregroundStyle(.secondary).multilineTextAlignment(.center).frame(maxWidth: 420).fixedSize(horizontal: false, vertical: true)605 }606 if let actionTitle, let action { Button(actionTitle, action: action).buttonStyle(.borderedProminent).padding(.top, 4) }607 }608 .padding(28)609 .frame(maxWidth: .infinity, maxHeight: .infinity)610 .accessibilityElement(children: .combine)611 }612}613614/// A site's initial on a tinted tile, or its icon when the platform supplied one.615struct AuraSiteGlyph: View {616 var image: PlatformImage?617 let fallback: String618 var size: CGFloat = 18619 var tint: Color = .secondary620621 var body: some View {622 Group {623 if let image {624 Image(platformImage: image).resizable().interpolation(.high)625 } else {626 RoundedRectangle(cornerRadius: size * 0.25, style: .continuous).fill(tint.opacity(0.2))627 .overlay(Text(String(fallback.prefix(1)).uppercased()).font(.system(size: size * 0.58, weight: .semibold)).foregroundStyle(tint))628 }629 }630 .frame(width: size, height: size)631 .clipShape(RoundedRectangle(cornerRadius: size * 0.22, style: .continuous))632 .accessibilityHidden(true)633 }634}635636// MARK: - Appearance choreography and reading progress637638/// Sections of a page arrive one after the other, a few points below their place, then settle. Under Reduce Motion they simply fade.639private struct AuraAppear: ViewModifier {640 let order: Int641 @State private var shown = false642 @Environment(\.auraPolicy) private var policy643644 func body(content: Content) -> some View {645 content646 .opacity(shown ? 1 : 0)647 .offset(y: shown || !policy.allowsSpatial ? 0 : 14)648 .onAppear { withAnimation(AuraMotion.animation(.expressive, policy: policy)?.delay(Double(min(order, 10)) * 0.045)) { shown = true } }649 }650}651652extension View {653 func auraAppear(_ order: Int = 0) -> some View { modifier(AuraAppear(order: order)) }654}655656private struct ReadingFrameKey: PreferenceKey {657 static let defaultValue: CGRect = .zero658 static func reduce(value: inout CGRect, nextValue: () -> CGRect) { value = nextValue() }659}660661/// A vertical scroll view that reports how far it is scrolled (0…1). Used by the reader for the Focus Trail.662struct ReadingScrollView<Content: View>: View {663 @Binding var progress: Double664 @ViewBuilder var content: () -> Content665666 var body: some View {667 GeometryReader { outer in668 ScrollView {669 content()670 .background(GeometryReader { inner in Color.clear.preference(key: ReadingFrameKey.self, value: inner.frame(in: .named("aura.reading"))) })671 }672 .coordinateSpace(name: "aura.reading")673 .onPreferenceChange(ReadingFrameKey.self) { frame in674 let travel = frame.height - outer.size.height675 let value = travel > 1 ? min(max(-frame.minY / travel, 0), 1) : 0676 Task { @MainActor in if abs(value - progress) > 0.003 || value == 0 || value == 1 { progress = value } }677 }678 }679 }680}681682enum ReaderTheme: String, CaseIterable, Identifiable {683 case auto, paper, dark684 var id: String { rawValue }685 var title: String {686 switch self {687 case .auto: return L("Automatic")688 case .paper: return L("Paper")689 case .dark: return L("Dark")690 }691 }692 var background: Color? {693 switch self {694 case .auto: return nil695 case .paper: return Color(red: 0.976, green: 0.957, blue: 0.918)696 case .dark: return Aura.Palette.obsidian697 }698 }699 var scheme: ColorScheme? {700 switch self {701 case .auto: return nil702 case .paper: return .light703 case .dark: return .dark704 }705 }706}707708/// The reader, shared by the Mac and iOS apps: the article as text, a measure that follows the type size,709/// three themes, the reading time and the Focus Trail.710struct AuraReaderView: View {711 let article: ReaderArticle712 let url: URL?713 var showsTrail = true714 var onClose: (() -> Void)?715 @AppStorage("readerFontSize") private var fontSize = 18.0716 @AppStorage("readerTheme") private var themeName = ReaderTheme.auto.rawValue717 @State private var progress = 0.0718719 private var theme: ReaderTheme { ReaderTheme(rawValue: themeName) ?? .auto }720721 var body: some View {722 VStack(spacing: 0) {723 if showsTrail { FocusTrail(progress: progress) }724 ReadingScrollView(progress: $progress) {725 VStack(alignment: .leading, spacing: 0) {726 header.padding(.bottom, 18)727 Text(article.title).font(.system(size: fontSize * 1.7, weight: .bold, design: .serif)).padding(.bottom, 6)728 .accessibilityAddTraits(.isHeader)729 if let byline = article.byline, !byline.isEmpty {730 Text(byline).font(.system(size: fontSize * 0.8)).foregroundStyle(.secondary).padding(.bottom, 14)731 }732 ForEach(Array(article.blocks.enumerated()), id: \.offset) { _, block in blockView(block) }733 }734 .textSelection(.enabled)735 // The measure follows the type size: about 68 characters per line whatever the zoom.736 .frame(maxWidth: fontSize * 38, alignment: .leading)737 .padding(.horizontal, 28).padding(.vertical, 32)738 .frame(maxWidth: .infinity)739 }740 }741 .background(theme.background ?? Aura.Palette.surface)742 .environment(\.colorScheme, theme.scheme ?? currentScheme)743 .accessibilityIdentifier("reader")744 }745746 @Environment(\.colorScheme) private var currentScheme747748 private var header: some View {749 HStack(spacing: 8) {750 Label(url?.host ?? "", systemImage: "doc.plaintext").font(.caption).foregroundStyle(.secondary).lineLimit(1)751 Text("· " + L("%d min read", article.readingMinutes)).font(.caption).foregroundStyle(.tertiary)752 Spacer()753 Menu {754 Picker(L("Theme"), selection: $themeName) { ForEach(ReaderTheme.allCases) { Text($0.title).tag($0.rawValue) } }755 } label: { Image(systemName: "circle.lefthalf.filled").frame(width: 26, height: 26) }756 .menuStyle(.borderlessButton).fixedSize().accessibilityLabel(L("Reader theme"))757 AuraIconButton(symbol: "textformat.size.smaller", label: L("Smaller text"), size: 26) { fontSize = max(13, fontSize - 1) }758 AuraIconButton(symbol: "textformat.size.larger", label: L("Larger text"), size: 26) { fontSize = min(30, fontSize + 1) }759 if let onClose { AuraIconButton(symbol: "xmark", label: L("Close Reader"), size: 26, action: onClose) }760 }761 }762763 @ViewBuilder private func blockView(_ block: ReaderArticle.Block) -> some View {764 switch block.kind {765 case "h1", "h2":766 Text(block.text).font(.system(size: fontSize * 1.3, weight: .semibold, design: .serif)).padding(.top, 20).padding(.bottom, 8)767 .accessibilityAddTraits(.isHeader)768 case "h3":769 Text(block.text).font(.system(size: fontSize * 1.1, weight: .semibold, design: .serif)).padding(.top, 14).padding(.bottom, 6)770 .accessibilityAddTraits(.isHeader)771 case "li":772 HStack(alignment: .firstTextBaseline, spacing: 8) {773 Text("•").foregroundStyle(.secondary)774 Text(block.text).font(.system(size: fontSize, design: .serif)).lineSpacing(fontSize * 0.35)775 }776 .padding(.bottom, 6)777 case "quote":778 Text(block.text).font(.system(size: fontSize, design: .serif)).italic().lineSpacing(fontSize * 0.35).padding(.leading, 14)779 .overlay(alignment: .leading) { Capsule().fill(Aura.gradient).frame(width: 3) }.padding(.vertical, 8)780 case "pre":781 ScrollView(.horizontal) { Text(block.text).font(.system(size: fontSize * 0.75, design: .monospaced)).padding(12) }782 .background(RoundedRectangle(cornerRadius: Aura.Radius.s).fill(Aura.Palette.hover)).padding(.vertical, 8)783 default:784 Text(block.text).font(.system(size: fontSize, design: .serif)).lineSpacing(fontSize * 0.35).padding(.bottom, fontSize * 0.75)785 }786 }787}788