euv-cli 0.51.0

Cross-platform CLI tool for the euv native UI framework supporting run, build, check, fmt, doctor, new, and Hyperlane SSE dynamic updates across all supported platforms.
Documentation
import CoreGraphics
import CoreMotion
import SwiftUI
import WatchKit

@main
struct EuvWatchApp: App {
    @Environment(\.scenePhase) private var scenePhase
    @Environment(\.isLuminanceReduced) private var isLuminanceReduced
    @StateObject private var bridge = EuvWatchBridgeController()
    @State private var crownValue: Double = 0.0
    @State private var lastCrownValue: Double = 0.0

    var body: some Scene {
        WindowGroup {
            EuvWatchCanvasHostView(
                bridge: bridge,
                isAmbient: isLuminanceReduced
            )
            .focusable(true)
            .digitalCrownRotation(
                $crownValue,
                from: -10000.0,
                through: 10000.0,
                by: 1.0,
                sensitivity: .medium,
                isContinuous: true,
                isHapticFeedbackEnabled: true
            )
            .onChange(of: crownValue) { _, newValue in
                let delta = Float(newValue - lastCrownValue)
                lastCrownValue = newValue
                if abs(delta) > 0.001 {
                    bridge.dispatchCrown(detents: delta)
                    WKInterfaceDevice.current().play(.click)
                }
            }
            .ignoresSafeArea()
            .onChange(of: scenePhase) { _, phase in
                switch phase {
                case .active:
                    bridge.onLifecycle(tag: 1)
                case .inactive:
                    bridge.onLifecycle(tag: 2)
                case .background:
                    bridge.onLifecycle(tag: 3)
                @unknown default:
                    break
                }
            }
            .onChange(of: isLuminanceReduced) { _, ambient in
                bridge.onAmbientChanged(ambient: ambient)
            }
        }
    }
}

struct EuvWatchCanvasHostView: View {
    @ObservedObject var bridge: EuvWatchBridgeController
    let isAmbient: Bool

    var body: some View {
        TimelineView(.periodic(from: .now, by: isAmbient ? 1.0 : (1.0 / 60.0))) { timeline in
            Canvas { context, size in
                let _ = timeline.date
                if let image = bridge.renderFrame(size: size, isAmbient: isAmbient) {
                    context.draw(Image(decorative: image, scale: bridge.screenScale), in: CGRect(origin: .zero, size: size))
                }
            }
            .gesture(
                DragGesture(minimumDistance: 0)
                    .onChanged { value in
                        bridge.dispatchTouch(
                            phase: bridge.isTouchDown ? 1 : 0,
                            x: Float(value.location.x),
                            y: Float(value.location.y)
                        )
                    }
                    .onEnded { value in
                        bridge.dispatchTouch(
                            phase: 2,
                            x: Float(value.location.x),
                            y: Float(value.location.y)
                        )
                    }
            )
        }
    }
}

final class EuvWatchBridgeController: ObservableObject {
    private var handle: OpaquePointer?
    private let motionManager = CMMotionManager()
    private let colorSpace = CGColorSpaceCreateDeviceRGB()
    private var lastFrame: CGImage?
    private var lastTickTime: CFAbsoluteTime = CFAbsoluteTimeGetCurrent()
    var isTouchDown: Bool = false
    let screenScale: CGFloat = max(1.0, WKInterfaceDevice.current().screenScale)

    init() {
        let bounds = WKInterfaceDevice.current().screenBounds
        let w = UInt32(max(120.0, bounds.width.rounded()))
        let h = UInt32(max(120.0, bounds.height.rounded()))
        handle = euv_watchos_create(w, h, Float(screenScale))
        startMotionUpdates()
    }

    deinit {
        motionManager.stopDeviceMotionUpdates()
        if let ptr = handle {
            euv_watchos_destroy(ptr)
            handle = nil
        }
    }

    private func startMotionUpdates() {
        guard motionManager.isDeviceMotionAvailable else { return }
        motionManager.deviceMotionUpdateInterval = 1.0 / 30.0
        motionManager.startDeviceMotionUpdates(to: .main) { [weak self] motion, _ in
            guard let self, let ptr = self.handle, let m = motion else { return }
            euv_watchos_device_motion(
                ptr,
                Float(m.gravity.x * 9.80665),
                Float(-m.gravity.y * 9.80665),
                Float(m.gravity.z * 9.80665),
                Float(m.rotationRate.x),
                Float(m.rotationRate.y),
                Float(m.rotationRate.z)
            )
        }
    }

    func onLifecycle(tag: Int32) {
        guard let ptr = handle else { return }
        euv_watchos_lifecycle(ptr, tag)
    }

    func onAmbientChanged(ambient: Bool) {
        guard let ptr = handle else { return }
        euv_watchos_ambient(ptr, ambient ? 1 : 0)
    }

    func dispatchCrown(detents: Float) {
        guard let ptr = handle else { return }
        euv_watchos_crown(ptr, detents)
    }

    func dispatchTouch(phase: Int32, x: Float, y: Float) {
        guard let ptr = handle else { return }
        isTouchDown = (phase == 0 || phase == 1)
        euv_watchos_touch(ptr, phase, 0, x, y)
    }

    func renderFrame(size: CGSize, isAmbient: Bool) -> CGImage? {
        guard let ptr = handle, size.width >= 1.0, size.height >= 1.0 else { return lastFrame }
        euv_watchos_resize(ptr, UInt32(size.width.rounded()), UInt32(size.height.rounded()), Float(screenScale))
        let now = CFAbsoluteTimeGetCurrent()
        let dt = Float(min(1.0, max(0.002, now - lastTickTime)))
        lastTickTime = now
        let updated = euv_watchos_step(ptr, isAmbient ? 1.0 : dt, lastFrame == nil ? 1 : 0)
        if updated != 0 || lastFrame == nil {
            let pw = Int(euv_watchos_framebuffer_width(ptr))
            let ph = Int(euv_watchos_framebuffer_height(ptr))
            if pw > 0, ph > 0, let rawPtr = euv_watchos_framebuffer_ptr(ptr) {
                let byteLen = pw * ph * 4
                let data = Data(bytes: rawPtr, count: byteLen) as CFData
                if let provider = CGDataProvider(data: data) {
                    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
                    lastFrame = CGImage(
                        width: pw,
                        height: ph,
                        bitsPerComponent: 8,
                        bitsPerPixel: 32,
                        bytesPerRow: pw * 4,
                        space: colorSpace,
                        bitmapInfo: bitmapInfo,
                        provider: provider,
                        decode: nil,
                        shouldInterpolate: false,
                        intent: .defaultIntent
                    )
                }
            }
        }
        return lastFrame
    }
}