From 5dbb2c6abc42030c2001e25bf2a05f86244954e3 Mon Sep 17 00:00:00 2001 From: casey langen Date: Mon, 12 Feb 2018 09:44:16 -0800 Subject: [PATCH] A handful of additional small bugfixes for directory browsing: 1. Update the tracklist on indexer progress 2. Optimized DirectoryAdapter::HasSubdirectories() 3. Added directory name to tracklist title 4. Hide direcotry chooser for root paths without any subdirs --- src/musikcube/app/layout/DirectoryLayout.cpp | 44 ++++++++++++++++---- src/musikcube/app/layout/DirectoryLayout.h | 6 ++- src/musikcube/app/model/DirectoryAdapter.cpp | 36 +++++++++++++--- src/musikcube/app/model/DirectoryAdapter.h | 1 + src/musikcube/data/locales/en_US.json | 1 + 5 files changed, 74 insertions(+), 14 deletions(-) diff --git a/src/musikcube/app/layout/DirectoryLayout.cpp b/src/musikcube/app/layout/DirectoryLayout.cpp index 6c4ef5f72..425d092fa 100644 --- a/src/musikcube/app/layout/DirectoryLayout.cpp +++ b/src/musikcube/app/layout/DirectoryLayout.cpp @@ -64,8 +64,11 @@ DirectoryLayout::DirectoryLayout( : LayoutBase() , playback(playback) , library(library) -, queryHash(0) { +, queryHash(0) +, hasSubdirectories(true) { this->InitializeWindows(); + this->library->Indexer()->Progress.connect(this, &DirectoryLayout::OnIndexerProgress); + this->library->Indexer()->Finished.connect(this, &DirectoryLayout::OnIndexerProgress); } DirectoryLayout::~DirectoryLayout() { @@ -74,11 +77,20 @@ DirectoryLayout::~DirectoryLayout() { void DirectoryLayout::OnLayout() { size_t cx = this->GetWidth(), cy = this->GetHeight(); size_t x = 0, y = 0; - size_t directoryWidth = std::min(MAX_CATEGORY_WIDTH, cx / 4); - this->directoryList->MoveAndResize(x, y, directoryWidth, cy); - this->trackList->MoveAndResize(x + directoryWidth, y, cx - directoryWidth, cy); - this->directoryList->SetFocusOrder(0); - this->trackList->SetFocusOrder(1); + + if (this->hasSubdirectories) { + size_t directoryWidth = std::min(MAX_CATEGORY_WIDTH, cx / 4); + this->directoryList->Show(); + this->directoryList->MoveAndResize(x, y, directoryWidth, cy); + this->trackList->MoveAndResize(x + directoryWidth, y, cx - directoryWidth, cy); + this->directoryList->SetFocusOrder(0); + this->trackList->SetFocusOrder(1); + } + else { + this->directoryList->Hide(); + this->trackList->MoveAndResize(x, y, cx, cy); + this->trackList->SetFocusOrder(0); + } } void DirectoryLayout::InitializeWindows() { @@ -114,6 +126,7 @@ void DirectoryLayout::SetDirectory(const std::string& directory) { this->rootDirectory = directory; this->adapter->SetRootDirectory(directory); this->directoryList->SetSelectedIndex(0); + this->UpdateTitle(); this->Refresh(true); } @@ -124,6 +137,10 @@ void DirectoryLayout::OnVisibilityChanged(bool visible) { } } +void DirectoryLayout::OnIndexerProgress(int count) { + this->Requery(true); +} + void DirectoryLayout::RequeryTrackList(ListWindow *view) { size_t selected = this->directoryList->GetSelectedIndex(); std::string fullPath = ""; @@ -156,15 +173,28 @@ void DirectoryLayout::RequeryTrackList(ListWindow *view) { void DirectoryLayout::OnDirectoryChanged( ListWindow *view, size_t newIndex, size_t oldIndex) { + this->UpdateTitle(); this->RequeryTrackList(view); } +void DirectoryLayout::UpdateTitle() { + std::string title = + _TSTR("browse_title_directory_tracks") + + this->adapter->GetCurrentPath(); + + this->trackList->SetFrameTitle(title); +} + void DirectoryLayout::Refresh(bool requery) { this->adapter->Refresh(); + this->hasSubdirectories = this->adapter->HasSubDirectories(); if (requery) { this->Requery(); } } -void DirectoryLayout::Requery() { +void DirectoryLayout::Requery(bool invalidate) { + if (invalidate) { + this->queryHash = 0; + } this->RequeryTrackList(this->directoryList.get()); } diff --git a/src/musikcube/app/layout/DirectoryLayout.h b/src/musikcube/app/layout/DirectoryLayout.h index 0b5090d95..f2689e6c7 100644 --- a/src/musikcube/app/layout/DirectoryLayout.h +++ b/src/musikcube/app/layout/DirectoryLayout.h @@ -73,8 +73,9 @@ namespace musik { private: void InitializeWindows(); void Refresh(bool requery = false); - void Requery(); + void Requery(bool invalidate = false); void RequeryTrackList(cursespp::ListWindow *view); + void UpdateTitle(); bool IsParentSelected(); bool IsParentRoot(); @@ -89,6 +90,8 @@ namespace musik { size_t newIndex, size_t oldIndex); + void OnIndexerProgress(int count); + musik::core::audio::PlaybackService& playback; musik::core::ILibraryPtr library; std::string rootDirectory; @@ -96,6 +99,7 @@ namespace musik { std::shared_ptr directoryList; std::shared_ptr trackList; size_t queryHash; + bool hasSubdirectories; }; } } diff --git a/src/musikcube/app/model/DirectoryAdapter.cpp b/src/musikcube/app/model/DirectoryAdapter.cpp index 4ebaa6de5..b60e8ad96 100755 --- a/src/musikcube/app/model/DirectoryAdapter.cpp +++ b/src/musikcube/app/model/DirectoryAdapter.cpp @@ -46,7 +46,7 @@ using namespace cursespp; using namespace boost::filesystem; #ifdef WIN32 -void buildDriveList(std::vector& target) { +static void buildDriveList(std::vector& target) { target.clear(); static char buffer[4096]; DWORD result = ::GetLogicalDriveStringsA(4096, buffer); @@ -60,7 +60,30 @@ void buildDriveList(std::vector& target) { } #endif -void buildDirectoryList( +static bool hasSubdirectories( + boost::filesystem::path p, bool showDotfiles) +{ + try { + directory_iterator end; + directory_iterator file(p); + + while (file != end) { + if (is_directory(file->status())) { + if (showDotfiles || file->path().leaf().string()[0] != '.') { + return true; + } + } + ++file; + } + } + catch (...) { + } + + return false; +} + + +static void buildDirectoryList( const path& p, std::vector& target, bool showDotfiles) @@ -82,7 +105,6 @@ void buildDirectoryList( } } catch (...) { - /* todo: log maybe? */ } std::sort(target.begin(), target.end(), std::locale("")); @@ -228,9 +250,11 @@ bool DirectoryAdapter::HasSubDirectories(size_t index) { } index = hasParent ? index - 1 : index; - std::vector subdirs; - buildDirectoryList(this->dir / this->subdirs[index], subdirs, this->showDotfiles); - return subdirs.size() > 0; + return hasSubdirectories(this->dir / this->subdirs[index], this->showDotfiles); +} + +bool DirectoryAdapter::HasSubDirectories() { + return hasSubdirectories(this->dir, this->showDotfiles); } IScrollAdapter::EntryPtr DirectoryAdapter::GetEntry(cursespp::ScrollableWindow* window, size_t index) { diff --git a/src/musikcube/app/model/DirectoryAdapter.h b/src/musikcube/app/model/DirectoryAdapter.h index 581e45f5e..bd1f36ae4 100755 --- a/src/musikcube/app/model/DirectoryAdapter.h +++ b/src/musikcube/app/model/DirectoryAdapter.h @@ -58,6 +58,7 @@ namespace musik { std::string GetFullPathAt(size_t index); std::string GetLeafAt(size_t index); bool HasSubDirectories(size_t index); + bool HasSubDirectories(); void SetRootDirectory(const std::string& fullPath); void SetAllowEscapeRoot(bool allowEscape); size_t IndexOf(const std::string& leaf); diff --git a/src/musikcube/data/locales/en_US.json b/src/musikcube/data/locales/en_US.json index db9df5a85..784b94d1f 100644 --- a/src/musikcube/data/locales/en_US.json +++ b/src/musikcube/data/locales/en_US.json @@ -22,6 +22,7 @@ "browse_title_directory": "directory", "browse_playlist_modified": "playlist modified. press '%s' to save.", "browse_categories_title": "select category", + "browse_title_directory_tracks": "tracks in ", "browse_pick_path_overlay_title": "select root directory", "browse_pick_path_last_directory": "last directory",