@@ -544,40 +544,43 @@ impl Timeline {
544
544
}
545
545
}
546
546
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
+
547
562
/// Efficiently request redraws for animations.
548
563
/// Automatically checks if animations are in a state where redraws arn't necessary.
549
564
#[ cfg( not( feature = "libcosmic" ) ) ]
550
565
pub fn as_subscription < Event > (
551
566
& self ,
552
567
) -> Subscription < iced_native:: Hasher , ( iced_native:: Event , iced_native:: event:: Status ) , Instant >
553
568
{
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 ( ) {
563
570
Subscription :: none ( )
571
+ } else {
572
+ iced:: window:: frames ( )
564
573
}
565
574
}
566
575
567
576
/// Efficiently request redraws for animations.
568
577
/// Automatically checks if animations are in a state where redraws arn't necessary.
569
578
#[ cfg( feature = "libcosmic" ) ]
570
579
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 ( ) {
580
581
Subscription :: none ( )
582
+ } else {
583
+ cosmic:: iced:: time:: every ( Duration :: from_millis ( 8 ) ) // ~120FPS
581
584
}
582
585
}
583
586
}
0 commit comments