Skip to content

Commit

Permalink
compiler: fix materialize_fake_property for properties in base class
Browse files Browse the repository at this point in the history
Especially for font-metrics
  • Loading branch information
ogoffart committed Dec 10, 2024
1 parent afd6c65 commit 8797faa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
36 changes: 18 additions & 18 deletions internal/compiler/passes/materialize_fake_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,8 @@ fn should_materialize(
}
let has_declared_property = match base_type {
ElementType::Component(c) => has_declared_property(&c.root_element.borrow(), prop),
ElementType::Builtin(b) => {
if let Some(p) = b.properties.get(prop) {
if b.native_class.lookup_property(prop).is_none() {
return Some(p.ty.clone());
}
true
} else {
false
}
}
ElementType::Native(n) => {
n.lookup_property(prop).map_or(false, |prop_type| prop_type.is_property_type())
}
ElementType::Builtin(b) => b.native_class.lookup_property(prop).is_some(),
ElementType::Native(n) => n.lookup_property(prop).is_some(),
ElementType::Global | ElementType::Error => false,
};

Expand All @@ -122,6 +111,9 @@ fn should_materialize(
return Some(Type::Enumeration(
crate::typeregister::BUILTIN.with(|e| e.enums.PopupClosePolicy.clone()),
));
} else {
let ty = base_type.lookup_property(prop).property_type.clone();
return (ty != Type::Invalid).then_some(ty);
}
}
None
Expand All @@ -135,18 +127,26 @@ fn has_declared_property(elem: &Element, prop: &str) -> bool {
}
match &elem.base_type {
ElementType::Component(c) => has_declared_property(&c.root_element.borrow(), prop),
ElementType::Builtin(b) => b.properties.contains_key(prop),
ElementType::Builtin(b) => b.native_class.lookup_property(prop).is_some(),
ElementType::Native(n) => n.lookup_property(prop).is_some(),
ElementType::Global | ElementType::Error => false,
}
}

/// Initialize a sensible default binding for the now materialized property
pub fn initialize(elem: &ElementRc, name: &str) -> Option<Expression> {
if let ElementType::Builtin(b) = &elem.borrow().base_type {
if let Some(expr) = b.properties.get(name).and_then(|prop| prop.default_value.expr(elem)) {
return Some(expr);
}
let mut base_type = elem.borrow().base_type.clone();
loop {
base_type = match base_type {
ElementType::Component(ref c) => c.root_element.borrow().base_type.clone(),
ElementType::Builtin(b) => {
match b.properties.get(name).and_then(|prop| prop.default_value.expr(elem)) {
Some(expr) => return Some(expr),
None => break,
}
}
_ => break,
};
}

let expr = match name {
Expand Down
7 changes: 6 additions & 1 deletion tests/cases/text/metrics.slint
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

component Text2 inherits Text {}

export component TestCase inherits Window {
width: 100phx;
height: 100phx;
Expand All @@ -10,9 +12,12 @@ export component TestCase inherits Window {
stroke-width: 2px;
}

inherit-text := Text2 {}

text-input := TextInput { }

out property <bool> test: simple-text.font-metrics.ascent == complex-text.font-metrics.ascent && complex-text.font-metrics.ascent == text-input.font-metrics.ascent && text-input.font-metrics.ascent == 7px;
out property <bool> test: simple-text.font-metrics.ascent == complex-text.font-metrics.ascent && complex-text.font-metrics.ascent == text-input.font-metrics.ascent
&& inherit-text.font-metrics.ascent == simple-text.font-metrics.ascent && text-input.font-metrics.ascent == 7px;
}

/*
Expand Down

0 comments on commit 8797faa

Please sign in to comment.