diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 017b0e48..1b82808c 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -1,45 +1,32 @@ #pragma once -#ifdef FILESYSTEM_EXPERIMENTAL -#include -#else #include -#endif -#include #if defined(__linux__) #include #endif -#include -#include -#include -#include - #include "ALabel.hpp" #include "util/sleeper_thread.hpp" namespace waybar::modules { -#ifdef FILESYSTEM_EXPERIMENTAL -namespace fs = std::experimental::filesystem; -#else namespace fs = std::filesystem; -#endif -class Battery : public ALabel { +class Battery final : public ALabel { public: Battery(const std::string&, const Json::Value&); virtual ~Battery(); auto update() -> void override; private: - static inline const fs::path data_dir_ = "/sys/class/power_supply/"; + static inline const fs::path data_dir_{"/sys/class/power_supply/"}; void refreshBatteries(); void worker(); const std::string getAdapterStatus(uint8_t capacity) const; const std::tuple getInfos(); const std::string formatTimeRemaining(float hoursRemaining); + void setBarClass(std::string&); int global_watch; std::map batteries_; diff --git a/include/modules/systemd_failed_units.hpp b/include/modules/systemd_failed_units.hpp index 7e0b1a91..d305264d 100644 --- a/include/modules/systemd_failed_units.hpp +++ b/include/modules/systemd_failed_units.hpp @@ -1,15 +1,16 @@ #pragma once -#include #include +#include + #include "ALabel.hpp" namespace waybar::modules { class SystemdFailedUnits : public ALabel { public: - SystemdFailedUnits(const std::string&, const Json::Value&); + SystemdFailedUnits(const std::string &, const Json::Value &); virtual ~SystemdFailedUnits(); auto update() -> void override; diff --git a/man/waybar-battery.5.scd b/man/waybar-battery.5.scd index 7827f4a8..52a6a2d1 100644 --- a/man/waybar-battery.5.scd +++ b/man/waybar-battery.5.scd @@ -167,3 +167,10 @@ The *battery* module allows one to define custom formats based on up to two fact - ** can be defined in the *config*. For more information see *states*. - *#battery..* - Combination of both ** and **. + +The following classes are applied to the entire Waybar rather than just the +battery widget: + +- *window#waybar.battery-* + - ** can be defined in the *config*, as previously mentioned. + diff --git a/meson.build b/meson.build index a9d16df6..9f72116f 100644 --- a/meson.build +++ b/meson.build @@ -22,8 +22,6 @@ endif if compiler.has_link_argument('-lc++fs') cpp_link_args += ['-lc++fs'] -elif compiler.has_link_argument('-lc++experimental') - cpp_link_args += ['-lc++experimental'] elif compiler.has_link_argument('-lstdc++fs') cpp_link_args += ['-lstdc++fs'] endif @@ -44,15 +42,6 @@ else endif endif -if not compiler.has_header('filesystem') - if compiler.has_header('experimental/filesystem') - add_project_arguments('-DFILESYSTEM_EXPERIMENTAL', language: 'cpp') - else - add_project_arguments('-DNO_FILESYSTEM', language: 'cpp') - warning('No filesystem header found, some modules may not work') - endif -endif - code = ''' #include #include @@ -194,6 +183,17 @@ if cava.found() endif inc_dirs = ['include'] + +if is_linux + src_files += files('src/modules/battery.cpp') + man_files += files('man/waybar-battery.5.scd') +endif + +if is_freebsd + src_files += files('src/modules/battery.cpp') + man_files += files('man/waybar-battery.5.scd') +endif + subdir('protocol') executable( diff --git a/src/bar.cpp b/src/bar.cpp index 6e7de108..6ce0f8ee 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -535,7 +535,7 @@ auto waybar::Bar::setupWidgets() -> void { for (auto const& module : modules_center_) { center_.prepend(*module); } - std::reverse(modules_right_.begin(), modules_right_.end()); +// std::reverse(modules_right_.begin(), modules_right_.end()); for (auto const& module : modules_right_) { right_.append(*module); } diff --git a/src/factory.cpp b/src/factory.cpp index 2505204a..ac5f5de6 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -116,12 +116,12 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name, auto hash_pos = name.find('#'); auto ref = name.substr(0, hash_pos); auto id = hash_pos != std::string::npos ? name.substr(hash_pos + 1) : ""; -/* gtk4 todo -#if defined(__FreeBSD__) || (defined(__linux__) && !defined(NO_FILESYSTEM)) +#if defined(__FreeBSD__) || defined(__linux__) if (ref == "battery") { return new waybar::modules::Battery(id, config_[name]); } #endif +/* gtk4 todo #ifdef HAVE_GAMEMODE if (ref == "gamemode") { return new waybar::modules::Gamemode(id, config_[name]); diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index 70268c8a..3296dfb7 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -1,10 +1,12 @@ #include "modules/battery.hpp" + +#include +#include + #if defined(__FreeBSD__) #include #endif -#include -#include waybar::modules::Battery::Battery(const std::string& id, const Json::Value& config) : ALabel(config, "battery", id, "{capacity}%", 60) { #if defined(__linux__) @@ -627,7 +629,7 @@ const std::string waybar::modules::Battery::formatTimeRemaining(float hoursRemai auto waybar::modules::Battery::update() -> void { #if defined(__linux__) if (batteries_.empty()) { - event_box_.hide(); + Gtk::Label::hide(); return; } #endif @@ -641,6 +643,7 @@ auto waybar::modules::Battery::update() -> void { [](char ch) { return ch == ' ' ? '-' : std::tolower(ch); }); auto format = format_; auto state = getState(capacity, true); + setBarClass(state); auto time_remaining_formatted = formatTimeRemaining(time_remaining); if (tooltipEnabled()) { std::string tooltip_text_default; @@ -660,15 +663,15 @@ auto waybar::modules::Battery::update() -> void { } else if (config_["tooltip-format"].isString()) { tooltip_format = config_["tooltip-format"].asString(); } - label_.set_tooltip_text(fmt::format(fmt::runtime(tooltip_format), + Gtk::Label::set_tooltip_text(fmt::format(fmt::runtime(tooltip_format), fmt::arg("timeTo", tooltip_text_default), fmt::arg("power", power), fmt::arg("capacity", capacity), fmt::arg("time", time_remaining_formatted))); } if (!old_status_.empty()) { - label_.get_style_context()->remove_class(old_status_); + Gtk::Label::get_style_context()->remove_class(old_status_); } - label_.get_style_context()->add_class(status); + Gtk::Label::get_style_context()->add_class(status); old_status_ = status; if (!state.empty() && config_["format-" + status + "-" + state].isString()) { format = config_["format-" + status + "-" + state].asString(); @@ -678,14 +681,50 @@ auto waybar::modules::Battery::update() -> void { format = config_["format-" + state].asString(); } if (format.empty()) { - event_box_.hide(); + Gtk::Label::hide(); } else { - event_box_.show(); + Gtk::Label::show(); auto icons = std::vector{status + "-" + state, status, state}; - label_.set_markup(fmt::format( + Gtk::Label::set_markup(fmt::format( fmt::runtime(format), fmt::arg("capacity", capacity), fmt::arg("power", power), fmt::arg("icon", getIcon(capacity, icons)), fmt::arg("time", time_remaining_formatted))); } // Call parent update ALabel::update(); } + +void waybar::modules::Battery::setBarClass(std::string& state) { + // Drop style + auto classes = Gtk::Widget::get_css_classes(); + const Glib::ustring prefix{"battery-"}; + + auto old_class_it = std::find_if(classes.cbegin(), classes.cend(), [&prefix](auto classname) { + return classname.rfind(prefix, 0) == 0; + }); + + auto new_class = prefix + state; + + // If the bar doesn't have any `battery-` class + if (old_class_it == classes.end()) { + if (!state.empty()) { + Gtk::Label::get_style_context()->add_class(new_class); + } + return; + } + + auto old_class = *old_class_it; + + // If the bar has a `battery-` class, + // but `state` is empty + if (state.empty()) { + Gtk::Label::get_style_context()->remove_class(old_class); + return; + } + + // If the bar has a `battery-` class, + // and `state` is NOT empty + if (old_class != new_class) { + Gtk::Label::get_style_context()->remove_class(old_class); + Gtk::Label::get_style_context()->add_class(new_class); + } +} diff --git a/src/modules/systemd_failed_units.cpp b/src/modules/systemd_failed_units.cpp index 382eea4a..56e624cf 100644 --- a/src/modules/systemd_failed_units.cpp +++ b/src/modules/systemd_failed_units.cpp @@ -1,10 +1,11 @@ #include "modules/systemd_failed_units.hpp" -#include #include #include #include +#include + static const unsigned UPDATE_DEBOUNCE_TIME_MS = 1000; namespace waybar::modules { @@ -55,23 +56,21 @@ SystemdFailedUnits::~SystemdFailedUnits() { if (user_proxy) user_proxy.reset(); } -auto SystemdFailedUnits::notify_cb( - const Glib::ustring &sender_name, - const Glib::ustring &signal_name, - const Glib::VariantContainerBase &arguments) -> void { +auto SystemdFailedUnits::notify_cb(const Glib::ustring& sender_name, + const Glib::ustring& signal_name, + const Glib::VariantContainerBase& arguments) -> void { if (signal_name == "PropertiesChanged" && !update_pending) { update_pending = true; /* The fail count may fluctuate due to restarting. */ - Glib::signal_timeout().connect_once( - sigc::mem_fun(*this, &SystemdFailedUnits::updateData), - UPDATE_DEBOUNCE_TIME_MS); + Glib::signal_timeout().connect_once(sigc::mem_fun(*this, &SystemdFailedUnits::updateData), + UPDATE_DEBOUNCE_TIME_MS); } } void SystemdFailedUnits::updateData() { update_pending = false; - auto load = [](const char* kind, Glib::RefPtr &proxy) -> uint32_t { + auto load = [](const char* kind, Glib::RefPtr& proxy) -> uint32_t { try { auto parameters = Glib::VariantContainerBase( g_variant_new("(ss)", "org.freedesktop.systemd1.Manager", "NFailedUnits")); @@ -123,11 +122,9 @@ auto SystemdFailedUnits::update() -> void { last_status = status; label_.set_markup(fmt::format( - fmt::runtime(nr_failed == 0 ? format_ok : format_), - fmt::arg("nr_failed", nr_failed), - fmt::arg("nr_failed_system", nr_failed_system), - fmt::arg("nr_failed_user", nr_failed_user))); + fmt::runtime(nr_failed == 0 ? format_ok : format_), fmt::arg("nr_failed", nr_failed), + fmt::arg("nr_failed_system", nr_failed_system), fmt::arg("nr_failed_user", nr_failed_user))); ALabel::update(); } -} // namespace waybar::modules::systemd_failed_units +} // namespace waybar::modules