@@ -23,6 +23,7 @@ public partial class BitCarousel : BitComponentBase
23
23
24
24
25
25
[ Inject ] private IJSRuntime _js { get ; set ; } = default ! ;
26
+ [ Inject ] private BitPageVisibility _pageVisibility { get ; set ; } = default ! ;
26
27
27
28
28
29
@@ -128,6 +129,22 @@ public async Task GoTo(int index)
128
129
await GotoPage ( index - 1 ) ;
129
130
}
130
131
132
+ /// <summary>
133
+ /// Pauses the AutoPlay if enabled.
134
+ /// </summary>
135
+ public async Task Pause ( )
136
+ {
137
+ _autoPlayTimer ? . Stop ( ) ;
138
+ }
139
+
140
+ /// <summary>
141
+ /// Resumes the AutoPlay if enabled.
142
+ /// </summary>
143
+ public async Task Resume ( )
144
+ {
145
+ _autoPlayTimer ? . Start ( ) ;
146
+ }
147
+
131
148
132
149
133
150
[ JSInvokable ( "OnResize" ) ]
@@ -179,6 +196,8 @@ protected override void OnInitialized()
179
196
{
180
197
_dotnetObj = DotNetObjectReference . Create ( this ) ;
181
198
199
+ _pageVisibility . OnChange += PageVisibilityChange ;
200
+
182
201
base . OnInitialized ( ) ;
183
202
}
184
203
@@ -195,17 +214,26 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
195
214
196
215
await base . OnAfterRenderAsync ( firstRender ) ;
197
216
198
- if ( firstRender is false ) return ;
199
-
200
- await _js . BitObserversRegisterResize ( UniqueId , RootElement , _dotnetObj ) ;
201
-
202
- if ( AutoPlay )
217
+ if ( AutoPlay && _autoPlayTimer is null )
203
218
{
204
219
_autoPlayTimer = new System . Timers . Timer ( AutoPlayInterval ) ;
205
220
_autoPlayTimer . Elapsed += AutoPlayTimerElapsed ;
206
221
_autoPlayTimer . Start ( ) ;
207
222
}
208
223
224
+ if ( AutoPlay is false && _autoPlayTimer is not null )
225
+ {
226
+ _autoPlayTimer . Elapsed -= AutoPlayTimerElapsed ;
227
+ _autoPlayTimer . Dispose ( ) ;
228
+ _autoPlayTimer = null ;
229
+ }
230
+
231
+ if ( firstRender is false ) return ;
232
+
233
+ await _pageVisibility . Init ( ) ;
234
+
235
+ await _js . BitObserversRegisterResize ( UniqueId , RootElement , _dotnetObj ) ;
236
+
209
237
if ( ScrollItemsCount > VisibleItemsCount )
210
238
{
211
239
_internalScrollItemsCount = VisibleItemsCount ;
@@ -421,12 +449,28 @@ private async void AutoPlayTimerElapsed(object? sender, System.Timers.ElapsedEve
421
449
await InvokeAsync ( Next ) ;
422
450
}
423
451
452
+ private async Task PageVisibilityChange ( bool hidden )
453
+ {
454
+ Console . WriteLine ( $ "PageVisibilityChange: { hidden } ") ;
455
+
456
+ if ( hidden )
457
+ {
458
+ await Pause ( ) ;
459
+ }
460
+ else
461
+ {
462
+ await Resume ( ) ;
463
+ }
464
+ }
465
+
424
466
425
467
426
468
protected override async ValueTask DisposeAsync ( bool disposing )
427
469
{
428
470
if ( IsDisposed || disposing is false ) return ;
429
471
472
+ _pageVisibility . OnChange -= PageVisibilityChange ;
473
+
430
474
if ( _autoPlayTimer is not null )
431
475
{
432
476
_autoPlayTimer . Elapsed -= AutoPlayTimerElapsed ;
0 commit comments