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

Indicator: send notification for low power #229

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
47 changes: 47 additions & 0 deletions src/Indicator.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

public class Power.Indicator : Wingpanel.Indicator {
private const double CRITICAL_BATTERY_PERCENTAGE = 10;
private const double LOW_BATTERY_PERCENTAGE = 20;

public bool is_in_session { get; construct; default = false; }
Expand All @@ -33,6 +34,9 @@ public class Power.Indicator : Wingpanel.Indicator {

private Settings settings;

private bool notification_low_sent = false;
private bool notification_critical_sent = false;

public Indicator (bool is_in_session) {
Object (
code_name : Wingpanel.Indicator.POWER,
Expand Down Expand Up @@ -148,10 +152,53 @@ public class Power.Indicator : Wingpanel.Indicator {
display_widget.allow_percent = true;
}

if (display_device.is_charging) {
if (display_device.percentage >= CRITICAL_BATTERY_PERCENTAGE) {
notification_critical_sent = false;

if (display_device.percentage >= LOW_BATTERY_PERCENTAGE) {
notification_low_sent = false;
}
}
} else if (display_device.percentage <= LOW_BATTERY_PERCENTAGE) {
send_power_notification ();
}

update_tooltip ();
}
}

private void send_power_notification () {
var notification = new Notify.Notification (
_("Low Power"),
display_device.get_info (),
display_device.get_icon_name_for_battery ()
) {
id = int.parse ("io.elementary.low-power")
};

if (display_device.percentage <= CRITICAL_BATTERY_PERCENTAGE) {
if (notification_critical_sent) {
return;
}

notification.set_urgency (Notify.Urgency.CRITICAL);
notification_critical_sent = true;
} else {
if (notification_low_sent) {
return;
}

notification_low_sent = true;
}

try {
notification.show ();
} catch (Error e) {
critical ("Unable to show lower power notification: %s", e.message);
}
}

private void show_backlight_data () {
if (display_widget != null) {
display_widget.icon_name = "display-brightness-symbolic";
Expand Down
10 changes: 3 additions & 7 deletions src/Services/Device.vala
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public class Power.Services.Device : Object {

if (connect_to_bus ()) {
update_properties ();
connect_signals ();
device.g_properties_changed.connect (update_properties);
}
}

Expand All @@ -179,10 +179,6 @@ public class Power.Services.Device : Object {
return device != null;
}

private void connect_signals () {
device.g_properties_changed.connect (update_properties);
}

private void update_properties () {
try {
device.refresh ();
Expand Down Expand Up @@ -296,7 +292,7 @@ public class Power.Services.Device : Object {
info += _("%i%% charged").printf (percent);

if (time_to_full > 0) {
info += " - ";
info += " ";
if (time_to_full >= 86400) {
var days = time_to_full / 86400;
info += dngettext (
Expand Down Expand Up @@ -334,7 +330,7 @@ public class Power.Services.Device : Object {
info += _("%i%% remaining").printf (percent);

if (time_to_empty > 0) {
info += " - ";
info += " ";
if (time_to_empty >= 86400) {
var days = time_to_empty / 86400;
info += dngettext (
Expand Down