diff --git a/src/Indicator.vala b/src/Indicator.vala index 674ce93f..f126048f 100644 --- a/src/Indicator.vala +++ b/src/Indicator.vala @@ -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; } @@ -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, @@ -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"; diff --git a/src/Services/Device.vala b/src/Services/Device.vala index bf57b39e..40c08827 100644 --- a/src/Services/Device.vala +++ b/src/Services/Device.vala @@ -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); } } @@ -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 (); @@ -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 ( @@ -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 (