From d1b062bad937c71ac7f089f1c997c6972c484696 Mon Sep 17 00:00:00 2001 From: scorpion-26 <58082714+scorpion-26@users.noreply.github.com> Date: Sat, 28 Jan 2023 17:29:10 +0100 Subject: [PATCH] Install styling automatically. --- README.md | 18 ++++++++----- meson.build | 6 +++++ src/Window.cpp | 70 ++++++++++++++++++++++++++++++++++++++------------ src/Window.h | 2 ++ 4 files changed, 74 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index fe5160a..7bdb1f5 100644 --- a/README.md +++ b/README.md @@ -29,10 +29,6 @@ My personal blazingly fast and efficient status bar + widgets, in case anyone fi ``` ninja -C build && sudo ninja -C build install ``` -5. Copy css styling into your config directory($XDG_CONFIG_HOME). This will most likely be ~/.config - ``` - mkdir ~/.config/gBar && cp css/* ~/.config/gBar/ - ``` ## Running gBar *Open bar on monitor 0* @@ -119,9 +115,19 @@ And lastly: Implementing it myself is fun and a great excuse to learn something The colors are from the Dracula theme: https://draculatheme.com ### I want to customize the colors -If you have SASS installed: Edit ~/.config/gBar/style.scss and regenerate style.css with it +First, find where the data is located for gBar. Possible locations: + - /usr/share/gBar + - /usr/local/share/gBar + - ~/.local/share/gBar + - If you cloned this repository locally: Inside css/ + + Copy the scss and css files from within the data direction into ~/.config/gBar. e.g.: + ``` + mkdir ~/.config/gBar/ + cp /usr/local/share/gBar/* ~/.config/gBar/ + ``` + This will override the default behaviour. If you have sass installed, you can modify the scss file and then regenerate the css file accordingly. Else modify the css file directly. -Else: Edit ~/.config/gBar/style.css directly! ### The Audio widget doesn't open Delete /tmp/gBar__audio. diff --git a/meson.build b/meson.build index 4db0422..9e9cf78 100644 --- a/meson.build +++ b/meson.build @@ -60,3 +60,9 @@ install_headers( headers, subdir: 'gBar' ) + +install_data( + ['css/style.css', + 'css/style.scss'], + install_dir: get_option('datadir') / 'gBar' +) diff --git a/src/Window.cpp b/src/Window.cpp index afbb635..c23dfb8 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -1,6 +1,9 @@ #include "Window.h" #include "Common.h" +#include +#include + #include #include @@ -15,28 +18,63 @@ 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(); - GError* err = nullptr; - const char* xdgConfig = getenv("XDG_CONFIG_HOME"); - if (xdgConfig) - { - gtk_css_provider_load_from_path(cssprovider, (std::string(xdgConfig) + "/gBar/style.css").c_str(), &err); - } - else - { - const char* home = getenv("HOME"); - gtk_css_provider_load_from_path(cssprovider, (std::string(home) + "/.config/gBar/style.css").c_str(), &err); - } - if (err) - { - printf("%s\n", err->message); - g_error_free(err); - } + LoadCSS(cssprovider); gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), (GtkStyleProvider*)cssprovider, GTK_STYLE_PROVIDER_PRIORITY_USER); diff --git a/src/Window.h b/src/Window.h index f046e42..e4b13f4 100644 --- a/src/Window.h +++ b/src/Window.h @@ -32,6 +32,8 @@ public: private: void UpdateMargin(); + void LoadCSS(GtkCssProvider* provider); + GtkWindow* m_Window; GtkApplication* m_App = nullptr;