diff options
Diffstat (limited to 'src/component/title.rs')
-rw-r--r-- | src/component/title.rs | 93 |
1 files changed, 0 insertions, 93 deletions
diff --git a/src/component/title.rs b/src/component/title.rs deleted file mode 100644 index 870fe1e..0000000 --- a/src/component/title.rs +++ /dev/null @@ -1,93 +0,0 @@ -use std::{sync::Arc, time::Duration}; - -use tokio::{ - sync::{mpsc::Sender, Mutex}, - task::{AbortHandle, JoinSet}, -}; - -use crate::{ - dbus::player::{PlaybackStatus, PlayerProxy}, - i3bar::{Align, Block, MinWidth}, - Error, -}; - -use super::{Component, Output, Update}; - -const TICK_RATE: Duration = Duration::from_millis(500); - -pub struct Title; - -impl Component for Title { - type Updater = Self; - type Colorer = PlaybackStatus; - type Handler = (); - - fn initialize() -> Block { - Block { - name: Some("mpris-title".into()), - separator: Some(false), - separator_block_width: Some(0), - ..Default::default() - } - } -} - -impl Update for Title { - type Value = String; - - async fn listen(tx: Sender<Self::Value>, proxy: PlayerProxy<'_>) -> Result<(), Error> { - use futures_util::StreamExt; - - let mut join_set = JoinSet::new(); - let mut rotator: Option<AbortHandle> = None; - let mut old_title = String::new(); - let mut stream = proxy.receive_metadata_changed().await; - while let Some(signal) = stream.next().await { - if let Ok(metadata) = signal.get().await { - let Some(owned_value) = metadata.get("xesam:title") else { - continue; - }; - - let title: String = owned_value.try_to_owned()?.try_into()?; - - if old_title == title { - continue; - } - - if let Some(h) = rotator.take() { - h.abort(); - }; - - old_title.clone_from(&title); - - if title.len() >= 10 { - let tx = tx.clone(); - let mut chars = title.chars().collect::<Vec<char>>(); - chars.push(' '); - rotator = Some(join_set.spawn(async move { - let mut interval = tokio::time::interval(TICK_RATE); - loop { - interval.tick().await; - tx.send(String::from_iter(chars[0..10].iter())) - .await - .unwrap(); - chars.rotate_left(1); - } - })); - } else { - tx.send(title).await?; - } - } - } - Ok(()) - } - - async fn update(value: Self::Value, block: Arc<Mutex<Block>>) -> Result<Output, Error> { - let mut block = block.lock().await; - block.full_text = value; - block.min_width = Some(MinWidth::Text(format!("x{}", block.full_text))); - block.align = Align::Center; - - Ok(Output::Print) - } -} |