Gtk4: battery migration

build: drop std::filesystem checks

The `<experimental/filesystem>` and `-lc++experimental` aren't needed
since LLVM 9.0. And since we now require C++20, checking for the
`<filesystem>` support shouldn't be necessary either.

Signed-off-by: Viktar Lukashonak <myxabeer@gmail.com>
pull/2956/head
Viktar Lukashonak 2024-03-05 18:04:59 +03:00
parent 69648ab8f4
commit 7802cab40d
No known key found for this signature in database
GPG Key ID: 08A413AA87200A6F
8 changed files with 86 additions and 55 deletions

View File

@ -1,45 +1,32 @@
#pragma once
#ifdef FILESYSTEM_EXPERIMENTAL
#include <experimental/filesystem>
#else
#include <filesystem>
#endif
#include <fmt/format.h>
#if defined(__linux__)
#include <sys/inotify.h>
#endif
#include <algorithm>
#include <fstream>
#include <string>
#include <vector>
#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<uint8_t, float, std::string, float> getInfos();
const std::string formatTimeRemaining(float hoursRemaining);
void setBarClass(std::string&);
int global_watch;
std::map<fs::path, int> batteries_;

View File

@ -1,8 +1,9 @@
#pragma once
#include <string>
#include <giomm/dbusproxy.h>
#include <string>
#include "ALabel.hpp"
namespace waybar::modules {

View File

@ -167,3 +167,10 @@ The *battery* module allows one to define custom formats based on up to two fact
- *<state>* can be defined in the *config*. For more information see *states*.
- *#battery.<status>.<state>*
- Combination of both *<status>* and *<state>*.
The following classes are applied to the entire Waybar rather than just the
battery widget:
- *window#waybar.battery-<state>*
- *<state>* can be defined in the *config*, as previously mentioned.

View File

@ -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 <langinfo.h>
#include <locale.h>
@ -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(

View File

@ -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);
}

View File

@ -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]);

View File

@ -1,10 +1,12 @@
#include "modules/battery.hpp"
#include <fstream>
#include <spdlog/spdlog.h>
#if defined(__FreeBSD__)
#include <sys/sysctl.h>
#endif
#include <spdlog/spdlog.h>
#include <iostream>
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<std::string>{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);
}
}

View File

@ -1,10 +1,11 @@
#include "modules/systemd_failed_units.hpp"
#include <cstdint>
#include <giomm/dbusproxy.h>
#include <glibmm/variant.h>
#include <spdlog/spdlog.h>
#include <cstdint>
static const unsigned UPDATE_DEBOUNCE_TIME_MS = 1000;
namespace waybar::modules {
@ -55,15 +56,13 @@ SystemdFailedUnits::~SystemdFailedUnits() {
if (user_proxy) user_proxy.reset();
}
auto SystemdFailedUnits::notify_cb(
const Glib::ustring &sender_name,
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),
Glib::signal_timeout().connect_once(sigc::mem_fun(*this, &SystemdFailedUnits::updateData),
UPDATE_DEBOUNCE_TIME_MS);
}
}
@ -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