@@ -45,9 +45,21 @@ use std::fmt::Write;
45
45
use std:: path:: Path ;
46
46
use std:: str:: FromStr ;
47
47
48
- pub use windows:: core:: { Error , Result , HSTRING } ;
48
+ pub use windows:: core:: HSTRING ;
49
49
pub use windows:: UI :: Notifications :: ToastNotification ;
50
50
51
+ use thiserror:: Error ;
52
+
53
+ #[ derive( Error , Debug ) ]
54
+ pub enum Error {
55
+ #[ error( "Windows API error: {0}" ) ]
56
+ Os ( #[ from] windows:: core:: Error ) ,
57
+ #[ error( "IO error: {0}" ) ]
58
+ Io ( #[ from] std:: io:: Error ) ,
59
+ }
60
+
61
+ pub type Result < T > = std:: result:: Result < T , Error > ;
62
+
51
63
/// `ToastDismissalReason` is a struct representing the reason a toast notification was dismissed.
52
64
///
53
65
/// Variants:
@@ -255,7 +267,7 @@ pub enum Scenario {
255
267
Default ,
256
268
/// This will be displayed pre-expanded and stay on the user's screen till dismissed. Audio will loop by default and will use alarm audio.
257
269
Alarm ,
258
- /// This will be displayed pre-expanded and stay on the user's screen till dismissed..
270
+ /// This will be displayed pre-expanded and stay on the user's screen till dismissed.
259
271
Reminder ,
260
272
/// This will be displayed pre-expanded in a special call format and stay on the user's screen till dismissed. Audio will loop by default and will use ringtone audio.
261
273
IncomingCall ,
@@ -264,7 +276,7 @@ pub enum Scenario {
264
276
impl Toast {
265
277
/// This can be used if you do not have a AppUserModelID.
266
278
///
267
- /// However, the toast will erroniously report its origin as powershell.
279
+ /// However, the toast will erroneously report its origin as powershell.
268
280
pub const POWERSHELL_APP_ID : & ' static str = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\
269
281
\\ WindowsPowerShell\\ v1.0\\ powershell.exe";
270
282
/// Constructor for the toast builder.
@@ -372,7 +384,7 @@ impl Toast {
372
384
) ;
373
385
self
374
386
} else {
375
- // Win81 rejects the above xml so we fallback to a simpler call
387
+ // Win81 rejects the above xml, so we fall back to a simpler call
376
388
self . image ( source, alt_text)
377
389
}
378
390
}
@@ -390,7 +402,7 @@ impl Toast {
390
402
) ;
391
403
self
392
404
} else {
393
- // win81 rejects the above xml so we fallback to a simpler call
405
+ // win81 rejects the above xml, so we fall back to a simpler call
394
406
self . image ( source, alt_text)
395
407
}
396
408
}
@@ -436,7 +448,7 @@ impl Toast {
436
448
437
449
/// Adds a button to the notification
438
450
/// `content` is the text of the button.
439
- /// `action` will be send as an argument [on_activated](Self::on_activated) when the button is clicked.
451
+ /// `action` will be sent as an argument [on_activated](Self::on_activated) when the button is clicked.
440
452
pub fn add_button ( mut self , content : & str , action : & str ) -> Toast {
441
453
self . buttons . push ( Button {
442
454
content : content. to_owned ( ) ,
@@ -447,12 +459,13 @@ impl Toast {
447
459
448
460
// HACK: f is static so that we know the function is valid to call.
449
461
// this would be nice to remove at some point
450
- pub fn on_activated < F : FnMut ( Option < String > ) -> Result < ( ) > + Send + ' static > (
451
- mut self ,
452
- mut f : F ,
453
- ) -> Self {
462
+ pub fn on_activated < F > ( mut self , mut f : F ) -> Self
463
+ where
464
+ F : FnMut ( Option < String > ) -> Result < ( ) > + Send + ' static ,
465
+ {
454
466
self . on_activated = Some ( TypedEventHandler :: new ( move |_, insp| {
455
- f ( Self :: get_activated_action ( insp) )
467
+ let _ = f ( Self :: get_activated_action ( insp) ) ;
468
+ Ok ( ( ) )
456
469
} ) ) ;
457
470
self
458
471
}
@@ -492,12 +505,13 @@ impl Toast {
492
505
/// Ok(())
493
506
/// }).show().expect("notification failed");
494
507
/// ```
495
- pub fn on_dismissed < F : Fn ( Option < ToastDismissalReason > ) -> Result < ( ) > + Send + ' static > (
496
- mut self ,
497
- f : F ,
498
- ) -> Self {
508
+ pub fn on_dismissed < F > ( mut self , f : F ) -> Self
509
+ where
510
+ F : Fn ( Option < ToastDismissalReason > ) -> Result < ( ) > + Send + ' static ,
511
+ {
499
512
self . on_dismissed = Some ( TypedEventHandler :: new ( move |_, args| {
500
- f ( Self :: get_dismissed_reason ( args) )
513
+ let _ = f ( Self :: get_dismissed_reason ( args) ) ;
514
+ Ok ( ( ) )
501
515
} ) ) ;
502
516
self
503
517
}
@@ -513,7 +527,7 @@ impl Toast {
513
527
None
514
528
}
515
529
516
- fn create_template ( & self ) -> windows :: core :: Result < ToastNotification > {
530
+ fn create_template ( & self ) -> Result < ToastNotification > {
517
531
//using this to get an instance of XmlDocument
518
532
let toast_xml = XmlDocument :: new ( ) ?;
519
533
@@ -542,16 +556,16 @@ impl Toast {
542
556
}
543
557
544
558
toast_xml. LoadXml ( & HSTRING :: from ( format ! (
545
- "<toast {} {}>
546
- <visual>
547
- <binding template=\ " {}\ " >
559
+ r# "<toast {} {}>
560
+ <visual>
561
+ <binding template="{}">
548
562
{}
549
563
{}{}{}
550
- </binding>
551
- </visual>
552
- {}
553
- {}
554
- </toast>" ,
564
+ </binding>
565
+ </visual>
566
+ {}
567
+ {}
568
+ </toast>"# ,
555
569
self . duration,
556
570
self . scenario,
557
571
template_binding,
@@ -564,11 +578,11 @@ impl Toast {
564
578
) ) ) ?;
565
579
566
580
// Create the toast
567
- ToastNotification :: CreateToastNotification ( & toast_xml)
581
+ ToastNotification :: CreateToastNotification ( & toast_xml) . map_err ( Into :: into )
568
582
}
569
583
570
584
/// Display the toast on the screen
571
- pub fn show ( & self ) -> windows :: core :: Result < ( ) > {
585
+ pub fn show ( & self ) -> Result < ( ) > {
572
586
let toast_template = self . create_template ( ) ?;
573
587
if let Some ( handler) = & self . on_activated {
574
588
toast_template. Activated ( handler) ?;
@@ -582,7 +596,7 @@ impl Toast {
582
596
ToastNotificationManager :: CreateToastNotifierWithId ( & HSTRING :: from ( & self . app_id ) ) ?;
583
597
584
598
// Show the toast.
585
- let result = toast_notifier. Show ( & toast_template) ;
599
+ let result = toast_notifier. Show ( & toast_template) . map_err ( Into :: into ) ;
586
600
std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 10 ) ) ;
587
601
result
588
602
}
0 commit comments