import SwiftUI struct ContentView: View { @EnvironmentObject var ble: GlassBLEManager @State private var selected = 0 @Environment(\.scenePhase) private var scenePhase var body: some View { VStack(spacing: 0) { // 顶栏(轻量) VStack(spacing: 0) { topBar if ble.connectionState == .ready && ble.hasReceivedClickReadyStatus && !ble.clickReady { smallWarn } } .background(.regularMaterial) Divider() // 内容区(占满剩余高度,绝不溢出到底栏) Group { if selected == 0 { ControlView() } else { CastView() } } .frame(maxWidth: .infinity, maxHeight: .infinity) Divider() // 自定义底部导航栏:全宽钉底 bottomBar } .background(Color(.systemGroupedBackground)) .onAppear { ble.connect() } // 回到前台做链路体检:后台断链后 connectionState 可能仍停在 .ready, // 表现为「显示已就绪但按键全部无效」。体检发现失效会自动重建链路。 .onChange(of: scenePhase) { _, phase in if phase == .active { ble.healthCheck() } } } // 浅色顶栏 private var topBar: some View { HStack(spacing: 10) { Image(systemName: "eyeglasses") .font(.headline).foregroundColor(.cyan) VStack(alignment: .leading, spacing: 1) { Text("GlassMouse").font(.subheadline.weight(.semibold)) HStack(spacing: 5) { Circle() .fill(ble.connectionState == .ready ? Color.green : Color.orange) .frame(width: 7, height: 7) Text(ble.connectionState.rawValue) .font(.caption2).foregroundColor(.secondary) } } Spacer() Button(action: { ble.connect() }) { Image(systemName: "arrow.clockwise") .font(.subheadline.weight(.semibold)) .frame(width: 32, height: 32) .background(Color.cyan.opacity(0.12)) .foregroundColor(.cyan) .clipShape(Circle()) } } .padding(.horizontal, 14).padding(.vertical, 6) } // 小条警告 private var smallWarn: some View { HStack(spacing: 6) { Image(systemName: "exclamationmark.triangle.fill") .font(.caption).foregroundColor(.orange) Text("无障碍未开:眼镜上 设置→无障碍→GlassMouse") .font(.caption).foregroundColor(.secondary) .lineLimit(1) } .padding(.horizontal, 14).padding(.vertical, 4) .frame(maxWidth: .infinity, alignment: .leading) .background(Color.orange.opacity(0.10)) } // 自定义底部导航栏:全宽钉底 private var bottomBar: some View { HStack(spacing: 0) { tabBtn(0, "hand.point.up.left.fill", "控制") tabBtn(1, "text.alignleft", "投屏") } .frame(height: 50) .background(.regularMaterial) } private func tabBtn(_ idx: Int, _ icon: String, _ label: String) -> some View { Button(action: { selected = idx }) { VStack(spacing: 2) { Image(systemName: icon).font(.system(size: 20)) Text(label).font(.caption2) } .frame(maxWidth: .infinity) .foregroundColor(selected == idx ? .cyan : .secondary) } } }