Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: several things happening on player event instead on song change #142

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 17 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ fn main_task<B: Backend + std::io::Write>(
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");

Expand Down Expand Up @@ -425,12 +426,16 @@ fn main_task<B: Backend + std::io::Write>(
}
}
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;
Expand Down Expand Up @@ -521,6 +526,7 @@ fn handle_idle_event(
context: &mut context::AppContext,
client: &mut Client<'_>,
render_loop: &mut RenderLoop,
result_ui_evs: &mut Vec<UiEvent>,
) -> Result<()> {
match event {
IdleEvent::Mixer => {
Expand Down Expand Up @@ -568,6 +574,7 @@ fn handle_idle_event(
}
};

result_ui_evs.push(UiEvent::SongChanged);
run_external(command, env);
};
}
Expand All @@ -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(())
}

Expand Down
2 changes: 2 additions & 0 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ impl<'ui> Ui<'ui> {
UiEvent::ModalClosed => {}
UiEvent::Exit => {}
UiEvent::LyricsIndexed => {}
UiEvent::SongChanged => {}
}

for name in context.config.tabs.active_panes {
Expand Down Expand Up @@ -567,6 +568,7 @@ pub enum UiEvent {
ModalClosed,
Exit,
LyricsIndexed,
SongChanged,
}

impl TryFrom<IdleEvent> for UiEvent {
Expand Down
4 changes: 2 additions & 2 deletions src/ui/panes/album_art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down Expand Up @@ -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!(
Expand Down
2 changes: 1 addition & 1 deletion src/ui/panes/lyrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down
12 changes: 3 additions & 9 deletions src/ui/panes/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
}
};

Expand Down