1+ use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
12use std:: sync:: Arc ;
23use std:: time:: Duration ;
34
@@ -7,6 +8,8 @@ use crate::render::PackedBundle;
78use crate :: tile_schema:: { TileIndex , WrappingTileIndex } ;
89use crate :: TileSchema ;
910
11+ const DEFAULT_FADE_IN_DURATION : Duration = Duration :: from_millis ( 300 ) ;
12+
1013#[ derive( Clone ) ]
1114pub ( crate ) struct DisplayedTile < StyleId : Copy > {
1215 pub ( crate ) index : WrappingTileIndex ,
3437 pub ( crate ) tiles : Mutex < Vec < DisplayedTile < StyleId > > > ,
3538 tile_schema : TileSchema ,
3639 pub ( crate ) tile_provider : Provider ,
40+ pub fade_in_duration : AtomicU64 ,
3741}
3842
3943impl < StyleId , Provider > TilesContainer < StyleId , Provider >
4650 tiles : Default :: default ( ) ,
4751 tile_schema,
4852 tile_provider,
53+ fade_in_duration : AtomicU64 :: new ( DEFAULT_FADE_IN_DURATION . as_millis ( ) as u64 ) ,
4954 }
5055 }
5156
6065 let mut to_substitute = vec ! [ ] ;
6166
6267 let now = web_time:: Instant :: now ( ) ;
63- let fade_in_time = self . fade_in_time ( ) ;
68+ let fade_in_time = self . fade_in_duration ( ) ;
6469 let mut requires_redraw = false ;
6570
6671 for index in needed_indices {
7075 {
7176 if !displayed. is_opaque ( ) {
7277 to_substitute. push ( index) ;
73- displayed. opacity = ( ( now. duration_since ( displayed. displayed_at ) ) . as_secs_f64 ( )
74- / fade_in_time. as_secs_f64 ( ) )
75- . min ( 1.0 ) as f32 ;
78+ let fade_in_secs = fade_in_time. as_secs_f64 ( ) ;
79+ displayed. opacity = if fade_in_secs > 0.001 {
80+ ( ( now. duration_since ( displayed. displayed_at ) ) . as_secs_f64 ( ) / fade_in_secs)
81+ . min ( 1.0 ) as f32
82+ } else {
83+ 1.0
84+ } ;
7685 requires_redraw = true ;
7786 }
7887
@@ -81,11 +90,12 @@ where
8190 match self . tile_provider . get_tile ( index. into ( ) , style_id) {
8291 None => to_substitute. push ( index) ,
8392 Some ( bundle) => {
93+ let opacity = if self . requires_animation ( ) { 0.0 } else { 1.0 } ;
8494 needed_tiles. push ( DisplayedTile {
8595 index,
8696 bundle,
8797 style_id,
88- opacity : 0.0 ,
98+ opacity,
8999 displayed_at : now,
90100 } ) ;
91101 to_substitute. push ( index) ;
@@ -126,7 +136,16 @@ where
126136 requires_redraw
127137 }
128138
129- fn fade_in_time ( & self ) -> Duration {
130- Duration :: from_millis ( 300 )
139+ pub fn fade_in_duration ( & self ) -> Duration {
140+ Duration :: from_millis ( self . fade_in_duration . load ( Ordering :: Relaxed ) )
141+ }
142+
143+ pub fn set_fade_in_duration ( & self , duration : Duration ) {
144+ self . fade_in_duration
145+ . store ( duration. as_millis ( ) as u64 , Ordering :: Relaxed ) ;
146+ }
147+
148+ fn requires_animation ( & self ) -> bool {
149+ self . fade_in_duration . load ( Ordering :: Relaxed ) > 1
131150 }
132151}
0 commit comments