Skip to content

Commit e6ff6d5

Browse files
committed
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.
1 parent 5e2881e commit e6ff6d5

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/timeline.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -544,40 +544,43 @@ impl Timeline {
544544
}
545545
}
546546

547+
/// Check if the timeline is idle
548+
/// The timeline is considered idle if all animations meet
549+
/// one of the final criteria:
550+
/// 1. Played until completion
551+
/// 2. Paused
552+
/// 3. Does not loop forever
553+
pub fn is_idle(&self) -> bool {
554+
let now = self.now;
555+
!(now.is_some()
556+
&& self.tracks.values().any(|track| {
557+
(track.0.repeat == Repeat::Forever && track.0.pause.is_playing())
558+
|| (track.0.end >= now.unwrap() && track.0.pause.is_playing())
559+
}))
560+
}
561+
547562
/// Efficiently request redraws for animations.
548563
/// Automatically checks if animations are in a state where redraws arn't necessary.
549564
#[cfg(not(feature = "libcosmic"))]
550565
pub fn as_subscription<Event>(
551566
&self,
552567
) -> Subscription<iced_native::Hasher, (iced_native::Event, iced_native::event::Status), Instant>
553568
{
554-
let now = self.now;
555-
if now.is_some()
556-
&& self.tracks.values().any(|track| {
557-
(track.0.repeat == Repeat::Forever && track.0.pause.is_playing())
558-
|| (track.0.end >= now.unwrap() && track.0.pause.is_playing())
559-
})
560-
{
561-
iced::window::frames()
562-
} else {
569+
if self.is_idle() {
563570
Subscription::none()
571+
} else {
572+
iced::window::frames()
564573
}
565574
}
566575

567576
/// Efficiently request redraws for animations.
568577
/// Automatically checks if animations are in a state where redraws arn't necessary.
569578
#[cfg(feature = "libcosmic")]
570579
pub fn as_subscription(&self) -> Subscription<Instant> {
571-
let now = self.now;
572-
if now.is_some()
573-
&& self.tracks.values().any(|track| {
574-
(track.0.repeat == Repeat::Forever && track.0.pause.is_playing())
575-
|| (track.0.end >= now.unwrap() && track.0.pause.is_playing())
576-
})
577-
{
578-
cosmic::iced::time::every(Duration::from_millis(8)) // ~120FPS
579-
} else {
580+
if self.is_idle() {
580581
Subscription::none()
582+
} else {
583+
cosmic::iced::time::every(Duration::from_millis(8)) // ~120FPS
581584
}
582585
}
583586
}

0 commit comments

Comments
 (0)