Skip to content

Commit c42a238

Browse files
committed
WIP: working layer-shell popups via gtk-layer-shell library
1 parent 890dec8 commit c42a238

File tree

2 files changed

+82
-22
lines changed

2 files changed

+82
-22
lines changed

include/bar.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class Bar {
5757
void onConfigure(GdkEventConfigure *ev);
5858
void onRealize();
5959
void onMap(GdkEventAny *ev);
60+
void initLayerSurface();
6061
void setExclusiveZone(uint32_t width, uint32_t height);
6162
void setSurfaceSize(uint32_t width, uint32_t height);
6263
auto setupWidgets() -> void;
@@ -70,6 +71,8 @@ class Bar {
7071
int bottom = 0;
7172
int left = 0;
7273
} margins_;
74+
// use gtk-layer-shell instead of handling layer surfaces directly
75+
bool use_gls_ = false;
7376
uint32_t width_ = 0;
7477
uint32_t height_ = 1;
7578
uint8_t anchor_;

src/bar.cpp

+79-22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifdef HAVE_GTK_LAYER_SHELL
2+
#include <gtk-layer-shell.h>
3+
#endif
14
#include "bar.hpp"
25
#include "client.hpp"
36
#include "factory.hpp"
@@ -98,6 +101,10 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
98101
margins_ = {.top = gaps, .right = gaps, .bottom = gaps, .left = gaps};
99102
}
100103

104+
#ifdef HAVE_GTK_LAYER_SHELL
105+
use_gls_ = config["gtk-layer-shell"].isBool() ? config["gtk-layer-shell"].asBool() : true;
106+
initLayerSurface();
107+
#endif
101108
setupWidgets();
102109

103110
if (window.get_realized()) {
@@ -131,11 +138,51 @@ void waybar::Bar::onConfigure(GdkEventConfigure* ev) {
131138
tmp_width = ev->width;
132139
}
133140
}
134-
if (tmp_width != width_ || tmp_height != height_) {
141+
spdlog::debug("onConfigure: set size (width: {} -> {}, height: {} -> {}) for output {}",
142+
width_,
143+
tmp_width,
144+
height_,
145+
tmp_height,
146+
output->name);
147+
if (use_gls_) {
148+
width_ = tmp_width;
149+
height_ = tmp_height;
150+
setExclusiveZone(tmp_width, tmp_height);
151+
} else if (tmp_width != width_ || tmp_height != height_) {
135152
setSurfaceSize(tmp_width, tmp_height);
136153
}
137154
}
138155

156+
void waybar::Bar::initLayerSurface() {
157+
#ifdef HAVE_GTK_LAYER_SHELL
158+
if (use_gls_) {
159+
auto gtk_window = window.gobj();
160+
// this has to be executed before GtkWindow.realize
161+
gtk_layer_init_for_window(gtk_window);
162+
gtk_layer_set_keyboard_interactivity(gtk_window, FALSE);
163+
auto layer =
164+
config["layer"] == "top" ? GTK_LAYER_SHELL_LAYER_TOP : GTK_LAYER_SHELL_LAYER_BOTTOM;
165+
gtk_layer_set_layer(gtk_window, layer);
166+
gtk_layer_set_monitor(gtk_window, output->monitor->gobj());
167+
gtk_layer_set_namespace(gtk_window, "waybar");
168+
169+
gtk_layer_set_anchor(
170+
gtk_window, GTK_LAYER_SHELL_EDGE_LEFT, anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT);
171+
gtk_layer_set_anchor(
172+
gtk_window, GTK_LAYER_SHELL_EDGE_RIGHT, anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
173+
gtk_layer_set_anchor(
174+
gtk_window, GTK_LAYER_SHELL_EDGE_TOP, anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP);
175+
gtk_layer_set_anchor(
176+
gtk_window, GTK_LAYER_SHELL_EDGE_BOTTOM, anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM);
177+
178+
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_LEFT, margins_.left);
179+
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_RIGHT, margins_.right);
180+
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_TOP, margins_.top);
181+
gtk_layer_set_margin(gtk_window, GTK_LAYER_SHELL_EDGE_BOTTOM, margins_.bottom);
182+
}
183+
#endif
184+
}
185+
139186
void waybar::Bar::onRealize() {
140187
auto gdk_window = window.get_window()->gobj();
141188
gdk_wayland_window_set_use_custom_surface(gdk_window);
@@ -145,29 +192,31 @@ void waybar::Bar::onMap(GdkEventAny* ev) {
145192
auto gdk_window = window.get_window()->gobj();
146193
surface = gdk_wayland_window_get_wl_surface(gdk_window);
147194

148-
auto client = waybar::Client::inst();
149-
// owned by output->monitor; no need to destroy
150-
auto wl_output = gdk_wayland_monitor_get_wl_output(output->monitor->gobj());
151-
auto layer =
152-
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(
154-
client->layer_shell, surface, wl_output, layer, "waybar");
195+
if (!use_gls_) {
196+
auto client = waybar::Client::inst();
197+
auto layer =
198+
config["layer"] == "top" ? ZWLR_LAYER_SHELL_V1_LAYER_TOP : ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM;
199+
// owned by output->monitor; no need to destroy
200+
auto wl_output = gdk_wayland_monitor_get_wl_output(output->monitor->gobj());
201+
layer_surface = zwlr_layer_shell_v1_get_layer_surface(
202+
client->layer_shell, surface, wl_output, layer, "waybar");
155203

156-
zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, false);
157-
zwlr_layer_surface_v1_set_anchor(layer_surface, anchor_);
158-
zwlr_layer_surface_v1_set_margin(
159-
layer_surface, margins_.top, margins_.right, margins_.bottom, margins_.left);
160-
setSurfaceSize(width_, height_);
161-
setExclusiveZone(width_, height_);
204+
zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, false);
205+
zwlr_layer_surface_v1_set_anchor(layer_surface, anchor_);
206+
zwlr_layer_surface_v1_set_margin(
207+
layer_surface, margins_.top, margins_.right, margins_.bottom, margins_.left);
208+
setSurfaceSize(width_, height_);
209+
setExclusiveZone(width_, height_);
162210

163-
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
164-
.configure = layerSurfaceHandleConfigure,
165-
.closed = layerSurfaceHandleClosed,
166-
};
167-
zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, this);
211+
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
212+
.configure = layerSurfaceHandleConfigure,
213+
.closed = layerSurfaceHandleClosed,
214+
};
215+
zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, this);
168216

169-
wl_surface_commit(surface);
170-
wl_display_roundtrip(client->wl_display);
217+
wl_surface_commit(surface);
218+
wl_display_roundtrip(client->wl_display);
219+
}
171220
}
172221

173222
void waybar::Bar::setExclusiveZone(uint32_t width, uint32_t height) {
@@ -184,7 +233,15 @@ void waybar::Bar::setExclusiveZone(uint32_t width, uint32_t height) {
184233
}
185234
}
186235
spdlog::debug("Set exclusive zone {} for output {}", zone, output->name);
187-
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, zone);
236+
237+
#ifdef HAVE_GTK_LAYER_SHELL
238+
if (use_gls_) {
239+
gtk_layer_set_exclusive_zone(window.gobj(), zone);
240+
} else
241+
#endif
242+
{
243+
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, zone);
244+
}
188245
}
189246

190247
void waybar::Bar::setSurfaceSize(uint32_t width, uint32_t height) {

0 commit comments

Comments
 (0)