Skip to content

Commit

Permalink
Device: handle coarse battery level (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
danirabbit authored Oct 9, 2024
1 parent c9e47b9 commit 4fe0188
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 73 deletions.
3 changes: 3 additions & 0 deletions data/power.metainfo.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<li>Updated translations</li>
</ul>
</description>
<issues>
<issue url="https://github.com/elementary/wingpanel-indicator-power/issues/256">Mouse battery indicator stuck at 50%</issue>
</issues>
</release>

<release version="8.0.0" date="2024-08-21" urgency="medium">
Expand Down
Binary file modified data/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/Indicator.vala
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public class Power.Indicator : Wingpanel.Indicator {
display_widget.show_percentage (false);
}
if (display_device.is_a_battery) {
primary_text = _("%s: %s").printf (display_device.device_type.get_name (), display_device.get_info ());
primary_text = _("%s: %s").printf (display_device.device_type.get_name (), display_device.description);
secondary_text = is_showing_percent ? _("Middle-click to hide percentage") : _("Middle-click to show percentage");

} else {
Expand Down
1 change: 1 addition & 0 deletions src/Services/DBusInterfaces/Device.vala
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ namespace Power.Services.DBusInterfaces {
public abstract string native_path { public owned get; public set; }
public abstract string serial { public owned get; public set; }
public abstract string vendor { public owned get; public set; }
public abstract uint32 battery_level { owned get; }
public abstract uint32 state { public owned get; public set; }
public abstract uint32 technology { public owned get; public set; }
public abstract uint32 Type { public owned get; public set; }
Expand Down
169 changes: 100 additions & 69 deletions src/Services/Device.vala
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,15 @@ public class Power.Services.Device : Object {

private DBusInterfaces.Device? device = null;

public bool coarse_battery_level { get; private set; default = false; }
public bool has_history { get; private set; }
public bool has_statistics { get; private set; }
public bool is_present { get; private set; }
public bool is_rechargeable { get; private set; }
public bool online { get; private set; }
public bool power_supply { get; private set; }
public double capacity { get; private set; }
public string description { get; private set; }
public double energy { get; private set; }
public double energy_empty { get; private set; }
public double energy_full { get; private set; }
Expand Down Expand Up @@ -164,6 +166,9 @@ public class Power.Services.Device : Object {
if (connect_to_bus ()) {
update_properties ();
connect_signals ();

update_description ();
notify["percentage"].connect (update_description);
}
}

Expand All @@ -184,6 +189,7 @@ public class Power.Services.Device : Object {
}

private void update_properties () {
coarse_battery_level = device.battery_level != 1;
has_history = device.has_history;
has_statistics = device.has_statistics;
is_present = device.is_present;
Expand Down Expand Up @@ -269,6 +275,20 @@ public class Power.Services.Device : Object {
}

private unowned string get_battery_icon () {
if (coarse_battery_level) {
if (percentage < 20) {
return "battery-empty";
} else if (percentage < 40) {
return "battery-caution";
} else if (percentage < 60) {
return "battery-low";
} else if (percentage < 80) {
return "battery-good";
} else {
return "battery-full";
}
}

if (percentage <= 0) {
return "battery-good";
}
Expand All @@ -292,85 +312,96 @@ public class Power.Services.Device : Object {
return "battery-full";
}

public string get_info () {
var percent = (int) Math.round (percentage);

private void update_description () {
if (!is_a_battery) {
return "";
}

if (percent <= 0) {
return _("Calculating…");
return;
}

if (percent == 100 && is_charging) {
return _("Fully charged");
if (percentage == 0 && state == UNKNOWN) {
description = _("Unknown. Device may be locked.");
return;
}

var info = "";

if (is_charging) {
info += _("%i%% charged").printf (percent);

if (time_to_full > 0) {
info += " - ";
if (time_to_full >= 3600) {
var hours = time_to_full / 3600;
info += dngettext (
Constants.GETTEXT_PACKAGE,
"%lld hour until full",
"%lld hours until full",
(ulong) hours
).printf (hours);
} else if (time_to_full >= 60) {
var minutes = time_to_full / 60;
info += dngettext (
Constants.GETTEXT_PACKAGE,
"%lld minute until full",
"%lld minutes until full",
(ulong) minutes
).printf (minutes);
} else {
info += dngettext (
Constants.GETTEXT_PACKAGE,
"%lld second until full",
"%lld seconds until full",
(ulong) time_to_full
).printf (time_to_full);
}
if (coarse_battery_level) {
// Coarse battery level can sometimes be unknown, percentage is more reliable
if (percentage < 20) {
description = C_("battery-level", "Critical");
} else if (percentage < 40) {
description = C_("battery-level", "Low");
} else if (percentage < 60) {
description = C_("battery-level", "Good");
} else if (percentage < 80) {
description = C_("battery-level", "High");
} else {
description = C_("battery-level", "Full");
}
} else {
info += _("%i%% remaining").printf (percent);

if (time_to_empty > 0) {
info += " - ";
if (time_to_empty >= 3600) {
var hours = time_to_empty / 3600;
info += dngettext (
Constants.GETTEXT_PACKAGE,
"%lld hour until empty",
"%lld hours until empty",
(ulong) hours
).printf (hours);
} else if (time_to_empty >= 60) {
var minutes = time_to_empty / 60;
info += dngettext (
Constants.GETTEXT_PACKAGE,
"%lld minute until empty",
"%lld minutes until empty",
(ulong) minutes
).printf (minutes);
} else {
info += dngettext (
Constants.GETTEXT_PACKAGE,
"%lld second until empty",
"%lld seconds until empty",
(ulong) time_to_empty
).printf (time_to_empty);
if (is_charging) {
if (percentage == 100) {
description = _("Fully charged");
return;
}

if (time_to_full > 0) {
description = _("%.0f%% charged").printf (percentage);
}
} else {
description = _("%.0f%% remaining").printf (percentage);
}
}

return info;
if (is_charging && time_to_full > 0) {
description += "";
if (time_to_full >= 3600) {
var hours = time_to_full / 3600;
description += dngettext (
Constants.GETTEXT_PACKAGE,
"%lld hour until full",
"%lld hours until full",
(ulong) hours
).printf (hours);
} else if (time_to_full >= 60) {
var minutes = time_to_full / 60;
description += dngettext (
Constants.GETTEXT_PACKAGE,
"%lld minute until full",
"%lld minutes until full",
(ulong) minutes
).printf (minutes);
} else {
description += dngettext (
Constants.GETTEXT_PACKAGE,
"%lld second until full",
"%lld seconds until full",
(ulong) time_to_full
).printf (time_to_full);
}
} else if (time_to_empty > 0) {
description += "";
if (time_to_empty >= 3600) {
var hours = time_to_empty / 3600;
description += dngettext (
Constants.GETTEXT_PACKAGE,
"%lld hour until empty",
"%lld hours until empty",
(ulong) hours
).printf (hours);
} else if (time_to_empty >= 60) {
var minutes = time_to_empty / 60;
description += dngettext (
Constants.GETTEXT_PACKAGE,
"%lld minute until empty",
"%lld minutes until empty",
(ulong) minutes
).printf (minutes);
} else {
description += dngettext (
Constants.GETTEXT_PACKAGE,
"%lld second until empty",
"%lld seconds until empty",
(ulong) time_to_empty
).printf (time_to_empty);
}
}
}
}
6 changes: 3 additions & 3 deletions src/Widgets/DeviceRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public class Power.Widgets.DeviceRow : Gtk.ListBoxRow {
valign = Gtk.Align.END
};


var info_label = new Gtk.Label (battery.get_info ()) {
var info_label = new Gtk.Label ("") {
halign = Gtk.Align.START,
valign = Gtk.Align.START
};
Expand All @@ -73,8 +72,9 @@ public class Power.Widgets.DeviceRow : Gtk.ListBoxRow {
battery.properties_updated.connect (() => {
update_icons ();
title_label.set_markup (get_title ());
info_label.label = battery.get_info ();
});

battery.bind_property ("description", info_label, "label", SYNC_CREATE);
}

private void update_icons () {
Expand Down

0 comments on commit 4fe0188

Please sign in to comment.