@@ -30,7 +30,7 @@ private class MouseNotOverManagedWaitHandle : IDisposable
30
30
private readonly ManualResetEvent _disposedWaitHandle = new ManualResetEvent ( false ) ;
31
31
private Action _cleanUp ;
32
32
private bool _isWaitHandleDisposed ;
33
- private readonly object _waitHandleGate = new object ( ) ;
33
+ private readonly object _waitHandleGate = new object ( ) ;
34
34
35
35
public MouseNotOverManagedWaitHandle ( UIElement uiElement )
36
36
{
@@ -124,7 +124,7 @@ private DurationMonitor(
124
124
125
125
Task . Factory . StartNew ( ( ) =>
126
126
{
127
- //keep upping the completion time in case it's paused...
127
+ //keep upping the completion time in case it's paused...
128
128
while ( DateTime . Now < _completionTime && ! ceaseWaitHandle . WaitOne ( granularity ) )
129
129
{
130
130
if ( pausedWaitHandle . WaitOne ( TimeSpan . Zero ) )
@@ -194,8 +194,7 @@ public void Enqueue(object content, bool neverConsiderToBeDuplicate)
194
194
{
195
195
if ( content == null ) throw new ArgumentNullException ( nameof ( content ) ) ;
196
196
197
- _snackbarMessages . AddLast ( new SnackbarMessageQueueItem ( content ) ) ;
198
- _messageWaitingEvent . Set ( ) ;
197
+ Enqueue ( content , null , null , null , false , neverConsiderToBeDuplicate ) ;
199
198
}
200
199
201
200
public void Enqueue ( object content , object actionContent , Action actionHandler )
@@ -206,34 +205,52 @@ public void Enqueue(object content, object actionContent, Action actionHandler)
206
205
public void Enqueue ( object content , object actionContent , Action actionHandler , bool promote )
207
206
{
208
207
if ( content == null ) throw new ArgumentNullException ( nameof ( content ) ) ;
209
-
210
- _snackbarMessages . AddLast ( new SnackbarMessageQueueItem ( content , actionContent , actionHandler ) ) ;
211
- _messageWaitingEvent . Set ( ) ;
208
+ if ( actionContent == null ) throw new ArgumentNullException ( nameof ( actionContent ) ) ;
209
+ if ( actionHandler == null ) throw new ArgumentNullException ( nameof ( actionHandler ) ) ;
210
+
211
+ Enqueue ( content , actionContent , _ => actionHandler ( ) , promote , false , false ) ;
212
212
}
213
213
214
214
public void Enqueue < TArgument > ( object content , object actionContent , Action < TArgument > actionHandler ,
215
215
TArgument actionArgument )
216
216
{
217
- Enqueue < TArgument > ( content , actionContent , actionHandler , actionArgument , false ) ;
217
+ Enqueue ( content , actionContent , actionHandler , actionArgument , false , false ) ;
218
218
}
219
219
220
220
public void Enqueue < TArgument > ( object content , object actionContent , Action < TArgument > actionHandler ,
221
- TArgument actionArgument , bool promote )
221
+ TArgument actionArgument , bool promote ) =>
222
+ Enqueue ( content , actionContent , actionHandler , actionArgument , promote , promote ) ;
223
+
224
+ public void Enqueue < TArgument > ( object content , object actionContent , Action < TArgument > actionHandler ,
225
+ TArgument actionArgument , bool promote , bool neverConsiderToBeDuplicate )
222
226
{
223
227
if ( content == null ) throw new ArgumentNullException ( nameof ( content ) ) ;
224
228
225
- if ( ( actionContent != null || actionHandler != null || actionArgument != null )
226
- &&
227
- actionContent == null && actionHandler == null && actionArgument == null )
229
+ if ( actionContent == null ^ actionHandler == null )
228
230
{
229
231
throw new ArgumentException ( "All action arguments must be provided if any are provided." ,
230
- nameof ( actionContent ) ) ;
232
+ actionContent != null ? nameof ( actionContent ) : nameof ( actionHandler ) ) ;
231
233
}
232
234
233
- var argumentType = actionArgument != null ? typeof ( TArgument ) : null ;
235
+ Action < object > handler = actionHandler != null
236
+ ? new Action < object > ( argument => actionHandler ( ( TArgument ) argument ) )
237
+ : null ;
238
+ Enqueue ( content , actionContent , handler , actionArgument , promote , neverConsiderToBeDuplicate ) ;
239
+ }
240
+
241
+ public void Enqueue ( object content , object actionContent , Action < object > actionHandler ,
242
+ object actionArgument , bool promote , bool neverConsiderToBeDuplicate )
243
+ {
244
+ if ( content == null ) throw new ArgumentNullException ( nameof ( content ) ) ;
245
+
246
+ if ( actionContent == null ^ actionHandler == null )
247
+ {
248
+ throw new ArgumentException ( "All action arguments must be provided if any are provided." ,
249
+ actionContent != null ? nameof ( actionContent ) : nameof ( actionHandler ) ) ;
250
+ }
234
251
235
252
var snackbarMessageQueueItem = new SnackbarMessageQueueItem ( content , actionContent , actionHandler ,
236
- actionArgument , argumentType , promote ) ;
253
+ actionArgument , promote , neverConsiderToBeDuplicate ) ;
237
254
if ( promote )
238
255
InsertAsLastNotPromotedNode ( snackbarMessageQueueItem ) ;
239
256
else
@@ -261,7 +278,7 @@ private async void PumpAsync()
261
278
{
262
279
while ( ! _isDisposed )
263
280
{
264
- var eventId = WaitHandle . WaitAny ( new WaitHandle [ ] { _disposedEvent , _messageWaitingEvent } ) ;
281
+ var eventId = WaitHandle . WaitAny ( new WaitHandle [ ] { _disposedEvent , _messageWaitingEvent } ) ;
265
282
if ( eventId == 0 ) continue ;
266
283
var exemplar = _pairedSnackbars . FirstOrDefault ( ) ;
267
284
if ( exemplar == null )
@@ -281,7 +298,7 @@ private async void PumpAsync()
281
298
var message = _snackbarMessages . First . Value ;
282
299
_snackbarMessages . RemoveFirst ( ) ;
283
300
if ( _latestShownItem == null
284
- || message . IsPromoted
301
+ || message . IgnoreDuplicate
285
302
|| ! Equals ( _latestShownItem . Item1 . Content , message . Content )
286
303
|| ! Equals ( _latestShownItem . Item1 . ActionContent , message . ActionContent )
287
304
|| _latestShownItem . Item2 <= DateTime . Now . Subtract ( _messageDuration ) )
@@ -331,7 +348,7 @@ await Task.Run(async () =>
331
348
_pausedEvent , durationPassedWaitHandle , _disposedEvent ) ;
332
349
333
350
//wait until time span completed (including pauses and mouse overs), or the action is clicked
334
- await WaitForCompletionAsync ( mouseNotOverManagedWaitHandle , durationPassedWaitHandle , actionClickWaitHandle ) ;
351
+ await WaitForCompletionAsync ( mouseNotOverManagedWaitHandle , durationPassedWaitHandle , actionClickWaitHandle ) ;
335
352
336
353
//close message on snackbar
337
354
await
@@ -399,18 +416,8 @@ private static void DoActionCallback(SnackbarMessageQueueItem messageQueueItem)
399
416
{
400
417
try
401
418
{
402
- var action = messageQueueItem . ActionHandler as Action ;
403
- if ( action != null )
404
- {
405
- action ( ) ;
406
- return ;
407
- }
419
+ messageQueueItem . ActionHandler ( messageQueueItem . ActionArgument ) ;
408
420
409
- if ( messageQueueItem . ArgumentType == null ) return ;
410
-
411
- var genericType = typeof ( Action < > ) . MakeGenericType ( messageQueueItem . ArgumentType ) ;
412
- var method = genericType . GetMethod ( "Invoke" ) ;
413
- method . Invoke ( messageQueueItem . ActionHandler , new [ ] { messageQueueItem . ActionArgument } ) ;
414
421
}
415
422
catch ( Exception exc )
416
423
{
@@ -419,7 +426,7 @@ private static void DoActionCallback(SnackbarMessageQueueItem messageQueueItem)
419
426
Trace . WriteLine ( exc . StackTrace ) ;
420
427
421
428
throw ;
422
- }
429
+ }
423
430
}
424
431
425
432
private static SnackbarMessage Create ( SnackbarMessageQueueItem messageQueueItem )
@@ -433,10 +440,10 @@ private static SnackbarMessage Create(SnackbarMessageQueueItem messageQueueItem)
433
440
434
441
public void Dispose ( )
435
442
{
436
- _isDisposed = true ;
443
+ _isDisposed = true ;
437
444
_disposedEvent . Set ( ) ;
438
445
_disposedEvent . Dispose ( ) ;
439
- _pausedEvent . Dispose ( ) ;
446
+ _pausedEvent . Dispose ( ) ;
440
447
}
441
448
}
442
449
}
0 commit comments