feat: allow waybar to be positioned on left/right

This commit is contained in:
Alex 2019-03-22 12:25:05 +01:00
parent f700319d7f
commit 47142a61ae
7 changed files with 48 additions and 19 deletions

View File

@ -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);

View File

@ -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 &();

View File

@ -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

View File

@ -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<GtkWidget*>(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<uint32_t>(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<uint32_t>(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;

View File

@ -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

View File

@ -2,8 +2,11 @@
#include <iostream>
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))
{

View File

@ -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()) {