mirror of
https://github.com/slint-ui/slint.git
synced 2025-09-30 22:01:13 +00:00
enable scripts to access the length of arrays
This commit is contained in:
parent
9f3d02c319
commit
ab665ba7b6
6 changed files with 40 additions and 0 deletions
|
@ -179,6 +179,7 @@ fn to_debug_string(
|
|||
match &ty {
|
||||
Type::Invalid => Expression::Invalid,
|
||||
Type::Void
|
||||
| Type::InferredGenericType
|
||||
| Type::InferredCallback
|
||||
| Type::InferredProperty
|
||||
| Type::Component(_)
|
||||
|
|
|
@ -45,6 +45,7 @@ pub enum BuiltinFunction {
|
|||
ColorBrighter,
|
||||
ColorDarker,
|
||||
ImageSize,
|
||||
ArrayLength,
|
||||
Rgb,
|
||||
ImplicitLayoutInfo(Orientation),
|
||||
RegisterCustomFontByPath,
|
||||
|
@ -125,6 +126,10 @@ impl BuiltinFunction {
|
|||
}),
|
||||
args: vec![Type::Image],
|
||||
},
|
||||
BuiltinFunction::ArrayLength => Type::Function {
|
||||
return_type: Box::new(Type::Int32),
|
||||
args: vec![Type::Array(Box::new(Type::InferredGenericType))],
|
||||
},
|
||||
BuiltinFunction::Rgb => Type::Function {
|
||||
return_type: Box::new(Type::Color),
|
||||
args: vec![Type::Int32, Type::Int32, Type::Int32, Type::Float32],
|
||||
|
@ -168,6 +173,7 @@ impl BuiltinFunction {
|
|||
BuiltinFunction::ImageSize => true,
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
BuiltinFunction::ImageSize => false,
|
||||
BuiltinFunction::ArrayLength => true,
|
||||
BuiltinFunction::Rgb => true,
|
||||
BuiltinFunction::ImplicitLayoutInfo(_) => false,
|
||||
BuiltinFunction::RegisterCustomFontByPath
|
||||
|
@ -985,6 +991,7 @@ impl Expression {
|
|||
pub fn default_value_for_type(ty: &Type) -> Expression {
|
||||
match ty {
|
||||
Type::Invalid
|
||||
| Type::InferredGenericType
|
||||
| Type::Component(_)
|
||||
| Type::Builtin(_)
|
||||
| Type::Native(_)
|
||||
|
|
|
@ -1627,6 +1627,9 @@ fn compile_expression(
|
|||
BuiltinFunction::ImageSize => {
|
||||
"[](const sixtyfps::Image &img) { return img.size(); }".into()
|
||||
}
|
||||
BuiltinFunction::ArrayLength => {
|
||||
"[](const sixtyfps::Model &model) { return (int) model.row_count(); }".into()
|
||||
}
|
||||
BuiltinFunction::Rgb => {
|
||||
"[](int r, int g, int b, float a) {{ return sixtyfps::Color::from_argb_uint8(std::clamp(a * 255., 0., 255.), std::clamp(r, 0, 255), std::clamp(g, 0, 255), std::clamp(b, 0, 255)); }}".into()
|
||||
}
|
||||
|
|
|
@ -1238,6 +1238,9 @@ fn compile_expression(expr: &Expression, component: &Rc<Component>) -> TokenStre
|
|||
BuiltinFunction::ImageSize => {
|
||||
quote!((|x: Image| -> Size { x.size() }))
|
||||
}
|
||||
BuiltinFunction::ArrayLength => {
|
||||
quote!((|x: ModelHandle<_>| -> i32 { x.row_count() as i32 }))
|
||||
}
|
||||
|
||||
BuiltinFunction::Rgb => {
|
||||
quote!((|r: i32, g: i32, b: i32, a: f32| {
|
||||
|
|
|
@ -76,11 +76,14 @@ pub enum Type {
|
|||
|
||||
/// This is a `SharedArray<f32>`
|
||||
LayoutCache,
|
||||
|
||||
InferredGenericType,
|
||||
}
|
||||
|
||||
impl core::cmp::PartialEq for Type {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
match self {
|
||||
Type::InferredGenericType => true,
|
||||
Type::Invalid => matches!(other, Type::Invalid),
|
||||
Type::Void => matches!(other, Type::Void),
|
||||
Type::InferredProperty => matches!(other, Type::InferredProperty),
|
||||
|
@ -124,6 +127,7 @@ impl core::cmp::PartialEq for Type {
|
|||
impl Display for Type {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Type::InferredGenericType => write!(f, "<_>"),
|
||||
Type::Invalid => write!(f, "<error>"),
|
||||
Type::Void => write!(f, "void"),
|
||||
Type::InferredProperty => write!(f, "?"),
|
||||
|
@ -469,6 +473,7 @@ impl Type {
|
|||
// Unit::Percent is special that it does not combine with other units like
|
||||
Type::Percent => None,
|
||||
Type::Angle => Some(Unit::Deg),
|
||||
Type::InferredGenericType => None,
|
||||
Type::Invalid => None,
|
||||
Type::Void => None,
|
||||
Type::InferredProperty | Type::InferredCallback => None,
|
||||
|
|
|
@ -521,6 +521,7 @@ impl LookupObject for Expression {
|
|||
Type::String => StringExpression(self).for_each_entry(ctx, f),
|
||||
Type::Color => ColorExpression(self).for_each_entry(ctx, f),
|
||||
Type::Image => ImageExpression(self).for_each_entry(ctx, f),
|
||||
Type::Array(_) => ArrayExpression(self).for_each_entry(ctx, f),
|
||||
_ => None,
|
||||
},
|
||||
}
|
||||
|
@ -547,6 +548,7 @@ impl LookupObject for Expression {
|
|||
Type::String => StringExpression(self).lookup(ctx, name),
|
||||
Type::Color => ColorExpression(self).lookup(ctx, name),
|
||||
Type::Image => ImageExpression(self).lookup(ctx, name),
|
||||
Type::Array(_) => ArrayExpression(self).lookup(ctx, name),
|
||||
_ => None,
|
||||
},
|
||||
}
|
||||
|
@ -614,3 +616,22 @@ impl<'a> LookupObject for ImageExpression<'a> {
|
|||
.or_else(|| f("height", field_access("height")))
|
||||
}
|
||||
}
|
||||
|
||||
struct ArrayExpression<'a>(&'a Expression);
|
||||
impl<'a> LookupObject for ArrayExpression<'a> {
|
||||
fn for_each_entry<R>(
|
||||
&self,
|
||||
ctx: &LookupCtx,
|
||||
f: &mut impl FnMut(&str, Expression) -> Option<R>,
|
||||
) -> Option<R> {
|
||||
let member_function = |f: BuiltinFunction| Expression::FunctionCall {
|
||||
function: Box::new(Expression::BuiltinFunctionReference(
|
||||
f,
|
||||
ctx.current_token.as_ref().map(|t| t.to_source_location()),
|
||||
)),
|
||||
source_location: ctx.current_token.as_ref().map(|t| t.to_source_location()),
|
||||
arguments: vec![self.0.clone()],
|
||||
};
|
||||
None.or_else(|| f("length", member_function(BuiltinFunction::ArrayLength)))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue