- 眼镜端 MouseService: 新增 volume/brightness/status/grant_write_settings 命令, adjustVolume/adjustBrightness(currentBrightness 走 Settings.System), 亮度依赖 WRITE_SETTINGS - AndroidManifest: 声明 WRITE_SETTINGS 特殊权限(需眼镜手动授予一次) - iOS GlassBLEManager: volumeUp/Down brightnessUp/Down queryStatus, 新增 glassVolume/Brightness/WriteSettings 等 @Published 状态, brightness no_permission 纠正 - ControlView: 音量/亮度 avBar + 未授权引导条, 链路 ready 时回读眼镜状态纠正 UI Co-Authored-By: Claude Opus 4.8 <[email protected]>
299 lines
14 KiB
Swift
299 lines
14 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("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
|
||
avBar
|
||
if ble.glassWriteSettings == false { brightnessHint }
|
||
}
|
||
.padding(.horizontal, 16).padding(.top, 8).padding(.bottom, 4)
|
||
// 链路就绪后回读眼镜状态(音量/亮度/授权),纠正「UI 与眼镜实际状态不一致」
|
||
.onChange(of: ble.connectionState) { _, state in
|
||
if state == .ready { ble.queryStatus() }
|
||
}
|
||
}
|
||
|
||
// 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: 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(spacing: 0) {
|
||
circleBtn(system: "arrow.uturn.backward", label: "返回") { ble.key("back") }
|
||
circleBtn(system: "house.fill", label: "主页") { ble.key("home") }
|
||
circleBtn(system: "camera.fill", label: "拍照") { ble.camera() }
|
||
}
|
||
.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 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)
|
||
}
|
||
}
|
||
.frame(maxWidth: .infinity)
|
||
}
|
||
|
||
// MARK: 音量 / 亮度 调节条
|
||
private var avBar: some View {
|
||
HStack(spacing: 0) {
|
||
avBtn(system: "speaker.slash.fill", label: "音量−") { ble.volumeDown() }
|
||
avBtn(system: "speaker.wave.3.fill", label: "音量+") { ble.volumeUp() }
|
||
Rectangle()
|
||
.fill(Color.secondary.opacity(0.22))
|
||
.frame(width: 1, height: 36)
|
||
.padding(.horizontal, 4)
|
||
avBtn(system: "sun.min.fill", label: "亮度−") { ble.brightnessDown() }
|
||
avBtn(system: "sun.max.fill", label: "亮度+") { ble.brightnessUp() }
|
||
}
|
||
.padding(.horizontal, 10).padding(.vertical, 8)
|
||
.background(Color(.secondarySystemGroupedBackground))
|
||
.clipShape(RoundedRectangle(cornerRadius: 18))
|
||
.shadow(color: .black.opacity(0.08), radius: 8, y: 2)
|
||
}
|
||
|
||
private func avBtn(system: String, label: String, action: @escaping () -> Void) -> some View {
|
||
Button(action: action) {
|
||
VStack(spacing: 3) {
|
||
Image(systemName: system)
|
||
.font(.title3)
|
||
.frame(width: 42, height: 42)
|
||
.background(Color(.systemGroupedBackground))
|
||
.clipShape(Circle())
|
||
.foregroundColor(.primary)
|
||
Text(label).font(.caption2).foregroundColor(.secondary)
|
||
}
|
||
}
|
||
.frame(maxWidth: .infinity)
|
||
}
|
||
|
||
/// 亮度未授权时的引导条:点一下跳到眼镜的「修改系统设置」页
|
||
private var brightnessHint: some View {
|
||
Button(action: { ble.grantWriteSettings() }) {
|
||
HStack(spacing: 5) {
|
||
Image(systemName: "exclamationmark.triangle.fill").font(.caption2)
|
||
Text("亮度需先授予「修改系统设置」,点此前往").font(.caption2)
|
||
}
|
||
.foregroundColor(.orange)
|
||
.padding(.vertical, 7).padding(.horizontal, 12)
|
||
.background(Color.orange.opacity(0.12))
|
||
.clipShape(Capsule())
|
||
}
|
||
}
|
||
} |