From 7c800ff8178fd8a1bb0586899f64d959e44994d4 Mon Sep 17 00:00:00 2001 From: Casey Langen Date: Mon, 23 Jan 2017 07:52:49 -0800 Subject: [PATCH] Add new `RowDecorator` to TrackListView, with a default implementation. Added a custom implementation to NowPlayingLayout to make sure the actively playing instance is highlighted, instead of all tracks that have the same ID. --- src/core/audio/PlaybackService.cpp | 5 +++ src/musikbox/app/layout/NowPlayingLayout.cpp | 27 +++++++++++++- src/musikbox/app/layout/NowPlayingLayout.h | 1 + src/musikbox/app/window/TrackListView.cpp | 37 ++++++++++++-------- src/musikbox/app/window/TrackListView.h | 8 ++++- 5 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/core/audio/PlaybackService.cpp b/src/core/audio/PlaybackService.cpp index 6b4f224ef..4fad392d1 100755 --- a/src/core/audio/PlaybackService.cpp +++ b/src/core/audio/PlaybackService.cpp @@ -521,6 +521,11 @@ IRetainedTrack* PlaybackService::GetTrack(size_t index) { TrackPtr PlaybackService::GetTrackAtIndex(size_t index) { std::unique_lock lock(this->playlistMutex); + + if (index >= this->playlist.Count()) { + return TrackPtr(); + } + return this->playlist.Get(index); } diff --git a/src/musikbox/app/layout/NowPlayingLayout.cpp b/src/musikbox/app/layout/NowPlayingLayout.cpp index 576eda592..7642f912a 100755 --- a/src/musikbox/app/layout/NowPlayingLayout.cpp +++ b/src/musikbox/app/layout/NowPlayingLayout.cpp @@ -80,6 +80,30 @@ NowPlayingLayout::~NowPlayingLayout() { } +int64 NowPlayingLayout::RowDecorator(musik::core::TrackPtr track, size_t index) { + bool selected = index == trackList->GetSelectedIndex(); + int64 attrs = selected ? COLOR_PAIR(CURSESPP_HIGHLIGHTED_LIST_ITEM) : -1LL; + size_t playingIndex = playback.GetIndex(); + + if (index == playingIndex) { + TrackPtr playing = playback.GetTrackAtIndex(playingIndex); + + if (playing && + playing->Id() == track->Id() && + playing->LibraryId() == track->LibraryId()) + { + if (selected) { + attrs = COLOR_PAIR(CURSESPP_HIGHLIGHTED_SELECTED_LIST_ITEM); + } + else { + attrs = COLOR_PAIR(CURSESPP_SELECTED_LIST_ITEM); + } + } + } + + return attrs; +} + void NowPlayingLayout::OnLayout() { this->trackList->MoveAndResize( 0, @@ -94,7 +118,8 @@ void NowPlayingLayout::InitializeWindows() { this->trackList.reset(new TrackListView( this->playback, this->library, - std::bind(formatWithAlbum, std::placeholders::_1, std::placeholders::_2))); + std::bind(formatWithAlbum, std::placeholders::_1, std::placeholders::_2), + std::bind(&NowPlayingLayout::RowDecorator, this, std::placeholders::_1, std::placeholders::_2))); this->trackList->Requeried.connect(this, &NowPlayingLayout::OnTrackListRequeried); this->AddWindow(this->trackList); diff --git a/src/musikbox/app/layout/NowPlayingLayout.h b/src/musikbox/app/layout/NowPlayingLayout.h index 1ae34738a..a88841f93 100755 --- a/src/musikbox/app/layout/NowPlayingLayout.h +++ b/src/musikbox/app/layout/NowPlayingLayout.h @@ -73,6 +73,7 @@ namespace musik { void InitializeWindows(); void RequeryTrackList(); bool ProcessEditOperation(const std::string& key); + int64 RowDecorator(musik::core::TrackPtr track, size_t index); musik::core::audio::PlaybackService& playback; musik::core::LibraryPtr library; diff --git a/src/musikbox/app/window/TrackListView.cpp b/src/musikbox/app/window/TrackListView.cpp index b7923717f..90aef4e60 100755 --- a/src/musikbox/app/window/TrackListView.cpp +++ b/src/musikbox/app/window/TrackListView.cpp @@ -74,7 +74,8 @@ static IScrollAdapter::EntryPtr MISSING_ENTRY = IScrollAdapter::EntryPtr(); TrackListView::TrackListView( PlaybackService& playback, LibraryPtr library, - RowFormatter formatter) + RowFormatter formatter, + RowDecorator decorator) : ListWindow(nullptr) , playback(playback) { this->library = library; @@ -84,6 +85,7 @@ TrackListView::TrackListView( this->lastQueryHash = 0; this->lastChanged = now(); this->formatter = formatter; + this->decorator = decorator; if (!MISSING_ENTRY) { auto e = std::shared_ptr(new SingleLineEntry("track missing")); @@ -251,25 +253,32 @@ static std::string formatWithoutAlbum(TrackPtr track, size_t width) { } IScrollAdapter::EntryPtr TrackListView::Adapter::GetEntry(cursespp::ScrollableWindow* window, size_t index) { - bool selected = index == parent.GetSelectedIndex(); - int64 attrs = selected ? COLOR_PAIR(CURSESPP_HIGHLIGHTED_LIST_ITEM) : -1LL; - TrackPtr track = parent.metadata->Get(index); if (!track) { return MISSING_ENTRY; } - TrackPtr playing = parent.playing; - if (playing && - playing->Id() == track->Id() && - playing->LibraryId() == track->LibraryId()) - { - if (selected) { - attrs = COLOR_PAIR(CURSESPP_HIGHLIGHTED_SELECTED_LIST_ITEM); - } - else { - attrs = COLOR_PAIR(CURSESPP_SELECTED_LIST_ITEM); + int64 attrs = -1LL; + + if (parent.decorator) { + attrs = parent.decorator(track, index); + } + else { + bool selected = index == parent.GetSelectedIndex(); + attrs = selected ? COLOR_PAIR(CURSESPP_HIGHLIGHTED_LIST_ITEM) : -1LL; + + TrackPtr playing = parent.playing; + if (playing && + playing->Id() == track->Id() && + playing->LibraryId() == track->LibraryId()) + { + if (selected) { + attrs = COLOR_PAIR(CURSESPP_HIGHLIGHTED_SELECTED_LIST_ITEM); + } + else { + attrs = COLOR_PAIR(CURSESPP_SELECTED_LIST_ITEM); + } } } diff --git a/src/musikbox/app/window/TrackListView.h b/src/musikbox/app/window/TrackListView.h index e1cbec9e4..e2cbc2f23 100755 --- a/src/musikbox/app/window/TrackListView.h +++ b/src/musikbox/app/window/TrackListView.h @@ -60,14 +60,19 @@ namespace musik { typedef std::function RowFormatter; + typedef std::function RowDecorator; + typedef std::shared_ptr > Headers; typedef musik::glue::TrackListQueryBase TrackListQueryBase; + TrackListView( musik::core::audio::PlaybackService& playback, musik::core::LibraryPtr library, - RowFormatter formatter = RowFormatter()); + RowFormatter formatter = RowFormatter(), + RowDecorator decorator = RowDecorator()); virtual ~TrackListView(); @@ -112,6 +117,7 @@ namespace musik { musik::core::LibraryPtr library; size_t lastQueryHash; RowFormatter formatter; + RowDecorator decorator; std::chrono::milliseconds lastChanged; }; }