summaryrefslogtreecommitdiffstats
path: root/src/component/prev.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-07-20 15:19:09 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-07-20 15:19:09 -0500
commit5b46ff1843bbed0ff1e167928d7f2b3b9968a918 (patch)
tree85e785067a162e67135c53e024f078b08f810ed5 /src/component/prev.rs
parentbab037ad907a4948799ecd545986fa2d4c709c7a (diff)
feat!: move to single executable with arguments
Diffstat (limited to 'src/component/prev.rs')
-rw-r--r--src/component/prev.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/component/prev.rs b/src/component/prev.rs
new file mode 100644
index 0000000..f67be3d
--- /dev/null
+++ b/src/component/prev.rs
@@ -0,0 +1,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 Prev;
+
+impl Component for Prev {
+ const NAME: &'static str = "previous";
+ type Updater = Self;
+ type Colorer = PlaybackStatus;
+ type Handler = Self;
+}
+
+impl Update for Prev {
+ 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 Prev {
+ 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_previous().await? => proxy.previous().await?,
+ 3 if proxy.can_seek().await? => proxy.seek(-5000).await?,
+ _ => {}
+ }
+
+ Ok(())
+ }
+}