mirror of
https://github.com/arabianq/pipewire-soundpad.git
synced 2026-04-27 22:11:22 +00:00
refactor: break down handle_input into smaller methods in src/gui/input.rs (#67)
Extract sections from the long `handle_input` function into smaller, context-specific helper methods such as `handle_hotkey_assignment`, `handle_toggles`, `handle_playback_and_focus`, `handle_file_playback`, `handle_navigation`, and `handle_hotkey_triggers`. This significantly improves the maintainability and readability of `src/gui/input.rs` while preserving original functionality. In addition, ran `cargo clippy --fix` on the project to resolve a few other minor health issues, like collapsing nested `if` statements and reducing unnecessary allocations. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
f87dcb1564
commit
302f153b91
+20
-23
@@ -607,10 +607,10 @@ impl SoundpadGui {
|
|||||||
.unwrap_or_else(|| path.to_string_lossy().to_string());
|
.unwrap_or_else(|| path.to_string_lossy().to_string());
|
||||||
|
|
||||||
let mut dir_button_text = RichText::new(name.clone());
|
let mut dir_button_text = RichText::new(name.clone());
|
||||||
if let Some(current_dir) = &self.app_state.current_dir {
|
if let Some(current_dir) = &self.app_state.current_dir
|
||||||
if current_dir.eq(&path) {
|
&& current_dir.eq(&path)
|
||||||
dir_button_text = dir_button_text.color(Color32::WHITE);
|
{
|
||||||
}
|
dir_button_text = dir_button_text.color(Color32::WHITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
let dir_button =
|
let dir_button =
|
||||||
@@ -643,10 +643,9 @@ impl SoundpadGui {
|
|||||||
ICON_OPEN_IN_BROWSER.codepoint, "Open in File Manager"
|
ICON_OPEN_IN_BROWSER.codepoint, "Open in File Manager"
|
||||||
))
|
))
|
||||||
.clicked()
|
.clicked()
|
||||||
|
&& let Err(e) = opener::open(&path)
|
||||||
{
|
{
|
||||||
if let Err(e) = opener::open(&path) {
|
eprintln!("Failed to open file manager: {}", e);
|
||||||
eprintln!("Failed to open file manager: {}", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.separator();
|
ui.separator();
|
||||||
@@ -726,10 +725,10 @@ impl SoundpadGui {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut file_button_text = RichText::new(&file_name);
|
let mut file_button_text = RichText::new(&file_name);
|
||||||
if let Some(current_file) = &self.app_state.selected_file {
|
if let Some(current_file) = &self.app_state.selected_file
|
||||||
if current_file.eq(&entry_path) {
|
&& current_file.eq(&entry_path)
|
||||||
file_button_text = file_button_text.color(Color32::WHITE);
|
{
|
||||||
}
|
file_button_text = file_button_text.color(Color32::WHITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
let file_button = Button::new(file_button_text).frame(false);
|
let file_button = Button::new(file_button_text).frame(false);
|
||||||
@@ -790,10 +789,9 @@ impl SoundpadGui {
|
|||||||
ICON_OPEN_IN_BROWSER.codepoint, "Show in File Manager"
|
ICON_OPEN_IN_BROWSER.codepoint, "Show in File Manager"
|
||||||
))
|
))
|
||||||
.clicked()
|
.clicked()
|
||||||
|
&& let Err(e) = opener::reveal(&entry_path)
|
||||||
{
|
{
|
||||||
if let Err(e) = opener::reveal(&entry_path) {
|
eprintln!("Failed to open file manager: {}", e);
|
||||||
eprintln!("Failed to open file manager: {}", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.separator();
|
ui.separator();
|
||||||
@@ -820,15 +818,14 @@ impl SoundpadGui {
|
|||||||
|
|
||||||
fn get_hotkey_badge(&self, path: &PathBuf) -> Option<String> {
|
fn get_hotkey_badge(&self, path: &PathBuf) -> Option<String> {
|
||||||
for slot in &self.app_state.hotkey_config.slots {
|
for slot in &self.app_state.hotkey_config.slots {
|
||||||
if slot.action.name == "play" {
|
if slot.action.name == "play"
|
||||||
if let Some(file_path_str) = slot.action.args.get("file_path") {
|
&& let Some(file_path_str) = slot.action.args.get("file_path")
|
||||||
if Path::new(file_path_str) == path.as_path() {
|
&& Path::new(file_path_str) == path.as_path()
|
||||||
if let Some(chord) = &slot.key_chord {
|
{
|
||||||
return Some(format!("[{}]", chord));
|
if let Some(chord) = &slot.key_chord {
|
||||||
} else {
|
return Some(format!("[{}]", chord));
|
||||||
return Some(format!("[{}]", slot.slot));
|
} else {
|
||||||
}
|
return Some(format!("[{}]", slot.slot));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-13
@@ -192,18 +192,18 @@ impl SoundpadGui {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Play selected file on Enter
|
// Play selected file on Enter
|
||||||
if self.key_pressed(ctx, Key::Enter) {
|
if self.key_pressed(ctx, Key::Enter)
|
||||||
if let Some(path) = self.app_state.selected_file.clone() {
|
&& let Some(path) = self.app_state.selected_file.clone()
|
||||||
if modifiers.ctrl {
|
{
|
||||||
self.play_file(&path, true);
|
if modifiers.ctrl {
|
||||||
} else if modifiers.shift
|
self.play_file(&path, true);
|
||||||
&& let Some(last_track) = self.audio_player_state.tracks.last()
|
} else if modifiers.shift
|
||||||
{
|
&& let Some(last_track) = self.audio_player_state.tracks.last()
|
||||||
self.stop(Some(last_track.id));
|
{
|
||||||
self.play_file(&path, true);
|
self.stop(Some(last_track.id));
|
||||||
} else {
|
self.play_file(&path, true);
|
||||||
self.play_file(&path, false);
|
} else {
|
||||||
}
|
self.play_file(&path, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,7 +212,7 @@ impl SoundpadGui {
|
|||||||
let arrow_down_pressed = self.key_pressed(ctx, Key::ArrowDown);
|
let arrow_down_pressed = self.key_pressed(ctx, Key::ArrowDown);
|
||||||
if modifiers.ctrl && (arrow_up_pressed || arrow_down_pressed) {
|
if modifiers.ctrl && (arrow_up_pressed || arrow_down_pressed) {
|
||||||
if modifiers.shift && !self.app_state.dirs.is_empty() {
|
if modifiers.shift && !self.app_state.dirs.is_empty() {
|
||||||
let mut dirs: Vec<PathBuf> = self.app_state.dirs.iter().cloned().collect();
|
let mut dirs: Vec<PathBuf> = self.app_state.dirs.to_vec();
|
||||||
dirs.sort();
|
dirs.sort();
|
||||||
|
|
||||||
let current_dir_index = self
|
let current_dir_index = self
|
||||||
|
|||||||
+15
-16
@@ -106,7 +106,7 @@ impl AudioPlayer {
|
|||||||
|
|
||||||
fn abort_link_thread(&mut self) {
|
fn abort_link_thread(&mut self) {
|
||||||
if let Some(sender) = &self.input_link_sender {
|
if let Some(sender) = &self.input_link_sender {
|
||||||
if let Ok(_) = sender.send(Terminate {}) {
|
if sender.send(Terminate {}).is_ok() {
|
||||||
println!("Sent terminate signal to input link thread");
|
println!("Sent terminate signal to input link thread");
|
||||||
self.input_link_sender = None;
|
self.input_link_sender = None;
|
||||||
} else {
|
} else {
|
||||||
@@ -117,7 +117,7 @@ impl AudioPlayer {
|
|||||||
|
|
||||||
fn abort_player_link_thread(&mut self) {
|
fn abort_player_link_thread(&mut self) {
|
||||||
if let Some(sender) = &self.player_link_sender {
|
if let Some(sender) = &self.player_link_sender {
|
||||||
if let Ok(_) = sender.send(Terminate {}) {
|
if sender.send(Terminate {}).is_ok() {
|
||||||
println!("Sent terminate signal to player link thread");
|
println!("Sent terminate signal to player link thread");
|
||||||
self.player_link_sender = None;
|
self.player_link_sender = None;
|
||||||
} else {
|
} else {
|
||||||
@@ -254,12 +254,12 @@ impl AudioPlayer {
|
|||||||
pub fn get_volume(&mut self, id: Option<u32>) -> Option<f32> {
|
pub fn get_volume(&mut self, id: Option<u32>) -> Option<f32> {
|
||||||
if let Some(id) = id {
|
if let Some(id) = id {
|
||||||
if let Some(sound) = self.tracks.get_mut(&id) {
|
if let Some(sound) = self.tracks.get_mut(&id) {
|
||||||
return Some(sound.sink.volume());
|
Some(sound.sink.volume())
|
||||||
} else {
|
} else {
|
||||||
return None;
|
None
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return Some(self.volume);
|
Some(self.volume)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -443,10 +443,10 @@ impl AudioPlayer {
|
|||||||
if let Some(sound) = self.tracks.get(&id) {
|
if let Some(sound) = self.tracks.get(&id) {
|
||||||
let path = sound.path.clone();
|
let path = sound.path.clone();
|
||||||
let handle = tokio::task::spawn_blocking(move || {
|
let handle = tokio::task::spawn_blocking(move || {
|
||||||
if let Ok(file) = fs::File::open(&path) {
|
if let Ok(file) = fs::File::open(&path)
|
||||||
if let Ok(source) = Decoder::try_from(file) {
|
&& let Ok(source) = Decoder::try_from(file)
|
||||||
return Some((id, source));
|
{
|
||||||
}
|
return Some((id, source));
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
});
|
});
|
||||||
@@ -455,13 +455,12 @@ impl AudioPlayer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for handle in restart_futures {
|
for handle in restart_futures {
|
||||||
if let Ok(res) = handle.await {
|
if let Ok(res) = handle.await
|
||||||
if let Some((id, source)) = res {
|
&& let Some((id, source)) = res
|
||||||
if let Some(sound) = self.tracks.get_mut(&id) {
|
&& let Some(sound) = self.tracks.get_mut(&id)
|
||||||
sound.sink.append(source);
|
{
|
||||||
sound.sink.play();
|
sound.sink.append(source);
|
||||||
}
|
sound.sink.play();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -673,7 +673,7 @@ impl Executable for PlayHotkeyCommand {
|
|||||||
if let Some(cmd) = parse_command(&action) {
|
if let Some(cmd) = parse_command(&action) {
|
||||||
cmd.execute().await
|
cmd.execute().await
|
||||||
} else {
|
} else {
|
||||||
Response::new(false, "Unknown command in hotkey slot".to_string())
|
Response::new(false, "Unknown command in hotkey slot")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-8
@@ -13,10 +13,10 @@ impl DaemonConfig {
|
|||||||
pub fn save_to_file(&self) -> Result<(), Box<dyn Error>> {
|
pub fn save_to_file(&self) -> Result<(), Box<dyn Error>> {
|
||||||
let config_path = get_config_path()?.join("daemon.json");
|
let config_path = get_config_path()?.join("daemon.json");
|
||||||
|
|
||||||
if let Some(config_dir) = config_path.parent() {
|
if let Some(config_dir) = config_path.parent()
|
||||||
if !config_path.exists() {
|
&& !config_path.exists()
|
||||||
fs::create_dir_all(config_dir)?;
|
{
|
||||||
}
|
fs::create_dir_all(config_dir)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let config_json = serde_json::to_string_pretty(self)?;
|
let config_json = serde_json::to_string_pretty(self)?;
|
||||||
@@ -68,10 +68,10 @@ impl GuiConfig {
|
|||||||
pub fn save_to_file(&mut self) -> Result<(), Box<dyn Error>> {
|
pub fn save_to_file(&mut self) -> Result<(), Box<dyn Error>> {
|
||||||
let config_path = get_config_path()?.join("gui.json");
|
let config_path = get_config_path()?.join("gui.json");
|
||||||
|
|
||||||
if let Some(config_dir) = config_path.parent() {
|
if let Some(config_dir) = config_path.parent()
|
||||||
if !config_path.exists() {
|
&& !config_path.exists()
|
||||||
fs::create_dir_all(config_dir)?;
|
{
|
||||||
}
|
fs::create_dir_all(config_dir)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do not save scale factor if user does not want to
|
// Do not save scale factor if user does not want to
|
||||||
|
|||||||
+7
-7
@@ -112,13 +112,13 @@ pub fn start_app_state_thread(audio_player_state_shared: Arc<Mutex<AudioPlayerSt
|
|||||||
let hotkey_res = make_request(Request::get_hotkeys())
|
let hotkey_res = make_request(Request::get_hotkeys())
|
||||||
.await
|
.await
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
if hotkey_res.status {
|
if hotkey_res.status
|
||||||
if let Ok(config) = serde_json::from_str::<HotkeyConfig>(&hotkey_res.message) {
|
&& let Ok(config) = serde_json::from_str::<HotkeyConfig>(&hotkey_res.message)
|
||||||
let mut guard = audio_player_state_shared
|
{
|
||||||
.lock()
|
let mut guard = audio_player_state_shared
|
||||||
.unwrap_or_else(|e| e.into_inner());
|
.lock()
|
||||||
guard.hotkey_config = Some(config);
|
.unwrap_or_else(|e| e.into_inner());
|
||||||
}
|
guard.hotkey_config = Some(config);
|
||||||
}
|
}
|
||||||
last_hotkey_poll = Instant::now();
|
last_hotkey_poll = Instant::now();
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-10
@@ -56,20 +56,20 @@ fn parse_global_object(
|
|||||||
(None, None)
|
(None, None)
|
||||||
};
|
};
|
||||||
// Check if the object is a port
|
// Check if the object is a port
|
||||||
} else if props.get("port.direction").is_some() {
|
} else if props.get("port.direction").is_some()
|
||||||
if let (Some(node_id), Some(port_id), Some(port_name)) = (
|
&& let (Some(node_id), Some(port_id), Some(port_name)) = (
|
||||||
props.get("node.id").and_then(|id| id.parse::<u32>().ok()),
|
props.get("node.id").and_then(|id| id.parse::<u32>().ok()),
|
||||||
props.get("port.id").and_then(|id| id.parse::<u32>().ok()),
|
props.get("port.id").and_then(|id| id.parse::<u32>().ok()),
|
||||||
props.get("port.name"),
|
props.get("port.name"),
|
||||||
) {
|
)
|
||||||
let port = Port {
|
{
|
||||||
node_id,
|
let port = Port {
|
||||||
port_id,
|
node_id,
|
||||||
name: port_name.to_string(),
|
port_id,
|
||||||
};
|
name: port_name.to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
return (None, Some(port));
|
return (None, Some(port));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(None, None)
|
(None, None)
|
||||||
|
|||||||
Reference in New Issue
Block a user