Add support for select-all(), cut(), copy() and paste() functions on text input elements (#2804)

In the compiler this is still very primitive, but an attempt to start a
generic interface. The basic assumption is that all item functions will
eventually need access to the window adapter and itemrc. Support for
additional arguments is still missing.

Also missing is support for the function access via rtti in the
interpreter, hence the hardcoding at the moment.
This commit is contained in:
Simon Hausmann 2023-06-01 16:04:53 +02:00 committed by GitHub
parent d52f40979c
commit c428601370
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 383 additions and 40 deletions

View file

@ -16,7 +16,7 @@ use std::rc::{Rc, Weak};
pub use crate::namedreference::NamedReference;
pub use crate::passes::resolving;
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
/// A function built into the run-time
pub enum BuiltinFunction {
GetWindowScaleFactor,
@ -39,6 +39,8 @@ pub enum BuiltinFunction {
Pow,
SetFocusItem,
ShowPopupWindow,
/// A function that belongs to an item (such as TextInput's select-all function).
ItemMemberFunction(String),
/// the "42".to_float()
StringToFloat,
/// the "42".is_float()
@ -122,6 +124,10 @@ impl BuiltinFunction {
return_type: Box::new(Type::Void),
args: vec![Type::ElementReference],
},
BuiltinFunction::ItemMemberFunction(..) => Type::Function {
return_type: Box::new(Type::Void),
args: vec![Type::ElementReference],
},
BuiltinFunction::StringToFloat => {
Type::Function { return_type: Box::new(Type::Float32), args: vec![Type::String] }
}
@ -205,6 +211,7 @@ impl BuiltinFunction {
| BuiltinFunction::ATan => true,
BuiltinFunction::SetFocusItem => false,
BuiltinFunction::ShowPopupWindow => false,
BuiltinFunction::ItemMemberFunction(..) => false,
BuiltinFunction::StringToFloat | BuiltinFunction::StringIsFloat => true,
BuiltinFunction::ColorBrighter | BuiltinFunction::ColorDarker => true,
// ImageSize is pure, except when loading images via the network. Then the initial size will be 0/0 and
@ -251,6 +258,7 @@ impl BuiltinFunction {
| BuiltinFunction::ATan => true,
BuiltinFunction::SetFocusItem => false,
BuiltinFunction::ShowPopupWindow => false,
BuiltinFunction::ItemMemberFunction(..) => false,
BuiltinFunction::StringToFloat | BuiltinFunction::StringIsFloat => true,
BuiltinFunction::ColorBrighter | BuiltinFunction::ColorDarker => true,
BuiltinFunction::ImageSize => true,
@ -1040,26 +1048,27 @@ impl Expression {
if let Some(conversion_powers) =
crate::langtype::unit_product_length_conversion(&left, &right)
{
let apply_power = |mut result, power: i8, builtin_fn| {
let op = if power < 0 { '*' } else { '/' };
for _ in 0..power.abs() {
result = Expression::BinaryExpression {
lhs: Box::new(result),
rhs: Box::new(Expression::FunctionCall {
function: Box::new(
Expression::BuiltinFunctionReference(
builtin_fn,
Some(node.to_source_location()),
let apply_power =
|mut result, power: i8, builtin_fn: BuiltinFunction| {
let op = if power < 0 { '*' } else { '/' };
for _ in 0..power.abs() {
result = Expression::BinaryExpression {
lhs: Box::new(result),
rhs: Box::new(Expression::FunctionCall {
function: Box::new(
Expression::BuiltinFunctionReference(
builtin_fn.clone(),
Some(node.to_source_location()),
),
),
),
arguments: vec![],
source_location: Some(node.to_source_location()),
}),
op,
arguments: vec![],
source_location: Some(node.to_source_location()),
}),
op,
}
}
}
result
};
result
};
let mut result = self;