From 070d80e78ff3bf289df71aeada38b02018cabca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danielle=20For=C3=A9?= Date: Thu, 29 May 2025 13:45:42 -0700 Subject: [PATCH 1/2] Indicator: overlay percentage --- data/Indicator.css | 19 +++++++++ data/icons.gresource.xml | 7 +++ data/icons/battery-labeled-charging.svg | 3 ++ data/icons/battery-labeled-critical.svg | 2 + data/icons/battery-labeled-full-charging.svg | 3 ++ data/icons/battery-labeled.svg | 45 ++++++++++++++++++++ src/Indicator.vala | 9 ++++ src/Services/Device.vala | 21 +++++++++ src/Widgets/DisplayWidget.vala | 19 ++++++--- 9 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 data/Indicator.css create mode 100644 data/icons/battery-labeled-charging.svg create mode 100644 data/icons/battery-labeled-critical.svg create mode 100644 data/icons/battery-labeled-full-charging.svg create mode 100644 data/icons/battery-labeled.svg diff --git a/data/Indicator.css b/data/Indicator.css new file mode 100644 index 00000000..55e8ebb9 --- /dev/null +++ b/data/Indicator.css @@ -0,0 +1,19 @@ +.battery-indicator label { + text-shadow: none; + margin-left: -1px; +} + +panel.color-dark .battery-indicator label, +panel.maximized .battery-indicator label { + color: #333; +} + +panel.color-light .battery-indicator label { + color: white; +} + +/*Workaround shadow clip, replace with overflow in GTK4*/ +.battery-indicator image { + margin-right: 1px; + margin-left: 1px; +} diff --git a/data/icons.gresource.xml b/data/icons.gresource.xml index 4e55c81b..746abe72 100644 --- a/data/icons.gresource.xml +++ b/data/icons.gresource.xml @@ -1,10 +1,17 @@ + Indicator.css + icons/balanced.svg icons/performance.svg icons/powersaver.svg + icons/battery-labeled.svg + icons/battery-labeled-charging.svg + icons/battery-labeled-critical.svg + icons/battery-labeled-full-charging.svg + icons/battery-100.svg icons/battery-95.svg icons/battery-90.svg diff --git a/data/icons/battery-labeled-charging.svg b/data/icons/battery-labeled-charging.svg new file mode 100644 index 00000000..83184150 --- /dev/null +++ b/data/icons/battery-labeled-charging.svg @@ -0,0 +1,3 @@ + +image/svg+xml diff --git a/data/icons/battery-labeled-critical.svg b/data/icons/battery-labeled-critical.svg new file mode 100644 index 00000000..1c40b4ed --- /dev/null +++ b/data/icons/battery-labeled-critical.svg @@ -0,0 +1,2 @@ + +image/svg+xml diff --git a/data/icons/battery-labeled-full-charging.svg b/data/icons/battery-labeled-full-charging.svg new file mode 100644 index 00000000..1057f6e1 --- /dev/null +++ b/data/icons/battery-labeled-full-charging.svg @@ -0,0 +1,3 @@ + +image/svg+xml diff --git a/data/icons/battery-labeled.svg b/data/icons/battery-labeled.svg new file mode 100644 index 00000000..27b4c90e --- /dev/null +++ b/data/icons/battery-labeled.svg @@ -0,0 +1,45 @@ + +image/svg+xml diff --git a/src/Indicator.vala b/src/Indicator.vala index f007817f..e07e28b1 100644 --- a/src/Indicator.vala +++ b/src/Indicator.vala @@ -57,6 +57,15 @@ public class Power.Indicator : Wingpanel.Indicator { public override Gtk.Widget get_display_widget () { if (display_widget == null) { + var provider = new Gtk.CssProvider (); + provider.load_from_resource ("io/elementary/desktop/wingpanel/power/Indicator.css"); + + Gtk.StyleContext.add_provider_for_screen ( + Gdk.Screen.get_default (), + provider, + Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION + ); + display_widget = new Widgets.DisplayWidget (); /* No need to display the indicator when the device is completely in AC mode */ diff --git a/src/Services/Device.vala b/src/Services/Device.vala index 810e723b..5600cf64 100644 --- a/src/Services/Device.vala +++ b/src/Services/Device.vala @@ -237,6 +237,11 @@ public class Power.Services.Device : Object { return "preferences-system-power-symbolic"; } + var settings = new GLib.Settings ("io.elementary.desktop.wingpanel.power"); + if (settings.get_boolean ("show-percentage")) { + return get_icon_name_labeled (); + } + var icon_name = "battery"; if (percentage > 10) { @@ -263,6 +268,22 @@ public class Power.Services.Device : Object { return icon_name += "-symbolic"; } + private string get_icon_name_labeled () { + var icon_name = "battery-labeled"; + + if (is_charging) { + if (percentage == 100) { + icon_name += "-full"; + } + + icon_name += "-charging"; + } else if (time_to_empty >= 0 && time_to_empty < 15 * 60 && percentage > 0) { + icon_name += "-critical"; + } + + return icon_name += "-symbolic"; + } + public string get_icon_name_for_battery () { if (!is_a_battery) { return "preferences-system-power-symbolic"; diff --git a/src/Widgets/DisplayWidget.vala b/src/Widgets/DisplayWidget.vala index 0e6f15c8..59be8acc 100644 --- a/src/Widgets/DisplayWidget.vala +++ b/src/Widgets/DisplayWidget.vala @@ -17,7 +17,7 @@ * Boston, MA 02110-1301, USA. */ -public class Power.Widgets.DisplayWidget : Gtk.Box { +public class Power.Widgets.DisplayWidget : Gtk.Bin { private Gtk.Revealer percent_revealer; public string icon_name { set { @@ -28,8 +28,7 @@ public class Power.Widgets.DisplayWidget : Gtk.Box { public bool allow_percent { get; set; default = false; } public int percentage { set { - ///Translators: This represents battery charge precentage with `%i` representing the number and `%%` representing the percent symbol - percent_label.label = _("%i%%").printf (value); + percent_label.label = "%i".printf (value); } } @@ -47,12 +46,18 @@ public class Power.Widgets.DisplayWidget : Gtk.Box { percent_label = new Gtk.Label (null); percent_revealer = new Gtk.Revealer () { - transition_type = Gtk.RevealerTransitionType.SLIDE_RIGHT + child = percent_label, + halign = START, + transition_type = CROSSFADE }; - percent_revealer.add (percent_label); - add (image); - add (percent_revealer); + var percent_overlay = new Gtk.Overlay () { + child = image + }; + percent_overlay.add_overlay (percent_revealer); + + get_style_context ().add_class ("battery-indicator"); + child = percent_overlay; var settings = new GLib.Settings ("io.elementary.desktop.wingpanel.power"); settings.bind ("show-percentage", percent_revealer, "reveal-child", GLib.SettingsBindFlags.GET); From c4dd035050f43f8a2e1d61ff23e354de8cac0289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danielle=20For=C3=A9?= Date: Thu, 29 May 2025 14:04:47 -0700 Subject: [PATCH 2/2] better centering? --- data/Indicator.css | 3 ++- src/Widgets/DisplayWidget.vala | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/data/Indicator.css b/data/Indicator.css index 55e8ebb9..3d811acb 100644 --- a/data/Indicator.css +++ b/data/Indicator.css @@ -1,6 +1,7 @@ .battery-indicator label { + margin-left: -4px; + margin-right: 3px; text-shadow: none; - margin-left: -1px; } panel.color-dark .battery-indicator label, diff --git a/src/Widgets/DisplayWidget.vala b/src/Widgets/DisplayWidget.vala index 59be8acc..5892d374 100644 --- a/src/Widgets/DisplayWidget.vala +++ b/src/Widgets/DisplayWidget.vala @@ -47,7 +47,7 @@ public class Power.Widgets.DisplayWidget : Gtk.Bin { percent_revealer = new Gtk.Revealer () { child = percent_label, - halign = START, + hexpand = true, transition_type = CROSSFADE };