示例工程 54-ios-app-anatomy
54-ios-app-anatomy/Sources/Ch54Kit/Anatomy.swift
import Foundation
import SwiftUI
public struct PlatformProfile: Sendable {
public let name: String
public let minimumSystem: String
public let menuBar: Bool
public let multipleWindows: Bool
public let pointer: Bool
public let safeAreaInsets: Bool
public init(
name: String,
minimumSystem: String,
menuBar: Bool,
multipleWindows: Bool,
pointer: Bool,
safeAreaInsets: Bool
) {
self.name = name
self.minimumSystem = minimumSystem
self.menuBar = menuBar
self.multipleWindows = multipleWindows
self.pointer = pointer
self.safeAreaInsets = safeAreaInsets
}
public static let macOS = PlatformProfile(
name: "macOS",
minimumSystem: "27.0",
menuBar: true,
multipleWindows: true,
pointer: true,
safeAreaInsets: false
)
public static let iOS = PlatformProfile(
name: "iOS",
minimumSystem: "26.0",
menuBar: false,
multipleWindows: false,
pointer: false,
safeAreaInsets: true
)
}
public struct SafeArea: Equatable, Sendable {
public let top: Double
public let bottom: Double
public let leading: Double
public let trailing: Double
public init(top: Double, bottom: Double, leading: Double = 0, trailing: Double = 0) {
self.top = top
self.bottom = bottom
self.leading = leading
self.trailing = trailing
}
public func contentRect(in bounds: CGRect) -> CGRect {
CGRect(
x: bounds.minX + leading,
y: bounds.minY + top,
width: max(0, bounds.width - leading - trailing),
height: max(0, bounds.height - top - bottom)
)
}
public static let phoneWithNotch = SafeArea(top: 59, bottom: 34)
public static let phoneWithoutNotch = SafeArea(top: 20, bottom: 0)
public static let pad = SafeArea(top: 24, bottom: 20)
}
public enum AppEvent: String, CaseIterable, Sendable {
case launch
case activate
case deactivate
case background
case foreground
case terminate
}
public enum AppState: String, Sendable {
case notRunning
case inactive
case active
case background
case terminated
}
public enum Lifecycle {
public static func reduce(_ state: AppState, _ event: AppEvent) -> AppState {
switch (state, event) {
case (.notRunning, .launch): .inactive
case (_, .activate): .active
case (.active, .deactivate): .inactive
case (.inactive, .background): .background
case (.background, .foreground): .inactive
case (_, .terminate): .terminated
default: state
}
}
public static func canAnimate(_ state: AppState) -> Bool {
state == .active
}
}
public struct AnatomyBoard: View {
public init() {}
public var body: some View {
VStack(alignment: .leading, spacing: 10) {
Text("iOS App 解剖").font(.system(size: 17, weight: .semibold))
row("入口", "@main App + WindowGroup")
row("场景", "单窗口(iPad 可分屏)")
row("安全区", "刘海 / 灵动岛 / Home 指示条")
row("生命周期", "active / inactive / background")
row("能力", "无菜单栏、指针可选")
row("部署", "iOS 26.0")
}
.padding(18)
.frame(width: 340, alignment: .leading)
.background(Color.white)
}
private func row(_ title: String, _ detail: String) -> some View {
HStack(spacing: 10) {
Text(title)
.font(.system(size: 10, design: .monospaced))
.frame(width: 60, alignment: .leading)
Text(detail)
.font(.system(size: 11))
.foregroundStyle(.secondary)
Spacer()
}
}
}