Prepare for use of percentages as widths in layouts

Delay the conversion of percentage to the float to code generation type
by inserting the multiplication into the syntax tree. That way we will
be able to detect plain uses of percetages and interpret them
differently.
This commit is contained in:
Simon Hausmann 2020-10-14 16:01:43 +02:00
parent 72b1f0259b
commit 071ab9fda1
5 changed files with 17 additions and 3 deletions

View file

@ -146,7 +146,7 @@ fn to_eval_value<'cx>(
) -> NeonResult<sixtyfps_interpreter::Value> {
use sixtyfps_interpreter::Value;
match ty {
Type::Float32 | Type::Int32 | Type::Duration | Type::Length | Type::LogicalLength => {
Type::Float32 | Type::Int32 | Type::Duration | Type::Length | Type::LogicalLength | Type::Percent => {
Ok(Value::Number(val.downcast_or_throw::<JsNumber, _>(cx)?.value()))
}
Type::String => Ok(Value::String(val.to_string(cx)?.value().into())),

View file

@ -133,7 +133,7 @@ declare_units! {
/// No unit was given
None = "" -> Float32,
///
Percent = "%" -> Float32 * 0.01,
Percent = "%" -> Percent,
// Lengths or Coord
@ -622,6 +622,11 @@ impl Expression {
}),
op: '*',
},
(Type::Percent, Type::Float32) => Expression::BinaryExpression {
lhs: Box::new(self),
rhs: Box::new(Expression::NumberLiteral(0.01, Unit::None)),
op: '*',
},
(Type::Object(ref a), Type::Object(b)) => {
if let Expression::Object { mut values, .. } = self {
let mut new_values = HashMap::new();
@ -722,6 +727,7 @@ impl Expression {
Type::Duration => Expression::NumberLiteral(0., Unit::Ms),
Type::Length => Expression::NumberLiteral(0., Unit::Phx),
Type::LogicalLength => Expression::NumberLiteral(0., Unit::Px),
Type::Percent => Expression::NumberLiteral(100., Unit::Percent),
// FIXME: Is that correct?
Type::Resource => Expression::ResourceReference { absolute_source_path: String::new() },
Type::Bool => Expression::BoolLiteral(false),

View file

@ -205,6 +205,7 @@ impl CppType for Type {
Type::Duration => Some("std::int64_t".to_owned()),
Type::Length => Some("float".to_owned()),
Type::LogicalLength => Some("float".to_owned()),
Type::Percent => Some("float".to_owned()),
Type::Bool => Some("bool".to_owned()),
Type::Object(o) => {
let elem = o.values().map(|v| v.cpp_type()).collect::<Option<Vec<_>>>()?;

View file

@ -33,6 +33,7 @@ fn rust_type(
Type::Duration => Ok(quote!(i64)),
Type::Length => Ok(quote!(f32)),
Type::LogicalLength => Ok(quote!(f32)),
Type::Percent => Ok(quote!(f32)),
Type::Bool => Ok(quote!(bool)),
Type::Resource => Ok(quote!(sixtyfps::re_exports::Resource)),
Type::Object(o) => {

View file

@ -39,6 +39,7 @@ pub enum Type {
Duration,
Length,
LogicalLength,
Percent,
Resource,
Bool,
Model,
@ -73,6 +74,7 @@ impl core::cmp::PartialEq for Type {
Type::Duration => matches!(other, Type::Duration),
Type::Length => matches!(other, Type::Length),
Type::LogicalLength => matches!(other, Type::LogicalLength),
Type::Percent => matches!(other, Type::Percent),
Type::Resource => matches!(other, Type::Resource),
Type::Bool => matches!(other, Type::Bool),
Type::Model => matches!(other, Type::Model),
@ -127,6 +129,7 @@ impl Display for Type {
Type::Duration => write!(f, "duration"),
Type::Length => write!(f, "length"),
Type::LogicalLength => write!(f, "logical_length"),
Type::Percent => write!(f, "percent"),
Type::Color => write!(f, "color"),
Type::Resource => write!(f, "resource"),
Type::Bool => write!(f, "bool"),
@ -165,6 +168,7 @@ impl Type {
| Self::Duration
| Self::Length
| Self::LogicalLength
| Self::Percent
| Self::Resource
| Self::Bool
| Self::Model
@ -312,7 +316,8 @@ impl Type {
| (Type::Float32, Type::Model)
| (Type::Int32, Type::Model)
| (Type::Length, Type::LogicalLength)
| (Type::LogicalLength, Type::Length) => true,
| (Type::LogicalLength, Type::Length)
| (Type::Percent, Type::Float32) => true,
(Type::Object(a), Type::Object(b)) if can_convert_object(a, b) => true,
(Type::Object(a), Type::Component(c)) if can_convert_object_to_component(a, c) => true,
_ => false,
@ -346,6 +351,7 @@ impl Type {
Type::Duration => Some(Unit::Ms),
Type::Length => Some(Unit::Phx),
Type::LogicalLength => Some(Unit::Px),
Type::Percent => Some(Unit::Percent),
Type::Invalid => None,
Type::Void => None,
Type::Component(_) => None,