diff --git a/src/CSS.cpp b/src/CSS.cpp new file mode 100644 index 0000000..e405475 --- /dev/null +++ b/src/CSS.cpp @@ -0,0 +1,68 @@ +#include "CSS.h" +#include "Common.h" + +#include +#include +#include + +namespace CSS +{ + static GtkCssProvider* sProvider; + + void Load() + { + sProvider = gtk_css_provider_new(); + + struct CSSDir + { + std::string xdgEnv; + std::string fallbackPath; + std::string relPath; + }; + std::string home = getenv("HOME"); + std::array locations = { + CSSDir{"XDG_CONFIG_HOME", home + "/.config", "/gBar/style.css"}, // Local config + CSSDir{"XDG_DATA_HOME", home + "/.local/share", "/gBar/style.css"}, // local user install + CSSDir{"", "/usr/local/share", "/gBar/style.css"}, // local all install + CSSDir{"", "/usr/share", "/gBar/style.css"}, // package manager all install + }; + + GError* err = nullptr; + for (auto& dir : locations) + { + const char* xdgConfig = dir.xdgEnv.size() ? getenv(dir.xdgEnv.c_str()) : nullptr; + std::string file; + if (xdgConfig) + { + file = (std::string(xdgConfig) + dir.relPath).c_str(); + } + else + { + file = (dir.fallbackPath + dir.relPath).c_str(); + } + if (!std::ifstream(file).is_open()) + { + LOG("Info: No CSS found in " << dir.fallbackPath); + continue; + } + + gtk_css_provider_load_from_path(sProvider, file.c_str(), &err); + + if (!err) + { + LOG("CSS found and loaded successfully!"); + return; + } + LOG("Warning: Failed loading config for " << dir.fallbackPath << ", trying next one!"); + // Log any errors + LOG(err->message); + g_error_free(err); + } + ASSERT(false, "No CSS file found!"); + } + + GtkCssProvider* GetProvider() + { + return sProvider; + } +} diff --git a/src/CSS.h b/src/CSS.h new file mode 100644 index 0000000..db9cc05 --- /dev/null +++ b/src/CSS.h @@ -0,0 +1,8 @@ +#pragma once +#include + +namespace CSS +{ + void Load(); + GtkCssProvider* GetProvider(); +}