From 211c0be8301d258a011ba44eebfac7df0a23f26d Mon Sep 17 00:00:00 2001 From: 13r0ck Date: Mon, 5 Jun 2023 15:37:56 -0600 Subject: [PATCH] Refactor timeline check into `is_idle()` Reduces code duplication and makes cosmic-time easier to use with iced integrations where `subscriptions` might not be used. --- src/timeline.rs | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/timeline.rs b/src/timeline.rs index 09b3f5e..427ea38 100644 --- a/src/timeline.rs +++ b/src/timeline.rs @@ -544,6 +544,21 @@ 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"))] @@ -551,16 +566,10 @@ impl Timeline { &self, ) -> Subscription { - 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() } } @@ -568,16 +577,10 @@ impl Timeline { /// Automatically checks if animations are in a state where redraws arn't necessary. #[cfg(feature = "libcosmic")] pub fn as_subscription(&self) -> Subscription { - 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 } } }