Skip to content

Commit

Permalink
feat: allow waybar to be positioned on left/right
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexays committed Mar 22, 2019
1 parent f700319 commit 47142a6
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 18 deletions.
1 change: 1 addition & 0 deletions include/bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion include/modules/sni/tray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 &();
Expand Down
2 changes: 1 addition & 1 deletion resources/config
Original file line number Diff line number Diff line change
@@ -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
Expand Down
47 changes: 35 additions & 12 deletions src/bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -84,29 +90,41 @@ 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,
.closed = layerSurfaceHandleClosed,
};
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;
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 {
anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP;
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;
}

auto height = config_["height"].isUInt() ? config_["height"].asUInt() : height_;
auto width = config_["width"].isUInt() ? config_["width"].asUInt() : width_;
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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/modules/sni/tray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
4 changes: 3 additions & 1 deletion src/modules/sway/workspaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit 47142a6

Please sign in to comment.