From aca539600c134659f62e98e09f450ec418bb9841 Mon Sep 17 00:00:00 2001 From: Yousef Khadadeh Date: Fri, 21 Nov 2025 10:36:46 +0300 Subject: [PATCH] SVG icons now preloaded when the program starts --- src/GUIStuff/Elements/SVGIcon.cpp | 24 ++---------------- src/ResourceDisplay/FileResourceDisplay.cpp | 10 ++------ src/Toolbar.cpp | 28 +++++++++++++++++++++ src/Toolbar.hpp | 2 ++ 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/GUIStuff/Elements/SVGIcon.cpp b/src/GUIStuff/Elements/SVGIcon.cpp index 26c467c..73fb3b0 100644 --- a/src/GUIStuff/Elements/SVGIcon.cpp +++ b/src/GUIStuff/Elements/SVGIcon.cpp @@ -1,7 +1,5 @@ #include "SVGIcon.hpp" #include "Helpers/ConvertVec.hpp" -#include -#include #include #include #include @@ -10,26 +8,8 @@ namespace GUIStuff { void SVGIcon::update(UpdateInputData& io, const std::string& newSvgPath, bool newIsHighlighted, const std::function& elemUpdate) { auto findSVGData = io.svgData.find(newSvgPath); - if(findSVGData == io.svgData.end()) { - auto stream = SkStream::MakeFromFile(newSvgPath.c_str()); - if(!stream) { - std::cout << "[SVGIcon::update] Could not open file " << newSvgPath << std::endl; - io.svgData[newSvgPath] = nullptr; - svgDom = nullptr; - } - auto newSvgDom = SkSVGDOM::Builder().make(*stream); - if(!newSvgDom) { - io.svgData[newSvgPath] = nullptr; - svgDom = nullptr; - std::cout << "[SVGIcon::update] Could not parse SVG " << newSvgPath << std::endl; - } - else { - io.svgData[newSvgPath] = newSvgDom; - svgDom = newSvgDom; - if(svgDom->containerSize().width() == 0 || svgDom->containerSize().height() == 0) - svgDom->setContainerSize({1000, 1000}); - } - } + if(findSVGData == io.svgData.end()) + throw std::runtime_error("[SVGIcon::update] Could not load icon " + newSvgPath); else svgDom = findSVGData->second; diff --git a/src/ResourceDisplay/FileResourceDisplay.cpp b/src/ResourceDisplay/FileResourceDisplay.cpp index 1aeb2a9..d69cb98 100644 --- a/src/ResourceDisplay/FileResourceDisplay.cpp +++ b/src/ResourceDisplay/FileResourceDisplay.cpp @@ -26,14 +26,8 @@ void FileResourceDisplay::draw(SkCanvas* canvas, const DrawData& drawData, const std::shared_ptr& io = drawData.main->toolbar.io; auto findSVGData = io->svgData.find(FILE_ICON_PATH); sk_sp svgDom; - if(findSVGData == io->svgData.end()) { - auto stream = SkStream::MakeFromFile(FILE_ICON_PATH.c_str()); - if(!stream) - throw std::runtime_error("[FileResourceDisplay::draw] Could not open file " + FILE_ICON_PATH); - svgDom = SkSVGDOM::Builder().make(*stream); - if(!svgDom) - throw std::runtime_error("[SVGIcon::update] Could not parse SVG " + FILE_ICON_PATH); - } + if(findSVGData == io->svgData.end()) + throw std::runtime_error("[FileResourceDisplay::draw] Could not load icon " + FILE_ICON_PATH); else svgDom = findSVGData->second; diff --git a/src/Toolbar.cpp b/src/Toolbar.cpp index 96b3c3d..2a355eb 100644 --- a/src/Toolbar.cpp +++ b/src/Toolbar.cpp @@ -28,6 +28,9 @@ #include #include +#include +#include + #define UPDATE_DOWNLOAD_URL "https://infinipaint.com/download.html" #define UPDATE_NOTIFICATION_URL "https://infinipaint.com/updateNotificationVersion.txt" @@ -43,11 +46,36 @@ Toolbar::Toolbar(MainProgram& initMain): { io->textTypeface = main.fonts->map["Roboto"]; io->fonts = main.fonts; + + // NOTE: On windows, when the native file picker is open, any call to MakeFromFile fails + // So, it's better to load the icons at the beginning of the program so that the icon loading doesn't fail later + load_icons_at("data/icons"); load_default_palette(); load_default_theme(); } +void Toolbar::load_icons_at(const std::filesystem::path& pathToLoad) { + for(auto& iconDirEntry : std::filesystem::recursive_directory_iterator(pathToLoad)) { + if(iconDirEntry.is_regular_file()) { + std::string iconRelativePath = iconDirEntry.path().relative_path().string(); + auto stream = SkStream::MakeFromFile(iconRelativePath.c_str()); + if(!stream) + throw std::runtime_error("[Toolbar::Toolbar] Could not open file " + iconRelativePath); + auto svgDom = SkSVGDOM::Builder().make(*stream); + if(!svgDom) + throw std::runtime_error("[Toolbar::Toolbar] Could not parse SVG " + iconRelativePath); + else { + if(svgDom->containerSize().width() == 0 || svgDom->containerSize().height() == 0) + svgDom->setContainerSize({1000, 1000}); + io->svgData[iconRelativePath] = svgDom; + } + } + else if(iconDirEntry.is_directory()) + load_icons_at(iconDirEntry.path()); + } +} + void Toolbar::load_default_theme() { io->theme = GUIStuff::get_default_dark_mode(); themeData.themeCurrentlyLoaded = "Default"; diff --git a/src/Toolbar.hpp b/src/Toolbar.hpp index 058b9e3..ba9361c 100644 --- a/src/Toolbar.hpp +++ b/src/Toolbar.hpp @@ -232,6 +232,8 @@ class Toolbar { void start_gui(); void end_gui(); + void load_icons_at(const std::filesystem::path& pathToLoad); + std::string serverToConnectTo; std::string serverLocalID;