mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-01 06:11:16 +00:00
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:
parent
72b1f0259b
commit
071ab9fda1
5 changed files with 17 additions and 3 deletions
|
@ -146,7 +146,7 @@ fn to_eval_value<'cx>(
|
||||||
) -> NeonResult<sixtyfps_interpreter::Value> {
|
) -> NeonResult<sixtyfps_interpreter::Value> {
|
||||||
use sixtyfps_interpreter::Value;
|
use sixtyfps_interpreter::Value;
|
||||||
match ty {
|
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()))
|
Ok(Value::Number(val.downcast_or_throw::<JsNumber, _>(cx)?.value()))
|
||||||
}
|
}
|
||||||
Type::String => Ok(Value::String(val.to_string(cx)?.value().into())),
|
Type::String => Ok(Value::String(val.to_string(cx)?.value().into())),
|
||||||
|
|
|
@ -133,7 +133,7 @@ declare_units! {
|
||||||
/// No unit was given
|
/// No unit was given
|
||||||
None = "" -> Float32,
|
None = "" -> Float32,
|
||||||
///
|
///
|
||||||
Percent = "%" -> Float32 * 0.01,
|
Percent = "%" -> Percent,
|
||||||
|
|
||||||
// Lengths or Coord
|
// Lengths or Coord
|
||||||
|
|
||||||
|
@ -622,6 +622,11 @@ impl Expression {
|
||||||
}),
|
}),
|
||||||
op: '*',
|
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)) => {
|
(Type::Object(ref a), Type::Object(b)) => {
|
||||||
if let Expression::Object { mut values, .. } = self {
|
if let Expression::Object { mut values, .. } = self {
|
||||||
let mut new_values = HashMap::new();
|
let mut new_values = HashMap::new();
|
||||||
|
@ -722,6 +727,7 @@ impl Expression {
|
||||||
Type::Duration => Expression::NumberLiteral(0., Unit::Ms),
|
Type::Duration => Expression::NumberLiteral(0., Unit::Ms),
|
||||||
Type::Length => Expression::NumberLiteral(0., Unit::Phx),
|
Type::Length => Expression::NumberLiteral(0., Unit::Phx),
|
||||||
Type::LogicalLength => Expression::NumberLiteral(0., Unit::Px),
|
Type::LogicalLength => Expression::NumberLiteral(0., Unit::Px),
|
||||||
|
Type::Percent => Expression::NumberLiteral(100., Unit::Percent),
|
||||||
// FIXME: Is that correct?
|
// FIXME: Is that correct?
|
||||||
Type::Resource => Expression::ResourceReference { absolute_source_path: String::new() },
|
Type::Resource => Expression::ResourceReference { absolute_source_path: String::new() },
|
||||||
Type::Bool => Expression::BoolLiteral(false),
|
Type::Bool => Expression::BoolLiteral(false),
|
||||||
|
|
|
@ -205,6 +205,7 @@ impl CppType for Type {
|
||||||
Type::Duration => Some("std::int64_t".to_owned()),
|
Type::Duration => Some("std::int64_t".to_owned()),
|
||||||
Type::Length => Some("float".to_owned()),
|
Type::Length => Some("float".to_owned()),
|
||||||
Type::LogicalLength => Some("float".to_owned()),
|
Type::LogicalLength => Some("float".to_owned()),
|
||||||
|
Type::Percent => Some("float".to_owned()),
|
||||||
Type::Bool => Some("bool".to_owned()),
|
Type::Bool => Some("bool".to_owned()),
|
||||||
Type::Object(o) => {
|
Type::Object(o) => {
|
||||||
let elem = o.values().map(|v| v.cpp_type()).collect::<Option<Vec<_>>>()?;
|
let elem = o.values().map(|v| v.cpp_type()).collect::<Option<Vec<_>>>()?;
|
||||||
|
|
|
@ -33,6 +33,7 @@ fn rust_type(
|
||||||
Type::Duration => Ok(quote!(i64)),
|
Type::Duration => Ok(quote!(i64)),
|
||||||
Type::Length => Ok(quote!(f32)),
|
Type::Length => Ok(quote!(f32)),
|
||||||
Type::LogicalLength => Ok(quote!(f32)),
|
Type::LogicalLength => Ok(quote!(f32)),
|
||||||
|
Type::Percent => Ok(quote!(f32)),
|
||||||
Type::Bool => Ok(quote!(bool)),
|
Type::Bool => Ok(quote!(bool)),
|
||||||
Type::Resource => Ok(quote!(sixtyfps::re_exports::Resource)),
|
Type::Resource => Ok(quote!(sixtyfps::re_exports::Resource)),
|
||||||
Type::Object(o) => {
|
Type::Object(o) => {
|
||||||
|
|
|
@ -39,6 +39,7 @@ pub enum Type {
|
||||||
Duration,
|
Duration,
|
||||||
Length,
|
Length,
|
||||||
LogicalLength,
|
LogicalLength,
|
||||||
|
Percent,
|
||||||
Resource,
|
Resource,
|
||||||
Bool,
|
Bool,
|
||||||
Model,
|
Model,
|
||||||
|
@ -73,6 +74,7 @@ impl core::cmp::PartialEq for Type {
|
||||||
Type::Duration => matches!(other, Type::Duration),
|
Type::Duration => matches!(other, Type::Duration),
|
||||||
Type::Length => matches!(other, Type::Length),
|
Type::Length => matches!(other, Type::Length),
|
||||||
Type::LogicalLength => matches!(other, Type::LogicalLength),
|
Type::LogicalLength => matches!(other, Type::LogicalLength),
|
||||||
|
Type::Percent => matches!(other, Type::Percent),
|
||||||
Type::Resource => matches!(other, Type::Resource),
|
Type::Resource => matches!(other, Type::Resource),
|
||||||
Type::Bool => matches!(other, Type::Bool),
|
Type::Bool => matches!(other, Type::Bool),
|
||||||
Type::Model => matches!(other, Type::Model),
|
Type::Model => matches!(other, Type::Model),
|
||||||
|
@ -127,6 +129,7 @@ impl Display for Type {
|
||||||
Type::Duration => write!(f, "duration"),
|
Type::Duration => write!(f, "duration"),
|
||||||
Type::Length => write!(f, "length"),
|
Type::Length => write!(f, "length"),
|
||||||
Type::LogicalLength => write!(f, "logical_length"),
|
Type::LogicalLength => write!(f, "logical_length"),
|
||||||
|
Type::Percent => write!(f, "percent"),
|
||||||
Type::Color => write!(f, "color"),
|
Type::Color => write!(f, "color"),
|
||||||
Type::Resource => write!(f, "resource"),
|
Type::Resource => write!(f, "resource"),
|
||||||
Type::Bool => write!(f, "bool"),
|
Type::Bool => write!(f, "bool"),
|
||||||
|
@ -165,6 +168,7 @@ impl Type {
|
||||||
| Self::Duration
|
| Self::Duration
|
||||||
| Self::Length
|
| Self::Length
|
||||||
| Self::LogicalLength
|
| Self::LogicalLength
|
||||||
|
| Self::Percent
|
||||||
| Self::Resource
|
| Self::Resource
|
||||||
| Self::Bool
|
| Self::Bool
|
||||||
| Self::Model
|
| Self::Model
|
||||||
|
@ -312,7 +316,8 @@ impl Type {
|
||||||
| (Type::Float32, Type::Model)
|
| (Type::Float32, Type::Model)
|
||||||
| (Type::Int32, Type::Model)
|
| (Type::Int32, Type::Model)
|
||||||
| (Type::Length, Type::LogicalLength)
|
| (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::Object(b)) if can_convert_object(a, b) => true,
|
||||||
(Type::Object(a), Type::Component(c)) if can_convert_object_to_component(a, c) => true,
|
(Type::Object(a), Type::Component(c)) if can_convert_object_to_component(a, c) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
|
@ -346,6 +351,7 @@ impl Type {
|
||||||
Type::Duration => Some(Unit::Ms),
|
Type::Duration => Some(Unit::Ms),
|
||||||
Type::Length => Some(Unit::Phx),
|
Type::Length => Some(Unit::Phx),
|
||||||
Type::LogicalLength => Some(Unit::Px),
|
Type::LogicalLength => Some(Unit::Px),
|
||||||
|
Type::Percent => Some(Unit::Percent),
|
||||||
Type::Invalid => None,
|
Type::Invalid => None,
|
||||||
Type::Void => None,
|
Type::Void => None,
|
||||||
Type::Component(_) => None,
|
Type::Component(_) => None,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue