Skip to content

Commit

Permalink
Merge pull request #2921 from alebastr/module-classes
Browse files Browse the repository at this point in the history
Add `module` class to the root elements of the modules
  • Loading branch information
Alexays authored Feb 16, 2024
2 parents 28cd9df + d590d50 commit 3cd3118
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 77 deletions.
2 changes: 2 additions & 0 deletions include/AModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace waybar {

class AModule : public IModule {
public:
static constexpr const char *MODULE_CLASS = "module";

virtual ~AModule();
auto update() -> void override;
virtual auto refresh(int) -> void{};
Expand Down
5 changes: 3 additions & 2 deletions include/bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class BarSurface {
virtual void setLayer(bar_layer layer) = 0;
virtual void setMargins(const struct bar_margins &margins) = 0;
virtual void setPassThrough(bool enable) = 0;
virtual void setPosition(const std::string_view &position) = 0;
virtual void setPosition(Gtk::PositionType position) = 0;
virtual void setSize(uint32_t width, uint32_t height) = 0;
virtual void commit(){};

Expand All @@ -89,8 +89,9 @@ class Bar {
Json::Value config;
struct wl_surface *surface;
bool visible = true;
bool vertical = false;
Gtk::Window window;
Gtk::Orientation orientation = Gtk::ORIENTATION_HORIZONTAL;
Gtk::PositionType position = Gtk::POS_TOP;

int x_global;
int y_global;
Expand Down
1 change: 1 addition & 0 deletions src/ALabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ALabel::ALabel(const Json::Value& config, const std::string& name, const std::st
if (!id.empty()) {
label_.get_style_context()->add_class(id);
}
label_.get_style_context()->add_class(MODULE_CLASS);
event_box_.add(label_);
if (config_["max-length"].isUInt()) {
label_.set_max_width_chars(config_["max-length"].asInt());
Expand Down
1 change: 1 addition & 0 deletions src/ASlider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ASlider::ASlider(const Json::Value& config, const std::string& name, const std::
if (!id.empty()) {
scale_.get_style_context()->add_class(id);
}
scale_.get_style_context()->add_class(MODULE_CLASS);
event_box_.add(scale_);
scale_.signal_value_changed().connect(sigc::mem_fun(*this, &ASlider::onValueChanged));

Expand Down
159 changes: 102 additions & 57 deletions src/bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ void from_json(const Json::Value& j, bar_mode& m) {
}
}

/* Deserializer for enum Gtk::PositionType */
void from_json(const Json::Value& j, Gtk::PositionType& pos) {

Check warning on line 97 in src/bar.cpp

View workflow job for this annotation

GitHub Actions / build

src/bar.cpp:97:6 [readability-identifier-naming]

invalid case style for function 'from_json'
if (j == "left") {
pos = Gtk::POS_LEFT;
} else if (j == "right") {
pos = Gtk::POS_RIGHT;
} else if (j == "top") {
pos = Gtk::POS_TOP;
} else if (j == "bottom") {
pos = Gtk::POS_BOTTOM;
}
}

Glib::ustring to_string(Gtk::PositionType pos) {

Check warning on line 109 in src/bar.cpp

View workflow job for this annotation

GitHub Actions / build

src/bar.cpp:109:15 [readability-identifier-naming]

invalid case style for function 'to_string'
switch (pos) {
case Gtk::POS_LEFT:
return "left";
case Gtk::POS_RIGHT:
return "right";
case Gtk::POS_TOP:
return "top";
case Gtk::POS_BOTTOM:
return "bottom";
}
}

/* Deserializer for JSON Object -> map<string compatible type, Value>
* Assumes that all the values in the object are deserializable to the same type.
*/
Expand Down Expand Up @@ -158,18 +184,26 @@ struct GLSSurfaceImpl : public BarSurface, public sigc::trackable {
}
}

void setPosition(const std::string_view& position) override {
void setPosition(Gtk::PositionType position) override {
auto unanchored = GTK_LAYER_SHELL_EDGE_BOTTOM;
vertical_ = false;
if (position == "bottom") {
unanchored = GTK_LAYER_SHELL_EDGE_TOP;
} else if (position == "left") {
unanchored = GTK_LAYER_SHELL_EDGE_RIGHT;
vertical_ = true;
} else if (position == "right") {
vertical_ = true;
unanchored = GTK_LAYER_SHELL_EDGE_LEFT;
}
orientation_ = Gtk::ORIENTATION_HORIZONTAL;
switch (position) {
case Gtk::POS_LEFT:
unanchored = GTK_LAYER_SHELL_EDGE_RIGHT;
orientation_ = Gtk::ORIENTATION_VERTICAL;
break;
case Gtk::POS_RIGHT:
unanchored = GTK_LAYER_SHELL_EDGE_LEFT;
orientation_ = Gtk::ORIENTATION_VERTICAL;
break;
case Gtk::POS_TOP:
unanchored = GTK_LAYER_SHELL_EDGE_BOTTOM;
break;
case Gtk::POS_BOTTOM:
unanchored = GTK_LAYER_SHELL_EDGE_TOP;
break;
};

for (auto edge : {GTK_LAYER_SHELL_EDGE_LEFT, GTK_LAYER_SHELL_EDGE_RIGHT,
GTK_LAYER_SHELL_EDGE_TOP, GTK_LAYER_SHELL_EDGE_BOTTOM}) {
gtk_layer_set_anchor(window_.gobj(), edge, unanchored != edge);
Expand All @@ -178,10 +212,10 @@ struct GLSSurfaceImpl : public BarSurface, public sigc::trackable {
// Disable anchoring for other edges too if the width
// or the height has been set to a value other than 'auto'
// otherwise the bar will use all space
if (vertical_ && height_ > 1) {
if (orientation_ == Gtk::ORIENTATION_VERTICAL && height_ > 1) {
gtk_layer_set_anchor(window_.gobj(), GTK_LAYER_SHELL_EDGE_BOTTOM, false);
gtk_layer_set_anchor(window_.gobj(), GTK_LAYER_SHELL_EDGE_TOP, false);
} else if (!vertical_ && width_ > 1) {
} else if (orientation_ == Gtk::ORIENTATION_HORIZONTAL && width_ > 1) {
gtk_layer_set_anchor(window_.gobj(), GTK_LAYER_SHELL_EDGE_LEFT, false);
gtk_layer_set_anchor(window_.gobj(), GTK_LAYER_SHELL_EDGE_RIGHT, false);
}
Expand All @@ -195,11 +229,11 @@ struct GLSSurfaceImpl : public BarSurface, public sigc::trackable {

private:
Gtk::Window& window_;
Gtk::Orientation orientation_ = Gtk::ORIENTATION_HORIZONTAL;

Check warning on line 232 in src/bar.cpp

View workflow job for this annotation

GitHub Actions / build

src/bar.cpp:232:20 [readability-identifier-naming]

invalid case style for private member 'orientation_'
std::string output_name_;
uint32_t width_;
uint32_t height_;
bool passthrough_ = false;
bool vertical_ = false;

void onMap(GdkEventAny* ev) { setPassThrough(passthrough_); }

Expand All @@ -212,7 +246,7 @@ struct GLSSurfaceImpl : public BarSurface, public sigc::trackable {
* Note: forced resizing to a window smaller than required by GTK would not work with
* gtk-layer-shell.
*/
if (vertical_) {
if (orientation_ == Gtk::ORIENTATION_VERTICAL) {
if (width_ > 1 && ev->width > static_cast<int>(width_)) {
spdlog::warn(MIN_WIDTH_MSG, width_, ev->width);
}
Expand Down Expand Up @@ -304,15 +338,21 @@ struct RawSurfaceImpl : public BarSurface, public sigc::trackable {
}
}

void setPosition(const std::string_view& position) override {
anchor_ = HORIZONTAL_ANCHOR | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP;
if (position == "bottom") {
anchor_ = HORIZONTAL_ANCHOR | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
} else if (position == "left") {
anchor_ = VERTICAL_ANCHOR | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT;
} else if (position == "right") {
anchor_ = VERTICAL_ANCHOR | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
}
void setPosition(Gtk::PositionType position) override {
switch (position) {
case Gtk::POS_LEFT:
anchor_ = VERTICAL_ANCHOR | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT;
break;
case Gtk::POS_RIGHT:
anchor_ = VERTICAL_ANCHOR | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
break;
case Gtk::POS_TOP:
anchor_ = HORIZONTAL_ANCHOR | ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP;
break;
case Gtk::POS_BOTTOM:
anchor_ = HORIZONTAL_ANCHOR | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM;
break;
};

// updating already mapped window
if (layer_surface_) {
Expand Down Expand Up @@ -493,17 +533,18 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
window.set_decorated(false);
window.get_style_context()->add_class(output->name);
window.get_style_context()->add_class(config["name"].asString());
window.get_style_context()->add_class(config["position"].asString());

auto position = config["position"].asString();
from_json(config["position"], position);
orientation = (position == Gtk::POS_LEFT || position == Gtk::POS_RIGHT)
? Gtk::ORIENTATION_VERTICAL
: Gtk::ORIENTATION_HORIZONTAL;

if (position == "right" || position == "left") {
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;
}
window.get_style_context()->add_class(to_string(position));

left_ = Gtk::Box(orientation, 0);
center_ = Gtk::Box(orientation, 0);
right_ = Gtk::Box(orientation, 0);
box_ = Gtk::Box(orientation, 0);

left_.get_style_context()->add_class("modules-left");
center_.get_style_context()->add_class("modules-center");
Expand Down Expand Up @@ -829,34 +870,38 @@ void waybar::Bar::onConfigure(GdkEventConfigure* ev) {

void waybar::Bar::configureGlobalOffset(int width, int height) {
auto monitor_geometry = *output->monitor->property_geometry().get_value().gobj();
auto position = config["position"].asString();
int x;
int y;
if (position == "bottom") {
if (width + margins_.left + margins_.right >= monitor_geometry.width)
switch (position) {
case Gtk::POS_BOTTOM:
if (width + margins_.left + margins_.right >= monitor_geometry.width)
x = margins_.left;
else
x = (monitor_geometry.width - width) / 2;
y = monitor_geometry.height - height - margins_.bottom;
break;
case Gtk::POS_LEFT:
x = margins_.left;
else
x = (monitor_geometry.width - width) / 2;
y = monitor_geometry.height - height - margins_.bottom;
} else if (position == "left") {
x = margins_.left;
if (height + margins_.top + margins_.bottom >= monitor_geometry.height)
y = margins_.top;
else
y = (monitor_geometry.height - height) / 2;
} else if (position == "right") {
x = monitor_geometry.width - width - margins_.right;
if (height + margins_.top + margins_.bottom >= monitor_geometry.height)
if (height + margins_.top + margins_.bottom >= monitor_geometry.height)
y = margins_.top;
else
y = (monitor_geometry.height - height) / 2;
break;
case Gtk::POS_RIGHT:
x = monitor_geometry.width - width - margins_.right;
if (height + margins_.top + margins_.bottom >= monitor_geometry.height)
y = margins_.top;
else
y = (monitor_geometry.height - height) / 2;
break;
case Gtk::POS_TOP:
// position is top
if (width + margins_.left + margins_.right >= monitor_geometry.width)
x = margins_.left;
else
x = (monitor_geometry.width - width) / 2;
y = margins_.top;
else
y = (monitor_geometry.height - height) / 2;
} else {
// position is top
if (width + margins_.left + margins_.right >= monitor_geometry.width)
x = margins_.left;
else
x = (monitor_geometry.width - width) / 2;
y = margins_.top;
break;
}

x_global = x + monitor_geometry.x;
Expand Down
12 changes: 7 additions & 5 deletions src/modules/custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,18 @@ auto waybar::modules::Custom::update() -> void {
}
}
}
auto classes = label_.get_style_context()->list_classes();
auto style = label_.get_style_context();
auto classes = style->list_classes();
for (auto const& c : classes) {
if (c == id_) continue;
label_.get_style_context()->remove_class(c);
style->remove_class(c);
}
for (auto const& c : class_) {
label_.get_style_context()->add_class(c);
style->add_class(c);
}
label_.get_style_context()->add_class("flat");
label_.get_style_context()->add_class("text-button");
style->add_class("flat");
style->add_class("text-button");
style->add_class(MODULE_CLASS);
event_box_.show();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/dwl/tags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Tags::Tags(const std::string &id, const waybar::Bar &bar, const Json::Value &con
status_manager_{nullptr},
seat_{nullptr},
bar_(bar),
box_{bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0},
box_{bar.orientation, 0},
output_status_{nullptr} {
struct wl_display *display = Client::inst()->wl_display;
struct wl_registry *registry = wl_display_get_registry(display);
Expand All @@ -113,6 +113,7 @@ Tags::Tags(const std::string &id, const waybar::Bar &bar, const Json::Value &con
if (!id.empty()) {
box_.get_style_context()->add_class(id);
}
box_.get_style_context()->add_class(MODULE_CLASS);
event_box_.add(box_);

// Default to 9 tags, cap at 32
Expand Down
5 changes: 2 additions & 3 deletions src/modules/hyprland/workspaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ int Workspaces::windowRewritePriorityFunction(std::string const &window_rule) {
}

Workspaces::Workspaces(const std::string &id, const Bar &bar, const Json::Value &config)
: AModule(config, "workspaces", id, false, false),
m_bar(bar),
m_box(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0) {
: AModule(config, "workspaces", id, false, false), m_bar(bar), m_box(bar.orientation, 0) {
modulesReady = true;
parseConfig(config);

m_box.set_name("workspaces");
if (!id.empty()) {
m_box.get_style_context()->add_class(id);
}
m_box.get_style_context()->add_class(MODULE_CLASS);
event_box_.add(m_box);

if (!gIPC) {
Expand Down
1 change: 1 addition & 0 deletions src/modules/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ waybar::modules::Image::Image(const std::string& id, const Json::Value& config)
if (!id.empty()) {
box_.get_style_context()->add_class(id);
}
box_.get_style_context()->add_class(MODULE_CLASS);
event_box_.add(box_);

dp.emit();
Expand Down
3 changes: 2 additions & 1 deletion src/modules/keyboard_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ auto supportsLockStates(const libevdev* dev) -> bool {
waybar::modules::KeyboardState::KeyboardState(const std::string& id, const Bar& bar,
const Json::Value& config)
: AModule(config, "keyboard-state", id, false, !config["disable-scroll"].asBool()),
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0),
box_(bar.orientation, 0),
numlock_label_(""),
capslock_label_(""),
numlock_format_(config_["format"].isString() ? config_["format"].asString()
Expand Down Expand Up @@ -132,6 +132,7 @@ waybar::modules::KeyboardState::KeyboardState(const std::string& id, const Bar&
if (!id.empty()) {
box_.get_style_context()->add_class(id);
}
box_.get_style_context()->add_class(MODULE_CLASS);
event_box_.add(box_);
if (config_["device-path"].isString()) {
Expand Down
3 changes: 2 additions & 1 deletion src/modules/river/tags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Tags::Tags(const std::string &id, const waybar::Bar &bar, const Json::Value &con
control_{nullptr},
seat_{nullptr},
bar_(bar),
box_{bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0},
box_{bar.orientation, 0},
output_status_{nullptr} {
struct wl_display *display = Client::inst()->wl_display;
struct wl_registry *registry = wl_display_get_registry(display);
Expand All @@ -111,6 +111,7 @@ Tags::Tags(const std::string &id, const waybar::Bar &bar, const Json::Value &con
if (!id.empty()) {
box_.get_style_context()->add_class(id);
}
box_.get_style_context()->add_class(MODULE_CLASS);
event_box_.add(box_);

// Default to 9 tags, cap at 32
Expand Down
3 changes: 2 additions & 1 deletion src/modules/sni/tray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace waybar::modules::SNI {

Tray::Tray(const std::string& id, const Bar& bar, const Json::Value& config)
: AModule(config, "tray", id),
box_(bar.vertical ? Gtk::ORIENTATION_VERTICAL : Gtk::ORIENTATION_HORIZONTAL, 0),
box_(bar.orientation, 0),
watcher_(SNI::Watcher::getInstance()),
host_(nb_hosts_, config, bar, std::bind(&Tray::onAdd, this, std::placeholders::_1),
std::bind(&Tray::onRemove, this, std::placeholders::_1)) {
Expand All @@ -15,6 +15,7 @@ Tray::Tray(const std::string& id, const Bar& bar, const Json::Value& config)
if (!id.empty()) {
box_.get_style_context()->add_class(id);
}
box_.get_style_context()->add_class(MODULE_CLASS);
if (config_["spacing"].isUInt()) {
box_.set_spacing(config_["spacing"].asUInt());
}
Expand Down
Loading

0 comments on commit 3cd3118

Please sign in to comment.