summaryrefslogtreecommitdiffstats
path: root/src/component/volume.rs
blob: 83341f0fcb76ae259dea319bd73254a0b98e6a4d (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::sync::Arc;

use tokio::sync::{mpsc::Sender, Mutex};
use zbus::Connection;

use crate::{
    dbus::player::{PlaybackStatus, PlayerProxy},
    i3bar::{Align, Block, Click, MinWidth},
    Error,
};

use super::{Button, Component, Output, Update};

pub struct Volume;

impl Component for Volume {
    type Updater = Self;
    type Colorer = PlaybackStatus;
    type Handler = Self;

    fn initialize() -> Block {
        Block {
            name: Some("mpris-volume".into()),
            full_text: " ".to_string(),
            align: Align::Center,
            ..Default::default()
        }
    }
}

impl Update for Volume {
    type Value = Option<f64>;

    async fn listen(tx: Sender<Self::Value>, proxy: PlayerProxy<'_>) -> Result<(), Error> {
        use futures_util::StreamExt;
        let mut stream = proxy.receive_volume_changed().await;
        while let Some(signal) = stream.next().await {
            if let Ok(value) = signal.get().await {
                tx.send(Some(value)).await?;
            }
        }
        Ok(())
    }

    async fn update(value: Self::Value, block: Arc<Mutex<Block>>) -> Result<Output, Error> {
        let volume = match value {
            Some(v) => (v * 100_f64) as u32,
            None => return Ok(Output::Clear),
        };

        let mut block = block.lock().await;
        block.full_text = match volume {
            v @ 66.. => format!("󰕾 {v}%"),
            v @ 33.. => format!("󰖀 {v}%"),
            v @ 0.. => format!("󰕿 {v}%"),
        };
        block.min_width = Some(MinWidth::Text(format!("x{}", block.full_text)));

        Ok(Output::Print)
    }
}

impl Button for Volume {
    async fn handle(conn: Connection, click: Click) -> Result<(), Error> {
        let Some(name) = click.instance else {
            return Ok(());
        };

        let proxy = PlayerProxy::builder(&conn)
            .destination(name)?
            .build()
            .await?;

        match (click.button, proxy.volume().await) {
            (4, Ok(v)) if proxy.can_control().await? => proxy.set_volume(v + 0.05).await?,
            (5, Ok(v)) if proxy.can_control().await? => proxy.set_volume(v - 0.05).await?,
            _ => (),
        }

        Ok(())
    }
}