示例工程 55-ios-adaptive-layout

55-ios-adaptive-layout/Sources/Ch55Kit/Adaptive.swift

swift · 4.5 KB

import Foundation
import SwiftUI
 
public enum SizeClass: String, Sendable {
    case compact
    case regular
}
 
public enum Orientation: String, Sendable {
    case portrait
    case landscape
}
 
public struct AdaptiveInput: Sendable {
    public let width: SizeClass
    public let height: SizeClass
    public let orientation: Orientation
    public let isSplitView: Bool
 
    public init(
        width: SizeClass,
        height: SizeClass,
        orientation: Orientation = .portrait,
        isSplitView: Bool = false
    ) {
        self.width = width
        self.height = height
        self.orientation = orientation
        self.isSplitView = isSplitView
    }
 
    public static let phonePortrait = AdaptiveInput(width: .compact, height: .regular)
    public static let phoneLandscape = AdaptiveInput(
        width: .regular,
        height: .compact,
        orientation: .landscape
    )
    public static let padFull = AdaptiveInput(width: .regular, height: .regular)
    public static let padSplit = AdaptiveInput(
        width: .compact,
        height: .regular,
        isSplitView: true
    )
}
 
public struct LayoutDecision: Equatable, Sendable {
    public let columns: Int
    public let showsSidebar: Bool
    public let usesSplitNavigation: Bool
    public let contentMaxWidth: Double?
 
    public init(
        columns: Int, showsSidebar: Bool, usesSplitNavigation: Bool, contentMaxWidth: Double?
    ) {
        self.columns = columns
        self.showsSidebar = showsSidebar
        self.usesSplitNavigation = usesSplitNavigation
        self.contentMaxWidth = contentMaxWidth
    }
}
 
public enum AdaptiveLayout {
    public static func decide(_ input: AdaptiveInput) -> LayoutDecision {
        if input.width == .regular && input.height == .regular && !input.isSplitView {
            return LayoutDecision(
                columns: 3,
                showsSidebar: true,
                usesSplitNavigation: true,
                contentMaxWidth: nil
            )
        }
        if input.width == .regular {
            return LayoutDecision(
                columns: 2,
                showsSidebar: false,
                usesSplitNavigation: input.orientation == .landscape,
                contentMaxWidth: 720
            )
        }
        return LayoutDecision(
            columns: 1,
            showsSidebar: false,
            usesSplitNavigation: false,
            contentMaxWidth: 560
        )
    }
}
 
public struct AdaptiveBoard: View {
    public init() {}
 
    public var body: some View {
        VStack(alignment: .leading, spacing: 10) {
            Text("自适应布局").font(.system(size: 17, weight: .semibold))
            row("iPhone 竖屏", "1 列 · 栈式导航")
            row("iPhone 横屏", "2 列 · 可选分栏")
            row("iPad 全屏", "3 列 · 侧栏 + 详情")
            row("iPad 分屏", "1 列 · 栈式导航")
            row("键盘", "安全区避让")
            row("触控", "≥ 44pt")
        }
        .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: 90, alignment: .leading)
            Text(detail)
                .font(.system(size: 11))
                .foregroundStyle(.secondary)
            Spacer()
        }
    }
}
 
/// 截图:同一份声明布局在手机/平板两种宽度下的列数。
public struct AdaptivePreview: View {
    public let title: String
    public let columns: [String]
    public let width: CGFloat
 
    public init(title: String, columns: [String], width: CGFloat) {
        self.title = title
        self.columns = columns
        self.width = width
    }
 
    public var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text(title).font(.system(size: 13, weight: .semibold))
            HStack(spacing: 8) {
                ForEach(columns, id: \.self) { column in
                    Text(column)
                        .font(.system(size: 9, design: .monospaced))
                        .frame(maxWidth: .infinity, maxHeight: .infinity)
                        .background(.quaternary, in: RoundedRectangle(cornerRadius: 6))
                }
            }
            .frame(height: width < 500 ? 160 : 220)
        }
        .padding(12)
        .frame(width: width)
        .background(Color.white)
    }
}