Files
glass/ios/GlassRemote/Sources/Views/CastView.swift
T
admin 2840e82685 Initial commit: Android GlassMouse + iOS GlassRemote 智能眼镜遥控项目
包含本次会话修复:
- iOS clickReady 警告 bug(默认 false 永远显示)
- BLE 'handle is invalid' 排查(stale handle auto-retry 实验 + 回退)

项目结构:
- glass-mouse/: 眼镜端原生 Kotlin App,前台服务 MouseService + 自定义 BLE GATT 桥接 ProxyBleServer + 无障碍 GestureDescription 注入
- ios/GlassRemote/: iPhone 端 SwiftUI + CoreBluetooth,通过 BLE 直连眼镜
- HTTP 投屏服务(:8000) 接 PPT/WPS 备注宏 POST 推送

技术栈: Android Kotlin (gradle 8.14.4, compileSdk 34), iOS Swift 5.10 (Xcode 26.5)
2026-08-26 10:34:23 +08:00

144 lines
5.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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..<min(i + linesPerPage, lines.count)].joined(separator: "\n"))
}
return res.isEmpty ? [""] : res
}
private func castCurrentPage() {
guard !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return }
guard ble.connectionState == .ready else {
ble.appendLog("❌ 投屏失败:BLE 未就绪(\(ble.connectionState.rawValue))")
return
}
pages = paginate(text)
let payload = pages[curPage]
ble.appendLog("📺 投屏请求:第 \(curPage+1)/\(pages.count) 页,\(payload.count) 字")
ble.sendFragmented(["type": "text", "content": payload])
}
private func flip(_ d: Int) {
guard !autoScroll else { return }
let n = curPage + d
guard n >= 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)
}
}