Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions internal/compiler/llr/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,14 +717,15 @@ impl<'a, T> EvaluationContext<'a, T> {
r: &'_ LocalMemberIndex,
map: ContextMap,
) -> PropertyInfoResult<'a> {
let binding = g.init_values.get(r).map(|b| (b, map));
let binding = g.init_values.get(r).map(|b| (b, map.clone()));
let animation = g.animations.get(r).map(|a| (a, map));
match r {
LocalMemberIndex::Property(index) => {
let property_decl = &g.properties[*index];
PropertyInfoResult {
analysis: Some(&g.prop_analysis[*index]),
binding,
animation: None,
animation,
ty: property_decl.ty.clone(),
use_count: Some(&property_decl.use_count),
}
Expand Down
2 changes: 2 additions & 0 deletions internal/compiler/llr/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ pub struct GlobalComponent {
pub functions: TiVec<FunctionIdx, Function>,
/// One entry per property
pub init_values: BTreeMap<LocalMemberIndex, BindingExpression>,
/// The animation for properties which are animated
pub animations: BTreeMap<LocalMemberIndex, Expression>,
// maps property to its changed callback
pub change_callbacks: BTreeMap<PropertyIdx, MutExpression>,
pub const_properties: TiVec<PropertyIdx, bool>,
Expand Down
12 changes: 10 additions & 2 deletions internal/compiler/llr/lower_to_item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,7 @@ fn lower_global(
GlobalComponent {
name: global.root_element.borrow().id.clone(),
init_values: BTreeMap::new(),
animations: BTreeMap::new(),
properties,
callbacks,
functions,
Expand Down Expand Up @@ -1012,9 +1013,13 @@ fn lower_global_expressions(

for (prop, binding) in &global.root_element.borrow().bindings {
assert!(binding.borrow().two_way_bindings.is_empty());
assert!(binding.borrow().animation.is_none());
let expression =
super::lower_expression::lower_expression(&binding.borrow().expression, &mut ctx);
let animation = binding
.borrow()
.animation
.as_ref()
.map(|a| super::lower_expression::lower_animation(a, &mut ctx));

let nr = NamedReference::new(&global.root_element, prop.clone());
let member_index = match &ctx.state.global_properties[&nr] {
Expand All @@ -1027,12 +1032,15 @@ fn lower_global_expressions(
MemberReference::Global { member, .. } => member.clone(),
_ => unreachable!(),
};
if let Some(Animation::Static(animation)) = animation.as_ref() {
lowered.animations.insert(member_index.clone(), animation.clone());
}
let is_constant = binding.borrow().analysis.as_ref().is_some_and(|a| a.is_const);
lowered.init_values.insert(
member_index,
BindingExpression {
expression: expression.into(),
animation: None,
animation,
is_constant,
is_state_info: false,
use_count: 0.into(),
Expand Down
11 changes: 11 additions & 0 deletions internal/compiler/llr/optim_passes/remove_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub fn remove_unused(root: &mut CompilationUnit) {
}
for (idx, g) in root.globals.iter_mut_enumerated() {
g.init_values.retain(|x, _| mappings.glob_mappings[idx].keep(x));
g.animations.retain(|x, _| mappings.glob_mappings[idx].keep(x));
}

impl visitor::Visitor for &RemoveUnusedMappings {
Expand Down Expand Up @@ -423,6 +424,7 @@ mod visitor {
callbacks: _,
functions,
init_values,
animations,
change_callbacks,
const_properties: _,
public_properties,
Expand Down Expand Up @@ -450,6 +452,15 @@ mod visitor {
})
.collect();

*animations = std::mem::take(animations)
.into_iter()
.map(|(mut k, mut v)| {
visit_member_index(&mut k, &scope, state, visitor);
visit_expression(&mut v, &scope, state, visitor);
(k, v)
})
.collect();

*change_callbacks = std::mem::take(change_callbacks)
.into_iter()
.map(|(mut k, mut v)| {
Expand Down
14 changes: 14 additions & 0 deletions internal/compiler/llr/pretty_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,20 @@ impl PrettyPrinter<'_> {
_ => unreachable!(),
}
}
for (p, animation) in &global.animations {
self.indent()?;
match p {
LocalMemberIndex::Property(p) => {
writeln!(
self.writer,
"animate {} {{ {} }}",
global.properties[*p].name,
DisplayExpression(animation, &ctx),
)?;
}
_ => unreachable!(),
}
}

for (p, e) in &global.change_callbacks {
self.indent()?;
Expand Down
50 changes: 50 additions & 0 deletions tests/cases/issues/issue_9961_animation_alias_to_global.slint
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

export global GlobalInfo {
in-out property <int> animated: 8;
}

export component TestCase inherits Window {
property <int> animated <=> GlobalInfo.animated;
animate animated { duration: 300ms; }
}

/*

```rust
let instance = TestCase::new().unwrap();
let global = instance.global::<GlobalInfo<'_>>();
assert_eq!(global.get_animated(), 8);
global.set_animated(108);
assert_eq!(global.get_animated(), 8);
slint_testing::mock_elapsed_time(150);
assert_eq!(global.get_animated(), 58);
slint_testing::mock_elapsed_time(150);
assert_eq!(global.get_animated(), 108);
```

```cpp
auto handle = TestCase::create();
const TestCase &instance = *handle;
assert_eq(instance.global<GlobalInfo>().get_animated(), 8);
instance.global<GlobalInfo>().set_animated(108);
assert_eq(instance.global<GlobalInfo>().get_animated(), 8);
slint_testing::mock_elapsed_time(150);
assert_eq(instance.global<GlobalInfo>().get_animated(), 58);
slint_testing::mock_elapsed_time(150);
assert_eq(instance.global<GlobalInfo>().get_animated(), 108);
```

```js
var instance = new slint.TestCase();
assert.equal(instance.GlobalInfo.animated, 8);
instance.GlobalInfo.animated = 108;
assert.equal(instance.GlobalInfo.animated, 8);
slintlib.private_api.mock_elapsed_time(150);
assert.equal(instance.GlobalInfo.animated, 58);
slintlib.private_api.mock_elapsed_time(150);
assert.equal(instance.GlobalInfo.animated, 108);
```

*/
Loading