Skip to content

Commit dde700f

Browse files
committed
feat: use gtk-layer-shell library for correct positioning of popups
To enable: use sway >= 1.2, compile waybar with `-Dgtk-layer-shell=enabled` meson option. Original behavior could be restored at runtime by setting `"gtk-layer-shell": false` in waybar config.
1 parent 0e87b39 commit dde700f

File tree

2 files changed

+83
-25
lines changed

2 files changed

+83
-25
lines changed

include/bar.hpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ class Bar {
3232
auto toggle() -> void;
3333
void handleSignal(int);
3434

35-
struct waybar_output * output;
36-
Json::Value config;
37-
Gtk::Window window;
38-
struct wl_surface * surface;
39-
struct zwlr_layer_surface_v1 *layer_surface;
40-
bool visible = true;
41-
bool vertical = false;
35+
struct waybar_output *output;
36+
Json::Value config;
37+
Gtk::Window window;
38+
struct wl_surface * surface;
39+
bool visible = true;
40+
bool vertical = false;
4241

4342
private:
4443
static constexpr const char *MIN_HEIGHT_MSG =
@@ -53,7 +52,9 @@ class Bar {
5352
uint32_t, uint32_t);
5453
static void layerSurfaceHandleClosed(void *, struct zwlr_layer_surface_v1 *);
5554

56-
void destroyOutput();
55+
#ifdef HAVE_GTK_LAYER_SHELL
56+
void initGtkLayerShell();
57+
#endif
5758
void onConfigure(GdkEventConfigure *ev);
5859
void onRealize();
5960
void onMap(GdkEventAny *ev);
@@ -70,6 +71,9 @@ class Bar {
7071
int bottom = 0;
7172
int left = 0;
7273
} margins_;
74+
struct zwlr_layer_surface_v1 *layer_surface_;
75+
// use gtk-layer-shell instead of handling layer surfaces directly
76+
bool use_gls_ = false;
7377
uint32_t width_ = 0;
7478
uint32_t height_ = 1;
7579
uint8_t anchor_;

src/bar.cpp

+71-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#ifdef HAVE_GTK_LAYER_SHELL
2+
#include <gtk-layer-shell.h>
3+
#endif
4+
15
#include "bar.hpp"
26
#include "client.hpp"
37
#include "factory.hpp"
@@ -8,7 +12,7 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
812
config(w_config),
913
window{Gtk::WindowType::WINDOW_TOPLEVEL},
1014
surface(nullptr),
11-
layer_surface(nullptr),
15+
layer_surface_(nullptr),
1216
anchor_(ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP),
1317
left_(Gtk::ORIENTATION_HORIZONTAL, 0),
1418
center_(Gtk::ORIENTATION_HORIZONTAL, 0),
@@ -28,11 +32,6 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
2832
height_ = config["height"].isUInt() ? config["height"].asUInt() : height_;
2933
width_ = config["width"].isUInt() ? config["width"].asUInt() : width_;
3034

31-
window.signal_realize().connect_notify(sigc::mem_fun(*this, &Bar::onRealize));
32-
window.signal_map_event().connect_notify(sigc::mem_fun(*this, &Bar::onMap));
33-
window.signal_configure_event().connect_notify(sigc::mem_fun(*this, &Bar::onConfigure));
34-
window.set_size_request(width_, height_);
35-
3635
if (config["position"] == "bottom") {
3736
anchor_ = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
3837
} else if (config["position"] == "left") {
@@ -98,6 +97,17 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
9897
margins_ = {.top = gaps, .right = gaps, .bottom = gaps, .left = gaps};
9998
}
10099

100+
#ifdef HAVE_GTK_LAYER_SHELL
101+
use_gls_ = config["gtk-layer-shell"].isBool() ? config["gtk-layer-shell"].asBool() : true;
102+
if (use_gls_) {
103+
initGtkLayerShell();
104+
}
105+
#endif
106+
107+
window.signal_realize().connect_notify(sigc::mem_fun(*this, &Bar::onRealize));
108+
window.signal_map_event().connect_notify(sigc::mem_fun(*this, &Bar::onMap));
109+
window.signal_configure_event().connect_notify(sigc::mem_fun(*this, &Bar::onConfigure));
110+
window.set_size_request(width_, height_);
101111
setupWidgets();
102112

103113
if (window.get_realized()) {
@@ -131,11 +141,43 @@ void waybar::Bar::onConfigure(GdkEventConfigure* ev) {
131141
tmp_width = ev->width;
132142
}
133143
}
134-
if (tmp_width != width_ || tmp_height != height_) {
144+
if (use_gls_) {
145+
width_ = tmp_width;
146+
height_ = tmp_height;
147+
spdlog::debug("Set surface size {}x{} for output {}", width_, height_, output->name);
148+
setExclusiveZone(tmp_width, tmp_height);
149+
} else if (tmp_width != width_ || tmp_height != height_) {
135150
setSurfaceSize(tmp_width, tmp_height);
136151
}
137152
}
138153

154+
#ifdef HAVE_GTK_LAYER_SHELL
155+
void waybar::Bar::initGtkLayerShell() {
156+
auto gtk_window = window.gobj();
157+
// this has to be executed before GtkWindow.realize
158+
gtk_layer_init_for_window(gtk_window);
159+
gtk_layer_set_keyboard_interactivity(gtk_window, FALSE);
160+
auto layer = config["layer"] == "top" ? GTK_LAYER_SHELL_LAYER_TOP : GTK_LAYER_SHELL_LAYER_BOTTOM;
161+
gtk_layer_set_layer(gtk_window, layer);
162+
gtk_layer_set_monitor(gtk_window, output->monitor->gobj());
163+
gtk_layer_set_namespace(gtk_window, "waybar");
164+
165+
gtk_layer_set_anchor(
166+
gtk_window, GTK_LAYER_SHELL_EDGE_LEFT, anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT);
167+
gtk_layer_set_anchor(
168+
gtk_window, GTK_LAYER_SHELL_EDGE_RIGHT, anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
169+
gtk_layer_set_anchor(
170+
gtk_window, GTK_LAYER_SHELL_EDGE_TOP, anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP);
171+
gtk_layer_set_anchor(
172+
gtk_window, GTK_LAYER_SHELL_EDGE_BOTTOM, anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM);
173+
174+
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_LEFT, margins_.left);
175+
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_RIGHT, margins_.right);
176+
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_TOP, margins_.top);
177+
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_BOTTOM, margins_.bottom);
178+
}
179+
#endif
180+
139181
void waybar::Bar::onRealize() {
140182
auto gdk_window = window.get_window()->gobj();
141183
gdk_wayland_window_set_use_custom_surface(gdk_window);
@@ -145,26 +187,30 @@ void waybar::Bar::onMap(GdkEventAny* ev) {
145187
auto gdk_window = window.get_window()->gobj();
146188
surface = gdk_wayland_window_get_wl_surface(gdk_window);
147189

190+
if (use_gls_) {
191+
return;
192+
}
193+
148194
auto client = waybar::Client::inst();
149195
// owned by output->monitor; no need to destroy
150196
auto wl_output = gdk_wayland_monitor_get_wl_output(output->monitor->gobj());
151197
auto layer =
152198
config["layer"] == "top" ? ZWLR_LAYER_SHELL_V1_LAYER_TOP : ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM;
153-
layer_surface = zwlr_layer_shell_v1_get_layer_surface(
199+
layer_surface_ = zwlr_layer_shell_v1_get_layer_surface(
154200
client->layer_shell, surface, wl_output, layer, "waybar");
155201

156-
zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, false);
157-
zwlr_layer_surface_v1_set_anchor(layer_surface, anchor_);
202+
zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface_, false);
203+
zwlr_layer_surface_v1_set_anchor(layer_surface_, anchor_);
158204
zwlr_layer_surface_v1_set_margin(
159-
layer_surface, margins_.top, margins_.right, margins_.bottom, margins_.left);
205+
layer_surface_, margins_.top, margins_.right, margins_.bottom, margins_.left);
160206
setSurfaceSize(width_, height_);
161207
setExclusiveZone(width_, height_);
162208

163209
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
164210
.configure = layerSurfaceHandleConfigure,
165211
.closed = layerSurfaceHandleClosed,
166212
};
167-
zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, this);
213+
zwlr_layer_surface_v1_add_listener(layer_surface_, &layer_surface_listener, this);
168214

169215
wl_surface_commit(surface);
170216
wl_display_roundtrip(client->wl_display);
@@ -184,7 +230,15 @@ void waybar::Bar::setExclusiveZone(uint32_t width, uint32_t height) {
184230
}
185231
}
186232
spdlog::debug("Set exclusive zone {} for output {}", zone, output->name);
187-
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, zone);
233+
234+
#ifdef HAVE_GTK_LAYER_SHELL
235+
if (use_gls_) {
236+
gtk_layer_set_exclusive_zone(window.gobj(), zone);
237+
} else
238+
#endif
239+
{
240+
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface_, zone);
241+
}
188242
}
189243

190244
void waybar::Bar::setSurfaceSize(uint32_t width, uint32_t height) {
@@ -200,7 +254,7 @@ void waybar::Bar::setSurfaceSize(uint32_t width, uint32_t height) {
200254
width += margins_.right + margins_.left;
201255
}
202256
spdlog::debug("Set surface size {}x{} for output {}", width, height, output->name);
203-
zwlr_layer_surface_v1_set_size(layer_surface, width, height);
257+
zwlr_layer_surface_v1_set_size(layer_surface_, width, height);
204258
}
205259

206260
// Converting string to button code rn as to avoid doing it later
@@ -284,9 +338,9 @@ void waybar::Bar::layerSurfaceHandleConfigure(void* data, struct zwlr_layer_surf
284338

285339
void waybar::Bar::layerSurfaceHandleClosed(void* data, struct zwlr_layer_surface_v1* /*surface*/) {
286340
auto o = static_cast<waybar::Bar*>(data);
287-
if (o->layer_surface) {
288-
zwlr_layer_surface_v1_destroy(o->layer_surface);
289-
o->layer_surface = nullptr;
341+
if (o->layer_surface_) {
342+
zwlr_layer_surface_v1_destroy(o->layer_surface_);
343+
o->layer_surface_ = nullptr;
290344
}
291345
o->modules_left_.clear();
292346
o->modules_center_.clear();

0 commit comments

Comments
 (0)