SVG icons now preloaded when the program starts

This commit is contained in:
Yousef Khadadeh 2025-11-21 10:36:46 +03:00
parent e954e55213
commit aca539600c
4 changed files with 34 additions and 30 deletions

View file

@ -1,7 +1,5 @@
#include "SVGIcon.hpp"
#include "Helpers/ConvertVec.hpp"
#include <modules/svg/include/SkSVGNode.h>
#include <include/core/SkStream.h>
#include <include/core/SkColorFilter.h>
#include <include/core/SkColorSpace.h>
#include <iostream>
@ -10,26 +8,8 @@ namespace GUIStuff {
void SVGIcon::update(UpdateInputData& io, const std::string& newSvgPath, bool newIsHighlighted, const std::function<void()>& 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;

View file

@ -26,14 +26,8 @@ void FileResourceDisplay::draw(SkCanvas* canvas, const DrawData& drawData, const
std::shared_ptr<GUIStuff::UpdateInputData>& io = drawData.main->toolbar.io;
auto findSVGData = io->svgData.find(FILE_ICON_PATH);
sk_sp<SkSVGDOM> 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;

View file

@ -28,6 +28,9 @@
#include <include/core/SkFontStyle.h>
#include <modules/skunicode/include/SkUnicode_icu.h>
#include <modules/svg/include/SkSVGNode.h>
#include <include/core/SkStream.h>
#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";

View file

@ -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;