use crate::dynamic_component::ErasedComponentBox;
use super::*;
use core::ptr::NonNull;
use i_slint_core::model::{Model, ModelNotify, SharedVectorModel};
use i_slint_core::slice::Slice;
use i_slint_core::window::WindowAdapter;
use std::ffi::c_void;
use vtable::VRef;
#[repr(C)]
#[cfg(target_pointer_width = "64")]
pub struct ValueOpaque([usize; 7]);
#[repr(C)]
#[cfg(target_pointer_width = "32")]
#[repr(align(8))]
pub struct ValueOpaque([usize; 9]);
const _: [(); std::mem::size_of::<ValueOpaque>()] = [(); std::mem::size_of::<Value>()];
const _: [(); std::mem::align_of::<ValueOpaque>()] = [(); std::mem::align_of::<Value>()];
impl ValueOpaque {
fn as_value(&self) -> &Value {
unsafe { std::mem::transmute::<&ValueOpaque, &Value>(self) }
}
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_value_new(val: *mut ValueOpaque) {
std::ptr::write(val as *mut Value, Value::default())
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_value_clone(other: &ValueOpaque, val: *mut ValueOpaque) {
std::ptr::write(val as *mut Value, other.as_value().clone())
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_value_destructor(val: *mut ValueOpaque) {
drop(std::ptr::read(val as *mut Value))
}
#[no_mangle]
pub extern "C" fn slint_interpreter_value_eq(a: &ValueOpaque, b: &ValueOpaque) -> bool {
a.as_value() == b.as_value()
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_value_new_string(
str: &SharedString,
val: *mut ValueOpaque,
) {
std::ptr::write(val as *mut Value, Value::String(str.clone()))
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_value_new_double(double: f64, val: *mut ValueOpaque) {
std::ptr::write(val as *mut Value, Value::Number(double))
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_value_new_bool(b: bool, val: *mut ValueOpaque) {
std::ptr::write(val as *mut Value, Value::Bool(b))
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_value_new_array_model(
a: &SharedVector<ValueOpaque>,
val: *mut ValueOpaque,
) {
let vec = std::mem::transmute::<SharedVector<ValueOpaque>, SharedVector<Value>>(a.clone());
std::ptr::write(
val as *mut Value,
Value::Model(ModelRc::new(SharedVectorModel::<Value>::from(vec))),
)
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_value_new_brush(brush: &Brush, val: *mut ValueOpaque) {
std::ptr::write(val as *mut Value, Value::Brush(brush.clone()))
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_value_new_struct(
struc: &StructOpaque,
val: *mut ValueOpaque,
) {
std::ptr::write(val as *mut Value, Value::Struct(struc.as_struct().clone()))
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_value_new_image(img: &Image, val: *mut ValueOpaque) {
std::ptr::write(val as *mut Value, Value::Image(img.clone()))
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_value_new_model(
model: NonNull<u8>,
vtable: &ModelAdaptorVTable,
val: *mut ValueOpaque,
) {
std::ptr::write(
val as *mut Value,
Value::Model(ModelRc::new(ModelAdaptorWrapper(vtable::VBox::from_raw(
NonNull::from(vtable),
model,
)))),
)
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_value_type(val: *const ValueOpaque) -> ValueType {
(&*val).as_value().value_type()
}
#[no_mangle]
pub extern "C" fn slint_interpreter_value_to_string(val: &ValueOpaque) -> Option<&SharedString> {
match val.as_value() {
Value::String(v) => Some(v),
_ => None,
}
}
#[no_mangle]
pub extern "C" fn slint_interpreter_value_to_number(val: &ValueOpaque) -> Option<&f64> {
match val.as_value() {
Value::Number(v) => Some(v),
_ => None,
}
}
#[no_mangle]
pub extern "C" fn slint_interpreter_value_to_bool(val: &ValueOpaque) -> Option<&bool> {
match val.as_value() {
Value::Bool(v) => Some(v),
_ => None,
}
}
#[no_mangle]
pub extern "C" fn slint_interpreter_value_to_array(
val: &ValueOpaque,
out: *mut SharedVector<ValueOpaque>,
) -> bool {
match val.as_value() {
Value::Model(m) => {
let vec = if let Some(model) = m.as_any().downcast_ref::<SharedVectorModel<Value>>() {
model.shared_vector()
} else {
m.iter().collect()
};
unsafe {
std::ptr::write(
out,
std::mem::transmute::<SharedVector<Value>, SharedVector<ValueOpaque>>(vec),
);
}
true
}
_ => false,
}
}
#[no_mangle]
pub extern "C" fn slint_interpreter_value_to_brush(val: &ValueOpaque) -> Option<&Brush> {
match val.as_value() {
Value::Brush(b) => Some(b),
_ => None,
}
}
#[no_mangle]
pub extern "C" fn slint_interpreter_value_to_struct(val: &ValueOpaque) -> *const StructOpaque {
match val.as_value() {
Value::Struct(s) => s as *const Struct as *const StructOpaque,
_ => std::ptr::null(),
}
}
#[no_mangle]
pub extern "C" fn slint_interpreter_value_to_image(val: &ValueOpaque) -> Option<&Image> {
match val.as_value() {
Value::Image(img) => Some(img),
_ => None,
}
}
#[repr(C)]
#[cfg(target_pointer_width = "64")]
pub struct StructOpaque([usize; 6]);
#[repr(C)]
#[cfg(target_pointer_width = "32")]
pub struct StructOpaque([u64; 4]);
const _: [(); std::mem::size_of::<StructOpaque>()] = [(); std::mem::size_of::<Struct>()];
const _: [(); std::mem::align_of::<StructOpaque>()] = [(); std::mem::align_of::<Struct>()];
impl StructOpaque {
fn as_struct(&self) -> &Struct {
unsafe { std::mem::transmute::<&StructOpaque, &Struct>(self) }
}
fn as_struct_mut(&mut self) -> &mut Struct {
unsafe { std::mem::transmute::<&mut StructOpaque, &mut Struct>(self) }
}
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_struct_new(val: *mut StructOpaque) {
std::ptr::write(val as *mut Struct, Struct::default())
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_struct_clone(
other: &StructOpaque,
val: *mut StructOpaque,
) {
std::ptr::write(val as *mut Struct, other.as_struct().clone())
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_struct_destructor(val: *mut StructOpaque) {
drop(std::ptr::read(val as *mut Struct))
}
#[no_mangle]
pub extern "C" fn slint_interpreter_struct_get_field<'a>(
stru: &'a StructOpaque,
name: Slice<u8>,
) -> Option<&'a ValueOpaque> {
stru.as_struct()
.get_field(std::str::from_utf8(&name).unwrap())
.and_then(|v| unsafe { (v as *const Value as *const ValueOpaque).as_ref() })
}
#[no_mangle]
pub extern "C" fn slint_interpreter_struct_set_field<'a>(
stru: &'a mut StructOpaque,
name: Slice<u8>,
value: &ValueOpaque,
) {
stru.as_struct_mut()
.set_field(std::str::from_utf8(&name).unwrap().into(), value.as_value().clone())
}
type StructIterator<'a> = std::collections::hash_map::Iter<'a, String, Value>;
#[repr(C)]
pub struct StructIteratorOpaque<'a>([usize; 5], std::marker::PhantomData<StructIterator<'a>>);
const _: [(); std::mem::size_of::<StructIteratorOpaque>()] =
[(); std::mem::size_of::<StructIterator>()];
const _: [(); std::mem::align_of::<StructIteratorOpaque>()] =
[(); std::mem::align_of::<StructIterator>()];
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_struct_iterator_destructor(
val: *mut StructIteratorOpaque,
) {
drop(std::ptr::read(val as *mut StructIterator))
}
#[repr(C)]
pub struct StructIteratorResult<'a> {
k: Slice<'a, u8>,
v: Option<&'a Value>,
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_struct_iterator_next<'a>(
iter: &'a mut StructIteratorOpaque,
) -> StructIteratorResult<'a> {
if let Some((str, val)) = (*(iter as *mut StructIteratorOpaque as *mut StructIterator)).next() {
StructIteratorResult { k: Slice::from_slice(str.as_bytes()), v: Some(val) }
} else {
StructIteratorResult { k: Slice::default(), v: None }
}
}
#[no_mangle]
pub extern "C" fn slint_interpreter_struct_make_iter(stru: &StructOpaque) -> StructIteratorOpaque {
let ret_it: StructIterator = stru.as_struct().0.iter();
unsafe {
let mut r = std::mem::MaybeUninit::<StructIteratorOpaque>::uninit();
std::ptr::write(r.as_mut_ptr() as *mut StructIterator, ret_it);
r.assume_init()
}
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_instance_get_property(
inst: &ErasedComponentBox,
name: Slice<u8>,
out: *mut ValueOpaque,
) -> bool {
generativity::make_guard!(guard);
let comp = inst.unerase(guard);
match comp
.description()
.get_property(comp.borrow(), &normalize_identifier(std::str::from_utf8(&name).unwrap()))
{
Ok(val) => {
std::ptr::write(out as *mut Value, val);
true
}
Err(_) => false,
}
}
#[no_mangle]
pub extern "C" fn slint_interpreter_component_instance_set_property(
inst: &ErasedComponentBox,
name: Slice<u8>,
val: &ValueOpaque,
) -> bool {
generativity::make_guard!(guard);
let comp = inst.unerase(guard);
comp.description()
.set_property(
comp.borrow(),
&normalize_identifier(std::str::from_utf8(&name).unwrap()),
val.as_value().clone(),
)
.is_ok()
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_instance_invoke(
inst: &ErasedComponentBox,
name: Slice<u8>,
args: Slice<ValueOpaque>,
out: *mut ValueOpaque,
) -> bool {
let args = std::mem::transmute::<Slice<ValueOpaque>, Slice<Value>>(args);
generativity::make_guard!(guard);
let comp = inst.unerase(guard);
match comp.description().invoke(
comp.borrow(),
&normalize_identifier(std::str::from_utf8(&name).unwrap()),
args.as_slice(),
) {
Ok(val) => {
std::ptr::write(out as *mut Value, val);
true
}
Err(_) => false,
}
}
struct CallbackUserData {
user_data: *mut c_void,
drop_user_data: Option<extern "C" fn(*mut c_void)>,
callback: extern "C" fn(user_data: *mut c_void, arg: Slice<ValueOpaque>, ret: *mut ValueOpaque),
}
impl Drop for CallbackUserData {
fn drop(&mut self) {
if let Some(x) = self.drop_user_data {
x(self.user_data)
}
}
}
impl CallbackUserData {
fn call(&self, args: &[Value]) -> Value {
unsafe {
let args = std::mem::transmute::<&[Value], &[ValueOpaque]>(args);
let mut ret = std::mem::MaybeUninit::<Value>::uninit();
(self.callback)(
self.user_data,
Slice::from_slice(args),
ret.as_mut_ptr() as *mut ValueOpaque,
);
ret.assume_init()
}
}
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_instance_set_callback(
inst: &ErasedComponentBox,
name: Slice<u8>,
callback: extern "C" fn(user_data: *mut c_void, arg: Slice<ValueOpaque>, ret: *mut ValueOpaque),
user_data: *mut c_void,
drop_user_data: Option<extern "C" fn(*mut c_void)>,
) -> bool {
let ud = CallbackUserData { user_data, drop_user_data, callback };
generativity::make_guard!(guard);
let comp = inst.unerase(guard);
comp.description()
.set_callback_handler(
comp.borrow(),
&normalize_identifier(std::str::from_utf8(&name).unwrap()),
Box::new(move |args| ud.call(args)),
)
.is_ok()
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_instance_get_global_property(
inst: &ErasedComponentBox,
global: Slice<u8>,
property_name: Slice<u8>,
out: *mut ValueOpaque,
) -> bool {
generativity::make_guard!(guard);
let comp = inst.unerase(guard);
match comp
.description()
.get_global(comp.borrow(), &normalize_identifier(std::str::from_utf8(&global).unwrap()))
.and_then(|g| {
g.as_ref()
.get_property(&normalize_identifier(std::str::from_utf8(&property_name).unwrap()))
}) {
Ok(val) => {
std::ptr::write(out as *mut Value, val);
true
}
Err(_) => false,
}
}
#[no_mangle]
pub extern "C" fn slint_interpreter_component_instance_set_global_property(
inst: &ErasedComponentBox,
global: Slice<u8>,
property_name: Slice<u8>,
val: &ValueOpaque,
) -> bool {
generativity::make_guard!(guard);
let comp = inst.unerase(guard);
comp.description()
.get_global(comp.borrow(), &normalize_identifier(std::str::from_utf8(&global).unwrap()))
.and_then(|g| {
g.as_ref()
.set_property(
&normalize_identifier(std::str::from_utf8(&property_name).unwrap()),
val.as_value().clone(),
)
.map_err(|_| ())
})
.is_ok()
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_instance_set_global_callback(
inst: &ErasedComponentBox,
global: Slice<u8>,
name: Slice<u8>,
callback: extern "C" fn(user_data: *mut c_void, arg: Slice<ValueOpaque>, ret: *mut ValueOpaque),
user_data: *mut c_void,
drop_user_data: Option<extern "C" fn(*mut c_void)>,
) -> bool {
let ud = CallbackUserData { user_data, drop_user_data, callback };
generativity::make_guard!(guard);
let comp = inst.unerase(guard);
comp.description()
.get_global(comp.borrow(), &normalize_identifier(std::str::from_utf8(&global).unwrap()))
.and_then(|g| {
g.as_ref().set_callback_handler(
&normalize_identifier(std::str::from_utf8(&name).unwrap()),
Box::new(move |args| ud.call(args)),
)
})
.is_ok()
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_instance_invoke_global(
inst: &ErasedComponentBox,
global: Slice<u8>,
callable_name: Slice<u8>,
args: Slice<ValueOpaque>,
out: *mut ValueOpaque,
) -> bool {
let args = std::mem::transmute::<Slice<ValueOpaque>, Slice<Value>>(args);
generativity::make_guard!(guard);
let comp = inst.unerase(guard);
let callable_name = std::str::from_utf8(&callable_name).unwrap();
match comp
.description()
.get_global(comp.borrow(), &normalize_identifier(std::str::from_utf8(&global).unwrap()))
.and_then(|g| {
if matches!(
comp.description()
.original
.root_element
.borrow()
.lookup_property(callable_name)
.property_type,
i_slint_compiler::langtype::Type::Function { .. }
) {
g.as_ref().eval_function(
&normalize_identifier(callable_name),
args.as_slice().iter().cloned().collect(),
)
} else {
g.as_ref().invoke_callback(&normalize_identifier(callable_name), args.as_slice())
}
}) {
Ok(val) => {
std::ptr::write(out as *mut Value, val);
true
}
Err(_) => false,
}
}
#[no_mangle]
pub extern "C" fn slint_interpreter_component_instance_show(
inst: &ErasedComponentBox,
is_visible: bool,
) {
generativity::make_guard!(guard);
let comp = inst.unerase(guard);
if let Some(w) = comp.borrow_instance().window_adapter().internal(i_slint_core::InternalToken) {
if is_visible {
w.show().unwrap();
} else {
w.hide().unwrap();
}
}
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_instance_window(
inst: &ErasedComponentBox,
out: *mut *const i_slint_core::window::ffi::WindowAdapterRcOpaque,
) {
assert_eq!(
core::mem::size_of::<Rc<dyn WindowAdapter>>(),
core::mem::size_of::<i_slint_core::window::ffi::WindowAdapterRcOpaque>()
);
core::ptr::write(
out as *mut *const Rc<dyn WindowAdapter>,
inst.window_adapter().unwrap() as *const _,
)
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_instance_create(
def: &ComponentDefinitionOpaque,
out: *mut ComponentInstance,
) {
std::ptr::write(out, def.as_component_definition().create().unwrap())
}
#[vtable::vtable]
#[repr(C)]
pub struct ModelAdaptorVTable {
pub row_count: extern "C" fn(VRef<ModelAdaptorVTable>) -> usize,
pub row_data:
unsafe extern "C" fn(VRef<ModelAdaptorVTable>, row: usize, out: *mut ValueOpaque) -> bool,
pub set_row_data: extern "C" fn(VRef<ModelAdaptorVTable>, row: usize, value: &ValueOpaque),
pub get_notify: extern "C" fn(VRef<ModelAdaptorVTable>) -> &ModelNotifyOpaque,
pub drop: extern "C" fn(VRefMut<ModelAdaptorVTable>),
}
struct ModelAdaptorWrapper(vtable::VBox<ModelAdaptorVTable>);
impl Model for ModelAdaptorWrapper {
type Data = Value;
fn row_count(&self) -> usize {
self.0.row_count()
}
fn row_data(&self, row: usize) -> Option<Value> {
unsafe {
let mut v = std::mem::MaybeUninit::<Value>::uninit();
if self.0.row_data(row, v.as_mut_ptr() as *mut ValueOpaque) {
Some(v.assume_init())
} else {
None
}
}
}
fn model_tracker(&self) -> &dyn i_slint_core::model::ModelTracker {
self.0.get_notify().as_model_notify()
}
fn set_row_data(&self, row: usize, data: Value) {
let val: &ValueOpaque = unsafe { std::mem::transmute::<&Value, &ValueOpaque>(&data) };
self.0.set_row_data(row, val);
}
}
#[repr(C)]
#[cfg(target_pointer_width = "64")]
pub struct ModelNotifyOpaque([usize; 8]);
#[repr(C)]
#[cfg(target_pointer_width = "32")]
pub struct ModelNotifyOpaque([usize; 12]);
const _: usize = std::mem::size_of::<ModelNotifyOpaque>() - std::mem::size_of::<ModelNotify>();
const _: usize = std::mem::align_of::<ModelNotifyOpaque>() - std::mem::align_of::<ModelNotify>();
impl ModelNotifyOpaque {
fn as_model_notify(&self) -> &ModelNotify {
unsafe { std::mem::transmute::<&ModelNotifyOpaque, &ModelNotify>(self) }
}
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_model_notify_new(val: *mut ModelNotifyOpaque) {
std::ptr::write(val as *mut ModelNotify, ModelNotify::default());
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_model_notify_destructor(val: *mut ModelNotifyOpaque) {
drop(std::ptr::read(val as *mut ModelNotify))
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_model_notify_row_changed(
notify: &ModelNotifyOpaque,
row: usize,
) {
notify.as_model_notify().row_changed(row);
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_model_notify_row_added(
notify: &ModelNotifyOpaque,
row: usize,
count: usize,
) {
notify.as_model_notify().row_added(row, count);
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_model_notify_reset(notify: &ModelNotifyOpaque) {
notify.as_model_notify().reset();
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_model_notify_row_removed(
notify: &ModelNotifyOpaque,
row: usize,
count: usize,
) {
notify.as_model_notify().row_removed(row, count);
}
#[derive(Clone)]
#[repr(C)]
pub enum DiagnosticLevel {
Error,
Warning,
}
#[derive(Clone)]
#[repr(C)]
pub struct Diagnostic {
message: SharedString,
source_file: SharedString,
line: usize,
column: usize,
level: DiagnosticLevel,
}
#[repr(transparent)]
pub struct ComponentCompilerOpaque(NonNull<ComponentCompiler>);
impl ComponentCompilerOpaque {
fn as_component_compiler(&self) -> &ComponentCompiler {
unsafe { self.0.as_ref() }
}
fn as_component_compiler_mut(&mut self) -> &mut ComponentCompiler {
unsafe { self.0.as_mut() }
}
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_compiler_new(
compiler: *mut ComponentCompilerOpaque,
) {
*compiler = ComponentCompilerOpaque(NonNull::new_unchecked(Box::into_raw(Box::new(
ComponentCompiler::default(),
))));
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_compiler_destructor(
compiler: *mut ComponentCompilerOpaque,
) {
drop(Box::from_raw((*compiler).0.as_ptr()))
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_compiler_set_include_paths(
compiler: &mut ComponentCompilerOpaque,
paths: &SharedVector<SharedString>,
) {
compiler
.as_component_compiler_mut()
.set_include_paths(paths.iter().map(|path| path.as_str().into()).collect())
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_compiler_set_style(
compiler: &mut ComponentCompilerOpaque,
style: Slice<u8>,
) {
compiler.as_component_compiler_mut().set_style(std::str::from_utf8(&style).unwrap().to_string())
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_compiler_get_style(
compiler: &ComponentCompilerOpaque,
style_out: &mut SharedString,
) {
*style_out =
compiler.as_component_compiler().style().map_or(SharedString::default(), |s| s.into());
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_compiler_get_include_paths(
compiler: &ComponentCompilerOpaque,
paths: &mut SharedVector<SharedString>,
) {
paths.extend(
compiler
.as_component_compiler()
.include_paths()
.iter()
.map(|path| path.to_str().map_or_else(Default::default, |str| str.into())),
);
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_compiler_get_diagnostics(
compiler: &ComponentCompilerOpaque,
out_diags: &mut SharedVector<Diagnostic>,
) {
out_diags.extend(compiler.as_component_compiler().diagnostics.iter().map(|diagnostic| {
let (line, column) = diagnostic.line_column();
Diagnostic {
message: diagnostic.message().into(),
source_file: diagnostic
.source_file()
.and_then(|path| path.to_str())
.map_or_else(Default::default, |str| str.into()),
line,
column,
level: match diagnostic.level() {
i_slint_compiler::diagnostics::DiagnosticLevel::Error => DiagnosticLevel::Error,
i_slint_compiler::diagnostics::DiagnosticLevel::Warning => DiagnosticLevel::Warning,
_ => DiagnosticLevel::Warning,
},
}
}));
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_compiler_build_from_source(
compiler: &mut ComponentCompilerOpaque,
source_code: Slice<u8>,
path: Slice<u8>,
component_definition_ptr: *mut ComponentDefinitionOpaque,
) -> bool {
match spin_on::spin_on(compiler.as_component_compiler_mut().build_from_source(
std::str::from_utf8(&source_code).unwrap().to_string(),
std::str::from_utf8(&path).unwrap().to_string().into(),
)) {
Some(definition) => {
std::ptr::write(component_definition_ptr as *mut ComponentDefinition, definition);
true
}
None => false,
}
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_compiler_build_from_path(
compiler: &mut ComponentCompilerOpaque,
path: Slice<u8>,
component_definition_ptr: *mut ComponentDefinitionOpaque,
) -> bool {
use std::str::FromStr;
match spin_on::spin_on(
compiler
.as_component_compiler_mut()
.build_from_path(PathBuf::from_str(std::str::from_utf8(&path).unwrap()).unwrap()),
) {
Some(definition) => {
std::ptr::write(component_definition_ptr as *mut ComponentDefinition, definition);
true
}
None => false,
}
}
#[derive(Clone)]
#[repr(C)]
pub struct PropertyDescriptor {
property_name: SharedString,
property_type: ValueType,
}
#[repr(C)]
pub struct ComponentDefinitionOpaque([usize; 1]);
const _: [(); std::mem::size_of::<ComponentDefinitionOpaque>()] =
[(); std::mem::size_of::<ComponentDefinition>()];
const _: [(); std::mem::align_of::<ComponentDefinitionOpaque>()] =
[(); std::mem::align_of::<ComponentDefinition>()];
impl ComponentDefinitionOpaque {
fn as_component_definition(&self) -> &ComponentDefinition {
unsafe { std::mem::transmute::<&ComponentDefinitionOpaque, &ComponentDefinition>(self) }
}
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_definition_clone(
other: &ComponentDefinitionOpaque,
def: *mut ComponentDefinitionOpaque,
) {
std::ptr::write(def as *mut ComponentDefinition, other.as_component_definition().clone())
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_definition_destructor(
val: *mut ComponentDefinitionOpaque,
) {
drop(std::ptr::read(val as *mut ComponentDefinition))
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_definition_properties(
def: &ComponentDefinitionOpaque,
props: &mut SharedVector<PropertyDescriptor>,
) {
props.extend((&*def).as_component_definition().properties().map(
|(property_name, property_type)| PropertyDescriptor {
property_name: property_name.into(),
property_type,
},
))
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_definition_callbacks(
def: &ComponentDefinitionOpaque,
callbacks: &mut SharedVector<SharedString>,
) {
callbacks.extend((&*def).as_component_definition().callbacks().map(|name| name.into()))
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_definition_name(
def: &ComponentDefinitionOpaque,
name: &mut SharedString,
) {
*name = (&*def).as_component_definition().name().into()
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_definition_globals(
def: &ComponentDefinitionOpaque,
names: &mut SharedVector<SharedString>,
) {
names.extend((&*def).as_component_definition().globals().map(|name| name.into()))
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_definition_global_properties(
def: &ComponentDefinitionOpaque,
global_name: Slice<u8>,
properties: &mut SharedVector<PropertyDescriptor>,
) -> bool {
if let Some(property_it) = (&*def)
.as_component_definition()
.global_properties(std::str::from_utf8(&global_name).unwrap())
{
properties.extend(property_it.map(|(property_name, property_type)| PropertyDescriptor {
property_name: property_name.into(),
property_type,
}));
true
} else {
false
}
}
#[no_mangle]
pub unsafe extern "C" fn slint_interpreter_component_definition_global_callbacks(
def: &ComponentDefinitionOpaque,
global_name: Slice<u8>,
names: &mut SharedVector<SharedString>,
) -> bool {
if let Some(name_it) = (&*def)
.as_component_definition()
.global_callbacks(std::str::from_utf8(&global_name).unwrap())
{
names.extend(name_it.map(|name| name.into()));
true
} else {
false
}
}