From c64058c94704b9435b93407f535050a242319e34 Mon Sep 17 00:00:00 2001 From: vaxerski Date: Wed, 17 Aug 2022 21:54:23 +0200 Subject: [PATCH] stabilize window module --- include/modules/hyprland/window.hpp | 3 +++ src/modules/hyprland/window.cpp | 25 ++++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/include/modules/hyprland/window.hpp b/include/modules/hyprland/window.hpp index e21426a..e844655 100644 --- a/include/modules/hyprland/window.hpp +++ b/include/modules/hyprland/window.hpp @@ -14,9 +14,12 @@ public: Window(const std::string&, const waybar::Bar&, const Json::Value&); ~Window() = default; + auto update() -> void; + private: void onEvent(const std::string&); + std::mutex mutex_; const Bar& bar_; util::JsonParser parser_; std::string lastView; diff --git a/src/modules/hyprland/window.cpp b/src/modules/hyprland/window.cpp index 96d07bd..f0462a0 100644 --- a/src/modules/hyprland/window.cpp +++ b/src/modules/hyprland/window.cpp @@ -20,10 +20,24 @@ Window::Window(const std::string& id, const Bar& bar, const Json::Value& config) gIPC->registerForIPC("activewindow", [&](const std::string& ev) { this->onEvent(ev); }); } +auto Window::update() -> void { + // fix ampersands + std::lock_guard lg(mutex_); + + if (!format_.empty()) { + label_.show(); + label_.set_markup(fmt::format(format_, lastView)); + } else { + label_.hide(); + } + + ALabel::update(); +} + void Window::onEvent(const std::string& ev) { + std::lock_guard lg(mutex_); auto windowName = ev.substr(ev.find_first_of(',') + 1).substr(0, 256); - // fix ampersands auto replaceAll = [](std::string str, const std::string& from, const std::string& to) -> std::string { size_t start_pos = 0; @@ -42,13 +56,6 @@ void Window::onEvent(const std::string& ev) { spdlog::debug("hyprland window onevent with {}", windowName); - if (!format_.empty()) { - label_.show(); - label_.set_markup(fmt::format(format_, windowName)); - } else { - label_.hide(); - } - - ALabel::update(); + dp.emit(); } } \ No newline at end of file