From 883037029f99dd73dd4ab88a3575342c0a331bfb Mon Sep 17 00:00:00 2001 From: scorpion-26 <58082714+scorpion-26@users.noreply.github.com> Date: Thu, 9 Feb 2023 16:13:40 +0100 Subject: [PATCH] Refactor CSS out of Window.cpp It has nothing to do with *windows* and obviously doesn't belong there. Plus I don't know whether I may need static access to the provider. --- meson.build | 6 ++++-- src/Window.cpp | 56 +++----------------------------------------------- 2 files changed, 7 insertions(+), 55 deletions(-) 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);