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
use std::ptr::null;

use euv::{
    ANDROID_ACTION_CANCEL, ANDROID_ACTION_DOWN, ANDROID_ACTION_MOVE, ANDROID_ACTION_UP, App,
    DeviceMotionSample, FrameStats, Framebuffer, KeyCode, KeyEvent, Modifiers, NativeSystemFont,
    PlatformKind, PointerEvent, Point, RenderMode, SafeAreaInsets, SystemEvent,
    TouchPointerTracker, WEARABLE_CROWN_PIXELS_PER_DETENT, WindowEvent, WindowId,
    compute_wearable_safe_area, WearableShape,
};
use euv_example::{ShowcaseState, create_showcase_app_for_platform_with_viewport};

pub struct EuvWatchOsHost {
    app: App,
    _state: ShowcaseState,
    touch_tracker: TouchPointerTracker,
    logical_w: u32,
    logical_h: u32,
    scale: f32,
    paused: bool,
    ambient: bool,
    dirty: bool,
}

#[unsafe(no_mangle)]
pub extern "C" fn euv_watchos_create(
    logical_w: u32,
    logical_h: u32,
    scale: f32,
) -> *mut EuvWatchOsHost {
    let w: u32 = logical_w.max(120);
    let h: u32 = logical_h.max(120);
    let dpr: f32 = if scale.is_finite() && scale > 0.0 {
        scale.clamp(0.25, 16.0)
    } else {
        2.0
    };
    let safe: SafeAreaInsets =
        compute_wearable_safe_area(WearableShape::Squircle, w as f32, h as f32, 0.0);
    let Ok((mut app, state)) = create_showcase_app_for_platform_with_viewport(
        PlatformKind::Wearable,
        RenderMode::Software,
        w,
        h,
        dpr,
        safe,
    ) else {
        return std::ptr::null_mut();
    };
    app.get_mut_renderer()
        .get_mut_software()
        .set_font_provider(NativeSystemFont::system());
    let _: bool = state
        .get_theme()
        .get_mode()
        .set_if_changed(euv::ThemeMode::Dark);
    let _: Result<FrameStats, _> = app.step_frame();
    Box::into_raw(Box::new(EuvWatchOsHost {
        app,
        _state: state,
        touch_tracker: TouchPointerTracker::default(),
        logical_w: w,
        logical_h: h,
        scale: dpr,
        paused: false,
        ambient: false,
        dirty: true,
    }))
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_watchos_destroy(ptr: *mut EuvWatchOsHost) {
    if !ptr.is_null() {
        unsafe { drop(Box::from_raw(ptr)) };
    }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_watchos_resize(
    ptr: *mut EuvWatchOsHost,
    logical_w: u32,
    logical_h: u32,
    scale: f32,
) {
    let Some(host) = (unsafe { ptr.as_mut() }) else {
        return;
    };
    let w: u32 = logical_w.max(120);
    let h: u32 = logical_h.max(120);
    let dpr: f32 = if scale.is_finite() && scale > 0.0 {
        scale.clamp(0.25, 16.0)
    } else {
        host.scale
    };
    if w == host.logical_w && h == host.logical_h && (dpr - host.scale).abs() < 0.01 {
        return;
    }
    host.logical_w = w;
    host.logical_h = h;
    host.scale = dpr;
    let pw: u32 = ((w as f32) * dpr).round().max(1.0) as u32;
    let ph: u32 = ((h as f32) * dpr).round().max(1.0) as u32;
    host.app.set_exact_physical_viewport(pw, ph, dpr);
    let safe: SafeAreaInsets =
        compute_wearable_safe_area(WearableShape::Squircle, w as f32, h as f32, 0.0);
    let wid: WindowId = host.app.get_window_state().get_id();
    host.app
        .dispatch_window_event(WindowEvent::SafeAreaChanged(wid, safe));
    host.dirty = true;
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_watchos_step(
    ptr: *mut EuvWatchOsHost,
    dt_secs: f32,
    force: i32,
) -> i32 {
    let Some(host) = (unsafe { ptr.as_mut() }) else {
        return 0;
    };
    if host.paused && force == 0 {
        return 0;
    }
    let animating: bool = host.app.has_active_animations();
    if !host.dirty && !animating && force == 0 {
        return 0;
    }
    let dt: f32 = if dt_secs.is_finite() && dt_secs > 0.0 {
        dt_secs.clamp(0.001, 1.0)
    } else {
        1.0 / 60.0
    };
    let res = if animating {
        host.app.step_game_frame(dt)
    } else {
        host.app.step_frame()
    };
    if res.is_ok() {
        host.dirty = false;
        1
    } else {
        0
    }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_watchos_framebuffer_ptr(ptr: *const EuvWatchOsHost) -> *const u8 {
    let Some(host) = (unsafe { ptr.as_ref() }) else {
        return null();
    };
    host.app
        .get_renderer()
        .framebuffer()
        .map(|fb: &Framebuffer| fb.get_pixels().as_ptr())
        .unwrap_or(null())
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_watchos_framebuffer_width(ptr: *const EuvWatchOsHost) -> u32 {
    let Some(host) = (unsafe { ptr.as_ref() }) else {
        return 0;
    };
    host.app
        .get_renderer()
        .framebuffer()
        .map(Framebuffer::get_width)
        .unwrap_or(0)
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_watchos_framebuffer_height(ptr: *const EuvWatchOsHost) -> u32 {
    let Some(host) = (unsafe { ptr.as_ref() }) else {
        return 0;
    };
    host.app
        .get_renderer()
        .framebuffer()
        .map(Framebuffer::get_height)
        .unwrap_or(0)
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_watchos_touch(
    ptr: *mut EuvWatchOsHost,
    phase: i32,
    pointer_id: i32,
    logical_x: f32,
    logical_y: f32,
) {
    let Some(host) = (unsafe { ptr.as_mut() }) else {
        return;
    };
    let wid: WindowId = host.app.get_window_state().get_id();
    let action: i32 = match phase {
        0 => ANDROID_ACTION_DOWN,
        1 => ANDROID_ACTION_MOVE,
        2 => ANDROID_ACTION_UP,
        _ => ANDROID_ACTION_CANCEL,
    };
    let evs: Vec<WindowEvent> = host.touch_tracker.handle_android_motion(
        wid,
        action,
        0,
        &[(pointer_id, logical_x, logical_y)],
    );
    for ev in evs {
        host.app.dispatch_window_event(ev);
    }
    host.dirty = true;
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_watchos_crown(ptr: *mut EuvWatchOsHost, detents: f32) {
    let Some(host) = (unsafe { ptr.as_mut() }) else {
        return;
    };
    if !detents.is_finite() || detents.abs() < 1e-4 {
        return;
    }
    let wid: WindowId = host.app.get_window_state().get_id();
    let cx: f32 = (host.logical_w as f32) * 0.5;
    let cy: f32 = (host.logical_h as f32) * 0.5;
    host.app
        .dispatch_window_event(WindowEvent::PointerMove(wid, PointerEvent::mouse(cx, cy)));
    host.app.dispatch_window_event(WindowEvent::Scroll(
        wid,
        Point::new(0.0, detents * WEARABLE_CROWN_PIXELS_PER_DETENT),
    ));
    host.dirty = true;
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_watchos_ambient(ptr: *mut EuvWatchOsHost, is_ambient: i32) {
    let Some(host) = (unsafe { ptr.as_mut() }) else {
        return;
    };
    host.ambient = is_ambient != 0;
    host.app
        .dispatch_window_event(WindowEvent::System(SystemEvent::LowPowerModeChanged(
            host.ambient,
        )));
    host.dirty = true;
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_watchos_lifecycle(ptr: *mut EuvWatchOsHost, tag: i32) {
    let Some(host) = (unsafe { ptr.as_mut() }) else {
        return;
    };
    let wid: WindowId = host.app.get_window_state().get_id();
    match tag {
        1 => {
            host.paused = false;
            host.app
                .dispatch_window_event(WindowEvent::System(SystemEvent::Foreground));
            host.dirty = true;
        }
        2 => {
            host.paused = true;
            for ev in host.touch_tracker.cancel_all_on_focus_loss(wid) {
                host.app.dispatch_window_event(ev);
            }
            host.app
                .dispatch_window_event(WindowEvent::System(SystemEvent::Disappear));
        }
        3 => {
            host.paused = true;
            for ev in host.touch_tracker.cancel_all_on_focus_loss(wid) {
                host.app.dispatch_window_event(ev);
            }
            host.app
                .dispatch_window_event(WindowEvent::System(SystemEvent::Background));
        }
        4 => {
            host.app.dispatch_window_event(WindowEvent::KeyDown(
                wid,
                KeyEvent::new(KeyCode::Back, Modifiers::default(), false),
            ));
            host.app
                .dispatch_window_event(WindowEvent::System(SystemEvent::BackPressed));
            host.dirty = true;
        }
        _ => {}
    }
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn euv_watchos_device_motion(
    ptr: *mut EuvWatchOsHost,
    ax: f32,
    ay: f32,
    az: f32,
    gx: f32,
    gy: f32,
    gz: f32,
) {
    let Some(host) = (unsafe { ptr.as_mut() }) else {
        return;
    };
    let sample: DeviceMotionSample = DeviceMotionSample::from_vectors(
        [ax, ay, az],
        [0.0, 0.0, 0.0],
        [gx, gy, gz],
        [0.0, 0.0, 0.0],
        0,
        true,
    );
    host.app
        .dispatch_window_event(WindowEvent::System(SystemEvent::DeviceMotion(sample)));
    host.dirty = true;
}