示例工程 40-windows-scenes-macos
40-windows-scenes-macos/Sources/Ch40Kit/Windows.swift
import Observation
import SwiftUI
/// 一个窗口的静态描述:id、标题、默认尺寸、最小尺寸。
public struct WindowSpec: Hashable, Sendable {
public let id: String
public let title: String
public let width: CGFloat
public let height: CGFloat
public let minWidth: CGFloat
public let minHeight: CGFloat
public init(
id: String,
title: String,
width: CGFloat,
height: CGFloat,
minWidth: CGFloat,
minHeight: CGFloat
) {
self.id = id
self.title = title
self.width = width
self.height = height
self.minWidth = minWidth
self.minHeight = minHeight
}
public var isValid: Bool {
width >= minWidth && height >= minHeight && minWidth > 0 && minHeight > 0
}
}
public enum WindowCatalog {
public static let main = WindowSpec(
id: "main",
title: "书库",
width: 900,
height: 600,
minWidth: 600,
minHeight: 400
)
public static let stats = WindowSpec(
id: "stats",
title: "统计",
width: 420,
height: 320,
minWidth: 320,
minHeight: 240
)
public static let settings = WindowSpec(
id: "settings",
title: "设置",
width: 480,
height: 360,
minWidth: 400,
minHeight: 300
)
public static var all: [WindowSpec] { [main, stats, settings] }
public static func spec(id: String) -> WindowSpec? {
all.first { $0.id == id }
}
}
/// 跨窗口共享的状态:放在 App 级,由所有场景读取。
@Observable
public final class Workspace {
public var documentCount: Int
public var selectedBookID: UUID?
public init(documentCount: Int = 0, selectedBookID: UUID? = nil) {
self.documentCount = documentCount
self.selectedBookID = selectedBookID
}
public func addDocument() {
documentCount += 1
}
}
/// 主窗口内容:打开统计窗口与设置。
public struct MainWindow: View {
@Environment(\.openWindow) private var openWindow
public let workspace: Workspace
public init(workspace: Workspace) {
self.workspace = workspace
}
public var body: some View {
VStack(spacing: 14) {
Text("主窗口").font(.title2)
Text("文档数:\(workspace.documentCount)")
.font(.system(size: 12, design: .monospaced))
HStack {
Button("新建文档") { workspace.addDocument() }
Button("统计") { openWindow(id: WindowCatalog.stats.id) }
Button("设置") { openWindow(id: WindowCatalog.settings.id) }
}
.buttonStyle(.bordered)
}
.padding(24)
}
}
/// 统计窗口:读取同一个共享模型,证明多窗口共享状态。
public struct StatsWindow: View {
public let workspace: Workspace
public init(workspace: Workspace) {
self.workspace = workspace
}
public var body: some View {
VStack(spacing: 10) {
Text("统计").font(.headline)
Text("文档数:\(workspace.documentCount)")
.font(.system(size: 28, weight: .bold, design: .rounded))
.monospacedDigit()
Text("窗口之间共享同一个 `Workspace` 实例。")
.font(.system(size: 11))
.foregroundStyle(.secondary)
}
.padding(24)
}
}
/// 截图:三张窗口规格卡片。
public struct WindowBoard: View {
public init() {}
public var body: some View {
VStack(alignment: .leading, spacing: 12) {
Text("窗口与场景")
.font(.system(size: 18, weight: .semibold))
ForEach(WindowCatalog.all, id: \.id) { spec in
HStack(spacing: 10) {
RoundedRectangle(cornerRadius: 6)
.fill(.blue.opacity(0.15))
.frame(width: 44, height: 32)
.overlay(
Text(spec.id.prefix(3))
.font(.system(size: 9, design: .monospaced))
)
VStack(alignment: .leading, spacing: 2) {
Text(spec.title).font(.system(size: 13, weight: .medium))
Text(
"\(Int(spec.width))×\(Int(spec.height)) · 最小 \(Int(spec.minWidth))×\(Int(spec.minHeight))"
)
.font(.system(size: 10, design: .monospaced))
.foregroundStyle(.secondary)
}
Spacer()
}
}
}
.padding(18)
.frame(width: 360, alignment: .leading)
.background(Color.white)
}
}
/// 截图:主窗口静态预览(不依赖 openWindow 环境)。
public struct MainWindowBoard: View {
public init() {}
public var body: some View {
VStack(spacing: 12) {
Text("主窗口").font(.system(size: 15, weight: .semibold))
Text("文档数:3").font(.system(size: 12, design: .monospaced))
HStack(spacing: 8) {
ForEach(["新建文档", "统计", "设置"], id: \.self) { title in
Text(title)
.font(.system(size: 10, design: .monospaced))
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(.quaternary, in: RoundedRectangle(cornerRadius: 5))
}
}
}
.padding(18)
.frame(width: 360)
.background(Color.white)
}
}
/// 截图:设置窗静态预览。
public struct SettingsWindowBoard: View {
public init() {}
private let rows: [(String, String)] = [
("外观", "跟随系统"),
("自动保存", "开"),
("同步", "关"),
]
public var body: some View {
VStack(alignment: .leading, spacing: 10) {
Text("设置").font(.system(size: 15, weight: .semibold))
ForEach(rows, id: \.0) { row in
HStack {
Text(row.0).font(.system(size: 11))
Spacer()
Text(row.1)
.font(.system(size: 11, design: .monospaced))
.foregroundStyle(.secondary)
}
}
}
.padding(18)
.frame(width: 360, alignment: .leading)
.background(Color.white)
}
}