1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
3
4use napi::Result;
5use slint_interpreter::ComponentDefinition;
6
7use super::{JsComponentInstance, JsProperty};
8
9#[napi(js_name = "ComponentDefinition")]
10pub struct JsComponentDefinition {
11 internal: ComponentDefinition,
12}
13
14impl From<ComponentDefinition> for JsComponentDefinition {
15 fn from(definition: ComponentDefinition) -> Self {
16 Self { internal: definition }
17 }
18}
19
20#[napi]
21impl JsComponentDefinition {
22 #[napi(constructor)]
23 pub fn new() -> napi::Result<Self> {
24 Err(napi::Error::from_reason(
25 "ComponentDefinition can only be created by using ComponentCompiler.".to_string(),
26 ))
27 }
28
29 #[napi(getter)]
30 pub fn properties(&self) -> Vec<JsProperty> {
31 self.internal
32 .properties()
33 .map(|(name, value_type)| JsProperty { name, value_type: value_type.into() })
34 .collect()
35 }
36
37 #[napi(getter)]
38 pub fn callbacks(&self) -> Vec<String> {
39 self.internal.callbacks().collect()
40 }
41
42 #[napi(getter)]
43 pub fn globals(&self) -> Vec<String> {
44 self.internal.globals().collect()
45 }
46
47 #[napi]
48 pub fn global_properties(&self, global_name: String) -> Option<Vec<JsProperty>> {
49 self.internal.global_properties(global_name.as_str()).map(|iter| {
50 iter.map(|(name, value_type)| JsProperty { name, value_type: value_type.into() })
51 .collect()
52 })
53 }
54
55 #[napi]
56 pub fn global_callbacks(&self, global_name: String) -> Option<Vec<String>> {
57 self.internal.global_callbacks(global_name.as_str()).map(|iter| iter.collect())
58 }
59
60 #[napi]
61 pub fn create(&self) -> Result<JsComponentInstance> {
62 Ok(self.internal.create().map_err(|e| napi::Error::from_reason(e.to_string()))?.into())
63 }
64
65 #[napi(getter)]
66 pub fn name(&self) -> String {
67 self.internal.name().into()
68 }
69}
70