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:
|
||||
Client() = default;
|
||||
const std::string getStyle(const std::string &style);
|
||||
void bindInterfaces();
|
||||
void handleOutput(struct waybar_output &output);
|
||||
auto setupCss(const std::string &css_file) -> void;
|
||||
|
@ -2,17 +2,26 @@
|
||||
|
||||
#include <json/json.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#ifndef SYSCONFDIR
|
||||
#define SYSCONFDIR "/etc"
|
||||
#endif
|
||||
|
||||
namespace waybar {
|
||||
|
||||
class Config {
|
||||
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;
|
||||
|
||||
void load(const std::string &config, const std::string &style);
|
||||
|
||||
const std::string &getStyle() { return css_file_; }
|
||||
void load(const std::string &config);
|
||||
|
||||
Json::Value &getConfig() { return config_; }
|
||||
|
||||
@ -24,7 +33,6 @@ class Config {
|
||||
void mergeConfig(Json::Value &a_config_, Json::Value &b_config_);
|
||||
|
||||
std::string config_file_;
|
||||
std::string css_file_;
|
||||
|
||||
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; });
|
||||
}
|
||||
|
||||
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 {
|
||||
css_provider_ = Gtk::CssProvider::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");
|
||||
}
|
||||
wl_display = gdk_wayland_display_get_wl_display(gdk_display->gobj());
|
||||
config.load(config_opt, style_opt);
|
||||
setupCss(config.getStyle());
|
||||
config.load(config_opt);
|
||||
auto css_file = getStyle(style_opt);
|
||||
setupCss(css_file);
|
||||
bindInterfaces();
|
||||
gtk_app->hold();
|
||||
gtk_app->run();
|
||||
|
@ -9,58 +9,51 @@
|
||||
|
||||
#include "util/json.hpp"
|
||||
|
||||
#ifndef SYSCONFDIR
|
||||
#define SYSCONFDIR "/etc"
|
||||
#endif
|
||||
|
||||
namespace waybar {
|
||||
|
||||
const std::string getValidPath(const std::vector<std::string> &paths) {
|
||||
wordexp_t p;
|
||||
const std::vector<std::string> Config::CONFIG_DIRS = {
|
||||
"$XDG_CONFIG_HOME/waybar/",
|
||||
"$HOME/.config/waybar/",
|
||||
"$HOME/waybar/",
|
||||
"/etc/xdg/waybar/",
|
||||
SYSCONFDIR "/xdg/waybar/",
|
||||
"./resources/",
|
||||
};
|
||||
|
||||
for (const std::string &path : paths) {
|
||||
if (wordexp(path.c_str(), &p, 0) == 0) {
|
||||
if (access(*p.we_wordv, F_OK) == 0) {
|
||||
std::string result = *p.we_wordv;
|
||||
wordfree(&p);
|
||||
return result;
|
||||
}
|
||||
std::optional<std::string> tryExpandPath(const std::string &path) {
|
||||
wordexp_t p;
|
||||
if (wordexp(path.c_str(), &p, 0) == 0) {
|
||||
if (access(*p.we_wordv, F_OK) == 0) {
|
||||
std::string result = *p.we_wordv;
|
||||
wordfree(&p);
|
||||
return result;
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
std::tuple<const std::string, const std::string> getConfigs(const std::string &config,
|
||||
const std::string &style) {
|
||||
auto config_file = config.empty() ? getValidPath({
|
||||
"$XDG_CONFIG_HOME/waybar/config",
|
||||
"$XDG_CONFIG_HOME/waybar/config.jsonc",
|
||||
"$HOME/.config/waybar/config",
|
||||
"$HOME/.config/waybar/config.jsonc",
|
||||
"$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");
|
||||
std::optional<std::string> Config::findConfigPath(const std::vector<std::string> &names,
|
||||
const std::vector<std::string> &dirs) {
|
||||
std::vector<std::string> paths;
|
||||
for (const auto &dir : dirs) {
|
||||
for (const auto &name : names) {
|
||||
if (auto res = tryExpandPath(dir + name); res) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
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) {
|
||||
@ -143,8 +136,13 @@ bool isValidOutput(const Json::Value &config, const std::string &name,
|
||||
return true;
|
||||
}
|
||||
|
||||
void Config::load(const std::string &config, const std::string &style) {
|
||||
std::tie(config_file_, css_file_) = getConfigs(config, style);
|
||||
void Config::load(const std::string &config) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user