diff --git a/include/modules/sway/bar.hpp b/include/modules/sway/bar.hpp index 821bff1..01be788 100644 --- a/include/modules/sway/bar.hpp +++ b/include/modules/sway/bar.hpp @@ -37,6 +37,7 @@ class BarIpcClient { void onModeUpdate(bool visible_by_modifier); void onUrgencyUpdate(bool visible_by_urgency); void update(); + bool isModuleEnabled(std::string name); Bar& bar_; util::JsonParser parser_; diff --git a/src/modules/sway/bar.cpp b/src/modules/sway/bar.cpp index 82e14be..5968d3a 100644 --- a/src/modules/sway/bar.cpp +++ b/src/modules/sway/bar.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include "bar.hpp" @@ -19,6 +20,23 @@ BarIpcClient::BarIpcClient(waybar::Bar& bar) : bar_{bar} { handle.disconnect(); } + Json::Value subscribe_events{Json::arrayValue}; + subscribe_events.append("bar_state_update"); + subscribe_events.append("barconfig_update"); + + bool has_mode = isModuleEnabled("sway/mode"); + bool has_workspaces = isModuleEnabled("sway/workspaces"); + + if (has_mode) { + subscribe_events.append("mode"); + } + if (has_workspaces) { + subscribe_events.append("workspace"); + } + if (has_mode || has_workspaces) { + subscribe_events.append("binding"); + } + signal_config_.connect(sigc::mem_fun(*this, &BarIpcClient::onConfigUpdate)); signal_visible_.connect(sigc::mem_fun(*this, &BarIpcClient::onVisibilityUpdate)); signal_urgency_.connect(sigc::mem_fun(*this, &BarIpcClient::onUrgencyUpdate)); @@ -26,7 +44,9 @@ BarIpcClient::BarIpcClient(waybar::Bar& bar) : bar_{bar} { // Subscribe to non bar events to determine if the modifier key press is followed by another // action. - ipc_.subscribe(R"(["bar_state_update", "barconfig_update", "workspace", "mode", "binding"])"); + std::ostringstream oss_events; + oss_events << subscribe_events; + ipc_.subscribe(oss_events.str()); ipc_.signal_event.connect(sigc::mem_fun(*this, &BarIpcClient::onIpcEvent)); ipc_.signal_cmd.connect(sigc::mem_fun(*this, &BarIpcClient::onCmd)); // Launch worker @@ -39,6 +59,19 @@ BarIpcClient::BarIpcClient(waybar::Bar& bar) : bar_{bar} { }); } +bool BarIpcClient::isModuleEnabled(std::string name) { + for (const auto& section : {"modules-left", "modules-center", "modules-right"}) { + if (const auto& modules = bar_.config.get(section, {}); modules.isArray()) { + for (const auto& module : modules) { + if (module.asString().rfind(name, 0) == 0) { + return true; + } + } + } + } + return false; +} + struct swaybar_config parseConfig(const Json::Value& payload) { swaybar_config conf; if (auto id = payload["id"]; id.isString()) {