Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/AModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AModule : public IModule {
static constexpr const char *MODULE_CLASS = "module";

~AModule() override;
sigc::signal<void, AModule*> signal_updated;
auto update() -> void override;
virtual auto refresh(int shouldRefresh) -> void {};
operator Gtk::Widget &() override;
Expand Down
7 changes: 6 additions & 1 deletion include/group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Group : public AModule {
operator Gtk::Widget &() override;

virtual Gtk::Box &getBox();
void addWidget(Gtk::Widget &widget);
void addWidget(AModule* module);

protected:
Gtk::Box box;
Expand All @@ -26,12 +26,17 @@ class Group : public AModule {
bool is_first_widget = true;
bool is_drawer = false;
bool click_to_reveal = false;
std::string always_visible_class;
std::string add_class_to_drawer_children;
bool handleMouseEnter(GdkEventCrossing *const &ev) override;
bool handleMouseLeave(GdkEventCrossing *const &ev) override;
bool handleToggle(GdkEventButton *const &ev) override;
void show_group();
void hide_group();
void manage_visibility(AModule* module);
void show_widget(Gtk::Widget& widget);
void hide_widget(Gtk::Widget& widget);
void hide_current_widget_if_inactive();
};

} // namespace waybar
1 change: 1 addition & 0 deletions src/AModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ auto AModule::update() -> void {
if (config_["on-update"].isString()) {
pid_.push_back(util::command::forkExec(config_["on-update"].asString()));
}
signal_updated.emit(this);
Copy link
Author

@piwonskp piwonskp Apr 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means that every module will emit an update signal.

An alternative solution is to wrap (before calling group->addWidget(module)) only the modules belonging to groups in a class that emits update signals, or limit it to those in dynamic drawers. This would limit signal emitting to dynamic-drawers and minimize the performance overhead on other modules to the bare minimum.

Depends on whether other modules could also benefit from update signaling.

}
// Get mapping between event name and module action name
// Then call overrided doAction in order to call appropriate module action
Expand Down
2 changes: 1 addition & 1 deletion src/bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ void waybar::Bar::getModules(const Factory& factory, const std::string& pos,
std::shared_ptr<AModule> module_sp(module);
modules_all_.emplace_back(module_sp);
if (group != nullptr) {
group->addWidget(*module);
group->addWidget(module);
} else {
if (pos == "modules-left") {
modules_left_.emplace_back(module_sp);
Expand Down
59 changes: 58 additions & 1 deletion src/group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ Group::Group(const std::string& name, const std::string& id, const Json::Value&
? drawer_config["transition-left-to-right"].asBool()
: true);
click_to_reveal = drawer_config["click-to-reveal"].asBool();
always_visible_class =
(drawer_config["always-visible-class"].isString() ? drawer_config["always-visible-class"].asString()
: "");

auto transition_type = getPreferredTransitionType(vertical);

Expand All @@ -89,6 +92,54 @@ void Group::show_group() {
revealer.set_reveal_child(true);
}

void Group::hide_widget(Gtk::Widget& widget) {
widget.get_style_context()->add_class(add_class_to_drawer_children);
box.remove(widget);
revealer_box.pack_start(widget, false, false);
}

void Group::show_widget(Gtk::Widget& widget) {
widget.get_style_context()->remove_class(add_class_to_drawer_children);
revealer_box.remove(widget);
box.pack_end(widget, false, false);
}

void Group::hide_current_widget_if_inactive() {
for (auto* event_box : box.get_children()) {
if (event_box == &revealer) {
continue;
}
if (auto event_box_container = dynamic_cast<Gtk::Container*>(event_box)) {
for (auto* the_only_visible : event_box_container->get_children()) {
if (!the_only_visible->get_style_context()->has_class(always_visible_class)) {
hide_widget(*event_box);
}
}
}
}
}

void Group::manage_visibility(AModule* module) {
Gtk::Widget& widget = *module;

if (auto container = dynamic_cast<Gtk::Container*>(&widget)) {
for (auto* base_element : container->get_children()) {
if (base_element->get_style_context()->has_class(always_visible_class)) {
if (box.get_children().size() == 2) {
Group::hide_current_widget_if_inactive();
}
show_widget(widget);
} else {
// Do not hide if it's the only widget + revealer
if (box.get_children().size() <= 2) {
return;
}
hide_widget(widget);
}
}
}
}

void Group::hide_group() {
box.unset_state_flags(Gtk::StateFlags::STATE_FLAG_PRELIGHT);
revealer.set_reveal_child(false);
Expand Down Expand Up @@ -126,14 +177,20 @@ auto Group::update() -> void {

Gtk::Box& Group::getBox() { return is_drawer ? (is_first_widget ? box : revealer_box) : box; }

void Group::addWidget(Gtk::Widget& widget) {
void Group::addWidget(AModule* module) {
Gtk::Widget& widget = *module;

getBox().pack_start(widget, false, false);

if (is_drawer && !is_first_widget) {
widget.get_style_context()->add_class(add_class_to_drawer_children);
}

is_first_widget = false;

if (!always_visible_class.empty()) {
module->signal_updated.connect(sigc::mem_fun(*this, &Group::manage_visibility));
}
}

Group::operator Gtk::Widget&() { return event_box_; }
Expand Down