包含本次会话修复: - 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)
278 lines
13 KiB
Swift
278 lines
13 KiB
Swift
import SwiftUI
|
||
|
||
/// 真实硬件 touch 风格的边缘手势控制板:
|
||
/// - 上/下边:左右滑 → 真实硬件 touch 滑动手势(视频进度/横向滚动)
|
||
/// - 左/右边:上下滑 → 真实硬件 touch 滑动手势(亮度/音量/纵向滚动)
|
||
/// - 中央:单击=点击;双击=返回;轻点拖动=短滑(fallback)
|
||
/// 所有边缘手势走 BLE → 眼镜端 GestureDescription → 系统级真实触摸事件,
|
||
/// 效果等同 INMO 官方硬件 touch。
|
||
struct ControlView: View {
|
||
@EnvironmentObject var ble: GlassBLEManager
|
||
@AppStorage("sensitivity") private var sensitivity = 1.6
|
||
@AppStorage("cursorOn") private var cursorOn = true
|
||
@AppStorage("screenOn") private var screenOnPref = true
|
||
@AppStorage("centerMode") private var centerMode: String = "cursor" // 默认 = 光标移动;"swipe" = 真实 touch 短滑
|
||
// 启动时强制检查:如果默认值是 swipe(v15 留下的),改回 cursor
|
||
private let didInitMode: Bool = {
|
||
UserDefaults.standard.register(defaults: ["centerMode": "cursor"])
|
||
return true
|
||
}()
|
||
|
||
@State private var lastTapTime: Date = .distantPast
|
||
|
||
// 屏幕几何假设(眼镜端 INMO AIR3 通常 1920x1080 或 1280x720;这里用通用值)
|
||
// 真实屏幕坐标通过 BLE status 拿到会更好;先用经验值
|
||
private let screenW = 1920
|
||
private let screenH = 1080
|
||
|
||
var body: some View {
|
||
VStack(spacing: 10) {
|
||
funcIcons
|
||
gesturePadCard
|
||
bottomBar
|
||
}
|
||
.padding(.horizontal, 16).padding(.top, 8).padding(.bottom, 4)
|
||
}
|
||
|
||
// MARK: 顶部 3 个功能图标
|
||
private var funcIcons: some View {
|
||
HStack(spacing: 12) {
|
||
iconBtn(system: "wand.and.rays",
|
||
label: "灵敏 \(String(format: "%.1f", sensitivity))") {
|
||
sensitivity = sensitivity >= 2.9 ? 0.3 : (sensitivity + 0.2)
|
||
sensitivity = (sensitivity * 10).rounded() / 10
|
||
}
|
||
iconBtn(system: cursorOn ? "scope" : "minus.scope",
|
||
label: cursorOn ? "光标开" : "光标关") {
|
||
cursorOn.toggle()
|
||
ble.setCursor(cursorOn)
|
||
}
|
||
iconBtn(system: screenOnPref ? "sun.max.fill" : "moon.fill",
|
||
label: screenOnPref ? "常亮" : "节能") {
|
||
screenOnPref.toggle()
|
||
ble.screenOn()
|
||
}
|
||
iconBtn(system: centerMode == "cursor" ? "scope" : "hand.draw.fill",
|
||
label: centerMode == "cursor" ? "光标模式" : "短滑模式") {
|
||
centerMode = centerMode == "cursor" ? "swipe" : "cursor"
|
||
}
|
||
}
|
||
}
|
||
|
||
private func iconBtn(system: String, label: String, action: @escaping () -> Void) -> some View {
|
||
Button(action: action) {
|
||
VStack(spacing: 5) {
|
||
Image(systemName: system)
|
||
.font(.title3).foregroundColor(.cyan)
|
||
.frame(width: 44, height: 44)
|
||
.background(Color(.secondarySystemGroupedBackground))
|
||
.clipShape(RoundedRectangle(cornerRadius: 14))
|
||
.shadow(color: .black.opacity(0.08), radius: 4, y: 1)
|
||
Text(label).font(.caption2).foregroundColor(.secondary)
|
||
.lineLimit(1)
|
||
}
|
||
.frame(maxWidth: .infinity)
|
||
}
|
||
}
|
||
|
||
// MARK: 边缘手势控制板(真实硬件 touch)
|
||
private var gesturePadCard: some View {
|
||
VStack(spacing: 0) {
|
||
// 顶部边缘带:左右滑 → 真实硬件 touch 横向滑动
|
||
// 从屏幕顶部 10% 起,向下滑到顶部 30%(避开状态栏,从 y=screenH*0.10 到 y=screenH*0.30)
|
||
// 横向幅度 = 灵敏度 * 100 px
|
||
edgeBand(axis: .horizontal, side: .top)
|
||
.frame(height: 36)
|
||
|
||
// 中间:左边缘 + 中央 + 右边缘
|
||
HStack(spacing: 0) {
|
||
edgeBand(axis: .vertical, side: .left)
|
||
.frame(width: 36)
|
||
centerPad
|
||
edgeBand(axis: .vertical, side: .right)
|
||
.frame(width: 36)
|
||
}
|
||
.frame(maxHeight: .infinity)
|
||
|
||
// 底部边缘带:左右滑 → 真实硬件 touch 横向滑动
|
||
edgeBand(axis: .horizontal, side: .bottom)
|
||
.frame(height: 36)
|
||
}
|
||
.background(Color(.secondarySystemGroupedBackground))
|
||
.clipShape(RoundedRectangle(cornerRadius: 24))
|
||
.shadow(color: .black.opacity(0.12), radius: 12, y: 4)
|
||
.frame(maxHeight: .infinity)
|
||
}
|
||
|
||
enum Axis { case horizontal, vertical }
|
||
enum Side { case top, bottom, left, right }
|
||
|
||
/// 边缘手势带:拖动 → 真实硬件 touch 滑动手势(GestureDescription 通道)
|
||
private func edgeBand(axis: Axis, side: Side) -> some View {
|
||
let isHorizontal = axis == .horizontal
|
||
// 起止 y/x:上边带从顶部 10%→30%;下边带 70%→90%;左边带从左 10%→30%;右边带 70%→90%
|
||
// 终点根据用户拖动距离计算(乘以灵敏度放大到屏幕坐标)
|
||
let stepPx: CGFloat = 300 // 单次手势的物理像素位移
|
||
return ZStack {
|
||
RoundedRectangle(cornerRadius: 0)
|
||
.fill(Color.cyan.opacity(0.10))
|
||
VStack(spacing: 2) {
|
||
Image(systemName: isHorizontal ? "arrow.left.and.right" : "arrow.up.and.down")
|
||
.font(.caption2).foregroundColor(.cyan.opacity(0.7))
|
||
Text(isHorizontal ? "左右滑\n进度条" : "上下滑\n亮度/音量")
|
||
.font(.system(size: 9))
|
||
.foregroundColor(.secondary.opacity(0.8))
|
||
.multilineTextAlignment(.center)
|
||
.lineLimit(2)
|
||
}
|
||
}
|
||
.gesture(
|
||
DragGesture(minimumDistance: 20)
|
||
.onEnded { v in
|
||
// 起点:屏幕边缘中段(避开状态栏 + 底部手势区)
|
||
let startX: Int
|
||
let startY: Int
|
||
switch side {
|
||
case .top: startX = screenW / 2; startY = Int(Double(screenH) * 0.20)
|
||
case .bottom: startX = screenW / 2; startY = Int(Double(screenH) * 0.80)
|
||
case .left: startX = Int(Double(screenW) * 0.20); startY = screenH / 2
|
||
case .right: startX = Int(Double(screenW) * 0.80); startY = screenH / 2
|
||
}
|
||
// 终点:起点 + 用户拖动方向 × 屏幕 30%
|
||
let endX: Int
|
||
let endY: Int
|
||
if isHorizontal {
|
||
let dx = Int(v.translation.width * CGFloat(sensitivity) * CGFloat(stepPx) / 100)
|
||
endX = min(max(startX + dx, 50), screenW - 50)
|
||
endY = startY
|
||
} else {
|
||
let dy = Int(v.translation.height * CGFloat(sensitivity) * CGFloat(stepPx) / 100)
|
||
endX = startX
|
||
endY = min(max(startY + dy, 50), screenH - 50)
|
||
}
|
||
// 手势时长 300ms:长一点更接近"硬件 touch"质感
|
||
ble.swipeGesture(x1: startX, y1: startY, x2: endX, y2: endY, durationMs: 300)
|
||
}
|
||
)
|
||
}
|
||
|
||
// 中央触摸板:单击=点击;双击=返回;拖动=短滑(兜底)
|
||
@State private var centerLast: CGPoint? = nil
|
||
private var centerPad: some View {
|
||
ZStack {
|
||
RoundedRectangle(cornerRadius: 0)
|
||
.fill(Color(.systemBackground))
|
||
VStack(spacing: 6) {
|
||
Image(systemName: centerMode == "cursor" ? "scope" : "hand.draw.fill")
|
||
.font(.system(size: 28))
|
||
.foregroundColor(.cyan.opacity(0.4))
|
||
Text(centerMode == "cursor" ? "拖动 = 移光标" : "拖动 = 短滑")
|
||
.font(.caption2).foregroundColor(.secondary)
|
||
Text("单击/双击 = 点击")
|
||
.font(.system(size: 10))
|
||
.foregroundColor(.secondary.opacity(0.7))
|
||
}
|
||
}
|
||
.contentShape(Rectangle())
|
||
.gesture(
|
||
// 拖动优先:8pt 阈值下发真实 touch 短滑;不足时 onEnded 判定 tap
|
||
DragGesture(minimumDistance: 0)
|
||
.onChanged { v in
|
||
// "光标模式"下:实时下发 mouse move(拖到哪移哪)
|
||
let dx = (v.location.x - (centerLast ?? v.location).x) * CGFloat(sensitivity)
|
||
let dy = (v.location.y - (centerLast ?? v.location).y) * CGFloat(sensitivity)
|
||
if centerMode == "cursor", abs(dx) >= 1 || abs(dy) >= 1 {
|
||
ble.move(dx: Int(dx), dy: Int(dy))
|
||
}
|
||
centerLast = v.location
|
||
}
|
||
.onEnded { v in
|
||
defer { centerLast = nil }
|
||
let totalDist = hypot(v.location.x - v.startLocation.x,
|
||
v.location.y - v.startLocation.y)
|
||
if totalDist > 8 {
|
||
// "短滑模式":拖动结束后发一次真实 touch 滑动手势
|
||
if centerMode == "swipe" {
|
||
let startX = screenW / 2
|
||
let startY = screenH / 2
|
||
let dx = Int(v.translation.width * CGFloat(sensitivity) * 2)
|
||
let dy = Int(v.translation.height * CGFloat(sensitivity) * 2)
|
||
let endX = min(max(startX + dx, 50), screenW - 50)
|
||
let endY = min(max(startY + dy, 50), screenH - 50)
|
||
ble.swipeGesture(x1: startX, y1: startY, x2: endX, y2: endY, durationMs: 250)
|
||
}
|
||
lastTapTime = .distantPast
|
||
} else if totalDist < 8 {
|
||
// 短按 → click / 双击 = back
|
||
handleTap()
|
||
}
|
||
}
|
||
)
|
||
}
|
||
|
||
private func handleTap() {
|
||
let now = Date()
|
||
if now.timeIntervalSince(lastTapTime) < 0.3 {
|
||
// 双击 = 再 click 一次(不放 back,让系统自然处理双击语义)
|
||
ble.click()
|
||
lastTapTime = .distantPast
|
||
} else {
|
||
ble.click()
|
||
lastTapTime = now
|
||
}
|
||
}
|
||
|
||
// MARK: 底部三按钮(屏幕开/关 + 返回 + 主页 + 拍照)
|
||
private var bottomBar: some View {
|
||
HStack {
|
||
screenBtn(system: "power", label: "屏幕", isOn: screenOnPref) {
|
||
if screenOnPref {
|
||
ble.screenOff()
|
||
screenOnPref = false
|
||
} else {
|
||
ble.screenOn()
|
||
screenOnPref = true
|
||
}
|
||
}
|
||
Spacer()
|
||
circleBtn(system: "arrow.uturn.backward", label: "返回") { ble.key("back") }
|
||
Spacer()
|
||
circleBtn(system: "house.fill", label: "主页") { ble.key("home") }
|
||
Spacer()
|
||
circleBtn(system: "camera.fill", label: "拍照") { ble.camera() }
|
||
Spacer()
|
||
}
|
||
.padding(.horizontal, 8).padding(.vertical, 10)
|
||
.background(Color(.secondarySystemGroupedBackground))
|
||
.clipShape(RoundedRectangle(cornerRadius: 18))
|
||
.shadow(color: .black.opacity(0.08), radius: 8, y: 2)
|
||
}
|
||
|
||
private func screenBtn(system: String, label: String, isOn: Bool, action: @escaping () -> Void) -> some View {
|
||
Button(action: action) {
|
||
VStack(spacing: 4) {
|
||
Image(systemName: system)
|
||
.font(.title3)
|
||
.frame(width: 48, height: 48)
|
||
.background(isOn ? Color.yellow.opacity(0.18) : Color(.systemGroupedBackground))
|
||
.clipShape(Circle())
|
||
.foregroundColor(isOn ? .yellow : .primary)
|
||
Text(label).font(.caption2).foregroundColor(.secondary)
|
||
}
|
||
}
|
||
}
|
||
|
||
private func circleBtn(system: String, label: String, action: @escaping () -> Void) -> some View {
|
||
Button(action: action) {
|
||
VStack(spacing: 4) {
|
||
Image(systemName: system)
|
||
.font(.title3)
|
||
.frame(width: 48, height: 48)
|
||
.background(Color(.systemGroupedBackground))
|
||
.clipShape(Circle())
|
||
.foregroundColor(.primary)
|
||
Text(label).font(.caption2).foregroundColor(.secondary)
|
||
}
|
||
}
|
||
}
|
||
} |