Skip to content

Commit 6dc6204

Browse files
nyyMannisomini
authored andcommitted
Add module sway/hide for swaybar integration
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
1 parent 5647d9e commit 6dc6204

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

include/factory.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "modules/sway/mode.hpp"
77
#include "modules/sway/window.hpp"
88
#include "modules/sway/workspaces.hpp"
9+
#include "modules/sway/hide.hpp"
910
#endif
1011
#ifdef HAVE_WLR
1112
#include "modules/wlr/taskbar.hpp"

include/modules/sway/hide.hpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include <fmt/format.h>
4+
#include <tuple>
5+
#include "ALabel.hpp"
6+
#include "bar.hpp"
7+
#include "client.hpp"
8+
#include "modules/sway/ipc/client.hpp"
9+
#include "util/json.hpp"
10+
#include "util/sleeper_thread.hpp"
11+
12+
namespace waybar::modules::sway {
13+
14+
class Hide : public ALabel, public sigc::trackable {
15+
public:
16+
Hide(const std::string&, const waybar::Bar&, const Json::Value&);
17+
~Hide() = default;
18+
auto update() -> void;
19+
20+
private:
21+
void onEvent(const struct Ipc::ipc_response&);
22+
void worker();
23+
24+
const Bar& bar_;
25+
std::string window_;
26+
int windowId_;
27+
util::JsonParser parser_;
28+
29+
util::SleeperThread thread_;
30+
Ipc ipc_;
31+
};
32+
33+
} // namespace waybar::modules::sway

meson.build

+2-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ src_files += [
158158
'src/modules/sway/ipc/client.cpp',
159159
'src/modules/sway/mode.cpp',
160160
'src/modules/sway/window.cpp',
161-
'src/modules/sway/workspaces.cpp'
161+
'src/modules/sway/workspaces.cpp',
162+
'src/modules/sway/hide.cpp',
162163
]
163164

164165
if true

src/factory.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
2222
if (ref == "sway/window") {
2323
return new waybar::modules::sway::Window(id, bar_, config_[name]);
2424
}
25+
if (ref == "sway/hide") {
26+
return new waybar::modules::sway::Hide(id, bar_, config_[name]);
27+
}
2528
#endif
2629
#ifdef HAVE_WLR
2730
if (ref == "wlr/taskbar") {

src/modules/sway/hide.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "modules/sway/hide.hpp"
2+
#include <spdlog/spdlog.h>
3+
4+
namespace waybar::modules::sway {
5+
6+
Hide::Hide(const std::string& id, const Bar& bar, const Json::Value& config)
7+
: ALabel(config, "hide", id, "{}", 0, true), bar_(bar), windowId_(-1) {
8+
ipc_.subscribe(R"(["bar_state_update"])");
9+
ipc_.signal_event.connect(sigc::mem_fun(*this, &Hide::onEvent));
10+
bar_.window.get_style_context()->add_class("hidden");
11+
// Launch worker
12+
worker();
13+
}
14+
15+
void Hide::onEvent(const struct Ipc::ipc_response& res) {
16+
17+
auto payload = parser_.parse(res.payload);
18+
if (payload.isMember("visible_by_modifier")) {
19+
if (payload["visible_by_modifier"].asBool())
20+
bar_.window.get_style_context()->remove_class("hidden");
21+
else
22+
bar_.window.get_style_context()->add_class("hidden");
23+
}
24+
}
25+
26+
void Hide::worker() {
27+
thread_ = [this] {
28+
try {
29+
ipc_.handleEvent();
30+
} catch (const std::exception& e) {
31+
spdlog::error("Hide: {}", e.what());
32+
}
33+
};
34+
}
35+
36+
auto Hide::update() -> void {
37+
}
38+
} // namespace waybar::modules::sway

0 commit comments

Comments
 (0)