From 01bf529f79a5972f8e1b6b28d4e82bb78ab143f9 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Fri, 6 May 2022 10:17:08 +0200 Subject: [PATCH] Properly initialize the default binding of a state to its materialized value Fixes #1237 --- internal/compiler/passes/lower_states.rs | 8 +-- .../passes/materialize_fake_properties.rs | 2 +- .../properties/issue1237_states_visible.slint | 53 +++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 tests/cases/properties/issue1237_states_visible.slint diff --git a/internal/compiler/passes/lower_states.rs b/internal/compiler/passes/lower_states.rs index 5e147df3f3a..96ea4bd554e 100644 --- a/internal/compiler/passes/lower_states.rs +++ b/internal/compiler/passes/lower_states.rs @@ -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) } diff --git a/internal/compiler/passes/materialize_fake_properties.rs b/internal/compiler/passes/materialize_fake_properties.rs index 2c22ad0297e..77696e1e611 100644 --- a/internal/compiler/passes/materialize_fake_properties.rs +++ b/internal/compiler/passes/materialize_fake_properties.rs @@ -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 { +pub fn initialize(elem: &ElementRc, name: &str) -> Option { let expr = match name { "min-height" => layout_constraint_prop(elem, "min", Orientation::Vertical), "min-width" => layout_constraint_prop(elem, "min", Orientation::Horizontal), diff --git a/tests/cases/properties/issue1237_states_visible.slint b/tests/cases/properties/issue1237_states_visible.slint new file mode 100644 index 00000000000..c5ff3bcd925 --- /dev/null +++ b/tests/cases/properties/issue1237_states_visible.slint @@ -0,0 +1,53 @@ +// Copyright © SixtyFPS GmbH +// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial + +TestCase := Window { + width: 100px; + height: 100px; + property 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 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); +``` + + +*/