From d1238f6a8e67ab4c137d5fcae4ac57171f4eb506 Mon Sep 17 00:00:00 2001
From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>
Date: Mon, 17 Feb 2025 12:50:56 +1100
Subject: bar: battery tooltip

---
 src/modules/bar.tsx | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

(limited to 'src/modules/bar.tsx')

diff --git a/src/modules/bar.tsx b/src/modules/bar.tsx
index cd36e42..2645fde 100644
--- a/src/modules/bar.tsx
+++ b/src/modules/bar.tsx
@@ -34,6 +34,12 @@ const getBatteryIcon = (perc: number) => {
     return "󰁹";
 };
 
+const formatSeconds = (sec: number) => {
+    if (sec >= 3600) return `${Math.floor(sec / 3600)} hour${sec % 3600 === 0 ? "" : "s"}`;
+    if (sec >= 60) return `${Math.floor(sec / 60)} minute${sec % 60 === 0 ? "" : "s"}`;
+    return `${sec} second${sec === 1 ? "" : "s"}`;
+};
+
 const hookFocusedClientProp = (
     self: AstalWidget,
     prop: keyof AstalHyprland.Client,
@@ -463,9 +469,17 @@ const NotifCount = () => (
 );
 
 const Battery = () => (
-    <box className={bind(AstalBattery.get_default(), "percentage").as(p => `module battery ${p < 0.2 ? "low" : ""}`)}>
+    <box
+        className={bind(AstalBattery.get_default(), "percentage").as(p => `module battery ${p < 0.2 ? "low" : ""}`)}
+        setup={self =>
+            setupCustomTooltip(
+                self,
+                bind(AstalBattery.get_default(), "timeToEmpty").as(p => `${formatSeconds(p)} remaining`)
+            )
+        }
+    >
         <label className="icon" label={bind(AstalBattery.get_default(), "percentage").as(getBatteryIcon)} />
-        <label label={bind(AstalBattery.get_default(), "percentage").as(p => `${p * 100}%`)} />
+        <label label={bind(AstalBattery.get_default(), "percentage").as(p => `${Math.round(p * 100)}%`)} />
     </box>
 );
 
-- 
cgit v1.2.3-freya

