From 47142a61ae3e5a19703870c3959013de3fef344a Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 22 Mar 2019 12:25:05 +0100 Subject: [PATCH] feat: allow waybar to be positioned on left/right --- include/bar.hpp | 1 + include/modules/sni/tray.hpp | 2 +- resources/config | 2 +- src/bar.cpp | 49 ++++++++++++++++++++++++--------- src/factory.cpp | 2 +- src/modules/sni/tray.cpp | 7 +++-- src/modules/sway/workspaces.cpp | 4 ++- 7 files changed, 48 insertions(+), 19 deletions(-) diff --git a/include/bar.hpp b/include/bar.hpp index 2235dc77..bb48aeb8 100644 --- a/include/bar.hpp +++ b/include/bar.hpp @@ -32,6 +32,7 @@ class Bar { std::string output_name; uint32_t wl_name; bool visible = true; + bool vertical = false; private: static void handleLogicalPosition(void *, struct zxdg_output_v1 *, int32_t, int32_t); diff --git a/include/modules/sni/tray.hpp b/include/modules/sni/tray.hpp index 2b27604a..960f0a0a 100644 --- a/include/modules/sni/tray.hpp +++ b/include/modules/sni/tray.hpp @@ -11,7 +11,7 @@ namespace waybar::modules::SNI { class Tray : public IModule { public: - Tray(const std::string&, const Json::Value&); + Tray(const std::string&, const Bar&, const Json::Value&); ~Tray() = default; auto update() -> void; operator Gtk::Widget &(); diff --git a/resources/config b/resources/config index 312a2657..28cceb98 100644 --- a/resources/config +++ b/resources/config @@ -1,6 +1,6 @@ { "layer": "top", // Waybar at top layer - // "position": "bottom", // Waybar at the bottom of your screen + // "position": "bottom", // Waybar position (top|bottom|left|right) // "height": 30, // Waybar height // "width": 1280, // Waybar width // Choose the order of the modules diff --git a/src/bar.cpp b/src/bar.cpp index 086def0a..820e54b8 100644 --- a/src/bar.cpp +++ b/src/bar.cpp @@ -28,6 +28,12 @@ waybar::Bar::Bar(const Client& client, setupConfig(); setupCss(); + if (config_["position"] == "right" || config_["position"] == "left") { + vertical = true; + height_ = 0; + width_ = 30; + } + auto wrap = reinterpret_cast(window.gobj()); gtk_widget_realize(wrap); GdkWindow *gdk_window = gtk_widget_get_window(wrap); @@ -84,10 +90,10 @@ void waybar::Bar::initBar() setupAltFormatKeyForModuleList("modules-left"); setupAltFormatKeyForModuleList("modules-right"); setupAltFormatKeyForModuleList("modules-center"); - std::size_t layer_top = config_["layer"] == "top" + std::size_t layer = config_["layer"] == "top" ? ZWLR_LAYER_SHELL_V1_LAYER_TOP : ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM; layer_surface = zwlr_layer_shell_v1_get_layer_surface( - client.layer_shell, surface, *output, layer_top, "waybar"); + client.layer_shell, surface, *output, layer, "waybar"); static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { .configure = layerSurfaceHandleConfigure, @@ -95,18 +101,30 @@ void waybar::Bar::initBar() }; zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, this); - std::size_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT - | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; - if (config_["position"] == "bottom") { - anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; - } else { - anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP; - } - auto height = config_["height"].isUInt() ? config_["height"].asUInt() : height_; auto width = config_["width"].isUInt() ? config_["width"].asUInt() : width_; + + std::size_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP; + if (config_["position"] == "bottom") { + anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; + } else if (config_["position"] == "left") { + anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT; + } else if (config_["position"] == "right") { + anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + } + if (anchor == ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM || anchor == ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP) { + anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + } else if (anchor == ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT || anchor == ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT) { + anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; + left_ = Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0); + center_ = Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0); + right_ = Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0); + box_ = Gtk::Box(Gtk::ORIENTATION_VERTICAL, 0); + vertical = true; + } + zwlr_layer_surface_v1_set_anchor(layer_surface, anchor); - zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, height); + zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, vertical ? width : height); zwlr_layer_surface_v1_set_size(layer_surface, width, height); wl_surface_commit(surface); @@ -213,13 +231,18 @@ void waybar::Bar::layerSurfaceHandleConfigure(void* data, o->window.set_size_request(o->width_, o->height_); o->window.resize(o->width_, o->height_); - int dummy_width, min_height; - o->window.get_size(dummy_width, min_height); + int min_width, min_height; + o->window.get_size(min_width, min_height); if (o->height_ < static_cast(min_height)) { std::cout << fmt::format("Requested height: {} exceeds the minimum \ height: {} required by the modules", o->height_, min_height) << std::endl; o->height_ = min_height; } + if (o->width_ < static_cast(min_width)) { + std::cout << fmt::format("Requested width: {} exceeds the minimum \ +width: {} required by the modules", o->height_, min_width) << std::endl; + o->width_ = min_width; + } std::cout << fmt::format( "Bar configured (width: {}, height: {}) for output: {}", o->width_, o->height_, o->output_name) << std::endl; diff --git a/src/factory.cpp b/src/factory.cpp index 18eef10d..11e3db26 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -38,7 +38,7 @@ waybar::IModule* waybar::Factory::makeModule(const std::string &name) const } #ifdef HAVE_DBUSMENU if (ref == "tray") { - return new waybar::modules::SNI::Tray(id, config_[name]); + return new waybar::modules::SNI::Tray(id, bar_, config_[name]); } #endif #ifdef HAVE_LIBNL diff --git a/src/modules/sni/tray.cpp b/src/modules/sni/tray.cpp index 2f372ba6..0cd764af 100644 --- a/src/modules/sni/tray.cpp +++ b/src/modules/sni/tray.cpp @@ -2,8 +2,11 @@ #include -waybar::modules::SNI::Tray::Tray(const std::string& id, const Json::Value &config) - : config_(config), watcher_(), host_(nb_hosts_, config, +waybar::modules::SNI::Tray::Tray(const std::string& id, const Bar& bar, + const Json::Value &config) + : config_(config), + box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0), + watcher_(), host_(nb_hosts_, config, std::bind(&Tray::onAdd, this, std::placeholders::_1), std::bind(&Tray::onRemove, this, std::placeholders::_1)) { diff --git a/src/modules/sway/workspaces.cpp b/src/modules/sway/workspaces.cpp index 2e760f91..2997f8e6 100644 --- a/src/modules/sway/workspaces.cpp +++ b/src/modules/sway/workspaces.cpp @@ -2,7 +2,9 @@ waybar::modules::sway::Workspaces::Workspaces(const std::string& id, const Bar& bar, const Json::Value& config) - : bar_(bar), config_(config), scrolling_(false) + : bar_(bar), config_(config), + box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0), + scrolling_(false) { box_.set_name("workspaces"); if (!id.empty()) {