use crate::i18n::{js_error_from_platform_error, js_internal_error};
use lingxia_platform::traits::app_runtime::AppRuntime;
use lxapp::{LxApp, lx};
use rong::{IntoJSObj, JSContext, JSFunc, JSResult};
use serde::Deserialize;
#[derive(Debug, Clone, Default, Deserialize, IntoJSObj)]
struct JSCapsuleRect {
width: Option<f64>,
height: Option<f64>,
top: Option<f64>,
right: Option<f64>,
bottom: Option<f64>,
left: Option<f64>,
}
async fn get_capsule_rect(ctx: JSContext) -> JSResult<JSCapsuleRect> {
let lxapp = LxApp::from_ctx(&ctx)?;
let json_str = lxapp
.runtime
.get_capsule_rect()
.await
.map_err(|e| js_error_from_platform_error(&e))?;
serde_json::from_str(&json_str)
.map_err(|e| js_internal_error(format!("getCapsuleRect invalid payload: {}", e)))
}
pub(crate) fn init(ctx: &JSContext) -> JSResult<()> {
let func = JSFunc::new(ctx, get_capsule_rect)?;
lx::register_js_api(ctx, "getCapsuleRect", func)?;
Ok(())
}