summaryrefslogtreecommitdiffstats
path: root/src/component/icon.rs
blob: 102c787fc084bf10e3805a5c7ea2d53841de0e04 (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
use std::sync::Arc;

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

use crate::{
    dbus::{
        media_player2::MediaPlayer2Proxy,
        player::{PlaybackStatus, PlayerProxy},
        playerctld::PlayerctldProxy,
    },
    i3bar::{Block, Click},
    Error,
};

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

pub struct Icon;

impl Component for Icon {
    const NAME: &'static str = "icon";
    type Updater = Self;
    type Colorer = PlaybackStatus;
    type Handler = Self;
}

impl Update for Icon {
    type Value = String;

    async fn listen(tx: Sender<Self::Value>, _: PlayerProxy<'_>) -> Result<(), Error> {
        tx.send(" 󰝚  ".into()).await.map_err(Into::into)
    }

    async fn update(value: Self::Value, block: Arc<Mutex<Block>>) -> Result<bool, Error> {
        let mut block = block.lock().await;
        block.full_text = value;
        Ok(true)
    }
}

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

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

        match click.button {
            3 => {
                PlayerctldProxy::builder(&conn)
                    .build()
                    .await?
                    .shift()
                    .await?;
            }
            1 if proxy.can_raise().await? => proxy.raise().await?,
            _ => {}
        }

        Ok(())
    }
}