Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement scrolling on the background of the waybar #3401

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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/bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Bar : public sigc::trackable {
void setVisible(bool value);
void toggle();
void handleSignal(int);
bool handleScroll(GdkEventScroll*);

struct waybar_output *output;
Json::Value config;
Expand Down
55 changes: 55 additions & 0 deletions src/bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "modules/sway/bar.hpp"
#endif

#include "util/command.hpp"

namespace waybar {
static constexpr const char* MIN_HEIGHT_MSG =
"Requested height: {} is less than the minimum height: {} required by the modules";
Expand Down Expand Up @@ -258,6 +260,12 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
setVisible(false);
}

if (config["on-scroll-up"].isString() || config["on-scroll-down"].isString() ||
config["on-scroll-left"].isString() || config["on-scroll-right"].isString()) {
window.add_events(Gdk::SCROLL_MASK);
window.signal_scroll_event().connect(sigc::mem_fun(*this, &Bar::handleScroll));
}

window.signal_map_event().connect_notify(sigc::mem_fun(*this, &Bar::onMap));

#if HAVE_SWAY
Expand Down Expand Up @@ -479,6 +487,53 @@ void waybar::Bar::handleSignal(int signal) {
}
}

bool _rect_contains_evt(const Gdk::Rectangle& rect, GdkEventScroll* e) {
return e->x >= rect.get_x() && e->x < rect.get_x() + rect.get_width() &&
e->y >= rect.get_y() && e->y < rect.get_y() + rect.get_height();
}

bool waybar::Bar::handleScroll(GdkEventScroll* e) {
if (_rect_contains_evt(left_.get_allocation(), e) ||
_rect_contains_evt(center_.get_allocation(), e) ||
_rect_contains_evt(right_.get_allocation(), e)) {
return false;
}

std::string eventName{};

// only affects up/down
bool reverse = config["reverse-scrolling"].asBool();
bool reverse_mouse = config["reverse-mouse-scrolling"].asBool();

// ignore reverse-scrolling if event comes from a mouse wheel
GdkDevice* device = gdk_event_get_source_device((GdkEvent*)e);
if (device != NULL && gdk_device_get_source(device) == GDK_SOURCE_MOUSE) {
reverse = reverse_mouse;
}

switch (e->direction) {
case GDK_SCROLL_UP:
eventName = (reverse ? "on-scroll-down" : "on-scroll-up");
break;
case GDK_SCROLL_DOWN:
eventName = (reverse ? "on-scroll-up" : "on-scroll-down");
break;
case GDK_SCROLL_LEFT:
eventName = "on-scroll-left";
break;
case GDK_SCROLL_RIGHT:
eventName = "on-scroll-right";
break;
}

if (config[eventName].isString()) {
util::command::forkExec(config[eventName].asString());
return true;
}

return false;
}

void waybar::Bar::getModules(const Factory& factory, const std::string& pos,
waybar::Group* group = nullptr) {
auto module_list = group != nullptr ? config[pos]["modules"] : config[pos];
Expand Down