Skip to content

battery: make scroll change brightness #209

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

Open
wants to merge 4 commits 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
8 changes: 8 additions & 0 deletions metadata/panel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@
<_short>Battery Font</_short>
<default>default</default>
</option>
<option name="battery_scrollup_command" type="string">
<_short>Battery Scroll Up Command</_short>
<default>brightnessctl -q set 10%+</default>
</option>
<option name="battery_scrolldown_command" type="string">
<_short>Battery Scroll Down Command</_short>
<default>brightnessctl -q set 10%-</default>
Copy link
Member

Choose a reason for hiding this comment

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

Why not do it on every discrete event at 3% by default? It seems like you're losing some granularity and possibly introducing buggy behavior by doing nothing on some scroll events.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I was just doing what I thought ammen99 originally suggested. It just made sense to me because running so many commands at once makes the brightness update laggy on my system.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds like another thing which ought to be an option (granularity)

</option>
</group>
<group>
<_short>Network</_short>
Expand Down
24 changes: 23 additions & 1 deletion src/panel/widgets/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,23 @@ void WayfireBatteryInfo::update_details()
}
}

void WayfireBatteryInfo::on_battery_scroll(GdkEventScroll *event)
{
if (scroll_counter <= 0) {
Copy link
Member

Choose a reason for hiding this comment

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

How does GDK behave when we have smooth scroll, do we get separate events for discrete and smooth scrolling, or are they bunched here together? Can delta_* be a floating point number?

In general I am kinda hesitant to have a hard threshold like this (i.e update every 3 events). Wouldn't it be better to update on every X units of scroll, or even better, every milliseconds, where N is a configurable period of time?

Copy link
Member

Choose a reason for hiding this comment

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

To complicate things further, there is also GdkEventScroll.GdkScrollDirection, which tells when the delta_x/y values are valid to use or which direction to scroll.

if (event->delta_y < 0)
{
std::system(battery_scrollup_command.value().c_str());
} else if (0 < event->delta_y)
{
std::system(battery_scrolldown_command.value().c_str());
}

scroll_counter = 3;
}

scroll_counter--;
}

void WayfireBatteryInfo::update_state()
{
std::cout << "unimplemented reached, in battery.cpp: "
Expand Down Expand Up @@ -222,10 +239,11 @@ bool WayfireBatteryInfo::setup_dbus()
}

// TODO: simplify config loading

static const std::string default_font = "default";
void WayfireBatteryInfo::init(Gtk::HBox *container)
{
scroll_counter = 0;

if (!setup_dbus())
{
return;
Expand All @@ -234,6 +252,10 @@ void WayfireBatteryInfo::init(Gtk::HBox *container)
button_box.add(icon);
button.get_style_context()->add_class("flat");

button.set_events(Gdk::SCROLL_MASK | Gdk::SMOOTH_SCROLL_MASK);
button.signal_scroll_event().connect_notify(
sigc::mem_fun(this, &WayfireBatteryInfo::on_battery_scroll));

status_opt.set_callback([=] () { update_details(); });
font_opt.set_callback([=] () { update_font(); });
size_opt.set_callback([=] () { update_icon(); });
Expand Down
5 changes: 5 additions & 0 deletions src/panel/widgets/battery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ class WayfireBatteryInfo : public WayfireWidget
{
WfOption<std::string> status_opt{"panel/battery_status"};
WfOption<std::string> font_opt{"panel/battery_font"};
WfOption<std::string> battery_scrollup_command{"panel/battery_scrollup_command"};
WfOption<std::string> battery_scrolldown_command{"panel/battery_scrolldown_command"};
WfOption<int> size_opt{"panel/battery_icon_size"};
WfOption<bool> invert_opt{"panel/battery_icon_invert"};

Gtk::Button button;
Gtk::Label label;
Gtk::HBox button_box;

int scroll_counter;

Gtk::Image icon;

DBusConnection connection;
Expand All @@ -41,6 +45,7 @@ class WayfireBatteryInfo : public WayfireWidget
void update_icon();
void update_details();
void update_state();
void on_battery_scroll(GdkEventScroll *event);

void on_properties_changed(
const Gio::DBus::Proxy::MapChangedProperties& properties,
Expand Down