Skip to content

Commit 2c5cbff

Browse files
committed
Add more information about animations for both users of Slint and developers working on Slint.
1 parent e41a460 commit 2c5cbff

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

docs/animations.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Animation System Internals
2+
3+
This document covers Slint's animation system architecture for developers working on the runtime.
4+
5+
For user-facing documentation on using animations, see:
6+
- [Animation guide](astro/src/content/docs/guide/language/coding/animation.mdx)
7+
- [Debugging techniques](astro/src/content/docs/guide/development/debugging_techniques.mdx)
8+
- [Easing types reference](astro/src/content/docs/reference/primitive-types.mdx)
9+
10+
## Animation Timing System
11+
12+
Slint animations use a **mocked time system** rather than real-time clocks. This provides:
13+
- Deterministic animation behavior for testing
14+
- Frame-rate independence
15+
- Consistent behavior across platforms
16+
17+
The animation driver (`internal/core/animations.rs`) manages a global instant that advances each frame:
18+
19+
```
20+
AnimationDriver
21+
├── global_instant: Property<Instant> // Current animation time
22+
├── active_animations: bool // Whether animations are running
23+
└── update_animations(new_tick) // Called per frame by the backend
24+
```
25+
26+
**Key components:**
27+
- `Instant` - Milliseconds since animation driver started
28+
- `current_tick()` - Get current animation time (registers as dependency)
29+
- `animation_tick()` - Same as above, but signals a frame is needed
30+
- `update_timers_and_animations()` - Called by the platform each frame
31+
32+
## Easing Curve Implementation
33+
34+
Easing curves are defined in the `EasingCurve` enum in `internal/core/items.rs`. The interpolation logic is in `internal/core/animations.rs`.
35+
36+
For `cubic-bezier(a, b, c, d)`, Slint uses a binary search algorithm to find the t parameter for a given x value, then evaluates the y component of the bezier curve.

docs/astro/src/content/docs/guide/development/debugging_techniques.mdx

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,55 @@ On this page we share different techniques and tools we've built into Slint that
1111

1212
Use the <Link type="DebugFn" label="debug()" /> function to print the values of properties to stderr.
1313

14-
## Slow Motion Animations
14+
## Debugging Animations
1515

1616
Animations in the user interface need to be carefully designed to have the correct duration and changes in element positioning or size need to follow an easing curve.
1717

18-
To inspect the animations in your application, set the `SLINT_SLOW_ANIMATIONS` environment variable before running the program. This variable accepts an unsigned integer value that is the factor by which to globally slow down the steps of all animations, automatically. This means that you don't have to make any manual changes to the `.slint` markup and recompile. For example,`SLINT_SLOW_ANIMATIONS=4` slows down animations by a factor of four.
18+
### Slow Motion Animations
19+
20+
To inspect the animations in your application, set the `SLINT_SLOW_ANIMATIONS` environment variable before running the program. This variable accepts an unsigned integer value that is the factor by which to globally slow down the steps of all animations, automatically. This means that you don't have to make any manual changes to the `.slint` markup and recompile.
21+
22+
```sh
23+
# Slow animations by a factor of 2
24+
SLINT_SLOW_ANIMATIONS=2 cargo run
25+
26+
# Slow by a factor of 10 for detailed inspection
27+
SLINT_SLOW_ANIMATIONS=10 cargo run
28+
```
29+
30+
This is particularly useful for:
31+
- Verifying easing curves look correct
32+
- Checking that animation start and end states are correct
33+
- Debugging timing issues between multiple animations
34+
- Inspecting complex state transitions
35+
36+
### Checking for Active Animations
37+
38+
In Rust, you can programmatically check if animations are currently running:
39+
40+
```rust
41+
if window.has_active_animations() {
42+
// Animations are in progress
43+
}
44+
```
45+
46+
This is useful when you need to synchronize application logic with animation completion.
47+
48+
### Mock Time in Tests
49+
50+
For automated testing, Slint provides a mock time system that allows precise control over animation timing. Instead of waiting for real time to pass, you can advance the animation clock directly:
51+
52+
```rust
53+
use slint_testing::mock_elapsed_time;
54+
55+
// Advance animation time by 100ms
56+
mock_elapsed_time(100);
57+
58+
// Advance to complete a 300ms animation
59+
mock_elapsed_time(300);
60+
```
61+
62+
This enables deterministic animation testing without slow, flaky time-based waits.
1963

2064
## User Interface Scaling
2165

docs/astro/src/content/docs/guide/language/coding/animation.mdx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,30 @@ Can be any of the following. See [`easings.net`](https://easings.net/) for a vis
7070
Use this to set or change the direction of the animation.
7171
</SlintProperty>
7272

73+
## Performance Considerations
74+
75+
Each animated property re-evaluates its binding every frame, marks dependents dirty, and triggers re-rendering of affected items. For best performance, avoid animating properties that cause layout recalculations.
76+
77+
```slint
78+
export component Example inherits Window {
79+
preferred-width: 200px;
80+
preferred-height: 100px;
81+
82+
Rectangle {
83+
// Good: Animate position (no layout recalculation)
84+
x: 0px;
85+
animate x { duration: 300ms; }
86+
}
87+
88+
Rectangle {
89+
// Avoid: Animating width/height causes layout recalculation
90+
width: self.pressed ? 100px : 200px;
91+
animate width { duration: 300ms; }
92+
93+
area := TouchArea {}
94+
}
95+
}
96+
```
97+
98+
Properties like `x`, `y`, `opacity`, `rotation-angle`, and `background` are efficient to animate. Properties like `width`, `height`, `preferred-width`, and `preferred-height` trigger layout recalculations and should be animated sparingly.
99+

docs/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ docs/
77
├── README.md # This file
88
├── building.md # How to build Slint
99
├── development.md # How to develop Slint
10+
├── animations.md # Internals of the animation system in Slint
1011
├── embedded-tutorials.md # Embedded tutorials template
1112
├── install_qt.md # How to install Qt
1213
├── nightly-release-notes.md # Release note template

0 commit comments

Comments
 (0)