From 0459c1ba781f7212c37df15be3071e190ac52c32 Mon Sep 17 00:00:00 2001 From: Henrik Nyman Date: Wed, 30 Oct 2019 20:38:14 +0200 Subject: [PATCH] Add module sway/hide for auto-showing the bar when the modifier is pressed Closes #255 Setup instructions: - Set the *mode* of the bar to "hide" in sway configuration file - Set the *layer* of the bar to "overlay" in Waybar configuration file - Add "sway/hide" into *modules-left* -list in Waybar configuration file --- include/factory.hpp | 1 + include/modules/sway/hide.hpp | 33 ++++++++++++++++++++++++++++++ meson.build | 3 ++- src/factory.cpp | 3 +++ src/modules/sway/hide.cpp | 38 +++++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 include/modules/sway/hide.hpp create mode 100644 src/modules/sway/hide.cpp diff --git a/include/factory.hpp b/include/factory.hpp index 7d4d14e3b..cd7a474fc 100644 --- a/include/factory.hpp +++ b/include/factory.hpp @@ -6,6 +6,7 @@ #include "modules/sway/mode.hpp" #include "modules/sway/window.hpp" #include "modules/sway/workspaces.hpp" +#include "modules/sway/hide.hpp" #endif #ifndef NO_FILESYSTEM #include "modules/battery.hpp" diff --git a/include/modules/sway/hide.hpp b/include/modules/sway/hide.hpp new file mode 100644 index 000000000..3dd306765 --- /dev/null +++ b/include/modules/sway/hide.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include +#include +#include "ALabel.hpp" +#include "bar.hpp" +#include "client.hpp" +#include "modules/sway/ipc/client.hpp" +#include "util/json.hpp" +#include "util/sleeper_thread.hpp" + +namespace waybar::modules::sway { + +class Hide : public ALabel, public sigc::trackable { + public: + Hide(const std::string&, const waybar::Bar&, const Json::Value&); + ~Hide() = default; + auto update() -> void; + + private: + void onEvent(const struct Ipc::ipc_response&); + void worker(); + + const Bar& bar_; + std::string window_; + int windowId_; + util::JsonParser parser_; + + util::SleeperThread thread_; + Ipc ipc_; +}; + +} // namespace waybar::modules::sway diff --git a/meson.build b/meson.build index a9d44eeb0..753aae7c5 100644 --- a/meson.build +++ b/meson.build @@ -102,7 +102,8 @@ if true # find_program('sway', required : false).found() 'src/modules/sway/ipc/client.cpp', 'src/modules/sway/mode.cpp', 'src/modules/sway/window.cpp', - 'src/modules/sway/workspaces.cpp' + 'src/modules/sway/workspaces.cpp', + 'src/modules/sway/hide.cpp' ] endif diff --git a/src/factory.cpp b/src/factory.cpp index 8f7cea755..afd63ce09 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -22,6 +22,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const { if (ref == "sway/window") { return new waybar::modules::sway::Window(id, bar_, config_[name]); } + if (ref == "sway/hide") { + return new waybar::modules::sway::Hide(id, bar_, config_[name]); + } #endif if (ref == "idle_inhibitor") { return new waybar::modules::IdleInhibitor(id, bar_, config_[name]); diff --git a/src/modules/sway/hide.cpp b/src/modules/sway/hide.cpp new file mode 100644 index 000000000..b31a7d3cf --- /dev/null +++ b/src/modules/sway/hide.cpp @@ -0,0 +1,38 @@ +#include "modules/sway/hide.hpp" +#include + +namespace waybar::modules::sway { + +Hide::Hide(const std::string& id, const Bar& bar, const Json::Value& config) + : ALabel(config, "hide", id, "{}", 0, true), bar_(bar), windowId_(-1) { + ipc_.subscribe(R"(["bar_state_update"])"); + ipc_.signal_event.connect(sigc::mem_fun(*this, &Hide::onEvent)); + bar_.window.get_style_context()->add_class("hidden"); + // Launch worker + worker(); +} + +void Hide::onEvent(const struct Ipc::ipc_response& res) { + + auto payload = parser_.parse(res.payload); + if (payload.isMember("visible_by_modifier")) { + if (payload["visible_by_modifier"].asBool()) + bar_.window.get_style_context()->remove_class("hidden"); + else + bar_.window.get_style_context()->add_class("hidden"); + } +} + +void Hide::worker() { + thread_ = [this] { + try { + ipc_.handleEvent(); + } catch (const std::exception& e) { + spdlog::error("Hide: {}", e.what()); + } + }; +} + +auto Hide::update() -> void { +} +} // namespace waybar::modules::sway