Files
Waybar/src/modules/hyprland/submap.cpp
T

95 lines
2.1 KiB
C++
Raw Normal View History

2022-12-21 01:45:57 +01:00
#include "modules/hyprland/submap.hpp"
2022-12-21 01:55:39 +01:00
2022-12-21 01:45:57 +01:00
#include <spdlog/spdlog.h>
2022-12-21 01:55:39 +01:00
2023-09-02 23:34:11 -05:00
#include "util/sanitize_str.hpp"
2022-12-21 01:45:57 +01: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;
2024-02-13 12:53:45 +01:00
parseConfig(config);
2024-02-25 12:11:22 +01:00
if (!gIPC) {
2022-12-21 01:45:57 +01:00
gIPC = std::make_unique<IPC>();
}
label_.hide();
ALabel::update();
2024-02-13 12:53:45 +01:00
// Displays widget immediately if always_on_ assuming default submap
// Needs an actual way to retrieve current submap on startup
2024-02-13 12:53:45 +01:00
if (always_on_) {
submap_ = default_submap_;
label_.get_style_context()->add_class(submap_);
}
2022-12-21 01:45:57 +01:00
// register for hyprland ipc
gIPC->registerForIPC("submap", this);
dp.emit();
2022-12-21 01:45:57 +01:00
}
Submap::~Submap() {
gIPC->unregisterForIPC(this);
// wait for possible event handler to finish
std::lock_guard<std::mutex> lg(mutex_);
}
2024-02-13 12:53:45 +01:00
auto Submap::parseConfig(const Json::Value& config) -> void {
2024-06-22 23:19:48 -05:00
auto const& alwaysOn = config["always-on"];
2024-02-13 12:53:45 +01:00
if (alwaysOn.isBool()) {
always_on_ = alwaysOn.asBool();
}
2024-06-22 23:19:48 -05:00
auto const& defaultSubmap = config["default-submap"];
2024-02-13 12:53:45 +01:00
if (defaultSubmap.isString()) {
default_submap_ = defaultSubmap.asString();
}
}
2022-12-21 01:45:57 +01:00
auto Submap::update() -> void {
std::lock_guard<std::mutex> lg(mutex_);
if (submap_.empty()) {
event_box_.hide();
} else {
2023-01-16 13:24:55 -08:00
label_.set_markup(fmt::format(fmt::runtime(format_), submap_));
2022-12-21 01:45:57 +01: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 12:53:45 +01:00
if (!submap_.empty()) {
2024-02-13 11:42:09 +01:00
label_.get_style_context()->remove_class(submap_);
}
2022-12-21 01:45:57 +01:00
submap_ = submapName;
2024-02-13 12:53:45 +01:00
if (submap_.empty() && always_on_) {
submap_ = default_submap_;
}
2024-02-13 11:42:09 +01:00
label_.get_style_context()->add_class(submap_);
2022-12-21 01:45:57 +01:00
spdlog::debug("hyprland submap onevent with {}", submap_);
dp.emit();
}
} // namespace waybar::modules::hyprland