File: show_volume.rs

package info (click to toggle)
rust-notify-rust 4.11.6-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 604 kB
  • sloc: sh: 18; makefile: 4
file content (53 lines) | stat: -rw-r--r-- 1,365 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Show Volume example
//!
//! Only works on unity
//!

#![allow(unused_imports, dead_code)]
use notify_rust::Hint;
use notify_rust::Notification;
use std::time::Duration;

enum Volume {
    Muted,
    Percent(i32),
}

#[cfg(all(unix, not(target_os = "macos")))]
fn show_volume(percent: Volume) {
    let icon = match percent {
        Volume::Muted => "notification-audio-volume-muted",
        Volume::Percent(0) => "notification-audio-volume-off",
        Volume::Percent(x) if x < 33 => "notification-audio-volume-low",
        Volume::Percent(x) if x < 67 => "notification-audio-volume-medium",
        _ => "notification-audio-volume-high",
    };

    let value = match percent {
        Volume::Muted => 0,
        Volume::Percent(p) => p,
    };

    Notification::new()
        .summary(" ")
        .icon(icon)
        .hint(Hint::SoundName("audio-volume-change".into()))
        .hint(Hint::Custom("synchronous".into(), "volume".into()))
        .hint(Hint::CustomInt("value".into(), value))
        .show()
        .unwrap();
}

#[cfg(any(windows, target_os = "macos"))]
fn main() {
    println!("this is an xdg only feature")
}

#[cfg(all(unix, not(target_os = "macos")))]
fn main() {
    show_volume(Volume::Muted);
    for i in 1..11 {
        std::thread::sleep(Duration::from_millis(1_000));
        show_volume(Volume::Percent(i * 10));
    }
}