Skip to content

Commit

Permalink
Refactor timeline check into is_idle()
Browse files Browse the repository at this point in the history
Reduces code duplication and makes cosmic-time easier to use with
iced integrations where `subscriptions` might not be used.
  • Loading branch information
13r0ck committed Jun 6, 2023
1 parent 8254d7e commit 211c0be
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,40 +544,43 @@ impl Timeline {
}
}

/// Check if the timeline is idle
/// The timeline is considered idle if all animations meet
/// one of the final criteria:
/// 1. Played until completion
/// 2. Paused
/// 3. Does not loop forever
pub fn is_idle(&self) -> bool {
let now = self.now;
!(now.is_some()
&& self.tracks.values().any(|track| {
(track.0.repeat == Repeat::Forever && track.0.pause.is_playing())
|| (track.0.end >= now.unwrap() && track.0.pause.is_playing())
}))
}

/// Efficiently request redraws for animations.
/// Automatically checks if animations are in a state where redraws arn't necessary.
#[cfg(not(feature = "libcosmic"))]
pub fn as_subscription<Event>(
&self,
) -> Subscription<iced_native::Hasher, (iced_native::Event, iced_native::event::Status), Instant>
{
let now = self.now;
if now.is_some()
&& self.tracks.values().any(|track| {
(track.0.repeat == Repeat::Forever && track.0.pause.is_playing())
|| (track.0.end >= now.unwrap() && track.0.pause.is_playing())
})
{
iced::window::frames()
} else {
if self.is_idle() {
Subscription::none()
} else {
iced::window::frames()
}
}

/// Efficiently request redraws for animations.
/// Automatically checks if animations are in a state where redraws arn't necessary.
#[cfg(feature = "libcosmic")]
pub fn as_subscription(&self) -> Subscription<Instant> {
let now = self.now;
if now.is_some()
&& self.tracks.values().any(|track| {
(track.0.repeat == Repeat::Forever && track.0.pause.is_playing())
|| (track.0.end >= now.unwrap() && track.0.pause.is_playing())
})
{
cosmic::iced::time::every(Duration::from_millis(8)) // ~120FPS
} else {
if self.is_idle() {
Subscription::none()
} else {
cosmic::iced::time::every(Duration::from_millis(8)) // ~120FPS
}
}
}
Expand Down

0 comments on commit 211c0be

Please sign in to comment.