diff --git a/include/ALabel.hpp b/include/ALabel.hpp index 6848d677..5b9ac541 100644 --- a/include/ALabel.hpp +++ b/include/ALabel.hpp @@ -19,7 +19,6 @@ class ALabel : public AModule { protected: Gtk::Label label_; std::string format_; - std::string click_param; const std::chrono::seconds interval_; bool alt_ = false; std::string default_format_; diff --git a/include/modules/idle_inhibitor.hpp b/include/modules/idle_inhibitor.hpp index 5ce324da..4b6c097f 100644 --- a/include/modules/idle_inhibitor.hpp +++ b/include/modules/idle_inhibitor.hpp @@ -12,12 +12,13 @@ class IdleInhibitor : public ALabel { IdleInhibitor(const std::string&, const waybar::Bar&, const Json::Value&); ~IdleInhibitor(); auto update() -> void; + static std::list modules; + static bool status; private: bool handleToggle(GdkEventButton* const& e); const Bar& bar_; - std::string status_; struct zwp_idle_inhibitor_v1* idle_inhibitor_; int pid_; }; diff --git a/src/modules/idle_inhibitor.cpp b/src/modules/idle_inhibitor.cpp index d94e9579..ba5d6943 100644 --- a/src/modules/idle_inhibitor.cpp +++ b/src/modules/idle_inhibitor.cpp @@ -1,16 +1,22 @@ #include "modules/idle_inhibitor.hpp" #include "util/command.hpp" +std::list waybar::modules::IdleInhibitor::modules; +bool waybar::modules::IdleInhibitor::status = false; + waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar, const Json::Value& config) : ALabel(config, "idle_inhibitor", id, "{status}"), bar_(bar), - status_("deactivated"), idle_inhibitor_(nullptr), pid_(-1) { event_box_.add_events(Gdk::BUTTON_PRESS_MASK); event_box_.signal_button_press_event().connect( sigc::mem_fun(*this, &IdleInhibitor::handleToggle)); + + // Add this to the modules list + waybar::modules::IdleInhibitor::modules.push_back(this); + dp.emit(); } @@ -19,6 +25,10 @@ waybar::modules::IdleInhibitor::~IdleInhibitor() { zwp_idle_inhibitor_v1_destroy(idle_inhibitor_); idle_inhibitor_ = nullptr; } + + // Remove this from the modules list + waybar::modules::IdleInhibitor::modules.remove(this); + if (pid_ != -1) { kill(-pid_, 9); pid_ = -1; @@ -26,11 +36,27 @@ waybar::modules::IdleInhibitor::~IdleInhibitor() { } auto waybar::modules::IdleInhibitor::update() -> void { + // Check status + if (status) { + label_.get_style_context()->remove_class("deactivated"); + if (idle_inhibitor_ == nullptr) { + idle_inhibitor_ = zwp_idle_inhibit_manager_v1_create_inhibitor( + waybar::Client::inst()->idle_inhibit_manager, bar_.surface); + } + } else { + label_.get_style_context()->remove_class("activated"); + if (idle_inhibitor_ != nullptr) { + zwp_idle_inhibitor_v1_destroy(idle_inhibitor_); + idle_inhibitor_ = nullptr; + } + } + + std::string status_text = status ? "activated" : "deactivated"; label_.set_markup( - fmt::format(format_, fmt::arg("status", status_), fmt::arg("icon", getIcon(0, status_)))); - label_.get_style_context()->add_class(status_); + fmt::format(format_, fmt::arg("status", status_text), fmt::arg("icon", getIcon(0, status_text)))); + label_.get_style_context()->add_class(status_text); if (tooltipEnabled()) { - label_.set_tooltip_text(status_); + label_.set_tooltip_text(status_text); } // Call parent update ALabel::update(); @@ -38,18 +64,16 @@ auto waybar::modules::IdleInhibitor::update() -> void { bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) { if (e->button == 1) { - label_.get_style_context()->remove_class(status_); - if (idle_inhibitor_ != nullptr) { - zwp_idle_inhibitor_v1_destroy(idle_inhibitor_); - idle_inhibitor_ = nullptr; - status_ = "deactivated"; - } else { - idle_inhibitor_ = zwp_idle_inhibit_manager_v1_create_inhibitor( - waybar::Client::inst()->idle_inhibit_manager, bar_.surface); - status_ = "activated"; + status = !status; + + // Make all other idle inhibitor modules update + for (auto const& module : waybar::modules::IdleInhibitor::modules) { + if (module != this) { + module->update(); + } } - click_param = status_; } + ALabel::handleToggle(e); return true; }