Skip to content

Commit

Permalink
Add module sway/hide for auto-showing the bar when the modifier is pr…
Browse files Browse the repository at this point in the history
…essed

Closes Alexays#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
  • Loading branch information
nyyManni committed Oct 30, 2019
1 parent 2d94a12 commit cda835a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
33 changes: 33 additions & 0 deletions include/modules/sway/hide.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <fmt/format.h>
#include <tuple>
#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
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
38 changes: 38 additions & 0 deletions src/modules/sway/hide.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "modules/sway/hide.hpp"
#include <spdlog/spdlog.h>

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

0 comments on commit cda835a

Please sign in to comment.