diff --git a/CHANGELOG.md b/CHANGELOG.md index cb82701..a1e3662 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ All notable changes to this project will be documented in this file. - Read stream not being emptied after encountering error while reading MPD's response - Rows not wrapping in the keybinds modal when the screen is too small - Unchecked panic inside the volume widget when volume exceeds certain value +- Several things that should have happened on song change were happening on every `Player` event, ie. seeking ### Deprecated diff --git a/src/main.rs b/src/main.rs index c4230a2..ce4df7c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -373,6 +373,7 @@ fn main_task( let max_fps = 30f64; let min_frame_duration = Duration::from_secs_f64(1f64 / max_fps); let mut last_render = std::time::Instant::now().sub(Duration::from_secs(10)); + let mut additional_evs = Vec::new(); ui.before_show(&mut context, &mut client) .expect("Initial render init to succeed"); @@ -425,12 +426,16 @@ fn main_task( } } AppEvent::IdleEvent(event) => { - if let Err(err) = handle_idle_event(event, &mut context, &mut client, &mut render_loop) { - status_error!(error:? = err, event:?; "Failed handle idle event, event: '{:?}', error: '{}'", event, err.to_status()); - } - if let Ok(ev) = event.try_into() { - if let Err(err) = ui.on_event(ev, &mut context, &mut client) { - status_error!(error:? = err, event:?; "UI failed to handle idle event, event: '{:?}', error: '{}'", event, err.to_status()); + match handle_idle_event(event, &mut context, &mut client, &mut render_loop, &mut additional_evs) { + Ok(()) => { + for ev in additional_evs.drain(..) { + if let Err(err) = ui.on_event(ev, &mut context, &mut client) { + status_error!(error:? = err, event:?; "UI failed to handle idle event, event: '{:?}', error: '{}'", event, err.to_status()); + } + } + } + Err(err) => { + status_error!(error:? = err, event:?; "Failed handle idle event, event: '{:?}', error: '{}'", event, err.to_status()); } } render_wanted = true; @@ -521,6 +526,7 @@ fn handle_idle_event( context: &mut context::AppContext, client: &mut Client<'_>, render_loop: &mut RenderLoop, + result_ui_evs: &mut Vec, ) -> Result<()> { match event { IdleEvent::Mixer => { @@ -568,6 +574,7 @@ fn handle_idle_event( } }; + result_ui_evs.push(UiEvent::SongChanged); run_external(command, env); }; } @@ -589,6 +596,10 @@ fn handle_idle_event( warn!(event:?; "Received unhandled event"); } }; + + if let Ok(ev) = event.try_into() { + result_ui_evs.push(ev); + } Ok(()) } diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 8eb4fe5..dc2e077 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -524,6 +524,7 @@ impl<'ui> Ui<'ui> { UiEvent::ModalClosed => {} UiEvent::Exit => {} UiEvent::LyricsIndexed => {} + UiEvent::SongChanged => {} } for name in context.config.tabs.active_panes { @@ -567,6 +568,7 @@ pub enum UiEvent { ModalClosed, Exit, LyricsIndexed, + SongChanged, } impl TryFrom for UiEvent { diff --git a/src/ui/panes/album_art.rs b/src/ui/panes/album_art.rs index bccf66f..6c52c42 100644 --- a/src/ui/panes/album_art.rs +++ b/src/ui/panes/album_art.rs @@ -101,7 +101,7 @@ impl Pane for AlbumArtPane { fn on_event(&mut self, event: &mut UiEvent, client: &mut impl MpdClient, context: &AppContext) -> Result<()> { match event { - UiEvent::Player => { + UiEvent::SongChanged => { self.album_art .set_image(AlbumArtPane::fetch_album_art(client, context)?)?; context.render()?; @@ -207,7 +207,7 @@ mod tests { let mut screen = AlbumArtPane::new(&app_context); screen - .on_event(&mut UiEvent::Player, &mut client, &app_context) + .on_event(&mut UiEvent::SongChanged, &mut client, &app_context) .unwrap(); assert_eq!( diff --git a/src/ui/panes/lyrics.rs b/src/ui/panes/lyrics.rs index 4190e80..e411eda 100644 --- a/src/ui/panes/lyrics.rs +++ b/src/ui/panes/lyrics.rs @@ -90,7 +90,7 @@ impl Pane for LyricsPane { fn on_event(&mut self, event: &mut UiEvent, _client: &mut impl MpdClient, context: &AppContext) -> Result<()> { match event { - UiEvent::Player => match context.find_lrc() { + UiEvent::SongChanged => match context.find_lrc() { Ok(lrc) => { self.current_lyrics = lrc; context.render()?; diff --git a/src/ui/panes/queue.rs b/src/ui/panes/queue.rs index 51f67a7..81f2937 100644 --- a/src/ui/panes/queue.rs +++ b/src/ui/panes/queue.rs @@ -187,18 +187,12 @@ impl Pane for QueuePane { } fn on_event(&mut self, event: &mut UiEvent, _client: &mut impl MpdClient, context: &AppContext) -> Result<()> { - if let UiEvent::Player = event { - if let Some((idx, _)) = context - .queue - .iter() - .enumerate() - .find(|(_, v)| Some(v.id) == context.status.songid) - { + if let UiEvent::SongChanged = event { + if let Some((idx, _)) = context.find_current_song_in_queue() { if context.config.select_current_song_on_change { self.scrolling_state.select(Some(idx), context.config.scrolloff); + context.render()?; } - - context.render()?; } };