fix: correct calculation of exclusive zone

This commit is contained in:
Aleksei Bavshin 2019-08-22 23:29:18 -07:00
parent 36fc8365ee
commit 089d1299c4
No known key found for this signature in database
GPG Key ID: 4F071603387A382A
2 changed files with 64 additions and 52 deletions

View File

@ -55,7 +55,7 @@ class Bar {
void onConfigure(GdkEventConfigure *ev); void onConfigure(GdkEventConfigure *ev);
void onRealize(); void onRealize();
void onMap(GdkEventAny *ev); void onMap(GdkEventAny *ev);
void setMarginsAndZone(uint32_t height, uint32_t width); void setExclusiveZone(uint32_t width, uint32_t height);
auto setupWidgets() -> void; auto setupWidgets() -> void;
void getModules(const Factory &, const std::string &); void getModules(const Factory &, const std::string &);
void setupAltFormatKeyForModule(const std::string &module_name); void setupAltFormatKeyForModule(const std::string &module_name);

View File

@ -53,6 +53,51 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
vertical = true; 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<std::string> margins{std::istream_iterator<std::string>(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(); setupWidgets();
if (window.get_realized()) { 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_keyboard_interactivity(layer_surface, false);
zwlr_layer_surface_v1_set_anchor(layer_surface, anchor_); zwlr_layer_surface_v1_set_anchor(layer_surface, anchor_);
zwlr_layer_surface_v1_set_size(layer_surface, width_, height_); 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 = { static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
.configure = layerSurfaceHandleConfigure, .configure = layerSurfaceHandleConfigure,
.closed = layerSurfaceHandleClosed, .closed = layerSurfaceHandleClosed,
@ -120,54 +168,20 @@ void waybar::Bar::onMap(GdkEventAny* ev) {
wl_display_roundtrip(client->wl_display); wl_display_roundtrip(client->wl_display);
} }
void waybar::Bar::setMarginsAndZone(uint32_t height, uint32_t width) { void waybar::Bar::setExclusiveZone(uint32_t width, uint32_t height) {
if (config["margin-top"].isInt() || config["margin-right"].isInt() || auto zone = 0;
config["margin-bottom"].isInt() || config["margin-left"].isInt()) { if (visible) {
margins_ = { // exclusive zone already includes margin for anchored edge,
config["margin-top"].isInt() ? config["margin-top"].asInt() : 0, // only opposite margin should be added
config["margin-right"].isInt() ? config["margin-right"].asInt() : 0, if (vertical) {
config["margin-bottom"].isInt() ? config["margin-bottom"].asInt() : 0, zone += width;
config["margin-left"].isInt() ? config["margin-left"].asInt() : 0, zone += (anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT) ? margins_.right : margins_.left;
}; } else {
} else if (config["margin"].isString()) { zone += height;
std::istringstream iss(config["margin"].asString()); zone += (anchor_ & ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) ? margins_.bottom : margins_.top;
std::vector<std::string> margins{std::istream_iterator<std::string>(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) { spdlog::debug("Set exclusive zone {} for output {}", zone, output->name);
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};
}
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;
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, zone); 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->height_ = height;
o->window.set_size_request(o->width_, o->height_); o->window.set_size_request(o->width_, o->height_);
o->window.resize(o->width_, o->height_); o->window.resize(o->width_, o->height_);
auto zone = o->vertical ? width + o->margins_.right : height + o->margins_.bottom; o->setExclusiveZone(width, height);
zwlr_layer_surface_v1_set_exclusive_zone(o->layer_surface, zone);
spdlog::info(BAR_SIZE_MSG, spdlog::info(BAR_SIZE_MSG,
o->width_ == 1 ? "auto" : std::to_string(o->width_), o->width_ == 1 ? "auto" : std::to_string(o->width_),
o->height_ == 1 ? "auto" : std::to_string(o->height_), 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 { auto waybar::Bar::toggle() -> void {
visible = !visible; visible = !visible;
auto zone = visible ? height_ : 0;
if (!visible) { if (!visible) {
window.get_style_context()->add_class("hidden"); window.get_style_context()->add_class("hidden");
} else { } else {
window.get_style_context()->remove_class("hidden"); window.get_style_context()->remove_class("hidden");
} }
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, zone); setExclusiveZone(width_, height_);
wl_surface_commit(surface); wl_surface_commit(surface);
} }