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

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

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

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

pub struct Next;

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

impl Update for Next {
    type Value = bool;

    async fn listen(tx: Sender<Self::Value>, proxy: PlayerProxy<'_>) -> Result<(), Error> {
        use futures_util::StreamExt;

        tx.send(proxy.can_go_previous().await?).await?;
        let mut stream = proxy.receive_can_go_previous_changed().await;
        while let Some(signal) = stream.next().await {
            if let Ok(value) = signal.get().await {
                tx.send(value).await?;
            }
        }
        Ok(())
    }

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

impl Button for Next {
    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 {
            1 if proxy.can_go_next().await? => proxy.next().await?,
            3 if proxy.can_seek().await? => proxy.seek(5000).await?,
            _ => {}
        }

        Ok(())
    }
}