diff --git a/meson.build b/meson.build index 54d1781..1cbea32 100644 --- a/meson.build +++ b/meson.build @@ -14,7 +14,8 @@ headers = [ 'src/PulseAudio.h', 'src/Widget.h', 'src/Window.h', - 'src/Config.h' + 'src/Config.h', + 'src/CSS.h' ] if get_option('WithHyprland') @@ -46,7 +47,8 @@ libgBar = library('gBar', 'src/AudioFlyin.cpp', 'src/BluetoothDevices.cpp', 'src/Plugin.cpp', - 'src/Config.cpp' + 'src/Config.cpp', + 'src/CSS.cpp' ], dependencies: [gtk, gtk_layer_shell, pulse], install: true) diff --git a/src/Window.cpp b/src/Window.cpp index c23dfb8..333c193 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -1,5 +1,6 @@ #include "Window.h" #include "Common.h" +#include "CSS.h" #include #include @@ -18,65 +19,14 @@ Window::~Window() } } -void Window::LoadCSS(GtkCssProvider* provider) -{ - 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(provider, 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!"); -} - void Window::Run(int argc, char** argv) { gtk_init(&argc, &argv); // Style - GtkCssProvider* cssprovider = gtk_css_provider_new(); - LoadCSS(cssprovider); + CSS::Load(); - gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), (GtkStyleProvider*)cssprovider, GTK_STYLE_PROVIDER_PRIORITY_USER); + gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), (GtkStyleProvider*)CSS::GetProvider(), GTK_STYLE_PROVIDER_PRIORITY_USER); m_Window = (GtkWindow*)gtk_window_new(GTK_WINDOW_TOPLEVEL);