Skip to content

Commit

Permalink
#5108: Use the registered icon in the file type registry to display i…
Browse files Browse the repository at this point in the history
…cons
  • Loading branch information
codereader committed Nov 19, 2020
1 parent 5b5f1d8 commit 0dbd819
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
36 changes: 34 additions & 2 deletions libs/wxutil/fsview/Populator.cpp
@@ -1,8 +1,10 @@
#include "Populator.h"

#include "iuimanager.h"
#include "ifiletypes.h"
#include "iarchive.h"
#include "os/path.h"
#include "string/case_conv.h"
#include "os/filesize.h"

#include <wx/artprov.h>
Expand Down Expand Up @@ -115,14 +117,44 @@ void Populator::Populate()
Run();
}

const wxIcon& Populator::GetIconForFile(const std::string& path)
{
auto extension = string::to_lower_copy(os::getExtension(path));
auto foundIcon = _iconsPerExtension.find(extension);

if (foundIcon != _iconsPerExtension.end())
{
return foundIcon->second;
}

// Try to find an icon in the file type registry
auto iconName = GlobalFiletypes().getIconForExtension(extension);

if (!iconName.empty())
{
wxIcon customIcon;
customIcon.CopyFromBitmap(wxArtProvider::GetBitmap(GlobalUIManager().ArtIdPrefix() + iconName));

foundIcon = _iconsPerExtension.emplace(extension, customIcon).first;
}
else
{
// No special icon, use the default file icon
foundIcon = _iconsPerExtension.emplace(extension, _fileIcon).first;
}

return foundIcon->second;
}

void Populator::visit(wxutil::TreeModel& /* store */, wxutil::TreeModel::Row& row,
const std::string& path, bool isExplicit)
{
if (TestDestroy()) return;

auto filename = path.substr(path.rfind("/") + 1);
// Get the display path, everything after rightmost slash
row[_columns.filename] = wxVariant(wxDataViewIconText(path.substr(path.rfind("/") + 1),
isExplicit ? _fileIcon : _folderIcon));
row[_columns.filename] = wxVariant(wxDataViewIconText(filename,
isExplicit ? GetIconForFile(filename) : _folderIcon));
row[_columns.vfspath] = _basePath + path;
row[_columns.isFolder] = !isExplicit;

Expand Down
2 changes: 2 additions & 0 deletions libs/wxutil/fsview/Populator.h
Expand Up @@ -45,6 +45,7 @@ class Populator :

wxIcon _fileIcon;
wxIcon _folderIcon;
std::map<std::string, wxIcon> _iconsPerExtension;

std::string _basePath;

Expand Down Expand Up @@ -74,6 +75,7 @@ class Populator :

void visitFile(const vfs::FileInfo& fileInfo);
void SearchForFilesMatchingExtension(const std::string& extension);
const wxIcon& GetIconForFile(const std::string& path);
};

}
Expand Down

0 comments on commit 0dbd819

Please sign in to comment.