Install styling automatically.

This commit is contained in:
scorpion-26 2023-01-28 17:29:10 +01:00
parent 785dc00c0f
commit d1b062bad9
4 changed files with 74 additions and 22 deletions

View file

@ -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 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 ## Running gBar
*Open bar on monitor 0* *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 The colors are from the Dracula theme: https://draculatheme.com
### I want to customize the colors ### 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 ### The Audio widget doesn't open
Delete /tmp/gBar__audio. Delete /tmp/gBar__audio.

View file

@ -60,3 +60,9 @@ install_headers(
headers, headers,
subdir: 'gBar' subdir: 'gBar'
) )
install_data(
['css/style.css',
'css/style.scss'],
install_dir: get_option('datadir') / 'gBar'
)

View file

@ -1,6 +1,9 @@
#include "Window.h" #include "Window.h"
#include "Common.h" #include "Common.h"
#include <tuple>
#include <fstream>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <gtk-layer-shell.h> #include <gtk-layer-shell.h>
@ -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<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(); GtkCssProvider* cssprovider = gtk_css_provider_new();
GError* err = nullptr; LoadCSS(cssprovider);
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);
}
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*)cssprovider, GTK_STYLE_PROVIDER_PRIORITY_USER);

View file

@ -32,6 +32,8 @@ public:
private: private:
void UpdateMargin(); void UpdateMargin();
void LoadCSS(GtkCssProvider* provider);
GtkWindow* m_Window; GtkWindow* m_Window;
GtkApplication* m_App = nullptr; GtkApplication* m_App = nullptr;