示例工程 59-capstone-polish-and-animation

59-capstone-polish-and-animation/Sources/Ch59Kit/Polish.swift

swift · 7.6 KB

import Foundation
import SwiftUI
 
public struct RankedBookmark: Equatable, Sendable {
    public let title: String
    public let url: String
    public let tags: [String]
    public let score: Int
 
    public init(title: String, url: String, tags: [String], score: Int) {
        self.title = title
        self.url = url
        self.tags = tags
        self.score = score
    }
}
 
public enum SearchRanker {
    /// 标题前缀 > 标题包含 > 标签 > URL;同分按标题排序。
    public static func score(title: String, url: String, tags: [String], query: String) -> Int {
        let q = query.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
        guard !q.isEmpty else { return 0 }
        let t = title.lowercased()
        var score = 0
        if t.hasPrefix(q) { score += 100 } else if t.contains(q) { score += 60 }
        if tags.contains(where: { $0.lowercased().contains(q) }) { score += 40 }
        if url.lowercased().contains(q) { score += 20 }
        return score
    }
 
    public static func rank(_ items: [RankedBookmark], query: String) -> [RankedBookmark] {
        items
            .map {
                RankedBookmark(
                    title: $0.title,
                    url: $0.url,
                    tags: $0.tags,
                    score: score(title: $0.title, url: $0.url, tags: $0.tags, query: query)
                )
            }
            .filter { query.trimmingCharacters(in: .whitespaces).isEmpty || $0.score > 0 }
            .sorted {
                if $0.score != $1.score { return $0.score > $1.score }
                return $0.title.localizedStandardCompare($1.title) == .orderedAscending
            }
    }
}
 
public enum MotionMode: String, Sendable {
    case full
    case reduced
 
    public static func resolve(reduceMotion: Bool) -> MotionMode {
        reduceMotion ? .reduced : .full
    }
 
    public var listInsertion: String {
        switch self {
        case .full: "移动 + 淡入"
        case .reduced: "仅淡入"
        }
    }
 
    public var duration: Double {
        switch self {
        case .full: 0.28
        case .reduced: 0.16
        }
    }
}
 
public enum RowAccessibility {
    public static func label(title: String, url: String, tags: [String]) -> String {
        let tagPart = tags.isEmpty ? "未分类" : tags.joined(separator: "、")
        return "\(title),\(url),标签 \(tagPart)"
    }
 
    public static func deleteLabel(title: String) -> String {
        "删除书签 \(title)"
    }
}
 
public struct Shortcut: Hashable, Sendable {
    public let key: String
    public let command: Bool
    public let action: String
 
    public init(key: String, command: Bool = true, action: String) {
        self.key = key
        self.command = command
        self.action = action
    }
 
    public static let table: [Shortcut] = [
        Shortcut(key: "n", action: "新建"),
        Shortcut(key: "f", action: "搜索"),
        Shortcut(key: "backspace", command: false, action: "删除"),
        Shortcut(key: "escape", command: false, action: "清空搜索"),
    ]
 
    public static func resolve(key: String, command: Bool) -> String? {
        table.first { $0.key == key && $0.command == command }?.action
    }
}
 
public struct PolishBoard: View {
    public init() {}
 
    public var body: some View {
        VStack(alignment: .leading, spacing: 9) {
            Text("打磨与动画").font(.system(size: 17, weight: .semibold))
            row("排序", "标题前缀 > 包含 > 标签 > URL")
            row("动效", "位移 + 淡入,减少动态时仅淡入")
            row("无障碍", "行标签、删除标签、焦点")
            row("快捷键", "⌘N / ⌘F / Delete / Esc")
            row("空状态", "引导 + 清除搜索")
            row("性能", "body 计数与滚动测量")
        }
        .padding(18)
        .frame(width: 360, 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: 56, alignment: .leading)
            Text(detail)
                .font(.system(size: 11))
                .foregroundStyle(.secondary)
            Spacer()
        }
    }
}
 
public struct ReducedMotionBoard: View {
    public init() {}
 
    private let rows: [(String, String)] = [
        ("默认", "移动 + 淡入 0.28s"),
        ("减少", "仅淡入 0.16s"),
        ("保留", "颜色与淡入反馈"),
        ("去掉", "位移、缩放、视差"),
    ]
 
    public var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("减少动态效果").font(.system(size: 15, weight: .semibold))
            ForEach(rows, id: \.0) { row in
                HStack(spacing: 10) {
                    Text(row.0)
                        .font(.system(size: 10, design: .monospaced))
                        .frame(width: 42, alignment: .leading)
                    Text(row.1)
                        .font(.system(size: 11))
                        .foregroundStyle(.secondary)
                    Spacer()
                }
            }
        }
        .padding(18)
        .frame(width: 340, alignment: .leading)
        .background(Color.white)
    }
}
 
public struct AccessibilityBoard: View {
    public init() {}
 
    private let rows: [(String, String)] = [
        ("行", RowAccessibility.label(title: "Swift", url: "swift.org", tags: ["语言"])),
        ("删除", RowAccessibility.deleteLabel(title: "Swift")),
        ("空态", "没有匹配的书签,清除搜索"),
        ("加载", "正在加载,请稍候"),
    ]
 
    public var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("无障碍标签").font(.system(size: 15, weight: .semibold))
            ForEach(rows, id: \.0) { row in
                HStack(alignment: .top, spacing: 10) {
                    Text(row.0)
                        .font(.system(size: 10, design: .monospaced))
                        .frame(width: 42, alignment: .leading)
                    Text(row.1)
                        .font(.system(size: 11))
                        .foregroundStyle(.secondary)
                        .fixedSize(horizontal: false, vertical: true)
                    Spacer()
                }
            }
        }
        .padding(18)
        .frame(width: 340, alignment: .leading)
        .background(Color.white)
    }
}
 
public struct ShortcutBoard: View {
    public init() {}
 
    public var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("快捷键").font(.system(size: 15, weight: .semibold))
            ForEach(Shortcut.table, id: \.key) { shortcut in
                HStack(spacing: 10) {
                    Text(shortcut.display)
                        .font(.system(size: 10, design: .monospaced))
                        .frame(width: 86, alignment: .leading)
                    Text(shortcut.action)
                        .font(.system(size: 11))
                        .foregroundStyle(.secondary)
                    Spacer()
                }
            }
        }
        .padding(18)
        .frame(width: 340, alignment: .leading)
        .background(Color.white)
    }
}
 
extension Shortcut {
    /// 展示用组合键文本,例如 "⌘F"、"Delete"。
    public var display: String {
        let base: String
        switch key {
        case "backspace": base = "Delete"
        case "escape": base = "Esc"
        default: base = key.uppercased()
        }
        return command ? "⌘\(base)" : base
    }
}