| 1 | //! Show Volume example |
| 2 | //! |
| 3 | //! Only works on unity |
| 4 | //! |
| 5 | |
| 6 | #![allow(unused_imports, dead_code)] |
| 7 | use notify_rust::Hint; |
| 8 | use notify_rust::Notification; |
| 9 | use std::time::Duration; |
| 10 | |
| 11 | enum Volume { |
| 12 | Muted, |
| 13 | Percent(i32), |
| 14 | } |
| 15 | |
| 16 | #[cfg(all(unix, not(target_os = "macos")))] |
| 17 | fn show_volume(percent: Volume) { |
| 18 | let icon = match percent { |
| 19 | Volume::Muted => "notification-audio-volume-muted", |
| 20 | Volume::Percent(0) => "notification-audio-volume-off", |
| 21 | Volume::Percent(x) if x < 33 => "notification-audio-volume-low", |
| 22 | Volume::Percent(x) if x < 67 => "notification-audio-volume-medium", |
| 23 | _ => "notification-audio-volume-high", |
| 24 | }; |
| 25 | |
| 26 | let value = match percent { |
| 27 | Volume::Muted => 0, |
| 28 | Volume::Percent(p) => p, |
| 29 | }; |
| 30 | |
| 31 | Notification::new() |
| 32 | .summary(" ") |
| 33 | .icon(icon) |
| 34 | .hint(Hint::SoundName("audio-volume-change".into())) |
| 35 | .hint(Hint::Custom("synchronous".into(), "volume".into())) |
| 36 | .hint(Hint::CustomInt("value".into(), value)) |
| 37 | .show() |
| 38 | .unwrap(); |
| 39 | } |
| 40 | |
| 41 | #[cfg(any(windows, target_os = "macos"))] |
| 42 | fn main() { |
| 43 | println!("this is an xdg only feature") |
| 44 | } |
| 45 | |
| 46 | #[cfg(all(unix, not(target_os = "macos")))] |
| 47 | fn main() { |
| 48 | show_volume(Volume::Muted); |
| 49 | for i in 1..11 { |
| 50 | std::thread::sleep(Duration::from_millis(1_000)); |
| 51 | show_volume(Volume::Percent(i * 10)); |
| 52 | } |
| 53 | } |