import SwiftUI struct CastView: View { @EnvironmentObject var ble: GlassBLEManager @AppStorage("scrollSpeed") private var speed = 7.0 @State private var text: String = "" @State private var autoScroll = false @State private var curPage = 0 @State private var pages: [String] = [""] private let linesPerPage = 8 var body: some View { ScrollView { VStack(spacing: 12) { // 眼镜 IP 小提示(投屏相关;连接状态走全局顶栏) if !ble.glassesIp.isEmpty { HStack(spacing: 5) { Image(systemName: "wifi").font(.caption2).foregroundColor(.cyan) Text("眼镜 IP \(ble.glassesIp)") .font(.caption2).foregroundColor(.secondary) Spacer() } } // 文本编辑(白卡圆角,与 Control 触摸板一致) TextEditor(text: $text) .font(.system(.body, design: .monospaced)) .scrollContentBackground(.hidden) .background(Color(.secondarySystemGroupedBackground)) .clipShape(RoundedRectangle(cornerRadius: 14)) .frame(minHeight: 100, idealHeight: 140, maxHeight: 220) .onChange(of: text) { _, _ in if !autoScroll { pages = paginate(text) if curPage >= pages.count { curPage = max(0, pages.count - 1) } } } // 翻页进度 HStack { Text("第 \(curPage + 1) / \(pages.count) 页") .font(.caption).foregroundColor(.secondary) Spacer() } // 自动滚动 + 速度(白卡) HStack(spacing: 10) { Button(action: toggleAutoScroll) { Label(autoScroll ? "暂停" : "自动滚动", systemImage: autoScroll ? "pause.fill" : "play.fill") .frame(maxWidth: .infinity) } .buttonStyle(.bordered).tint(.cyan) VStack(alignment: .leading, spacing: 2) { Text("速度 \(Int(speed))").font(.caption2).foregroundColor(.secondary) Slider(value: $speed, in: 1...10, step: 1) { _ in if autoScroll { ble.send(["type": "text", "action": "set_speed", "speed": Int(speed)], withResponse: true) } } .tint(.cyan) } } .padding(12) .background(Color(.secondarySystemGroupedBackground)) .clipShape(RoundedRectangle(cornerRadius: 14)) // 翻页按钮 HStack(spacing: 10) { Button(action: { flip(-1) }) { Label("上翻", systemImage: "chevron.up").frame(maxWidth: .infinity) }.buttonStyle(.bordered) Button(action: { flip(1) }) { Label("下翻", systemImage: "chevron.down").frame(maxWidth: .infinity) }.buttonStyle(.bordered) } // 投屏 / 关闭 HStack(spacing: 10) { Button(action: castCurrentPage) { Label("📺 投屏", systemImage: "airplayvideo").frame(maxWidth: .infinity) } .buttonStyle(.borderedProminent).tint(.cyan) Button(action: closeCast) { Label("关闭", systemImage: "xmark.circle").frame(maxWidth: .infinity) } .buttonStyle(.bordered) } Spacer(minLength: 4) } .padding(.horizontal, 16).padding(.top, 8).padding(.bottom, 4) } .scrollDismissesKeyboard(.interactively) .background(Color(.systemGroupedBackground)) .onAppear { pages = paginate(text) } } // MARK: - 逻辑 private func paginate(_ t: String) -> [String] { let lines = t.split(separator: "\n", omittingEmptySubsequences: false).map(String.init) var res: [String] = [] for i in stride(from: 0, to: max(1, lines.count), by: linesPerPage) { res.append(lines[i..= 0, n < pages.count else { return } curPage = n ble.sendFragmented(["type": "text", "content": pages[curPage]]) } private func toggleAutoScroll() { if !autoScroll { guard !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return } autoScroll = true ble.sendFragmented(["type": "text", "action": "auto_scroll", "content": text, "speed": Int(speed)]) } else { autoScroll = false ble.send(["type": "text", "action": "stop_scroll"], withResponse: true) } } private func closeCast() { autoScroll = false ble.send(["type": "text", "action": "close"], withResponse: true) } }