Crates.io | cosmic-time |
lib.rs | cosmic-time |
version | 0.2.0 |
source | src |
created_at | 2023-03-28 21:10:46.329162 |
updated_at | 2023-04-13 15:29:03.757605 |
description | An animation Crate for Iced and Cosmic DE |
homepage | |
repository | https://github.com/pop-os/cosmic-time |
max_upload_size | |
id | 823449 |
size | 179,530 |
This Project was build for Cosmic DE. Though this will work for any project that depends on Iced.
The goal of this project is to provide a simple API to build and show complex animations efficiently in applications built with Iced-rs/Iced.
To wire cosmic-time into Iced there are five steps to do.
Timeline
] This is the type that controls the animations.struct Counter {
timeline: Timeline
}
// ~ SNIP
impl Application for Counter {
// ~ SNIP
fn new(_flags: ()) -> (Self, Command<Message>) {
(Self { timeline: Timeline::new()}, Command::none())
}
}
new()
or update()
, or both!static CONTAINER: Lazy<id::Container> = Lazy::new(id::Container::unique);
let animation = chain![
CONTAINER,
container(Duration::ZERO).width(10),
container(Duration::from_secs(10)).width(100)
];
self.timeline.set_chain(animation).start();
There are some different things here!
static CONTAINER: Lazyid::Container = Lazy::new(id::Container::unique);
Cosmic Time refers to each animation with an Id. We export our own, but they are Identical to the widget Id's Iced uses for widget operations. Each animatable widget needs an Id. And each Id can only refer to one animation.
let animation = chain![
Cosmic Time refers to animations as [Chain
]s because of how we build then.
Each Keyframe is linked together like a chain. The Cosmic Time API doesn't
say "change your width from 10 to 100". We define each state we want the
widget to have .width(10)
at Duration::ZERO
then .width(100)
at
Duration::from_secs(10)
. Where the Duration
is the time after the previous
keyframe. This is why we call the animations chains. We cannot get to the
next state without animating though all previous Keyframes.
self.timeline.set_chain(animation).start();
Then we need to add the animation to the [Timeline
]. We call this .set_chain
,
because there can only be one chain per Id.
If we set_chain
with a different animation with the same Id, the first one is
replaced. This a actually a feature not a bug!
As well you can set multiple animations at once:
self.timeline.set_chain(animation1).set_chain(animation2).start()
.start()
This one function call is important enough that we should look at it specifically.
Cosmic Time is atomic, given the animation state held in the [Timeline
] at any
given time the global animations will be the exact same. The value used to
calculate any animation's interpolation is global. And we use .start()
to
sync them together.
Say you have two 5 seconds animations running at the same time. They should end
at the same time right? That all depends on when the widget thinks it's animation
should start. .start()
tells all pending animations to start at the moment that
.start()
is called. This guarantees they stay in sync.
IMPORTANT! Be sure to only call .start()
once per call to update()
.
The below is incorrect!
self.timeline.set_chain(animation1).start();
self.timeline.set_chain(animation2).start();
That code will compile, but will result in the animations not being in sync.
fn subscription(&self) -> Subscription<Message> {
self.timeline.as_subscription::<Event>().map(Message::Tick)
}
fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::Tick(now) => self.timeline.now(now),
}
}
If you skip this step your animations will not progress!
view()
!anim!(CONTIANER, &self.timeline, contents)
All done! There is a bit of wiring to get Cosmic Time working, but after that it's only a few lines to create rather complex animations! See the Pong example to see how a full game of pong can be implemented in only a few lines!
Done:
as_subscription
logicTODOs:
Cosmic
cargo feature for compatibility with both iced and System76's temporary fork.Iced Version | Required Cosmic Time Version |
---|---|
0.8 | 1.2 |
0.9 | 2.0 |