Play pause button updating on changes

This commit is contained in:
vandechat96
2023-08-03 23:56:21 +02:00
parent 55ee8095ab
commit 3c142313af
5 changed files with 45 additions and 40 deletions

View File

@@ -1,4 +1,4 @@
use std::{process::Command, string::FromUtf8Error, thread, time::Duration};
use std::{process::Command, thread, time::Duration};
use lib::{tcp::start_server, ClientMessage, ServerMessage};
use log::info;
@@ -18,6 +18,10 @@ fn main() {
sender_s
.send(ServerMessage::NowPlaying(get_np().unwrap()))
.unwrap();
thread::sleep(Duration::from_millis(200));
sender_s
.send(ServerMessage::Playing(get_playing().unwrap()))
.unwrap();
}
if receiver_c.is_empty() {
thread::sleep(Duration::from_millis(200));
@@ -48,14 +52,20 @@ fn main() {
}
}
fn get_np() -> Result<String, FromUtf8Error> {
fn get_np() -> anyhow::Result<String> {
let output = Command::new("playerctl")
.arg("metadata")
.arg("-f")
.arg("{{title}} - {{artist}}")
.output()
.unwrap()
.output()?
.stdout;
let title = String::from_utf8(output)?;
Ok(title.trim().to_string())
}
fn get_playing() -> anyhow::Result<bool> {
let output = Command::new("playerctl").arg("status").output()?.stdout;
let res = String::from_utf8(output)?;
Ok(res.contains("Playing"))
}

View File

@@ -1,4 +1,4 @@
use std::{net::IpAddr, str::FromStr, thread, time::Duration, process::exit};
use std::{net::IpAddr, process::exit, str::FromStr, thread, time::Duration};
use lib::{tcp::start_client, ServerMessage};
use log::info;
@@ -6,25 +6,24 @@ use log::info;
slint::include_modules!();
fn main() -> Result<(), slint::PlatformError> {
env_logger::init_from_env(
env_logger::init_from_env(
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
);
let mut retries = 1;
let (sender_co, receiver_s) = loop{
let (sender_co, receiver_s) = loop {
let res = start_client(IpAddr::from_str("192.168.0.101").unwrap());
if let Ok((s,r)) = res{
break (s,r)
if let Ok((s, r)) = res {
break (s, r);
}
println!("Failde to connect retrying in {} ms", 200*retries);
thread::sleep(Duration::from_millis(200*retries));
retries*=5;
println!("Failde to connect retrying in {} ms", 200 * retries);
thread::sleep(Duration::from_millis(200 * retries));
retries *= 5;
if retries >= 500 {
println!("Could not connect to server exiting...");
exit(1);
}
};
};
info!("Connected to Server");
@@ -47,22 +46,25 @@ fn main() -> Result<(), slint::PlatformError> {
});
let ui_handle = ui.as_weak();
thread::spawn(move || {
loop {
if receiver_s.is_empty(){
thread::sleep(Duration::from_millis(200));
continue;
}
let msg = receiver_s.recv().unwrap();
info!("Received : {msg:?}");
match msg {
ServerMessage::NowPlaying(title) => ui_handle.upgrade_in_event_loop(move |handle| handle.set_now_playing(title.into())).unwrap(),
};
thread::spawn(move || loop {
if receiver_s.is_empty() {
thread::sleep(Duration::from_millis(200));
continue;
}
let msg = receiver_s.recv().unwrap();
info!("Received : {msg:?}");
match msg {
ServerMessage::NowPlaying(title) => ui_handle
.upgrade_in_event_loop(move |handle| handle.set_now_playing(title.into()))
.unwrap(),
ServerMessage::Playing(bool) => ui_handle
.upgrade_in_event_loop(move |handle| {
handle.global::<MediaLogic>().set_playing(bool)
})
.unwrap(),
};
});
info!("Launching UI");
ui.run()
}

View File

@@ -5,6 +5,7 @@ export global MediaLogic {
pure callback play-pause();
pure callback next();
pure callback previous();
in-out property <bool> playing;
}
export component MediaControls inherits Clickable {
@@ -20,9 +21,9 @@ export component MediaControls inherits Clickable {
}
Clickable {
icon: playing ? @image-url("../../icons/pause.png"): @image-url("../../icons/play.png");
icon: MediaLogic.playing ? @image-url("../../icons/pause.png"): @image-url("../../icons/play.png");
action => {
root.playing = !root.playing;
MediaLogic.playing = !MediaLogic.playing;
MediaLogic.play-pause();
}
}
@@ -32,11 +33,6 @@ export component MediaControls inherits Clickable {
MediaLogic.next();
}
}
}
}

View File

@@ -3,7 +3,6 @@ use std::fmt::{Debug, Display};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub enum ClientMessage {
Next,
@@ -14,6 +13,7 @@ pub enum ClientMessage {
#[derive(Serialize, Deserialize, Debug)]
pub enum ServerMessage {
NowPlaying(String),
Playing(bool),
}
pub enum Origin {

View File

@@ -80,8 +80,6 @@ where
Ok(())
}
pub fn start_server() -> Result<(Sender<ServerMessage>, Receiver<ClientMessage>)> {
let (sender_c, reciever_c) = channel::bounded(4);
let (sender_s, reciever_s) = channel::bounded(4);
@@ -127,4 +125,3 @@ pub fn start_client(ip: IpAddr) -> Result<(Sender<ClientMessage>, Receiver<Serve
Ok((sender_c, reciever_s))
}