Skip to content

Commit

Permalink
fix: render failed
Browse files Browse the repository at this point in the history
  • Loading branch information
richerfu committed Nov 6, 2024
1 parent 3dc9979 commit 3733bb8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
35 changes: 21 additions & 14 deletions crates/arkui/src/common/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::ffi::CString;

use napi_ohos::bindgen_prelude::Either3;
use ohos_arkui_sys::{ArkUI_AttributeItem, ArkUI_NumberValue};

pub struct ArkUINodeAttributeNumber(pub(crate) Either3<f32, i32, u32>);
#[derive(Clone, Copy, PartialEq)]
pub enum ArkUINodeAttributeNumber {
Float(f32),
Int(i32),
Uint(u32),
}

pub enum ArkUINodeAttributeItem {
/// Accept number array
Expand All @@ -21,33 +25,36 @@ impl From<ArkUINodeAttributeItem> for ArkUI_AttributeItem {
fn from(value: ArkUINodeAttributeItem) -> Self {
match value {
ArkUINodeAttributeItem::NumberValue(value) => {
let v: Vec<ArkUI_NumberValue> = value
let mut v: Vec<ArkUI_NumberValue> = value
.iter()
.map(|v| match v.0 {
Either3::A(f) => ArkUI_NumberValue { f32_: f },
Either3::B(i) => ArkUI_NumberValue { i32_: i },
Either3::C(u) => ArkUI_NumberValue { u32_: u },
.map(|v| match v {
ArkUINodeAttributeNumber::Float(f) => ArkUI_NumberValue { f32_: *f },
ArkUINodeAttributeNumber::Int(i) => ArkUI_NumberValue { i32_: *i },
ArkUINodeAttributeNumber::Uint(u) => ArkUI_NumberValue { u32_: *u },
})
.collect();
let value_ptr = v.as_mut_ptr();
let len = v.len();
std::mem::forget(v);
ArkUI_AttributeItem {
value: v.as_ptr(),
size: v.len() as i32,
string: std::ptr::null(),
value: value_ptr,
size: len as i32,
string: std::ptr::null_mut(),
object: std::ptr::null_mut(),
}
}
ArkUINodeAttributeItem::Object(obj) => ArkUI_AttributeItem {
value: std::ptr::null(),
value: std::ptr::null_mut(),
size: 0,
string: std::ptr::null(),
string: std::ptr::null_mut(),
object: obj,
},
ArkUINodeAttributeItem::String(s) => {
let c_string = CString::new(s).unwrap();
ArkUI_AttributeItem {
value: std::ptr::null(),
value: std::ptr::null_mut(),
size: 0,
string: c_string.as_ptr(),
string: c_string.into_raw(),
object: std::ptr::null_mut(),
}
}
Expand Down
6 changes: 2 additions & 4 deletions crates/arkui/src/component/built_in_component/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use napi_ohos::{bindgen_prelude::Either3, Result};
use napi_ohos::Result;

use crate::{
ArkUINode, ArkUINodeAttributeItem, ArkUINodeAttributeNumber, ArkUINodeType,
Expand All @@ -21,9 +21,7 @@ impl Text {

pub fn set_font_size(&self, font_size: f32) -> Result<()> {
let font_size_property =
ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber(Either3::A(
font_size,
))]);
ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber::Float(font_size)]);
ARK_UI_NATIVE_NODE_API_1.set_attribute(
&self.0,
crate::ArkUINodeAttributeType::FontSize,
Expand Down
10 changes: 5 additions & 5 deletions crates/arkui/src/component/common_attr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use napi_ohos::{bindgen_prelude::Either3, Result};
use napi_ohos::Result;

use crate::{
ArkUINode, ArkUINodeAttributeItem, ArkUINodeAttributeNumber, ARK_UI_NATIVE_NODE_API_1,
Expand All @@ -13,7 +13,7 @@ pub trait ArkUICommonAttribute {
/// Set node height
fn set_height(&self, height: f32) -> Result<()> {
let percent_width_property =
ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber(Either3::A(height))]);
ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber::Float(height)]);
ARK_UI_NATIVE_NODE_API_1.set_attribute(
self.raw(),
crate::ArkUINodeAttributeType::Height,
Expand All @@ -25,7 +25,7 @@ pub trait ArkUICommonAttribute {
/// Set percent width
fn set_percent_width(&self, width: f32) -> Result<()> {
let percent_width_property =
ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber(Either3::A(width))]);
ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber::Float(width)]);
ARK_UI_NATIVE_NODE_API_1.set_attribute(
self.raw(),
crate::ArkUINodeAttributeType::WidthPercent,
Expand All @@ -37,7 +37,7 @@ pub trait ArkUICommonAttribute {
/// Set percent height
fn set_percent_height(&self, height: f32) -> Result<()> {
let percent_height_property =
ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber(Either3::A(height))]);
ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber::Float(height)]);
ARK_UI_NATIVE_NODE_API_1.set_attribute(
self.raw(),
crate::ArkUINodeAttributeType::HeightPercent,
Expand All @@ -49,7 +49,7 @@ pub trait ArkUICommonAttribute {
/// Set background-color
fn set_background_color(&self, color: u32) -> Result<()> {
let background_color_property =
ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber(Either3::C(color))]);
ArkUINodeAttributeItem::NumberValue(vec![ArkUINodeAttributeNumber::Uint(color)]);
ARK_UI_NATIVE_NODE_API_1.set_attribute(
self.raw(),
crate::ArkUINodeAttributeType::BackgroundColor,
Expand Down
2 changes: 2 additions & 0 deletions crates/arkui/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unused)]

mod common;
mod component;
mod r#type;
Expand Down

0 comments on commit 3733bb8

Please sign in to comment.