From 089d1299c4c930bc45611c7b706457b6d06268c0 Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Thu, 22 Aug 2019 23:29:18 -0700 Subject: [PATCH 1/2] fix: correct calculation of exclusive zone --- include/bar.hpp | 2 +- src/bar.cpp | 114 ++++++++++++++++++++++++++---------------------- 2 files changed, 64 insertions(+), 52 deletions(-) diff --git a/include/bar.hpp b/include/bar.hpp index 65db91a2..95cab89c 100644 --- a/include/bar.hpp +++ b/include/bar.hpp @@ -55,7 +55,7 @@ class Bar { void onConfigure(GdkEventConfigure *ev); void onRealize(); void onMap(GdkEventAny *ev); - void setMarginsAndZone(uint32_t height, uint32_t width); + void setExclusiveZone(uint32_t width, uint32_t height); auto setupWidgets() -> void; void getModules(const Factory &, const std::string &); void setupAltFormatKeyForModule(const std::string &module_name); diff --git a/src/bar.cpp b/src/bar.cpp index 8e643496..7204b9dc 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -53,6 +53,51 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config) vertical = true; } + if (config["margin-top"].isInt() || config["margin-right"].isInt() || + config["margin-bottom"].isInt() || config["margin-left"].isInt()) { + margins_ = { + config["margin-top"].isInt() ? config["margin-top"].asInt() : 0, + config["margin-right"].isInt() ? config["margin-right"].asInt() : 0, + config["margin-bottom"].isInt() ? config["margin-bottom"].asInt() : 0, + config["margin-left"].isInt() ? config["margin-left"].asInt() : 0, + }; + } else if (config["margin"].isString()) { + std::istringstream iss(config["margin"].asString()); + std::vector margins{std::istream_iterator(iss), {}}; + try { + if (margins.size() == 1) { + auto gaps = std::stoi(margins[0], nullptr, 10); + margins_ = {.top = gaps, .right = gaps, .bottom = gaps, .left = gaps}; + } + if (margins.size() == 2) { + auto vertical_margins = std::stoi(margins[0], nullptr, 10); + auto horizontal_margins = std::stoi(margins[1], nullptr, 10); + margins_ = {.top = vertical_margins, + .right = horizontal_margins, + .bottom = vertical_margins, + .left = horizontal_margins}; + } + if (margins.size() == 3) { + auto horizontal_margins = std::stoi(margins[1], nullptr, 10); + margins_ = {.top = std::stoi(margins[0], nullptr, 10), + .right = horizontal_margins, + .bottom = std::stoi(margins[2], nullptr, 10), + .left = horizontal_margins}; + } + if (margins.size() == 4) { + margins_ = {.top = std::stoi(margins[0], nullptr, 10), + .right = std::stoi(margins[1], nullptr, 10), + .bottom = std::stoi(margins[2], nullptr, 10), + .left = std::stoi(margins[3], nullptr, 10)}; + } + } catch (...) { + spdlog::warn("Invalid margins: {}", config["margin"].asString()); + } + } else if (config["margin"].isInt()) { + auto gaps = config["margin"].asInt(); + margins_ = {.top = gaps, .right = gaps, .bottom = gaps, .left = gaps}; + } + setupWidgets(); if (window.get_realized()) { @@ -109,7 +154,10 @@ void waybar::Bar::onMap(GdkEventAny* ev) { zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, false); zwlr_layer_surface_v1_set_anchor(layer_surface, anchor_); zwlr_layer_surface_v1_set_size(layer_surface, width_, height_); - setMarginsAndZone(height_, width_); + zwlr_layer_surface_v1_set_margin( + layer_surface, margins_.top, margins_.right, margins_.bottom, margins_.left); + setExclusiveZone(width_, height_); + static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { .configure = layerSurfaceHandleConfigure, .closed = layerSurfaceHandleClosed, @@ -120,54 +168,20 @@ void waybar::Bar::onMap(GdkEventAny* ev) { wl_display_roundtrip(client->wl_display); } -void waybar::Bar::setMarginsAndZone(uint32_t height, uint32_t width) { - if (config["margin-top"].isInt() || config["margin-right"].isInt() || - config["margin-bottom"].isInt() || config["margin-left"].isInt()) { - margins_ = { - config["margin-top"].isInt() ? config["margin-top"].asInt() : 0, - config["margin-right"].isInt() ? config["margin-right"].asInt() : 0, - config["margin-bottom"].isInt() ? config["margin-bottom"].asInt() : 0, - config["margin-left"].isInt() ? config["margin-left"].asInt() : 0, - }; - } else if (config["margin"].isString()) { - std::istringstream iss(config["margin"].asString()); - std::vector margins{std::istream_iterator(iss), {}}; - try { - if (margins.size() == 1) { - auto gaps = std::stoi(margins[0], nullptr, 10); - margins_ = {.top = gaps, .right = gaps, .bottom = gaps, .left = gaps}; - } - if (margins.size() == 2) { - auto vertical_margins = std::stoi(margins[0], nullptr, 10); - auto horizontal_margins = std::stoi(margins[1], nullptr, 10); - margins_ = {.top = vertical_margins, - .right = horizontal_margins, - .bottom = vertical_margins, - .left = horizontal_margins}; - } - if (margins.size() == 3) { - auto horizontal_margins = std::stoi(margins[1], nullptr, 10); - margins_ = {.top = std::stoi(margins[0], nullptr, 10), - .right = horizontal_margins, - .bottom = std::stoi(margins[2], nullptr, 10), - .left = horizontal_margins}; - } - if (margins.size() == 4) { - margins_ = {.top = std::stoi(margins[0], nullptr, 10), - .right = std::stoi(margins[1], nullptr, 10), - .bottom = std::stoi(margins[2], nullptr, 10), - .left = std::stoi(margins[3], nullptr, 10)}; - } - } catch (...) { - spdlog::warn("Invalid margins: {}", config["margin"].asString()); +void waybar::Bar::setExclusiveZone(uint32_t width, uint32_t height) { + auto zone = 0; + if (visible) { + // exclusive zone already includes margin for anchored edge, + // only opposite margin should be added + if (vertical) { + zone += width; + zone += (anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) ? margins_.right : margins_.left; + } else { + zone += height; + zone += (anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) ? margins_.bottom : margins_.top; } - } else if (config["margin"].isInt()) { - auto gaps = config["margin"].asInt(); - margins_ = {.top = gaps, .right = gaps, .bottom = gaps, .left = gaps}; } - zwlr_layer_surface_v1_set_margin( - layer_surface, margins_.top, margins_.right, margins_.bottom, margins_.left); - auto zone = vertical ? width + margins_.right : height + margins_.bottom; + spdlog::debug("Set exclusive zone {} for output {}", zone, output->name); zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, zone); } @@ -240,8 +254,7 @@ void waybar::Bar::layerSurfaceHandleConfigure(void* data, struct zwlr_layer_surf o->height_ = height; o->window.set_size_request(o->width_, o->height_); o->window.resize(o->width_, o->height_); - auto zone = o->vertical ? width + o->margins_.right : height + o->margins_.bottom; - zwlr_layer_surface_v1_set_exclusive_zone(o->layer_surface, zone); + o->setExclusiveZone(width, height); spdlog::info(BAR_SIZE_MSG, o->width_ == 1 ? "auto" : std::to_string(o->width_), o->height_ == 1 ? "auto" : std::to_string(o->height_), @@ -264,13 +277,12 @@ void waybar::Bar::layerSurfaceHandleClosed(void* data, struct zwlr_layer_surface auto waybar::Bar::toggle() -> void { visible = !visible; - auto zone = visible ? height_ : 0; if (!visible) { window.get_style_context()->add_class("hidden"); } else { window.get_style_context()->remove_class("hidden"); } - zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, zone); + setExclusiveZone(width_, height_); wl_surface_commit(surface); } From 529daedcecf8e7f4f9c280f7e110a4990bc286a2 Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Sun, 1 Sep 2019 00:54:53 -0700 Subject: [PATCH 2/2] fix: correct handling of margins on anchored axis --- include/bar.hpp | 1 + src/bar.cpp | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/include/bar.hpp b/include/bar.hpp index 95cab89c..7566b088 100644 --- a/include/bar.hpp +++ b/include/bar.hpp @@ -56,6 +56,7 @@ class Bar { void onRealize(); void onMap(GdkEventAny *ev); void setExclusiveZone(uint32_t width, uint32_t height); + void setSurfaceSize(uint32_t width, uint32_t height); auto setupWidgets() -> void; void getModules(const Factory &, const std::string &); void setupAltFormatKeyForModule(const std::string &module_name); diff --git a/src/bar.cpp b/src/bar.cpp index 7204b9dc..9de09b87 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -132,7 +132,7 @@ void waybar::Bar::onConfigure(GdkEventConfigure* ev) { } } if (tmp_width != width_ || tmp_height != height_) { - zwlr_layer_surface_v1_set_size(layer_surface, tmp_width, tmp_height); + setSurfaceSize(tmp_width, tmp_height); } } @@ -153,9 +153,9 @@ void waybar::Bar::onMap(GdkEventAny* ev) { zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, false); zwlr_layer_surface_v1_set_anchor(layer_surface, anchor_); - zwlr_layer_surface_v1_set_size(layer_surface, width_, height_); zwlr_layer_surface_v1_set_margin( layer_surface, margins_.top, margins_.right, margins_.bottom, margins_.left); + setSurfaceSize(width_, height_); setExclusiveZone(width_, height_); static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { @@ -185,6 +185,22 @@ void waybar::Bar::setExclusiveZone(uint32_t width, uint32_t height) { zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, zone); } +void waybar::Bar::setSurfaceSize(uint32_t width, uint32_t height) { + /* If the client is anchored to two opposite edges, layer_surface.configure will return + * size without margins for the axis. + * layer_surface.set_size, however, expects size with margins for the anchored axis. + * This is not specified by wlr-layer-shell and based on actual behavior of sway. + */ + if (vertical && height > 1) { + height += margins_.top + margins_.bottom; + } + if (!vertical && width > 1) { + width += margins_.right + margins_.left; + } + spdlog::debug("Set surface size {}x{} for output {}", width, height, output->name); + zwlr_layer_surface_v1_set_size(layer_surface, width, height); +} + // Converting string to button code rn as to avoid doing it later void waybar::Bar::setupAltFormatKeyForModule(const std::string& module_name) { if (config.isMember(module_name)) {