diff --git a/CMakeLists.txt b/CMakeLists.txt index 974c2d5..a3d68b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,7 +59,7 @@ set(CORE_SOURCES set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) add_library(clrsync_core SHARED ${CORE_SOURCES}) target_include_directories(clrsync_core PUBLIC src SYSTEM lib) -target_compile_definitions(clrsync_core PRIVATE +target_compile_definitions(clrsync_core PUBLIC CLRSYNC_DATADIR=\"${CMAKE_INSTALL_FULL_DATADIR}/clrsync\" ) diff --git a/src/core/config/config.cpp b/src/core/config/config.cpp index 684dbe6..4058553 100644 --- a/src/core/config/config.cpp +++ b/src/core/config/config.cpp @@ -1,4 +1,5 @@ #include "config.hpp" +#include "core/utils.hpp" #include #include @@ -23,35 +24,34 @@ void config::initialize(std::unique_ptr file) std::filesystem::path config::get_user_config_dir() { + auto home = expand_user("~"); #ifdef _WIN32 - if (const char *appdata = std::getenv("APPDATA")) - return std::filesystem::path(appdata) / "clrsync"; - else - return std::filesystem::path("C:/clrsync"); + return home + "\\.config\\clrsync"; + #else - if (const char *xdg = std::getenv("XDG_CONFIG_HOME")) - return std::filesystem::path(xdg) / "clrsync"; - else if (const char *home = std::getenv("HOME")) - return std::filesystem::path(home) / ".config/clrsync"; - else - return std::filesystem::path("/tmp/clrsync"); + return home + "/.config/clrsync"; #endif } void config::copy_default_configs() { std::filesystem::path user_config = get_user_config_dir(); + std::filesystem::path default_dir = CLRSYNC_DATADIR; if (!std::filesystem::exists(user_config)) { std::filesystem::create_directories(user_config); - std::filesystem::path default_dir = CLRSYNC_DATADIR; std::filesystem::copy(default_dir / "config.toml", user_config / "config.toml"); std::filesystem::copy(default_dir / "templates", user_config / "templates", std::filesystem::copy_options::recursive); std::filesystem::copy(default_dir / "palettes", user_config / "palettes", std::filesystem::copy_options::recursive); + return; + } + if (!std::filesystem::exists(user_config / "config.toml")) + { + std::filesystem::copy(default_dir / "config.toml", user_config / "config.toml"); } } diff --git a/src/core/palette/palette_manager.hpp b/src/core/palette/palette_manager.hpp index 78ef495..5e4a532 100644 --- a/src/core/palette/palette_manager.hpp +++ b/src/core/palette/palette_manager.hpp @@ -2,6 +2,7 @@ #define CLRSYNC_CORE_PALETTE_PALETTE_MANAGER_HPP #include "core/utils.hpp" +#include #include #include @@ -19,6 +20,11 @@ template class palette_manager void load_palettes_from_directory(const std::string &directory_path) { auto directory_path_expanded = expand_user(directory_path); + if (!std::filesystem::exists(directory_path_expanded)) + { + std::cerr << "Palettes directory does not exist\n" ; + return; + } for (const auto &entry : std::filesystem::directory_iterator(directory_path_expanded)) { if (entry.is_regular_file()) diff --git a/src/core/theme/theme_template.cpp b/src/core/theme/theme_template.cpp index 1991076..7fb1f8c 100644 --- a/src/core/theme/theme_template.cpp +++ b/src/core/theme/theme_template.cpp @@ -1,8 +1,8 @@ #include "theme_template.hpp" #include "core/utils.hpp" #include -#include #include +#include namespace clrsync::core { @@ -45,6 +45,11 @@ void theme_template::set_output_path(const std::string &path) void theme_template::load_template() { + if (!std::filesystem::exists(m_template_path)) + { + std::cerr << "Template file '" << m_template_path << "' is missing\n"; + return; + } std::ifstream input(m_template_path, std::ios::binary); if (!input) throw std::runtime_error("Failed to open template file: " + m_template_path); diff --git a/src/core/utils.cpp b/src/core/utils.cpp index 3608d6f..257271c 100644 --- a/src/core/utils.cpp +++ b/src/core/utils.cpp @@ -13,16 +13,12 @@ void print_color_keys() std::string get_default_config_path() { + auto home = expand_user("~"); #ifdef _WIN32 - const char *appdata = std::getenv("APPDATA"); // "C:\Users\\AppData\Roaming" - if (!appdata) - throw std::runtime_error("APPDATA environment variable not set"); - return std::string(appdata) + "\\clrsync\\config.toml"; + return home + "\\.config\\clrsync\\config.toml"; + #else - const char *home = std::getenv("HOME"); - if (!home) - throw std::runtime_error("HOME environment variable not set"); - return std::string(home) + "/.config/clrsync/config.toml"; + return home + "/.config/clrsync/config.toml"; #endif } diff --git a/src/gui/color_scheme_editor.cpp b/src/gui/color_scheme_editor.cpp index b1f4442..c15ea90 100644 --- a/src/gui/color_scheme_editor.cpp +++ b/src/gui/color_scheme_editor.cpp @@ -172,6 +172,7 @@ void color_scheme_editor::render_controls() apply_palette_to_imgui(); apply_palette_to_editor(); notify_palette_changed(); + m_controller.select_palette(new_palette_name_buf); new_palette_name_buf[0] = 0; } ImGui::CloseCurrentPopup(); @@ -328,6 +329,12 @@ void color_scheme_editor::render_preview_content() { const auto ¤t = m_controller.current_palette(); + if (current.colors().empty()) + { + ImGui::TextColored(ImVec4(1.0f, 0.3f, 0.3f, 1.0f), "Current palette is empty"); + return; + } + auto get_color = [&](const std::string &key) -> ImVec4 { auto it = current.colors().find(key); if (it != current.colors().end()) @@ -389,6 +396,8 @@ void color_scheme_editor::render_preview_content() void color_scheme_editor::apply_palette_to_editor() { const auto ¤t = m_controller.current_palette(); + if (current.colors().empty()) + return; auto get_color_u32 = [&](const std::string &key, const std::string &fallback = "") -> uint32_t { auto it = current.colors().find(key); @@ -446,6 +455,9 @@ void color_scheme_editor::apply_palette_to_editor() void color_scheme_editor::apply_palette_to_imgui() const { const auto ¤t = m_controller.current_palette(); + + if (current.colors().empty()) + return; auto getColor = [&](const std::string &key, const std::string &fallback = "") -> ImVec4 { auto it = current.colors().find(key); diff --git a/src/gui/palette_controller.cpp b/src/gui/palette_controller.cpp index 7f9b751..db55a9f 100644 --- a/src/gui/palette_controller.cpp +++ b/src/gui/palette_controller.cpp @@ -28,14 +28,9 @@ void palette_controller::select_palette(const std::string& name) void palette_controller::create_palette(const std::string& name) { - clrsync::core::palette new_palette = m_current_palette; + clrsync::core::palette new_palette = m_palette_manager.load_palette_from_file(std::string(CLRSYNC_DATADIR) + "/palettes/cursed.toml"); new_palette.set_name(name); - - auto colors = m_current_palette.colors(); - for (auto& pair : colors) { - new_palette.set_color(pair.first, pair.second); - } - + auto dir = clrsync::core::config::instance().palettes_path(); m_palette_manager.save_palette_to_file(new_palette, dir); diff --git a/src/gui/template_editor.cpp b/src/gui/template_editor.cpp index bf5004e..68db00b 100644 --- a/src/gui/template_editor.cpp +++ b/src/gui/template_editor.cpp @@ -33,6 +33,8 @@ template_editor::template_editor() : m_template_name("new_template") void template_editor::apply_current_palette(const clrsync::core::palette &pal) { auto colors = pal.colors(); + if (colors.empty()) + return; auto get_color_u32 = [&](const std::string &key, const std::string &fallback = "") -> uint32_t { auto it = colors.find(key); if (it == colors.end() && !fallback.empty())