Skip to content

Commit

Permalink
Include user palettes in Presets popup. Fixes #377.
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Manga authored and Felipe Manga committed Dec 15, 2023
1 parent 394c857 commit c0bef8f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
12 changes: 6 additions & 6 deletions src/app/res/palettes_loader_delegate.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite | Copyright (C) 2001-2015 David Capello
// LibreSprite | Copyright (C) 2021 LibreSprite contributors
// LibreSprite | Copyright (C) 2023 LibreSprite contributors
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
Expand All @@ -26,17 +26,17 @@ using namespace app;

class PalettesLoaderDelegate : public ResourcesLoader {
public:
std::string resourcesLocation() const override {
std::string path;
std::vector<std::string> resourcesLocation() const override {
std::vector<std::string> paths;
ResourceFinder rf;
rf.includeDataDir("palettes");
rf.includeUserDir("palettes");
while (rf.next()) {
if (base::is_directory(rf.filename())) {
path = rf.filename();
break;
paths.push_back(base::fix_path_separators(rf.filename()));
}
}
return base::fix_path_separators(path);
return paths;
}

Resource loadResource(const std::string& filename) override {
Expand Down
36 changes: 22 additions & 14 deletions src/app/res/resources_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,31 @@

namespace app {
void ResourcesLoader::load(Callback&& callback) {
std::string path = resourcesLocation();
LOG("Loading resources from %s...\n", path.c_str());
if (path.empty()) {
callback({});
return;
}

auto fs = FileSystemModule::instance();
LockFS lock{fs};
IFileItem* item = fs->getFileItemFromPath(path);
if (!item)
return;

std::size_t pending{};
std::deque<std::string> files;
for (auto child : item->children()) {
if (!child->isFolder())
files.push_back(child->fileName());

for (auto& path : resourcesLocation()) {
LOG("Loading resources from %s...\n", path.c_str());
if (path.empty())
return;

IFileItem* item = fs->getFileItemFromPath(path);
if (!item)
continue;

for (auto child : item->children()) {
if (child->isFolder())
continue;
files.push_back(child->fileName());
pending++;
}
}

if (!pending) {
callback({});
return;
}

task = TaskManager::instance().addTask<Resource>(
Expand Down
2 changes: 1 addition & 1 deletion src/app/res/resources_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace app {
using Callback = std::function<void(Resource)>;
void load(Callback&& callback);

virtual std::string resourcesLocation() const = 0;
virtual std::vector<std::string> resourcesLocation() const = 0;

protected:
virtual Resource loadResource(const std::string& fileName) = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/app/ui/palette_popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ void PalettePopup::onLoadPal()
void PalettePopup::onOpenFolder()
{
inject<ResourcesLoader> loader{"palette"};
launcher::open_folder(loader->resourcesLocation());
auto paths = loader->resourcesLocation();
if (!paths.empty())
launcher::open_folder(paths.back());
}

} // namespace app

0 comments on commit c0bef8f

Please sign in to comment.