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.
This commit is contained in:
scorpion-26 2023-02-09 16:13:40 +01:00
parent 9e48830dc8
commit 883037029f
2 changed files with 7 additions and 55 deletions

View file

@ -14,7 +14,8 @@ headers = [
'src/PulseAudio.h', 'src/PulseAudio.h',
'src/Widget.h', 'src/Widget.h',
'src/Window.h', 'src/Window.h',
'src/Config.h' 'src/Config.h',
'src/CSS.h'
] ]
if get_option('WithHyprland') if get_option('WithHyprland')
@ -46,7 +47,8 @@ libgBar = library('gBar',
'src/AudioFlyin.cpp', 'src/AudioFlyin.cpp',
'src/BluetoothDevices.cpp', 'src/BluetoothDevices.cpp',
'src/Plugin.cpp', 'src/Plugin.cpp',
'src/Config.cpp' 'src/Config.cpp',
'src/CSS.cpp'
], ],
dependencies: [gtk, gtk_layer_shell, pulse], dependencies: [gtk, gtk_layer_shell, pulse],
install: true) install: true)

View file

@ -1,5 +1,6 @@
#include "Window.h" #include "Window.h"
#include "Common.h" #include "Common.h"
#include "CSS.h"
#include <tuple> #include <tuple>
#include <fstream> #include <fstream>
@ -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<CSSDir, 4> 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) void Window::Run(int argc, char** argv)
{ {
gtk_init(&argc, &argv); gtk_init(&argc, &argv);
// Style // Style
GtkCssProvider* cssprovider = gtk_css_provider_new(); CSS::Load();
LoadCSS(cssprovider);
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); m_Window = (GtkWindow*)gtk_window_new(GTK_WINDOW_TOPLEVEL);