Skip to content

Conversation

@Johni0702
Copy link
Contributor

@Johni0702 Johni0702 commented Feb 24, 2025

Intended to be used in almost all places where animationFrame was used before (only major exception being animated constraints, which will receive a different replacement).

Unlike animationFrame, which runs at a fixed rate of (by default) 244 times per second, the UpdateFunc API is variable rate, meaning it'll be called exactly once per frame with the time that passed since the last frame.
This allows it to match the true framerate exactly, animations won't slow down under high load, and it won't waste tons of CPU time on traversing the entire tree potentially mutliple times each frame.

The UpdateFunc API notably also allows modifying the component hierarchy and accessing layout information directly from within a UpdateFunc call, both of which are quite tricky to do correctly from animationFrame.

(technically depends on #154 to avoid merge conflicts but is otherwise independent)

The old API is difficult to work with because it will continue to render
floating components even if they've been removed from the component
tree. Additionally it can CME if components are removed from the
floating list during rendering, further complicating the workarounds
required.

This new API fixes the issue by tracking when components are
removed/added from/to the tree and updating its internal floating list
accordingly.
It also allows setting the floating state at any time, even before the
component has a parent, another thing the old API did not support.

The order in which floating components appear also differs in the new
API. While the old API showed floating components in the order in which
they were set to be floating, this often isn't all too useful when the
order in which components are added/removed to/from the tree is not
particularily well defined. As such, the new API choses to instead order
floating components in exactly the same way as they appear in the
component tree (pre-order tree traversal, i.e. first parent, then
children). This results in consistent ordering and is generally the
order you want for nested floating components to behave in a useful way.

This has been implemented as a new, completely separate API instead of
an ElementaVersion primarily to easy migration (the new API can be used
even with Windows still on older ElementaVersions; both APIs can be used
at the same time) but also because there isn't anything reasonable the
old-API methods in `Window` could do in the new version, they really
should have been internal to begin with.
Intended to be used in almost all places where `animationFrame` was used
before (only major exception being animated constraints, which will
receive a different replacement).

Unlike `animationFrame`, which runs at a fixed rate of (by default) 244
times per second, the [UpdateFunc] API is variable rate, meaning it'll
be called exactly once per frame with the time that passed since the
last frame.
This allows it to match the true framerate exactly, animations won't
slow down under high load, and it won't waste tons of CPU time on
traversing the entire tree potentially mutliple times each frame.

The UpdateFunc API notably also allows modifying the component hierarchy
and accessing layout information directly from within a UpdateFunc call,
both of which are quite tricky to do correctly from `animationFrame`.
Copy link
Contributor

@CallumBugajski CallumBugajski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good to me!

Base automatically changed from feature/rewrite-floating-api to master February 27, 2025 10:30
@Johni0702 Johni0702 requested review from CallumBugajski and removed request for CallumBugajski February 27, 2025 10:39
@Johni0702 Johni0702 merged commit fcc27e2 into master Feb 27, 2025
1 check passed
@Johni0702 Johni0702 deleted the feature/update-func-api branch February 27, 2025 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants