mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
refactor(config): remove style handling from Config
This commit is contained in:
parent
4fff2eaaa0
commit
1f7d399b8e
@ -32,6 +32,7 @@ class Client {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
Client() = default;
|
Client() = default;
|
||||||
|
const std::string getStyle(const std::string &style);
|
||||||
void bindInterfaces();
|
void bindInterfaces();
|
||||||
void handleOutput(struct waybar_output &output);
|
void handleOutput(struct waybar_output &output);
|
||||||
auto setupCss(const std::string &css_file) -> void;
|
auto setupCss(const std::string &css_file) -> void;
|
||||||
|
@ -2,17 +2,26 @@
|
|||||||
|
|
||||||
#include <json/json.h>
|
#include <json/json.h>
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#ifndef SYSCONFDIR
|
||||||
|
#define SYSCONFDIR "/etc"
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace waybar {
|
namespace waybar {
|
||||||
|
|
||||||
class Config {
|
class Config {
|
||||||
public:
|
public:
|
||||||
|
static const std::vector<std::string> CONFIG_DIRS;
|
||||||
|
|
||||||
|
/* Try to find any of provided names in the supported set of config directories */
|
||||||
|
static std::optional<std::string> findConfigPath(
|
||||||
|
const std::vector<std::string> &names, const std::vector<std::string> &dirs = CONFIG_DIRS);
|
||||||
|
|
||||||
Config() = default;
|
Config() = default;
|
||||||
|
|
||||||
void load(const std::string &config, const std::string &style);
|
void load(const std::string &config);
|
||||||
|
|
||||||
const std::string &getStyle() { return css_file_; }
|
|
||||||
|
|
||||||
Json::Value &getConfig() { return config_; }
|
Json::Value &getConfig() { return config_; }
|
||||||
|
|
||||||
@ -24,7 +33,6 @@ class Config {
|
|||||||
void mergeConfig(Json::Value &a_config_, Json::Value &b_config_);
|
void mergeConfig(Json::Value &a_config_, Json::Value &b_config_);
|
||||||
|
|
||||||
std::string config_file_;
|
std::string config_file_;
|
||||||
std::string css_file_;
|
|
||||||
|
|
||||||
Json::Value config_;
|
Json::Value config_;
|
||||||
};
|
};
|
||||||
|
@ -151,6 +151,15 @@ void waybar::Client::handleDeferredMonitorRemoval(Glib::RefPtr<Gdk::Monitor> mon
|
|||||||
outputs_.remove_if([&monitor](const auto &output) { return output.monitor == monitor; });
|
outputs_.remove_if([&monitor](const auto &output) { return output.monitor == monitor; });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::string waybar::Client::getStyle(const std::string &style) {
|
||||||
|
auto css_file = style.empty() ? Config::findConfigPath({"style.css"}) : style;
|
||||||
|
if (!css_file) {
|
||||||
|
throw std::runtime_error("Missing required resource files");
|
||||||
|
}
|
||||||
|
spdlog::info("Using CSS file {}", css_file.value());
|
||||||
|
return css_file.value();
|
||||||
|
};
|
||||||
|
|
||||||
auto waybar::Client::setupCss(const std::string &css_file) -> void {
|
auto waybar::Client::setupCss(const std::string &css_file) -> void {
|
||||||
css_provider_ = Gtk::CssProvider::create();
|
css_provider_ = Gtk::CssProvider::create();
|
||||||
style_context_ = Gtk::StyleContext::create();
|
style_context_ = Gtk::StyleContext::create();
|
||||||
@ -226,8 +235,9 @@ int waybar::Client::main(int argc, char *argv[]) {
|
|||||||
throw std::runtime_error("Bar need to run under Wayland");
|
throw std::runtime_error("Bar need to run under Wayland");
|
||||||
}
|
}
|
||||||
wl_display = gdk_wayland_display_get_wl_display(gdk_display->gobj());
|
wl_display = gdk_wayland_display_get_wl_display(gdk_display->gobj());
|
||||||
config.load(config_opt, style_opt);
|
config.load(config_opt);
|
||||||
setupCss(config.getStyle());
|
auto css_file = getStyle(style_opt);
|
||||||
|
setupCss(css_file);
|
||||||
bindInterfaces();
|
bindInterfaces();
|
||||||
gtk_app->hold();
|
gtk_app->hold();
|
||||||
gtk_app->run();
|
gtk_app->run();
|
||||||
|
@ -9,16 +9,19 @@
|
|||||||
|
|
||||||
#include "util/json.hpp"
|
#include "util/json.hpp"
|
||||||
|
|
||||||
#ifndef SYSCONFDIR
|
|
||||||
#define SYSCONFDIR "/etc"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace waybar {
|
namespace waybar {
|
||||||
|
|
||||||
const std::string getValidPath(const std::vector<std::string> &paths) {
|
const std::vector<std::string> Config::CONFIG_DIRS = {
|
||||||
wordexp_t p;
|
"$XDG_CONFIG_HOME/waybar/",
|
||||||
|
"$HOME/.config/waybar/",
|
||||||
|
"$HOME/waybar/",
|
||||||
|
"/etc/xdg/waybar/",
|
||||||
|
SYSCONFDIR "/xdg/waybar/",
|
||||||
|
"./resources/",
|
||||||
|
};
|
||||||
|
|
||||||
for (const std::string &path : paths) {
|
std::optional<std::string> tryExpandPath(const std::string &path) {
|
||||||
|
wordexp_t p;
|
||||||
if (wordexp(path.c_str(), &p, 0) == 0) {
|
if (wordexp(path.c_str(), &p, 0) == 0) {
|
||||||
if (access(*p.we_wordv, F_OK) == 0) {
|
if (access(*p.we_wordv, F_OK) == 0) {
|
||||||
std::string result = *p.we_wordv;
|
std::string result = *p.we_wordv;
|
||||||
@ -27,40 +30,30 @@ const std::string getValidPath(const std::vector<std::string> &paths) {
|
|||||||
}
|
}
|
||||||
wordfree(&p);
|
wordfree(&p);
|
||||||
}
|
}
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string getValidPath(const std::vector<std::string> &paths) {
|
||||||
|
for (const std::string &path : paths) {
|
||||||
|
if (auto res = tryExpandPath(path); res) {
|
||||||
|
return res.value();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<const std::string, const std::string> getConfigs(const std::string &config,
|
std::optional<std::string> Config::findConfigPath(const std::vector<std::string> &names,
|
||||||
const std::string &style) {
|
const std::vector<std::string> &dirs) {
|
||||||
auto config_file = config.empty() ? getValidPath({
|
std::vector<std::string> paths;
|
||||||
"$XDG_CONFIG_HOME/waybar/config",
|
for (const auto &dir : dirs) {
|
||||||
"$XDG_CONFIG_HOME/waybar/config.jsonc",
|
for (const auto &name : names) {
|
||||||
"$HOME/.config/waybar/config",
|
if (auto res = tryExpandPath(dir + name); res) {
|
||||||
"$HOME/.config/waybar/config.jsonc",
|
return res;
|
||||||
"$HOME/waybar/config",
|
|
||||||
"$HOME/waybar/config.jsonc",
|
|
||||||
"/etc/xdg/waybar/config",
|
|
||||||
"/etc/xdg/waybar/config.jsonc",
|
|
||||||
SYSCONFDIR "/xdg/waybar/config",
|
|
||||||
"./resources/config",
|
|
||||||
})
|
|
||||||
: config;
|
|
||||||
auto css_file = style.empty() ? getValidPath({
|
|
||||||
"$XDG_CONFIG_HOME/waybar/style.css",
|
|
||||||
"$HOME/.config/waybar/style.css",
|
|
||||||
"$HOME/waybar/style.css",
|
|
||||||
"/etc/xdg/waybar/style.css",
|
|
||||||
SYSCONFDIR "/xdg/waybar/style.css",
|
|
||||||
"./resources/style.css",
|
|
||||||
})
|
|
||||||
: style;
|
|
||||||
if (css_file.empty() || config_file.empty()) {
|
|
||||||
throw std::runtime_error("Missing required resources files");
|
|
||||||
}
|
}
|
||||||
spdlog::info("Resources files: {}, {}", config_file, css_file);
|
}
|
||||||
return {config_file, css_file};
|
}
|
||||||
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Config::setupConfig(const std::string &config_file, int depth) {
|
void Config::setupConfig(const std::string &config_file, int depth) {
|
||||||
@ -143,8 +136,13 @@ bool isValidOutput(const Json::Value &config, const std::string &name,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Config::load(const std::string &config, const std::string &style) {
|
void Config::load(const std::string &config) {
|
||||||
std::tie(config_file_, css_file_) = getConfigs(config, style);
|
auto file = config.empty() ? findConfigPath({"config", "config.jsonc"}) : config;
|
||||||
|
if (!file) {
|
||||||
|
throw std::runtime_error("Missing required resource files");
|
||||||
|
}
|
||||||
|
config_file_ = file.value();
|
||||||
|
spdlog::info("Using configuration file {}", config_file_);
|
||||||
setupConfig(config_file_, 0);
|
setupConfig(config_file_, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user