mirror of
https://github.com/arabianq/pipewire-soundpad.git
synced 2026-04-28 06:21:23 +00:00
feat: replace synchronous requests with asynchronous counterparts for improved performance
This commit is contained in:
+8
-8
@@ -13,7 +13,7 @@ use pwsp::{
|
||||
},
|
||||
utils::{
|
||||
daemon::get_daemon_config,
|
||||
gui::{get_gui_config, make_request_sync, start_app_state_thread},
|
||||
gui::{get_gui_config, make_request_async, make_request_sync, start_app_state_thread},
|
||||
},
|
||||
};
|
||||
use rfd::FileDialog;
|
||||
@@ -66,7 +66,7 @@ impl SoundpadGui {
|
||||
};
|
||||
|
||||
if let Some(req) = request {
|
||||
make_request_sync(req).ok();
|
||||
make_request_async(req);
|
||||
}
|
||||
|
||||
if let Some(state) = new_state {
|
||||
@@ -117,11 +117,11 @@ impl SoundpadGui {
|
||||
}
|
||||
|
||||
pub fn play_file(&mut self, path: &PathBuf, concurrent: bool) {
|
||||
make_request_sync(Request::play(path.to_str().unwrap(), concurrent)).ok();
|
||||
make_request_async(Request::play(path.to_str().unwrap(), concurrent));
|
||||
}
|
||||
|
||||
pub fn set_input(&mut self, name: String) {
|
||||
make_request_sync(Request::set_input(&name)).ok();
|
||||
make_request_async(Request::set_input(&name));
|
||||
|
||||
if self.config.save_input {
|
||||
let mut daemon_config = get_daemon_config();
|
||||
@@ -131,19 +131,19 @@ impl SoundpadGui {
|
||||
}
|
||||
|
||||
pub fn toggle_loop(&mut self, id: Option<u32>) {
|
||||
make_request_sync(Request::toggle_loop(id)).ok();
|
||||
make_request_async(Request::toggle_loop(id));
|
||||
}
|
||||
|
||||
pub fn pause(&mut self, id: Option<u32>) {
|
||||
make_request_sync(Request::pause(id)).ok();
|
||||
make_request_async(Request::pause(id));
|
||||
}
|
||||
|
||||
pub fn resume(&mut self, id: Option<u32>) {
|
||||
make_request_sync(Request::resume(id)).ok();
|
||||
make_request_async(Request::resume(id));
|
||||
}
|
||||
|
||||
pub fn stop(&mut self, id: Option<u32>) {
|
||||
make_request_sync(Request::stop(id)).ok();
|
||||
make_request_async(Request::stop(id));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+9
-13
@@ -3,10 +3,7 @@ use eframe::{App, Frame as EFrame};
|
||||
use egui::{CentralPanel, Context};
|
||||
use pwsp::{
|
||||
types::socket::Request,
|
||||
utils::{
|
||||
daemon::{get_daemon_config, is_daemon_running},
|
||||
gui::make_request_sync,
|
||||
},
|
||||
utils::{daemon::get_daemon_config, gui::make_request_async},
|
||||
};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
@@ -26,33 +23,32 @@ impl App for SoundpadGui {
|
||||
}
|
||||
|
||||
for (id, pos) in seek_requests {
|
||||
make_request_sync(Request::seek(pos, Some(id))).ok();
|
||||
make_request_async(Request::seek(pos, Some(id)));
|
||||
if let Some(ui_state) = self.app_state.track_ui_states.get_mut(&id) {
|
||||
ui_state.position_dragged = false;
|
||||
ui_state.ignore_position_update_until =
|
||||
Some(Instant::now() + Duration::from_millis(200));
|
||||
Some(Instant::now() + Duration::from_millis(300));
|
||||
}
|
||||
}
|
||||
|
||||
for (id, vol) in volume_requests {
|
||||
make_request_sync(Request::set_volume(vol, Some(id))).ok();
|
||||
make_request_async(Request::set_volume(vol, Some(id)));
|
||||
if let Some(ui_state) = self.app_state.track_ui_states.get_mut(&id) {
|
||||
ui_state.volume_dragged = false;
|
||||
ui_state.ignore_volume_update_until =
|
||||
Some(Instant::now() + Duration::from_millis(200));
|
||||
Some(Instant::now() + Duration::from_millis(300));
|
||||
}
|
||||
}
|
||||
|
||||
if self.app_state.volume_dragged {
|
||||
make_request_sync(Request::set_volume(
|
||||
make_request_async(Request::set_volume(
|
||||
self.app_state.volume_slider_value,
|
||||
None,
|
||||
))
|
||||
.ok();
|
||||
));
|
||||
|
||||
self.app_state.volume_dragged = false;
|
||||
self.app_state.ignore_volume_update_until =
|
||||
Some(Instant::now() + Duration::from_millis(200));
|
||||
Some(Instant::now() + Duration::from_millis(300));
|
||||
|
||||
if self.config.save_volume {
|
||||
let mut daemon_config = get_daemon_config();
|
||||
@@ -79,7 +75,7 @@ impl App for SoundpadGui {
|
||||
self.handle_input(ctx);
|
||||
|
||||
CentralPanel::default().show(ctx, |ui| {
|
||||
if !is_daemon_running().unwrap() {
|
||||
if !self.audio_player_state.is_daemon_running {
|
||||
self.draw_waiting_for_daemon(ui);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -54,4 +54,6 @@ pub struct AudioPlayerState {
|
||||
|
||||
pub current_input: String,
|
||||
pub all_inputs: HashMap<String, String>,
|
||||
|
||||
pub is_daemon_running: bool,
|
||||
}
|
||||
|
||||
+18
-2
@@ -5,7 +5,7 @@ use crate::{
|
||||
gui::AudioPlayerState,
|
||||
socket::{Request, Response},
|
||||
},
|
||||
utils::daemon::{make_request, wait_for_daemon},
|
||||
utils::daemon::{is_daemon_running, make_request},
|
||||
};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
@@ -30,6 +30,12 @@ pub fn make_request_sync(request: Request) -> Result<Response, Box<dyn Error>> {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn make_request_async(request: Request) {
|
||||
tokio::spawn(async move {
|
||||
make_request(request).await.ok();
|
||||
});
|
||||
}
|
||||
|
||||
pub fn format_time_pair(position: f32, duration: f32) -> String {
|
||||
fn format_time(seconds: f32) -> String {
|
||||
let total_seconds = seconds.round() as u32;
|
||||
@@ -46,7 +52,16 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerSt
|
||||
let sleep_duration = Duration::from_secs_f32(1.0 / 60.0);
|
||||
|
||||
loop {
|
||||
wait_for_daemon().await.ok();
|
||||
let is_running = is_daemon_running().unwrap_or(false);
|
||||
|
||||
if !is_running {
|
||||
{
|
||||
let mut guard = audio_player_state_shared.lock().unwrap();
|
||||
guard.is_daemon_running = false;
|
||||
}
|
||||
sleep(Duration::from_millis(500)).await;
|
||||
continue;
|
||||
}
|
||||
|
||||
let state_req = Request::get_state();
|
||||
let tracks_req = Request::get_tracks();
|
||||
@@ -128,6 +143,7 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerSt
|
||||
guard.volume = volume;
|
||||
guard.current_input = current_input;
|
||||
guard.all_inputs = all_inputs;
|
||||
guard.is_daemon_running = true;
|
||||
}
|
||||
|
||||
sleep(sleep_duration).await;
|
||||
|
||||
Reference in New Issue
Block a user