Skip to content

Commit

Permalink
Add module sway/hide for swaybar integration
Browse files Browse the repository at this point in the history
This allows auto-showing the bar when the modifier is pressed

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` in Waybar configuration file
  • Loading branch information
nyyManni authored and somini committed Sep 21, 2020
1 parent 013bfb9 commit 0840184
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
#ifdef HAVE_WLR
#include "modules/wlr/taskbar.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 @@ -158,7 +158,8 @@ src_files += [
'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',
]

if true
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
#ifdef HAVE_WLR
if (ref == "wlr/taskbar") {
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 0840184

Please sign in to comment.