Skip to content

Commit

Permalink
add custom image support in component selection (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
dslatt authored Oct 2, 2024
1 parent a46792b commit 1ab48f3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
1 change: 0 additions & 1 deletion source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}

brls::Logger::setLogLevel(brls::LogLevel::LOG_DEBUG);
brls::Application::createWindow("demo/title"_i18n);

brls::Application::getPlatform()->setThemeVariant(brls::ThemeVariant::DARK);
Expand Down
76 changes: 52 additions & 24 deletions source/view/icon_part_select.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

#include "view/icon_part_select.hpp"
#include "view/icon_part_select_grid.hpp"
#include "view/empty_message.hpp"
#include <vector>
#include "util/paths.hpp"

#include <filesystem>
#include <ranges>

using namespace brls::literals;
namespace fs = std::filesystem;

RecyclerCell::RecyclerCell()
Expand Down Expand Up @@ -40,7 +42,13 @@ std::string convertName(std::string name)
RecyclingGridItem *DataSource::cellForRow(RecyclingGrid *recycler, size_t index)
{
RecyclerCell *item = (RecyclerCell *)recycler->dequeueReusableCell("Cell");
item->label->setText(convertName(parts[index].name));
if (parts[index].name == "none") {
item->label->setText("app/settings/icon_cache/none"_i18n);
} else if (parts[index].name == "custom") {
item->label->setText("app/main/custom_image"_i18n);
} else{
item->label->setText(convertName(parts[index].name));
}
item->image->setImageFromFile(parts[index].icon);
return item;
}
Expand All @@ -49,34 +57,54 @@ void DataSource::onItemSelected(RecyclingGrid *recycler, size_t index)
{
brls::Logger::info("Selected {} ({})", parts[index].name, parts[index].icon);

if (parts[index].name == "none")
if (parts[index].name == "none" && parent)
{
if (parent)
onSelected("");
parent->dismiss();
return;
} else if (parts[index].name == "custom" && parent) {
auto files = std::ranges::subrange(fs::directory_iterator(fs::path(paths::BasePath)), fs::directory_iterator{}) |
std::views::filter([](const fs::directory_entry &entry) { return entry.is_regular_file() && (entry.path().extension() == ".png" || entry.path().extension() == ".jpg" || entry.path().extension() == ".jpeg"); }) |
std::views::transform([](const fs::directory_entry &entry) { return entry.path().string(); }) |
std::ranges::to<std::vector<std::string>>();

for (auto &file : files)
{
onSelected("");
parent->dismiss();
return;
brls::Logger::debug("{}", file);
}
}

auto files = std::ranges::subrange(fs::directory_iterator(fs::path(paths::IconCachePath) / parts[index].name / subcategory), fs::directory_iterator{}) |
std::views::filter([](const fs::directory_entry &entry) { return entry.is_regular_file() && entry.path().extension() == ".png"; }) |
std::views::transform([](const fs::directory_entry &entry) { return entry.path().string(); }) |
std::ranges::to<std::vector<std::string>>();

for (auto &file : files)
{
brls::Logger::debug("{}", file);
}
brls::Logger::debug("total {}", files.size());
brls::Logger::debug("total {}", files.size());

if (files.empty()) {
recycler->present(new EmptyMessage(fmt::format(fmt::runtime("app/errors/nothing_images"_i18n), paths::BasePath)));
} else {
recycler->present(new grid::IconPartSelectGrid(files, "app/main/available_images"_i18n, state, [this](std::string icon)
{
onSelected(icon);
if (parent)
{
parent->dismiss();
} }, onFocused));
}
} else {
auto files = std::ranges::subrange(fs::directory_iterator(fs::path(paths::IconCachePath) / parts[index].name / subcategory), fs::directory_iterator{}) |
std::views::filter([](const fs::directory_entry &entry) { return entry.is_regular_file() && entry.path().extension() == ".png"; }) |
std::views::transform([](const fs::directory_entry &entry) { return entry.path().string(); }) |
std::ranges::to<std::vector<std::string>>();

recycler->present(new grid::IconPartSelectGrid(files, convertName(parts[index].name), state, [this](std::string icon)
{
onSelected(icon);
if (parent)
for (auto &file : files)
{
parent->dismiss();
} }, onFocused));
brls::Logger::debug("{}", file);
}
brls::Logger::debug("total {}", files.size());

recycler->present(new grid::IconPartSelectGrid(files, convertName(parts[index].name), state, [this](std::string icon)
{
onSelected(icon);
if (parent)
{
parent->dismiss();
} }, onFocused));
}
}

size_t DataSource::getItemCount()
Expand Down
3 changes: 2 additions & 1 deletion source/view/main_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ std::expected<std::vector<CategoryPart>, std::string> getCategories(std::string_
if (res.empty()) {
return std::unexpected("No categories found");
} else {
// this is slow/stupid but it doesn't matter
res.insert(res.begin(), CategoryPart{"custom", ""});
res.insert(res.begin(), CategoryPart{"none", ""});
return res;
}
Expand Down Expand Up @@ -226,7 +228,6 @@ MainView::MainView()
brls::sync([]()
{ brls::Logger::info("{} the debug layer", true ? "Open" : "Close"); });

brls::Application::enableDebuggingView(true);
handleUserSelection();

}
Expand Down

0 comments on commit 1ab48f3

Please sign in to comment.