use core::convert::TryInto;
use sixtyfps_compilerlib::langtype::Type as LangType;
use sixtyfps_corelib::graphics::Image;
use sixtyfps_corelib::{Brush, PathData, SharedString, SharedVector};
use std::borrow::Cow;
use std::collections::HashMap;
use std::iter::FromIterator;
use std::path::{Path, PathBuf};
use std::rc::Rc;
#[doc(inline)]
pub use sixtyfps_compilerlib::diagnostics::{Diagnostic, DiagnosticLevel};
pub use sixtyfps_corelib::window::api::Window;
#[derive(Debug, Copy, Clone, PartialEq)]
#[repr(i8)]
pub enum ValueType {
Void,
Number,
String,
Bool,
Array,
Model,
Struct,
Brush,
Image,
Other = -1,
}
impl From<LangType> for ValueType {
fn from(ty: LangType) -> Self {
match ty {
LangType::Float32
| LangType::Int32
| LangType::Duration
| LangType::Angle
| LangType::PhysicalLength
| LangType::LogicalLength
| LangType::Percent
| LangType::UnitProduct(_) => Self::Number,
LangType::String => Self::String,
LangType::Color => Self::Brush,
LangType::Array(_) => Self::Array,
LangType::Bool => Self::Bool,
LangType::Struct { .. } => Self::Struct,
LangType::Void => Self::Void,
LangType::Image => Self::Image,
_ => Self::Other,
}
}
}
#[derive(Clone)]
#[non_exhaustive]
#[repr(C)]
pub enum Value {
Void,
Number(f64),
String(SharedString),
Bool(bool),
Image(Image),
Array(SharedVector<Value>),
Model(Rc<dyn sixtyfps_corelib::model::Model<Data = Value>>),
Struct(Struct),
Brush(Brush),
#[doc(hidden)]
PathData(PathData),
#[doc(hidden)]
EasingCurve(sixtyfps_corelib::animations::EasingCurve),
#[doc(hidden)]
EnumerationValue(String, String),
#[doc(hidden)]
LayoutCache(SharedVector<f32>),
}
impl Value {
pub fn value_type(&self) -> ValueType {
match self {
Value::Void => ValueType::Void,
Value::Number(_) => ValueType::Number,
Value::String(_) => ValueType::String,
Value::Bool(_) => ValueType::Bool,
Value::Array(_) => ValueType::Array,
Value::Model(_) => ValueType::Model,
Value::Struct(_) => ValueType::Struct,
Value::Brush(_) => ValueType::Brush,
Value::Image(_) => ValueType::Image,
_ => ValueType::Other,
}
}
}
impl Default for Value {
fn default() -> Self {
Value::Void
}
}
impl PartialEq for Value {
fn eq(&self, other: &Self) -> bool {
match self {
Value::Void => matches!(other, Value::Void),
Value::Number(lhs) => matches!(other, Value::Number(rhs) if lhs == rhs),
Value::String(lhs) => matches!(other, Value::String(rhs) if lhs == rhs),
Value::Bool(lhs) => matches!(other, Value::Bool(rhs) if lhs == rhs),
Value::Image(lhs) => matches!(other, Value::Image(rhs) if lhs == rhs),
Value::Array(lhs) => matches!(other, Value::Array(rhs) if lhs == rhs),
Value::Model(lhs) => matches!(other, Value::Model(rhs) if Rc::ptr_eq(lhs, rhs)),
Value::Struct(lhs) => matches!(other, Value::Struct(rhs) if lhs == rhs),
Value::Brush(lhs) => matches!(other, Value::Brush(rhs) if lhs == rhs),
Value::PathData(lhs) => matches!(other, Value::PathData(rhs) if lhs == rhs),
Value::EasingCurve(lhs) => matches!(other, Value::EasingCurve(rhs) if lhs == rhs),
Value::EnumerationValue(lhs_name, lhs_value) => {
matches!(other, Value::EnumerationValue(rhs_name, rhs_value) if lhs_name == rhs_name && lhs_value == rhs_value)
}
Value::LayoutCache(lhs) => matches!(other, Value::LayoutCache(rhs) if lhs == rhs),
}
}
}
impl std::fmt::Debug for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Value::Void => write!(f, "Value::Void"),
Value::Number(n) => write!(f, "Value::Number({:?})", n),
Value::String(s) => write!(f, "Value::String({:?})", s),
Value::Bool(b) => write!(f, "Value::Bool({:?})", b),
Value::Image(i) => write!(f, "Value::Image({:?})", i),
Value::Array(a) => write!(f, "Value::Array({:?})", a),
Value::Model(_) => write!(f, "Value::Model(<model object>)"),
Value::Struct(s) => write!(f, "Value::Struct({:?})", s),
Value::Brush(b) => write!(f, "Value::Brush({:?})", b),
Value::PathData(e) => write!(f, "Value::PathElements({:?})", e),
Value::EasingCurve(c) => write!(f, "Value::EasingCurve({:?})", c),
Value::EnumerationValue(n, v) => write!(f, "Value::EnumerationValue({:?}, {:?})", n, v),
Value::LayoutCache(v) => write!(f, "Value::LayoutCache({:?})", v),
}
}
}
macro_rules! declare_value_conversion {
( $value:ident => [$($ty:ty),*] ) => {
$(
impl From<$ty> for Value {
fn from(v: $ty) -> Self {
Value::$value(v as _)
}
}
impl TryInto<$ty> for Value {
type Error = Value;
fn try_into(self) -> Result<$ty, Value> {
match self {
Self::$value(x) => Ok(x as _),
_ => Err(self)
}
}
}
)*
};
}
declare_value_conversion!(Number => [u32, u64, i32, i64, f32, f64, usize, isize] );
declare_value_conversion!(String => [SharedString] );
declare_value_conversion!(Bool => [bool] );
declare_value_conversion!(Image => [Image] );
declare_value_conversion!(Struct => [Struct] );
declare_value_conversion!(Brush => [Brush] );
declare_value_conversion!(PathData => [PathData]);
declare_value_conversion!(EasingCurve => [sixtyfps_corelib::animations::EasingCurve]);
declare_value_conversion!(LayoutCache => [SharedVector<f32>] );
macro_rules! declare_value_struct_conversion {
(struct $name:path { $($field:ident),* $(, ..$extra:expr)? }) => {
impl From<$name> for Value {
fn from($name { $($field),* , .. }: $name) -> Self {
let mut struct_ = Struct::default();
$(struct_.set_field(stringify!($field).into(), $field.into());)*
Value::Struct(struct_)
}
}
impl TryInto<$name> for Value {
type Error = ();
fn try_into(self) -> Result<$name, ()> {
match self {
Self::Struct(x) => {
type Ty = $name;
Ok(Ty {
$($field: x.get_field(stringify!($field)).ok_or(())?.clone().try_into().map_err(|_|())?),*
$(, ..$extra)?
})
}
_ => Err(()),
}
}
}
};
}
declare_value_struct_conversion!(struct sixtyfps_corelib::model::StandardListViewItem { text });
declare_value_struct_conversion!(struct sixtyfps_corelib::properties::StateInfo { current_state, previous_state, change_time });
declare_value_struct_conversion!(struct sixtyfps_corelib::input::KeyboardModifiers { control, alt, shift, meta });
declare_value_struct_conversion!(struct sixtyfps_corelib::input::KeyEvent { event_type, text, modifiers });
declare_value_struct_conversion!(struct sixtyfps_corelib::layout::LayoutInfo { min, max, min_percent, max_percent, preferred, stretch });
declare_value_struct_conversion!(struct sixtyfps_corelib::graphics::Point { x, y, ..Default::default()});
declare_value_struct_conversion!(struct sixtyfps_corelib::items::PointerEvent { kind, button });
macro_rules! declare_value_enum_conversion {
($ty:ty, $n:ident) => {
impl From<$ty> for Value {
fn from(v: $ty) -> Self {
Value::EnumerationValue(stringify!($n).to_owned(), v.to_string().replace('_', "-"))
}
}
impl TryInto<$ty> for Value {
type Error = ();
fn try_into(self) -> Result<$ty, ()> {
use std::str::FromStr;
match self {
Self::EnumerationValue(enumeration, value) => {
if enumeration != stringify!($n) {
return Err(());
}
<$ty>::from_str(value.as_str())
.or_else(|_| <$ty>::from_str(&value.as_str().replace('-', "_")))
.map_err(|_| ())
}
_ => Err(()),
}
}
}
};
}
declare_value_enum_conversion!(
sixtyfps_corelib::items::TextHorizontalAlignment,
TextHorizontalAlignment
);
declare_value_enum_conversion!(
sixtyfps_corelib::items::TextVerticalAlignment,
TextVerticalAlignment
);
declare_value_enum_conversion!(sixtyfps_corelib::items::TextOverflow, TextOverflow);
declare_value_enum_conversion!(sixtyfps_corelib::items::TextWrap, TextWrap);
declare_value_enum_conversion!(sixtyfps_corelib::layout::LayoutAlignment, LayoutAlignment);
declare_value_enum_conversion!(sixtyfps_corelib::items::ImageFit, ImageFit);
declare_value_enum_conversion!(sixtyfps_corelib::items::ImageRendering, ImageRendering);
declare_value_enum_conversion!(sixtyfps_corelib::input::KeyEventType, KeyEventType);
declare_value_enum_conversion!(sixtyfps_corelib::items::EventResult, EventResult);
declare_value_enum_conversion!(sixtyfps_corelib::items::FillRule, FillRule);
declare_value_enum_conversion!(sixtyfps_corelib::items::MouseCursor, MouseCursor);
declare_value_enum_conversion!(sixtyfps_corelib::items::StandardButtonKind, StandardButtonKind);
declare_value_enum_conversion!(sixtyfps_corelib::items::PointerEventKind, PointerEventKind);
declare_value_enum_conversion!(sixtyfps_corelib::items::PointerEventButton, PointerEventButton);
declare_value_enum_conversion!(sixtyfps_corelib::items::DialogButtonRole, DialogButtonRole);
declare_value_enum_conversion!(sixtyfps_corelib::graphics::PathEvent, PathEvent);
impl From<sixtyfps_corelib::animations::Instant> for Value {
fn from(value: sixtyfps_corelib::animations::Instant) -> Self {
Value::Number(value.0 as _)
}
}
impl TryInto<sixtyfps_corelib::animations::Instant> for Value {
type Error = ();
fn try_into(self) -> Result<sixtyfps_corelib::animations::Instant, ()> {
match self {
Value::Number(x) => Ok(sixtyfps_corelib::animations::Instant(x as _)),
_ => Err(()),
}
}
}
impl From<()> for Value {
#[inline]
fn from(_: ()) -> Self {
Value::Void
}
}
impl TryInto<()> for Value {
type Error = ();
#[inline]
fn try_into(self) -> Result<(), ()> {
Ok(())
}
}
impl From<sixtyfps_corelib::Color> for Value {
#[inline]
fn from(c: sixtyfps_corelib::Color) -> Self {
Value::Brush(Brush::SolidColor(c))
}
}
impl TryInto<sixtyfps_corelib::Color> for Value {
type Error = Value;
#[inline]
fn try_into(self) -> Result<sixtyfps_corelib::Color, Value> {
match self {
Value::Brush(Brush::SolidColor(c)) => Ok(c),
_ => Err(self),
}
}
}
pub(crate) fn normalize_identifier(ident: &str) -> Cow<'_, str> {
if ident.contains('_') {
ident.replace('_', "-").into()
} else {
ident.into()
}
}
#[derive(Clone, PartialEq, Debug, Default)]
pub struct Struct(HashMap<String, Value>);
impl Struct {
pub fn get_field(&self, name: &str) -> Option<&Value> {
self.0.get(&*normalize_identifier(name))
}
pub fn set_field(&mut self, name: String, value: Value) {
if name.contains('_') {
self.0.insert(name.replace('_', "-"), value);
} else {
self.0.insert(name, value);
}
}
pub fn iter(&self) -> impl Iterator<Item = (&str, &Value)> {
self.0.iter().map(|(a, b)| (a.as_str(), b))
}
}
impl FromIterator<(String, Value)> for Struct {
fn from_iter<T: IntoIterator<Item = (String, Value)>>(iter: T) -> Self {
Self(
iter.into_iter()
.map(|(s, v)| (if s.contains('_') { s.replace('_', "-") } else { s }, v))
.collect(),
)
}
}
impl From<Vec<Value>> for Value {
fn from(a: Vec<Value>) -> Self {
Value::Array(a.into_iter().collect())
}
}
impl TryInto<Vec<Value>> for Value {
type Error = Value;
fn try_into(self) -> Result<Vec<Value>, Value> {
if let Value::Array(a) = self {
Ok(a.into_iter().collect())
} else {
Err(self)
}
}
}
pub struct ComponentCompiler {
config: sixtyfps_compilerlib::CompilerConfiguration,
diagnostics: Vec<Diagnostic>,
}
impl Default for ComponentCompiler {
fn default() -> Self {
Self {
config: sixtyfps_compilerlib::CompilerConfiguration::new(
sixtyfps_compilerlib::generator::OutputFormat::Interpreter,
),
diagnostics: vec![],
}
}
}
impl ComponentCompiler {
pub fn new() -> Self {
Self::default()
}
pub fn set_include_paths(&mut self, include_paths: Vec<std::path::PathBuf>) {
self.config.include_paths = include_paths;
}
pub fn include_paths(&self) -> &Vec<std::path::PathBuf> {
&self.config.include_paths
}
pub fn set_style(&mut self, style: String) {
self.config.style = Some(style);
}
pub fn style(&self) -> Option<&String> {
self.config.style.as_ref()
}
pub fn set_file_loader(
&mut self,
file_loader_fallback: impl Fn(
&Path,
) -> core::pin::Pin<
Box<dyn core::future::Future<Output = Option<std::io::Result<String>>>>,
> + 'static,
) {
self.config.open_import_fallback =
Some(Rc::new(move |path| file_loader_fallback(Path::new(path.as_str()))));
}
pub fn diagnostics(&self) -> &Vec<Diagnostic> {
&self.diagnostics
}
pub async fn build_from_path<P: AsRef<Path>>(
&mut self,
path: P,
) -> Option<ComponentDefinition> {
let path = path.as_ref();
let source = match sixtyfps_compilerlib::diagnostics::load_from_path(path) {
Ok(s) => s,
Err(d) => {
self.diagnostics = vec![d];
return None;
}
};
generativity::make_guard!(guard);
let (c, diag) =
crate::dynamic_component::load(source, path.into(), self.config.clone(), guard).await;
self.diagnostics = diag.into_iter().collect();
c.ok().map(|inner| ComponentDefinition { inner: inner.into() })
}
pub async fn build_from_source(
&mut self,
source_code: String,
path: PathBuf,
) -> Option<ComponentDefinition> {
generativity::make_guard!(guard);
let (c, diag) =
crate::dynamic_component::load(source_code, path, self.config.clone(), guard).await;
self.diagnostics = diag.into_iter().collect();
c.ok().map(|inner| ComponentDefinition { inner: inner.into() })
}
}
#[derive(Clone)]
pub struct ComponentDefinition {
inner: crate::dynamic_component::ErasedComponentDescription,
}
impl ComponentDefinition {
pub fn create(&self) -> ComponentInstance {
generativity::make_guard!(guard);
ComponentInstance {
inner: self.inner.unerase(guard).clone().create(
#[cfg(target_arch = "wasm32")]
"canvas".into(),
),
}
}
#[cfg(target_arch = "wasm32")]
pub fn create_with_canvas_id(&self, canvas_id: &str) -> ComponentInstance {
generativity::make_guard!(guard);
ComponentInstance { inner: self.inner.unerase(guard).clone().create(canvas_id.into()) }
}
#[doc(hidden)]
pub fn create_with_existing_window(&self, window: &Window) -> ComponentInstance {
use sixtyfps_corelib::window::WindowHandleAccess;
generativity::make_guard!(guard);
ComponentInstance {
inner: self
.inner
.unerase(guard)
.clone()
.create_with_existing_window(window.window_handle()),
}
}
#[doc(hidden)]
pub fn properties_and_callbacks(
&self,
) -> impl Iterator<Item = (String, sixtyfps_compilerlib::langtype::Type)> + '_ {
let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
self.inner.unerase(guard).properties()
}
pub fn properties(&self) -> impl Iterator<Item = (String, ValueType)> + '_ {
let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
self.inner.unerase(guard).properties().filter_map(|(prop_name, prop_type)| {
if prop_type.is_property_type() {
Some((prop_name, prop_type.into()))
} else {
None
}
})
}
pub fn callbacks(&self) -> impl Iterator<Item = String> + '_ {
let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
self.inner.unerase(guard).properties().filter_map(|(prop_name, prop_type)| {
if matches!(prop_type, LangType::Callback { .. }) {
Some(prop_name)
} else {
None
}
})
}
pub fn globals(&self) -> impl Iterator<Item = String> + '_ {
let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
self.inner.unerase(guard).global_names()
}
pub fn global_properties(
&self,
global_name: &str,
) -> Option<impl Iterator<Item = (String, ValueType)> + '_> {
let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
self.inner.unerase(guard).global_properties(global_name).map(|iter| {
iter.filter_map(|(prop_name, prop_type)| {
if prop_type.is_property_type() {
Some((prop_name, prop_type.into()))
} else {
None
}
})
})
}
pub fn global_callbacks(&self, global_name: &str) -> Option<impl Iterator<Item = String> + '_> {
let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
self.inner.unerase(guard).global_properties(global_name).map(|iter| {
iter.filter_map(|(prop_name, prop_type)| {
if matches!(prop_type, LangType::Callback { .. }) {
Some(prop_name)
} else {
None
}
})
})
}
pub fn name(&self) -> &str {
let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
self.inner.unerase(guard).id()
}
}
#[cfg(feature = "display-diagnostics")]
pub fn print_diagnostics(diagnostics: &[Diagnostic]) {
let mut build_diagnostics = sixtyfps_compilerlib::diagnostics::BuildDiagnostics::default();
for d in diagnostics {
build_diagnostics.push_compiler_error(d.clone())
}
build_diagnostics.print();
}
#[repr(C)]
pub struct ComponentInstance {
inner: vtable::VRc<
sixtyfps_corelib::component::ComponentVTable,
crate::dynamic_component::ErasedComponentBox,
>,
}
impl ComponentInstance {
pub fn definition(&self) -> ComponentDefinition {
generativity::make_guard!(guard);
ComponentDefinition { inner: self.inner.unerase(guard).description().into() }
}
pub fn get_property(&self, name: &str) -> Result<Value, GetPropertyError> {
generativity::make_guard!(guard);
let comp = self.inner.unerase(guard);
comp.description()
.get_property(comp.borrow(), &normalize_identifier(name))
.map_err(|()| GetPropertyError::NoSuchProperty)
}
pub fn set_property(&self, name: &str, value: Value) -> Result<(), SetPropertyError> {
generativity::make_guard!(guard);
let comp = self.inner.unerase(guard);
comp.description().set_property(comp.borrow(), &normalize_identifier(name), value)
}
pub fn set_callback(
&self,
name: &str,
callback: impl Fn(&[Value]) -> Value + 'static,
) -> Result<(), SetCallbackError> {
generativity::make_guard!(guard);
let comp = self.inner.unerase(guard);
comp.description()
.set_callback_handler(comp.borrow(), &normalize_identifier(name), Box::new(callback))
.map_err(|()| SetCallbackError::NoSuchCallback)
}
pub fn invoke_callback(&self, name: &str, args: &[Value]) -> Result<Value, CallCallbackError> {
generativity::make_guard!(guard);
let comp = self.inner.unerase(guard);
comp.description()
.invoke_callback(comp.borrow(), &normalize_identifier(name), args)
.map_err(|()| CallCallbackError::NoSuchCallback)
}
pub fn get_global_property(
&self,
global: &str,
property: &str,
) -> Result<Value, GetPropertyError> {
generativity::make_guard!(guard);
let comp = self.inner.unerase(guard);
comp.description()
.get_global(comp.borrow(), &normalize_identifier(global))
.map_err(|()| GetPropertyError::NoSuchProperty)? .as_ref()
.get_property(&normalize_identifier(property))
.map_err(|()| GetPropertyError::NoSuchProperty)
}
pub fn set_global_property(
&self,
global: &str,
property: &str,
value: Value,
) -> Result<(), SetPropertyError> {
generativity::make_guard!(guard);
let comp = self.inner.unerase(guard);
comp.description()
.get_global(comp.borrow(), &normalize_identifier(global))
.map_err(|()| SetPropertyError::NoSuchProperty)? .as_ref()
.set_property(&normalize_identifier(property), value)
}
pub fn set_global_callback(
&self,
global: &str,
name: &str,
callback: impl Fn(&[Value]) -> Value + 'static,
) -> Result<(), SetCallbackError> {
generativity::make_guard!(guard);
let comp = self.inner.unerase(guard);
comp.description()
.get_global(comp.borrow(), &normalize_identifier(global))
.map_err(|()| SetCallbackError::NoSuchCallback)? .as_ref()
.set_callback_handler(&normalize_identifier(name), Box::new(callback))
.map_err(|()| SetCallbackError::NoSuchCallback)
}
pub fn invoke_global_callback(
&self,
global: &str,
callback_name: &str,
args: &[Value],
) -> Result<Value, CallCallbackError> {
generativity::make_guard!(guard);
let comp = self.inner.unerase(guard);
comp.description()
.get_global(comp.borrow(), &normalize_identifier(global))
.map_err(|()| CallCallbackError::NoSuchCallback)? .as_ref()
.invoke_callback(&normalize_identifier(callback_name), args)
.map_err(|()| CallCallbackError::NoSuchCallback)
}
pub fn show(&self) {
generativity::make_guard!(guard);
let comp = self.inner.unerase(guard);
comp.borrow_instance().window().show();
}
pub fn hide(&self) {
generativity::make_guard!(guard);
let comp = self.inner.unerase(guard);
comp.borrow_instance().window().hide();
}
pub fn run(&self) {
self.show();
sixtyfps_rendering_backend_default::backend().run_event_loop(
sixtyfps_corelib::backend::EventLoopQuitBehavior::QuitOnLastWindowClosed,
);
self.hide();
}
#[must_use]
pub fn clone_strong(&self) -> Self {
Self { inner: self.inner.clone() }
}
pub fn as_weak(&self) -> WeakComponentInstance {
WeakComponentInstance { inner: vtable::VRc::downgrade(&self.inner) }
}
pub fn window(&self) -> &Window {
self.inner.window()
}
}
#[derive(Clone)]
pub struct WeakComponentInstance {
inner: vtable::VWeak<
sixtyfps_corelib::component::ComponentVTable,
crate::dynamic_component::ErasedComponentBox,
>,
}
impl WeakComponentInstance {
pub fn upgrade(&self) -> Option<ComponentInstance> {
self.inner.upgrade().map(|inner| ComponentInstance { inner })
}
pub fn unwrap(&self) -> ComponentInstance {
self.upgrade().unwrap()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum GetPropertyError {
#[error("no such property")]
NoSuchProperty,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum SetPropertyError {
#[error("no such property")]
NoSuchProperty,
#[error("wrong type")]
WrongType,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum SetCallbackError {
#[error("no such callback")]
NoSuchCallback,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum CallCallbackError {
#[error("no such callback")]
NoSuchCallback,
}
pub fn run_event_loop() {
sixtyfps_rendering_backend_default::backend()
.run_event_loop(sixtyfps_corelib::backend::EventLoopQuitBehavior::QuitOnLastWindowClosed);
}
pub mod testing {
use sixtyfps_corelib::window::WindowHandleAccess;
pub fn send_mouse_click(comp: &super::ComponentInstance, x: f32, y: f32) {
sixtyfps_corelib::tests::sixtyfps_send_mouse_click(
&vtable::VRc::into_dyn(comp.inner.clone()),
x,
y,
comp.window().window_handle(),
);
}
pub fn send_keyboard_string_sequence(
comp: &super::ComponentInstance,
string: sixtyfps_corelib::SharedString,
) {
sixtyfps_corelib::tests::send_keyboard_string_sequence(
&string,
Default::default(),
comp.window().window_handle(),
);
}
}
#[test]
fn component_definition_properties() {
sixtyfps_rendering_backend_testing::init();
let mut compiler = ComponentCompiler::default();
compiler.set_style("fluent".into());
let comp_def = spin_on::spin_on(
compiler.build_from_source(
r#"
export Dummy := Rectangle {
property <string> test;
property <int> underscores-and-dashes_preserved: 44;
callback hello;
}"#
.into(),
"".into(),
),
)
.unwrap();
let props = comp_def.properties().collect::<Vec<(_, _)>>();
assert_eq!(props.len(), 2);
assert_eq!(props[0].0, "test");
assert_eq!(props[0].1, ValueType::String);
assert_eq!(props[1].0, "underscores-and-dashes_preserved");
assert_eq!(props[1].1, ValueType::Number);
let instance = comp_def.create();
assert_eq!(instance.get_property("underscores_and-dashes-preserved"), Ok(Value::Number(44.)));
assert_eq!(
instance.get_property("underscoresanddashespreserved"),
Err(GetPropertyError::NoSuchProperty)
);
assert_eq!(
instance.set_property("underscores-and_dashes-preserved", Value::Number(88.)),
Ok(())
);
assert_eq!(
instance.set_property("underscoresanddashespreserved", Value::Number(99.)),
Err(SetPropertyError::NoSuchProperty)
);
assert_eq!(
instance.set_property("underscores-and_dashes-preserved", Value::String("99".into())),
Err(SetPropertyError::WrongType)
);
assert_eq!(instance.get_property("underscores-and-dashes-preserved"), Ok(Value::Number(88.)));
}
#[test]
fn component_definition_properties2() {
sixtyfps_rendering_backend_testing::init();
let mut compiler = ComponentCompiler::default();
compiler.set_style("fluent".into());
let comp_def = spin_on::spin_on(
compiler.build_from_source(
r#"
export Dummy := Rectangle {
property <string> sub-text <=> sub.text;
sub := Text { property <int> private-not-exported; }
callback hello;
}"#
.into(),
"".into(),
),
)
.unwrap();
let props = comp_def.properties().collect::<Vec<(_, _)>>();
assert_eq!(props.len(), 1);
assert_eq!(props[0].0, "sub-text");
assert_eq!(props[0].1, ValueType::String);
let callbacks = comp_def.callbacks().collect::<Vec<_>>();
assert_eq!(callbacks.len(), 1);
assert_eq!(callbacks[0], "hello");
}
#[test]
fn globals() {
sixtyfps_rendering_backend_testing::init();
let mut compiler = ComponentCompiler::default();
compiler.set_style("fluent".into());
let definition = spin_on::spin_on(
compiler.build_from_source(
r#"
export global My-Super_Global := {
property <int> the-property : 21;
callback my-callback();
}
export { My-Super_Global as AliasedGlobal }
export Dummy := Rectangle {
}"#
.into(),
"".into(),
),
)
.unwrap();
assert_eq!(definition.globals().collect::<Vec<_>>(), vec!["My-Super_Global", "AliasedGlobal"]);
assert!(definition.global_properties("not-there").is_none());
{
let expected_properties = vec![("the-property".to_string(), ValueType::Number)];
let expected_callbacks = vec!["my-callback".to_string()];
let assert_properties_and_callbacks = |global_name| {
assert_eq!(
definition
.global_properties(global_name)
.map(|props| props.collect::<Vec<_>>())
.as_ref(),
Some(&expected_properties)
);
assert_eq!(
definition
.global_callbacks(global_name)
.map(|props| props.collect::<Vec<_>>())
.as_ref(),
Some(&expected_callbacks)
);
};
assert_properties_and_callbacks("My-Super-Global");
assert_properties_and_callbacks("My_Super-Global");
assert_properties_and_callbacks("AliasedGlobal");
}
let instance = definition.create();
assert_eq!(
instance.set_global_property("My_Super-Global", "the_property", Value::Number(44.)),
Ok(())
);
assert_eq!(
instance.set_global_property("AliasedGlobal", "the_property", Value::Number(44.)),
Ok(())
);
assert_eq!(
instance.set_global_property("DontExist", "the-property", Value::Number(88.)),
Err(SetPropertyError::NoSuchProperty)
);
assert_eq!(
instance.set_global_property("My_Super-Global", "theproperty", Value::Number(88.)),
Err(SetPropertyError::NoSuchProperty)
);
assert_eq!(
instance.set_global_property("AliasedGlobal", "theproperty", Value::Number(88.)),
Err(SetPropertyError::NoSuchProperty)
);
assert_eq!(
instance.set_global_property("My_Super-Global", "the_property", Value::String("88".into())),
Err(SetPropertyError::WrongType)
);
assert_eq!(
instance.get_global_property("My-Super_Global", "yoyo"),
Err(GetPropertyError::NoSuchProperty)
);
assert_eq!(
instance.get_global_property("My-Super_Global", "the-property"),
Ok(Value::Number(44.))
);
assert_eq!(
instance.set_property("the-property", Value::Void),
Err(SetPropertyError::NoSuchProperty)
);
assert_eq!(instance.get_property("the-property"), Err(GetPropertyError::NoSuchProperty));
assert_eq!(
instance.set_global_callback("DontExist", "the-property", |_| panic!()),
Err(SetCallbackError::NoSuchCallback)
);
assert_eq!(
instance.set_global_callback("My_Super_Global", "the-property", |_| panic!()),
Err(SetCallbackError::NoSuchCallback)
);
assert_eq!(
instance.set_global_callback("My_Super_Global", "yoyo", |_| panic!()),
Err(SetCallbackError::NoSuchCallback)
);
assert_eq!(
instance.invoke_global_callback("DontExist", "the-property", &[]),
Err(CallCallbackError::NoSuchCallback)
);
assert_eq!(
instance.invoke_global_callback("My_Super_Global", "the-property", &[]),
Err(CallCallbackError::NoSuchCallback)
);
assert_eq!(
instance.invoke_global_callback("My_Super_Global", "yoyo", &[]),
Err(CallCallbackError::NoSuchCallback)
);
}
#[test]
fn component_definition_struct_properties() {
sixtyfps_rendering_backend_testing::init();
let mut compiler = ComponentCompiler::default();
compiler.set_style("fluent".into());
let comp_def = spin_on::spin_on(
compiler.build_from_source(
r#"
export struct Settings := {
string_value: string,
}
export Dummy := Rectangle {
property <Settings> test;
}"#
.into(),
"".into(),
),
)
.unwrap();
let props = comp_def.properties().collect::<Vec<(_, _)>>();
assert_eq!(props.len(), 1);
assert_eq!(props[0].0, "test");
assert_eq!(props[0].1, ValueType::Struct);
let instance = comp_def.create();
let valid_struct: Struct =
[("string_value".to_string(), Value::String("hello".into()))].iter().cloned().collect();
assert_eq!(instance.set_property("test", Value::Struct(valid_struct.clone())), Ok(()));
assert_eq!(instance.get_property("test").unwrap().value_type(), ValueType::Struct);
assert_eq!(instance.set_property("test", Value::Number(42.)), Err(SetPropertyError::WrongType));
let mut invalid_struct = valid_struct.clone();
invalid_struct.set_field("other".into(), Value::Number(44.));
assert_eq!(
instance.set_property("test", Value::Struct(invalid_struct)),
Err(SetPropertyError::WrongType)
);
let mut invalid_struct = valid_struct.clone();
invalid_struct.set_field("string_value".into(), Value::Number(44.));
assert_eq!(
instance.set_property("test", Value::Struct(invalid_struct)),
Err(SetPropertyError::WrongType)
);
}
#[cfg(feature = "ffi")]
#[allow(missing_docs)]
#[path = "ffi.rs"]
pub(crate) mod ffi;