mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
feat(sway/scratchpad): add basic counter
This commit is contained in:
@ -35,6 +35,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
|
||||
if (ref == "sway/language") {
|
||||
return new waybar::modules::sway::Language(id, config_[name]);
|
||||
}
|
||||
if (ref == "sway/scratchpad") {
|
||||
return new waybar::modules::sway::Scratchpad(id, bar_, config_[name]);
|
||||
}
|
||||
#endif
|
||||
#ifdef HAVE_WLR
|
||||
if (ref == "wlr/taskbar") {
|
||||
|
52
src/modules/sway/scratchpad.cpp
Normal file
52
src/modules/sway/scratchpad.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
#include "modules/sway/scratchpad.hpp"
|
||||
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace waybar::modules::sway {
|
||||
Scratchpad::Scratchpad(const std::string& id, const waybar::Bar& bar, const Json::Value& config)
|
||||
: ALabel(config, "scratchpad", id, "{count}"),
|
||||
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0),
|
||||
bar_(bar),
|
||||
count_(0) {
|
||||
ipc_.subscribe(R"(["window"])");
|
||||
ipc_.signal_event.connect(sigc::mem_fun(*this, &Scratchpad::onEvent));
|
||||
ipc_.signal_cmd.connect(sigc::mem_fun(*this, &Scratchpad::onCmd));
|
||||
|
||||
getTree();
|
||||
|
||||
ipc_.setWorker([this] {
|
||||
try {
|
||||
ipc_.handleEvent();
|
||||
} catch (const std::exception& e) {
|
||||
spdlog::error("Scratchpad: {}", e.what());
|
||||
}
|
||||
});
|
||||
}
|
||||
auto Scratchpad::update() -> void {
|
||||
label_.set_markup(fmt::format(format_, fmt::arg("count", count_)));
|
||||
AModule::update();
|
||||
}
|
||||
|
||||
auto Scratchpad::getTree() -> void {
|
||||
try {
|
||||
ipc_.sendCmd(IPC_GET_TREE);
|
||||
} catch (const std::exception& e) {
|
||||
spdlog::error("Scratchpad: {}", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
auto Scratchpad::onCmd(const struct Ipc::ipc_response& res) -> void {
|
||||
try {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
auto tree = parser_.parse(res.payload);
|
||||
count_ = tree["nodes"][0]["nodes"][0]["floating_nodes"].size();
|
||||
dp.emit();
|
||||
} catch (const std::exception& e) {
|
||||
spdlog::error("Scratchpad: {}", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
auto Scratchpad::onEvent(const struct Ipc::ipc_response& res) -> void { getTree(); }
|
||||
} // namespace waybar::modules::sway
|
Reference in New Issue
Block a user