Skip to content

Commit

Permalink
Properly initialize the default binding of a state to its materialize…
Browse files Browse the repository at this point in the history
…d value

Fixes #1237
  • Loading branch information
ogoffart committed May 6, 2022
1 parent 8be92ea commit 01bf529
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
8 changes: 5 additions & 3 deletions internal/compiler/passes/lower_states.rs
Expand Up @@ -201,7 +201,9 @@ fn expression_for_property(element: &ElementRc, name: &str) -> ExpressionForProp
None
};
}
ExpressionForProperty::Expression(Expression::default_value_for_type(
&element.borrow().lookup_property(name).property_type,
))
let expr = super::materialize_fake_properties::initialize(element, name).unwrap_or_else(|| {
Expression::default_value_for_type(&element.borrow().lookup_property(name).property_type)
});

ExpressionForProperty::Expression(expr)
}
2 changes: 1 addition & 1 deletion internal/compiler/passes/materialize_fake_properties.rs
Expand Up @@ -121,7 +121,7 @@ fn has_declared_property(elem: &Element, prop: &str) -> bool {
}

/// Initialize a sensible default binding for the now materialized property
fn initialize(elem: &ElementRc, name: &str) -> Option<Expression> {
pub fn initialize(elem: &ElementRc, name: &str) -> Option<Expression> {
let expr = match name {
"min-height" => layout_constraint_prop(elem, "min", Orientation::Vertical),
"min-width" => layout_constraint_prop(elem, "min", Orientation::Horizontal),
Expand Down
53 changes: 53 additions & 0 deletions tests/cases/properties/issue1237_states_visible.slint
@@ -0,0 +1,53 @@
// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial

TestCase := Window {
width: 100px;
height: 100px;
property<int> niceness: 33;
greeting := TouchArea {
clicked => { root.niceness += 1; }
t:= Text {
text: "Hello world";
}

states [
rude when root.niceness <= 0: {visible: false;}
mannered when root.niceness > 100: {t.text: "Hello, dearest world"; t.font-size: 32px;}
]
}

property <bool> test: greeting.visible;
}


/*
```cpp
auto handle = TestCase::create();
const TestCase &instance = *handle;
assert(instance.get_test());
slint::testing::send_mouse_click(&instance, 50., 50.);
assert_eq(instance.get_niceness(), 34);
instance.set_niceness(-1);
assert(!instance.get_test());
slint::testing::send_mouse_click(&instance, 50., 50.);
assert_eq(instance.get_niceness(), -1);
```
```rust
let instance = TestCase::new();
assert!(instance.get_test());
slint::testing::send_mouse_click(&instance, 50., 50.);
assert_eq!(instance.get_niceness(), 34);
instance.set_niceness(-1);
assert!(!instance.get_test());
slint::testing::send_mouse_click(&instance, 50., 50.);
assert_eq!(instance.get_niceness(), -1);
```
*/

0 comments on commit 01bf529

Please sign in to comment.