summaryrefslogtreecommitdiffstats
path: root/src/bin/mpris-icon.rs
blob: 39677f50acd47dde3a77985ace21c33f79d15f90 (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
use i3blocks::{
    dbus::{media_player2::MediaPlayer2Proxy, player::PlaybackStatus, playerctld::PlayerctldProxy},
    i3bar::{Block, Click},
    Button, Component, Error, Update,
};
use zbus::Connection;

#[tokio::main]
async fn main() -> Result<(), main_error::MainError> {
    i3blocks::run::<Icon>().await.map_err(Into::into)
}

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: tokio::sync::mpsc::Sender<Self::Value>,
        _: i3blocks::dbus::player::PlayerProxy<'_>,
    ) -> Result<(), Error> {
        tx.send(" 󰝚  ".into()).await.map_err(Into::into)
    }

    async fn update(
        value: Self::Value,
        block: std::sync::Arc<tokio::sync::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(())
    }
}