diff --git a/src/musikcube/app/layout/BrowseLayout.cpp b/src/musikcube/app/layout/BrowseLayout.cpp index 8ed6eb49d..c2e915cd7 100755 --- a/src/musikcube/app/layout/BrowseLayout.cpp +++ b/src/musikcube/app/layout/BrowseLayout.cpp @@ -245,6 +245,9 @@ void BrowseLayout::OnCategoryViewInvalidated( void BrowseLayout::SwitchCategory(const std::string& fieldName) { this->categoryList->SetFieldName(fieldName); this->categoryList->SetFrameTitle(getTitleForCategory(fieldName)); + + this->trackList->SetTrackNumType(fieldName == constants::Playlists::TABLE_NAME + ? TrackListView::TrackNumType::Sequential : TrackListView::TrackNumType::Metadata); } bool BrowseLayout::KeyPress(const std::string& key) { diff --git a/src/musikcube/app/window/TrackListView.cpp b/src/musikcube/app/window/TrackListView.cpp index c25132b7d..05f3dba92 100755 --- a/src/musikcube/app/window/TrackListView.cpp +++ b/src/musikcube/app/window/TrackListView.cpp @@ -88,6 +88,7 @@ TrackListView::TrackListView( this->lastChanged = now(); this->formatter = formatter; this->decorator = decorator; + this->trackNumType = TrackNumType::Metadata; } TrackListView::~TrackListView() { @@ -104,6 +105,13 @@ void TrackListView::SelectFirstTrack() { this->SetSelectedIndex(this->headers.HeaderAt(0) ? 1 : 0); } +void TrackListView::SetTrackNumType(TrackNumType type) { + if (this->trackNumType != type) { + this->trackNumType = type; + this->OnAdapterChanged(); + } +} + void TrackListView::OnQueryCompleted(IQuery* query) { if (this->query && query == this->query.get()) { if (this->query->GetStatus() == IQuery::Finished) { @@ -339,12 +347,24 @@ size_t TrackListView::Adapter::GetEntryCount() { #define TRACK_COL_WIDTH 3 #define ARTIST_COL_WIDTH 17 #define DURATION_COL_WIDTH 5 /* 00:00 */ +#define DIGITS(x) (x > 9 ? (int) log10((double) x) + 1 : 1) -static std::string formatWithoutAlbum(TrackPtr track, size_t width) { - std::string trackNum = text::Align( - track->GetString(constants::Track::TRACK_NUM), - text::AlignRight, - TRACK_COL_WIDTH); +using TrackNumType = TrackListView::TrackNumType; + +static std::string formatWithoutAlbum(TrackPtr track, size_t index, size_t width, TrackNumType type) { + std::string trackNum; + + int trackColWidth = TRACK_COL_WIDTH; + if (type == TrackNumType::Metadata) { + trackNum = text::Align( + track->GetString(constants::Track::TRACK_NUM), + text::AlignRight, + TRACK_COL_WIDTH); + } + else { + trackColWidth = std::max(TRACK_COL_WIDTH, DIGITS(index + 1)); + trackNum = text::Align(std::to_string(index + 1), text::AlignRight, trackColWidth); + } std::string duration = text::Align( musik::core::duration::Duration(track->GetString(constants::Track::DURATION)), @@ -358,7 +378,7 @@ static std::string formatWithoutAlbum(TrackPtr track, size_t width) { int titleWidth = width - - TRACK_COL_WIDTH - + trackColWidth - DURATION_COL_WIDTH - ARTIST_COL_WIDTH - (3 * 3); /* 3 = spacing */ @@ -442,7 +462,7 @@ IScrollAdapter::EntryPtr TrackListView::Adapter::GetEntry(cursespp::ScrollableWi std::string text = parent.formatter ? parent.formatter(track, this->GetWidth()) - : formatWithoutAlbum(track, this->GetWidth()); + : formatWithoutAlbum(track, rawIndex, this->GetWidth(), parent.trackNumType); std::shared_ptr entry( new TrackListEntry(text, trackIndex, RowType::Track)); diff --git a/src/musikcube/app/window/TrackListView.h b/src/musikcube/app/window/TrackListView.h index de487a71e..2e077a05f 100755 --- a/src/musikcube/app/window/TrackListView.h +++ b/src/musikcube/app/window/TrackListView.h @@ -56,6 +56,8 @@ namespace musik { public sigslot::has_slots<> { public: + enum class TrackNumType: int { Metadata = 0, Sequential = 1 }; + typedef musik::core::TrackPtr TrackPtr; typedef musik::core::db::local::TrackListQueryBase TrackListQueryBase; @@ -89,6 +91,7 @@ namespace musik { void Clear(); size_t TrackCount(); size_t EntryCount(); + void SetTrackNumType(TrackNumType type); void Requery(std::shared_ptr query); @@ -162,6 +165,7 @@ namespace musik { RowFormatter formatter; RowDecorator decorator; std::chrono::milliseconds lastChanged; + TrackNumType trackNumType; }; } }