use std::boxed::Box;
use crate::element::View;
use crate::platform::mobile::{
CDirtyRect, DeviceOrientation, MobileEmbedApp, MobileOs, TouchPhase,
};
use crate::pod::{Handle, SafeAreaInsets};
pub struct HarmonyHostBridge<V: View + 'static> {
app: *mut MobileEmbedApp,
pub root_handle: Handle<V>,
}
impl<V: View + Default + 'static> HarmonyHostBridge<V> {
#[inline]
pub fn new(width: i16, height: i16, avoid_top: i16, avoid_bottom: i16) -> Self {
Self::with_root(width, height, avoid_top, avoid_bottom, V::default())
}
}
impl<V: View + 'static> HarmonyHostBridge<V> {
pub const OH_TOUCH_DOWN: u8 = 0;
pub const OH_TOUCH_UP: u8 = 1;
pub const OH_TOUCH_MOVE: u8 = 2;
pub const OH_TOUCH_CANCEL: u8 = 3;
pub fn with_root(
width: i16,
height: i16,
avoid_top: i16,
avoid_bottom: i16,
root_view: V,
) -> Self {
let safe_area = SafeAreaInsets::new(avoid_top, 0, avoid_bottom, 0);
let mut embed = Box::new(MobileEmbedApp::new_for_os(
MobileOs::HarmonyOs,
width,
height,
safe_area,
));
let root_handle = embed.engine.mount_root(root_view);
Self {
app: Box::into_raw(embed),
root_handle,
}
}
#[inline]
pub fn set_density_scale(&mut self, scale: i16) {
unsafe { &mut *self.app }.set_dpi_scale(scale);
}
#[inline]
pub fn on_surface_changed(&mut self, width: i16, height: i16) {
unsafe { &mut *self.app }.resize(width, height);
}
#[inline]
pub fn on_avoid_area_changed(&mut self, top: i16, right: i16, bottom: i16, left: i16) {
unsafe { &mut *self.app }.set_safe_area(SafeAreaInsets::new(top, right, bottom, left));
}
#[inline]
pub fn set_orientation(&mut self, orientation: DeviceOrientation) {
unsafe { &mut *self.app }.set_orientation(orientation);
}
#[inline]
pub fn on_keyboard_avoid_area(&mut self, keyboard_bottom_px: i16) {
unsafe { &mut *self.app }.set_keyboard_inset(keyboard_bottom_px);
}
#[inline]
pub fn on_touch_event(&mut self, pointer_id: u32, oh_touch_type: u8, x: i16, y: i16) {
let phase = TouchPhase::from_harmony_touch_type(oh_touch_type);
unsafe { &mut *self.app }.push_touch(pointer_id, phase, x, y);
}
#[inline]
pub fn on_back_press(&mut self) -> bool {
unsafe { &mut *self.app }.handle_back()
}
#[inline]
pub fn on_frame_callback(&mut self, dt_ms: u16) -> usize {
unsafe { &mut *self.app }.step_frame(dt_ms)
}
#[inline]
pub fn rgba_slice(&self) -> &[u8] {
unsafe { &*self.app }.rgba_bytes()
}
#[inline]
pub fn dirty_rects(&self) -> &[CDirtyRect] {
unsafe { &*self.app }.dirty_rects()
}
#[inline]
pub fn embed_app(&self) -> &MobileEmbedApp {
unsafe { &*self.app }
}
#[inline]
pub fn embed_app_mut(&mut self) -> &mut MobileEmbedApp {
unsafe { &mut *self.app }
}
#[inline]
pub fn is_gpu_backend(&self) -> bool {
unsafe { &*self.app }.backend().is_gpu()
}
#[inline]
pub fn into_raw(mut self) -> *mut MobileEmbedApp {
let ptr = self.app;
self.app = core::ptr::null_mut();
ptr
}
}
impl<V: View + 'static> Drop for HarmonyHostBridge<V> {
fn drop(&mut self) {
if !self.app.is_null() {
unsafe {
let _ = Box::from_raw(self.app);
}
}
}
}
pub unsafe fn step_and_blit_harmony_window(
app: *mut MobileEmbedApp,
oh_native_window: *mut core::ffi::c_void,
dt_ms: u16,
) -> i32 {
let Some(app_ref) = (unsafe { app.as_mut() }) else {
return 0;
};
let dirty = app_ref.step_frame(dt_ms.max(1));
if dirty == 0 || oh_native_window.is_null() {
return dirty as i32;
}
if app_ref.has_active_gpu_window() {
return dirty as i32;
}
let _ = oh_native_window;
dirty as i32
}
pub const HARMONY_ENTRY_ABILITY_ETS: &str = r#"import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage): void {
const mainWin = windowStage.getMainWindowSync();
mainWin.setWindowLayoutFullScreen(true);
windowStage.loadContent('pages/Index');
}
}
"#;
pub const HARMONY_NAPI_INDEX_DTS: &str = r#"export const create: (width: number, height: number, avoidTop: number, avoidBottom: number) => number;
export const destroy: (handle: number) => void;
export const resize: (handle: number, width: number, height: number) => void;
export const setDpiScale: (handle: number, scale: number) => void;
export const setSafeArea: (handle: number, top: number, right: number, bottom: number, left: number) => void;
export const setKeyboardInset: (handle: number, heightVp: number) => void;
export const pushTouch: (handle: number, pointerId: number, touchType: number, x: number, y: number) => void;
export const handleBack: (handle: number) => boolean;
export const stepFrame: (handle: number, dtMs: number) => number;
"#;
pub const HARMONY_MODULE_JSON5: &str = r#"{
"module": {
"name": "entry",
"type": "entry",
"description": "Velox HarmonyOS NEXT Native XComponent Application",
"mainElement": "EntryAbility",
"deviceTypes": [
"phone",
"tablet",
"2in1"
],
"deliveryWithInstall": true,
"installationFree": false,
"pages": "$profile:main_pages",
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ets",
"description": "Velox HarmonyOS NEXT UIAbility",
"exported": true,
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
]
}
]
}
}
"#;
pub fn generate_harmony_index_ets(lib_name: &str) -> std::string::String {
std::format!(
r#"import window from '@ohos.window';
import display from '@ohos.display';
import veloxHarmony from 'lib{lib_name}.so';
/**
* 鸿蒙 HarmonyOS NEXT ArkTS 主页面(由 `velox-engine::platform::harmony` 自动生成,挂载 `XComponent` Native 渲染表面)。
*/
@Entry
@Component
struct Index {{
private xComponentContext: Record<string, () => void> | undefined = undefined;
private nativeHandle: number = 0;
aboutToAppear(): void {{
window.getLastWindow(getContext(this)).then((win) => {{
win.setWindowLayoutFullScreen(true);
const sysArea = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
const navArea = win.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);
if (this.nativeHandle !== 0) {{
veloxHarmony.setSafeArea(
this.nativeHandle,
px2vp(sysArea.topRect.height),
0,
px2vp(navArea.bottomRect.height),
0
);
}}
win.on('avoidAreaChange', (data) => {{
if (this.nativeHandle === 0) {{
return;
}}
if (data.type === window.AvoidAreaType.TYPE_KEYBOARD) {{
veloxHarmony.setKeyboardInset(this.nativeHandle, px2vp(data.area.bottomRect.height));
}} else if (
data.type === window.AvoidAreaType.TYPE_SYSTEM ||
data.type === window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR
) {{
veloxHarmony.setSafeArea(
this.nativeHandle,
px2vp(data.area.topRect.height),
px2vp(data.area.rightRect.width),
px2vp(data.area.bottomRect.height),
px2vp(data.area.leftRect.width)
);
}}
}});
}});
}}
onBackPress(): boolean {{
if (this.nativeHandle !== 0) {{
return veloxHarmony.handleBack(this.nativeHandle);
}}
return false;
}}
build() {{
Stack() {{
XComponent({{
id: 'velox_harmony_xcomponent',
type: XComponentType.SURFACE,
libraryname: '{lib_name}'
}})
.onLoad((context) => {{
this.xComponentContext = context as Record<string, () => void>;
const disp = display.getDefaultDisplaySync();
const scale = Math.max(1, Math.round(disp.densityPixels));
this.nativeHandle = veloxHarmony.create(384, 832, 38, 28);
veloxHarmony.setDpiScale(this.nativeHandle, scale);
}})
.onDestroy(() => {{
if (this.nativeHandle !== 0) {{
veloxHarmony.destroy(this.nativeHandle);
this.nativeHandle = 0;
}}
}})
.width('100%')
.height('100%')
}}
.width('100%')
.height('100%')
}}
}}
"#
)
}
pub fn emit_harmony_host_project(
out_dir: &std::path::Path,
lib_name: &str,
) -> std::io::Result<()> {
let pages_dir = out_dir.join("ets").join("pages");
let ability_dir = out_dir.join("ets").join("entryability");
let types_dir = out_dir.join("types").join(std::format!("lib{lib_name}"));
std::fs::create_dir_all(&pages_dir)?;
std::fs::create_dir_all(&ability_dir)?;
std::fs::create_dir_all(&types_dir)?;
std::fs::write(out_dir.join("module.json5"), HARMONY_MODULE_JSON5)?;
std::fs::write(
pages_dir.join("Index.ets"),
generate_harmony_index_ets(lib_name),
)?;
std::fs::write(
ability_dir.join("EntryAbility.ets"),
HARMONY_ENTRY_ABILITY_ETS,
)?;
std::fs::write(types_dir.join("index.d.ts"), HARMONY_NAPI_INDEX_DTS)?;
Ok(())
}
#[macro_export]
macro_rules! export_harmony_app {
($app_ty:ty) => {
#[unsafe(no_mangle)]
pub extern "C" fn velox_harmony_app_create(
width: i16,
height: i16,
avoid_top: i16,
avoid_bottom: i16,
) -> *mut $crate::platform::mobile::MobileEmbedApp {
$crate::platform::harmony::HarmonyHostBridge::<$app_ty>::new(
width,
height,
avoid_top,
avoid_bottom,
)
.into_raw()
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn velox_harmony_app_destroy(
app: *mut $crate::platform::mobile::MobileEmbedApp,
) {
unsafe {
$crate::platform::mobile::ffi::destroy(app);
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn velox_harmony_app_backend_is_gpu(
app: *const $crate::platform::mobile::MobileEmbedApp,
) -> bool {
unsafe { $crate::platform::mobile::ffi::backend_is_gpu(app) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn velox_harmony_app_resize(
app: *mut $crate::platform::mobile::MobileEmbedApp,
width: i16,
height: i16,
) {
unsafe {
$crate::platform::mobile::ffi::resize(app, width, height);
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn velox_harmony_app_set_dpi_scale(
app: *mut $crate::platform::mobile::MobileEmbedApp,
scale: i16,
) {
unsafe {
$crate::platform::mobile::ffi::set_dpi_scale(app, scale);
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn velox_harmony_app_set_safe_area(
app: *mut $crate::platform::mobile::MobileEmbedApp,
top: i16,
right: i16,
bottom: i16,
left: i16,
) {
unsafe {
$crate::platform::mobile::ffi::set_safe_area(app, top, right, bottom, left);
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn velox_harmony_app_set_keyboard_inset(
app: *mut $crate::platform::mobile::MobileEmbedApp,
height_px: i16,
) {
unsafe {
$crate::platform::mobile::ffi::set_keyboard_inset(app, height_px);
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn velox_harmony_app_push_touch(
app: *mut $crate::platform::mobile::MobileEmbedApp,
pointer_id: u32,
oh_touch_type: u8,
x: i16,
y: i16,
) {
let phase =
$crate::platform::mobile::TouchPhase::from_harmony_touch_type(oh_touch_type);
unsafe {
$crate::platform::mobile::ffi::push_touch(app, pointer_id, phase, x, y);
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn velox_harmony_app_handle_back(
app: *mut $crate::platform::mobile::MobileEmbedApp,
) -> bool {
unsafe { $crate::platform::mobile::ffi::handle_back(app) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn velox_harmony_app_step_frame(
app: *mut $crate::platform::mobile::MobileEmbedApp,
dt_ms: u16,
) -> usize {
unsafe { $crate::platform::mobile::ffi::step_frame(app, dt_ms) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn velox_harmony_app_framebuffer_ptr(
app: *const $crate::platform::mobile::MobileEmbedApp,
) -> *const u8 {
unsafe { $crate::platform::mobile::ffi::framebuffer_ptr(app) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn velox_harmony_app_dirty_rects_ptr(
app: *const $crate::platform::mobile::MobileEmbedApp,
) -> *const $crate::platform::mobile::CDirtyRect {
unsafe { $crate::platform::mobile::ffi::dirty_rects_ptr(app) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn velox_harmony_app_dirty_rects_count(
app: *const $crate::platform::mobile::MobileEmbedApp,
) -> usize {
unsafe { $crate::platform::mobile::ffi::dirty_rects_count(app) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn velox_harmony_app_step_and_blit_window(
app: *mut $crate::platform::mobile::MobileEmbedApp,
oh_native_window: *mut core::ffi::c_void,
dt_ms: u16,
) -> i32 {
unsafe {
$crate::platform::harmony::step_and_blit_harmony_window(
app,
oh_native_window,
dt_ms,
)
}
}
};
}