Waybar/src/modules/hyprland/submap.cpp

72 lines
1.5 KiB
C++
Raw Normal View History

2022-12-21 00:45:57 +00:00
#include "modules/hyprland/submap.hpp"
2022-12-21 00:55:39 +00:00
2022-12-21 00:45:57 +00:00
#include <spdlog/spdlog.h>
2022-12-21 00:55:39 +00:00
2023-09-03 04:34:11 +00:00
#include "util/sanitize_str.hpp"
2022-12-21 00:45:57 +00:00
namespace waybar::modules::hyprland {
Submap::Submap(const std::string& id, const Bar& bar, const Json::Value& config)
: ALabel(config, "submap", id, "{}", 0, true), bar_(bar) {
modulesReady = true;
if (!gIPC.get()) {
gIPC = std::make_unique<IPC>();
}
label_.hide();
ALabel::update();
// register for hyprland ipc
gIPC->registerForIPC("submap", this);
dp.emit();
2022-12-21 00:45:57 +00:00
}
Submap::~Submap() {
gIPC->unregisterForIPC(this);
// wait for possible event handler to finish
std::lock_guard<std::mutex> lg(mutex_);
}
auto Submap::update() -> void {
std::lock_guard<std::mutex> lg(mutex_);
if (submap_.empty()) {
event_box_.hide();
} else {
label_.set_markup(fmt::format(fmt::runtime(format_), submap_));
2022-12-21 00:45:57 +00:00
if (tooltipEnabled()) {
label_.set_tooltip_text(submap_);
}
event_box_.show();
}
// Call parent update
ALabel::update();
}
void Submap::onEvent(const std::string& ev) {
std::lock_guard<std::mutex> lg(mutex_);
if (ev.find("submap") == std::string::npos) {
return;
}
auto submapName = ev.substr(ev.find_last_of('>') + 1);
submapName = waybar::util::sanitize_string(submapName);
2024-02-13 10:42:09 +00:00
if (!submap_.empty()){
label_.get_style_context()->remove_class(submap_);
}
2022-12-21 00:45:57 +00:00
submap_ = submapName;
2024-02-13 10:42:09 +00:00
label_.get_style_context()->add_class(submap_);
2022-12-21 00:45:57 +00:00
spdlog::debug("hyprland submap onevent with {}", submap_);
dp.emit();
}
} // namespace waybar::modules::hyprland