Skip to content

Commit

Permalink
only change album art when visible
Browse files Browse the repository at this point in the history
  • Loading branch information
mierak committed Dec 30, 2024
1 parent 78c1225 commit c2a6e11
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 30 deletions.
35 changes: 19 additions & 16 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,19 +552,22 @@ impl<'ui> Ui<'ui> {
}

pub fn on_event(&mut self, mut event: UiEvent, context: &mut AppContext) -> Result<()> {
let contains_pane = |p| {
self.tabs
.get(&self.active_tab)
.is_some_and(|tab| tab.panes.panes_iter().any(|pane| pane.pane == p))
};

match event {
UiEvent::Player => {}
UiEvent::Database => {
status_warn!("The music database has been updated. Some parts of the UI may have been reinitialized to prevent inconsistent behaviours.");
}
UiEvent::StoredPlaylist => {}
UiEvent::LogAdded(_) => {
UiEvent::LogAdded(_) =>
{
#[cfg(debug_assertions)]
if self
.tabs
.get(&self.active_tab)
.is_some_and(|tab| tab.panes.panes_iter().any(|pane| matches!(pane.pane, PaneType::Logs)))
{
if contains_pane(PaneType::Logs) {
context.render()?;
}
}
Expand All @@ -579,16 +582,16 @@ impl<'ui> Ui<'ui> {
for name in context.config.tabs.active_panes {
match self.panes.get_mut(*name) {
#[cfg(debug_assertions)]
Panes::Logs(p) => p.on_event(&mut event, context),
Panes::Queue(p) => p.on_event(&mut event, context),
Panes::Directories(p) => p.on_event(&mut event, context),
Panes::Albums(p) => p.on_event(&mut event, context),
Panes::Artists(p) => p.on_event(&mut event, context),
Panes::Playlists(p) => p.on_event(&mut event, context),
Panes::Search(p) => p.on_event(&mut event, context),
Panes::AlbumArtists(p) => p.on_event(&mut event, context),
Panes::AlbumArt(p) => p.on_event(&mut event, context),
Panes::Lyrics(p) => p.on_event(&mut event, context),
Panes::Logs(p) => p.on_event(&mut event, contains_pane(PaneType::Logs), context),
Panes::Queue(p) => p.on_event(&mut event, contains_pane(PaneType::Queue), context),
Panes::Directories(p) => p.on_event(&mut event, contains_pane(PaneType::Directories), context),
Panes::Albums(p) => p.on_event(&mut event, contains_pane(PaneType::Albums), context),
Panes::Artists(p) => p.on_event(&mut event, contains_pane(PaneType::Artists), context),
Panes::Playlists(p) => p.on_event(&mut event, contains_pane(PaneType::Playlists), context),
Panes::Search(p) => p.on_event(&mut event, contains_pane(PaneType::Search), context),
Panes::AlbumArtists(p) => p.on_event(&mut event, contains_pane(PaneType::AlbumArtists), context),
Panes::AlbumArt(p) => p.on_event(&mut event, contains_pane(PaneType::AlbumArt), context),
Panes::Lyrics(p) => p.on_event(&mut event, contains_pane(PaneType::Lyrics), context),
}?;
}

Expand Down
8 changes: 3 additions & 5 deletions src/ui/panes/album_art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,19 @@ impl Pane for AlbumArtPane {
Ok(())
}

fn on_event(&mut self, event: &mut UiEvent, context: &AppContext) -> Result<()> {
fn on_event(&mut self, event: &mut UiEvent, is_visible: bool, context: &AppContext) -> Result<()> {
match event {
UiEvent::SongChanged | UiEvent::Reconnected => {
UiEvent::SongChanged | UiEvent::Reconnected if is_visible => {
if AlbumArtPane::fetch_album_art(context).is_none() {
self.album_art.show_default()?;
}
}
UiEvent::ModalOpened => {
self.album_art.hide()?;

context.render()?;
}
UiEvent::ModalClosed => {
self.album_art.show_current()?;

context.render()?;
}
UiEvent::Exit => {
Expand Down Expand Up @@ -227,7 +225,7 @@ mod tests {
app_context.status.state = State::Play;
let mut screen = AlbumArtPane::new(&app_context);

screen.on_event(&mut UiEvent::SongChanged, &app_context).unwrap();
screen.on_event(&mut UiEvent::SongChanged, true, &app_context).unwrap();

if should_search {
assert!(matches!(
Expand Down
2 changes: 1 addition & 1 deletion src/ui/panes/albums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Pane for AlbumsPane {
Ok(())
}

fn on_event(&mut self, event: &mut UiEvent, context: &AppContext) -> Result<()> {
fn on_event(&mut self, event: &mut UiEvent, _is_visible: bool, context: &AppContext) -> Result<()> {
match event {
UiEvent::Database => {
context
Expand Down
2 changes: 1 addition & 1 deletion src/ui/panes/artists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl Pane for ArtistsPane {
Ok(())
}

fn on_event(&mut self, event: &mut UiEvent, context: &AppContext) -> Result<()> {
fn on_event(&mut self, event: &mut UiEvent, _is_visible: bool, context: &AppContext) -> Result<()> {
match event {
UiEvent::Database => {
let target = self.target_pane();
Expand Down
2 changes: 1 addition & 1 deletion src/ui/panes/directories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl Pane for DirectoriesPane {
Ok(())
}

fn on_event(&mut self, event: &mut UiEvent, context: &AppContext) -> Result<()> {
fn on_event(&mut self, event: &mut UiEvent, _is_visible: bool, context: &AppContext) -> Result<()> {
match event {
UiEvent::Database => {
context
Expand Down
2 changes: 1 addition & 1 deletion src/ui/panes/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Pane for LogsPane {
Ok(())
}

fn on_event(&mut self, event: &mut UiEvent, context: &AppContext) -> Result<()> {
fn on_event(&mut self, event: &mut UiEvent, _is_visible: bool, context: &AppContext) -> Result<()> {
if let UiEvent::LogAdded(msg) = event {
self.logs.push_back(std::mem::take(msg));
if self.logs.len() > 1000 {
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 @@ -86,7 +86,7 @@ impl Pane for LyricsPane {
Ok(())
}

fn on_event(&mut self, event: &mut UiEvent, context: &AppContext) -> Result<()> {
fn on_event(&mut self, event: &mut UiEvent, _is_visible: bool, context: &AppContext) -> Result<()> {
match event {
UiEvent::SongChanged | UiEvent::Reconnected => match context.find_lrc() {
Ok(lrc) => {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/panes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub(super) trait Pane {
}

/// Used to keep the current state but refresh data
fn on_event(&mut self, event: &mut UiEvent, context: &AppContext) -> Result<()> {
fn on_event(&mut self, event: &mut UiEvent, is_visible: bool, context: &AppContext) -> Result<()> {
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/ui/panes/playlists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Pane for PlaylistsPane {
Ok(())
}

fn on_event(&mut self, event: &mut UiEvent, context: &AppContext) -> Result<()> {
fn on_event(&mut self, event: &mut UiEvent, _is_visible: bool, context: &AppContext) -> Result<()> {
let id = match event {
UiEvent::Database => Some(INIT),
UiEvent::StoredPlaylist => Some(REINIT),
Expand Down
2 changes: 1 addition & 1 deletion src/ui/panes/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl Pane for QueuePane {
Ok(())
}

fn on_event(&mut self, event: &mut UiEvent, context: &AppContext) -> Result<()> {
fn on_event(&mut self, event: &mut UiEvent, _is_visible: bool, context: &AppContext) -> Result<()> {
match event {
UiEvent::SongChanged => {
if let Some((idx, _)) = context.find_current_song_in_queue() {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/panes/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ impl Pane for SearchPane {
Ok(())
}

fn on_event(&mut self, event: &mut UiEvent, context: &AppContext) -> Result<()> {
fn on_event(&mut self, event: &mut UiEvent, _is_visible: bool, context: &AppContext) -> Result<()> {
match event {
UiEvent::Database => {
self.songs_dir = Dir::default();
Expand Down

0 comments on commit c2a6e11

Please sign in to comment.