♻️ refactor(dock/battery): simplify battery component initialization and event handling

This commit is contained in:
sankalp committed 2025-06-22 18:39:49 +05:45
1 parent 73488b188c
commit 822672c399
1 file changed
-34
-34
View File
@@ -16,11 +16,9 @@ class Battery(Box):
name="battery", spacing=0, orientation=orientation, visible=True, **kwargs name="battery", spacing=0, orientation=orientation, visible=True, **kwargs
) )
# Initialize battery service
self._battery = BatteryService() self._battery = BatteryService()
self._battery.changed.connect(self.update_battery) self._battery.changed.connect(self.update_battery)
# Create the battery icon and circular progress
self.icon = Label(name="battery-icon", markup=icons.battery) self.icon = Label(name="battery-icon", markup=icons.battery)
self.circle = CircularProgressBar( self.circle = CircularProgressBar(
name="battery-circle", name="battery-circle",
@@ -33,13 +31,11 @@ class Battery(Box):
child=self.icon, child=self.icon,
) )
# Create button to hold the circle
self.circle_button = Button( self.circle_button = Button(
name="battery-button", name="battery-button",
child=self.circle, child=self.circle,
) )
# Add event handling for hover effects
self.circle_button.add_events( self.circle_button.add_events(
Gdk.EventMask.ENTER_NOTIFY_MASK Gdk.EventMask.ENTER_NOTIFY_MASK
| Gdk.EventMask.LEAVE_NOTIFY_MASK | Gdk.EventMask.LEAVE_NOTIFY_MASK
@@ -49,14 +45,12 @@ class Battery(Box):
self.circle_button.connect("leave-notify-event", self.on_button_leave) self.circle_button.connect("leave-notify-event", self.on_button_leave)
self.circle_button.connect("button-press-event", self.on_button_press) self.circle_button.connect("button-press-event", self.on_button_press)
# Create percentage label
self.level = Label( self.level = Label(
name="battery-level", name="battery-level",
style_classes="battery", style_classes="battery",
label="0%", label="0%",
) )
# Create the container box
self.battery_box = Box( self.battery_box = Box(
name="battery-box", name="battery-box",
orientation=orientation, orientation=orientation,
@@ -64,40 +58,32 @@ class Battery(Box):
children=[self.circle_button], children=[self.circle_button],
) )
# Only add level label in horizontal mode
if not data.VERTICAL: if not data.VERTICAL:
self.battery_box.add(self.level) self.battery_box.add(self.level)
# Enable tooltips
self.circle_button.set_has_tooltip(True) self.circle_button.set_has_tooltip(True)
self.battery_box.set_has_tooltip(True) self.battery_box.set_has_tooltip(True)
self.level.set_has_tooltip(True) self.level.set_has_tooltip(True)
# Connect tooltip signals
self.circle_button.connect("query-tooltip", self.on_query_tooltip) self.circle_button.connect("query-tooltip", self.on_query_tooltip)
self.battery_box.connect("query-tooltip", self.on_query_tooltip) self.battery_box.connect("query-tooltip", self.on_query_tooltip)
self.level.connect("query-tooltip", self.on_query_tooltip) self.level.connect("query-tooltip", self.on_query_tooltip)
# Add the battery box to self
self.add(self.battery_box) self.add(self.battery_box)
# Initial update
GLib.idle_add(self.update_battery) GLib.idle_add(self.update_battery)
def on_button_enter(self, widget, event): def on_button_enter(self, widget, event):
"""Handle mouse enter event"""
window = widget.get_window() window = widget.get_window()
if window: if window:
window.set_cursor(Gdk.Cursor(Gdk.CursorType.HAND2)) window.set_cursor(Gdk.Cursor(Gdk.CursorType.HAND2))
def on_button_leave(self, widget, event): def on_button_leave(self, widget, event):
"""Handle mouse leave event"""
window = widget.get_window() window = widget.get_window()
if window: if window:
window.set_cursor(None) window.set_cursor(None)
def on_button_press(self, widget, event): def on_button_press(self, widget, event):
"""Handle button press event - toggle power profiles"""
if event.button == 1: # Left click if event.button == 1: # Left click
try: try:
if ( if (
@@ -107,11 +93,9 @@ class Battery(Box):
return False return False
current_profile = self._battery._profile_proxy.ActiveProfile current_profile = self._battery._profile_proxy.ActiveProfile
# Get available profiles from D-Bus property
profiles = self._battery._profile_proxy.Profiles profiles = self._battery._profile_proxy.Profiles
profile_names = [] profile_names = []
# Extract profile names from the D-Bus structure
for profile in profiles: for profile in profiles:
if isinstance(profile, dict) and "Profile" in profile: if isinstance(profile, dict) and "Profile" in profile:
profile_names.append(profile["Profile"]) profile_names.append(profile["Profile"])
@@ -125,7 +109,6 @@ class Battery(Box):
profile_names profile_names
) )
next_profile = profile_names[next_index] next_profile = profile_names[next_index]
# Set profile directly on the D-Bus proxy
self._battery._profile_proxy.ActiveProfile = next_profile self._battery._profile_proxy.ActiveProfile = next_profile
else: else:
return False return False
@@ -135,22 +118,18 @@ class Battery(Box):
return False return False
def on_query_tooltip(self, widget, x, y, keyboard_mode, tooltip): def on_query_tooltip(self, widget, x, y, keyboard_mode, tooltip):
"""Handle tooltip query"""
tooltip.set_markup(self.get_tooltip_text()) tooltip.set_markup(self.get_tooltip_text())
return True return True
def get_tooltip_text(self): def get_tooltip_text(self):
"""Get formatted tooltip text in bullet points"""
if not self._battery.is_present: if not self._battery.is_present:
return "No battery detected" return "No battery detected"
percentage = self._battery.percentage percentage = self._battery.percentage
state = self._battery.state state = self._battery.state
# Get capacity as integer by converting from float
capacity = int(float(self._battery.capacity.rstrip("%"))) capacity = int(float(self._battery.capacity.rstrip("%")))
tooltip_points = [] tooltip_points = []
# Status and percentage
if state == "FULLY_CHARGED": if state == "FULLY_CHARGED":
status = f"{icons.bat_full} Fully Charged" status = f"{icons.bat_full} Fully Charged"
elif state == "CHARGING": elif state == "CHARGING":
@@ -163,14 +142,11 @@ class Battery(Box):
status = "Battery" status = "Battery"
tooltip_points.append(f"• Status: {status}") tooltip_points.append(f"• Status: {status}")
# Battery level and capacity
tooltip_points.append(f"• Level: {int(percentage)}%") tooltip_points.append(f"• Level: {int(percentage)}%")
tooltip_points.append(f"• Battery Health: {capacity}%") tooltip_points.append(f"• Battery Health: {capacity}%")
# Add power profile if available
power_profile = self._battery.power_profile power_profile = self._battery.power_profile
if power_profile: if power_profile:
# Add icon based on profile
if power_profile == "power-saver": if power_profile == "power-saver":
profile_icon = icons.power_saving profile_icon = icons.power_saving
elif power_profile == "performance": elif power_profile == "performance":
@@ -179,12 +155,10 @@ class Battery(Box):
profile_icon = icons.power_balanced profile_icon = icons.power_balanced
tooltip_points.append(f"• Profile: {profile_icon} {power_profile}") tooltip_points.append(f"• Profile: {profile_icon} {power_profile}")
# Add temperature if available
temp = self._battery.temperature temp = self._battery.temperature
if temp != "N/A": if temp != "N/A":
tooltip_points.append(f"• Temperature: {temp}") tooltip_points.append(f"• Temperature: {temp}")
# Add time estimates
if state == "CHARGING": if state == "CHARGING":
time_to_full = self._battery.time_to_full time_to_full = self._battery.time_to_full
if time_to_full: if time_to_full:
@@ -197,7 +171,6 @@ class Battery(Box):
return "\n".join(tooltip_points) return "\n".join(tooltip_points)
def update_battery(self, *args): def update_battery(self, *args):
"""Update battery status and UI"""
if not self._battery.is_present: if not self._battery.is_present:
self.set_visible(False) self.set_visible(False)
return True return True
@@ -207,17 +180,14 @@ class Battery(Box):
charging = state in ["CHARGING", "FULLY_CHARGED"] charging = state in ["CHARGING", "FULLY_CHARGED"]
power_profile = self._battery.power_profile power_profile = self._battery.power_profile
# Update circle progress
self.circle.set_value(percentage / 100.0) self.circle.set_value(percentage / 100.0)
# Only show percentage label if not at 100%
if percentage < 100: if percentage < 100:
self.level.set_label(f"{int(percentage)}%") self.level.set_label(f"{int(percentage)}%")
self.level.set_visible(True) self.level.set_visible(True)
else: else:
self.level.set_visible(False) self.level.set_visible(False)
# Remove any existing profile and discharging classes
self.circle.remove_style_class("power-saver") self.circle.remove_style_class("power-saver")
self.circle.remove_style_class("performance") self.circle.remove_style_class("performance")
self.circle.remove_style_class("balanced") self.circle.remove_style_class("balanced")
@@ -228,7 +198,6 @@ class Battery(Box):
self.icon.remove_style_class("discharging-low") self.icon.remove_style_class("discharging-low")
self.icon.remove_style_class("discharging-critical") self.icon.remove_style_class("discharging-critical")
# Apply power profile styling and icons
if power_profile: if power_profile:
self.circle.add_style_class(power_profile) self.circle.add_style_class(power_profile)
if power_profile == "power-saver": if power_profile == "power-saver":
@@ -238,7 +207,6 @@ class Battery(Box):
else: # balanced else: # balanced
# In balanced mode, show discharging icons when discharging # In balanced mode, show discharging icons when discharging
if state == "DISCHARGING": if state == "DISCHARGING":
# Show different discharging icons and apply styling based on battery level
if percentage <= 15: if percentage <= 15:
self.icon.set_markup(icons.bat_low) self.icon.set_markup(icons.bat_low)
self.circle.add_style_class("discharging-critical") self.circle.add_style_class("discharging-critical")
@@ -256,7 +224,6 @@ class Battery(Box):
else: else:
self.icon.set_markup(icons.battery) self.icon.set_markup(icons.battery)
else: else:
# No power profile - show basic battery state icons
if state == "FULLY_CHARGED": if state == "FULLY_CHARGED":
self.icon.set_markup(icons.battery) self.icon.set_markup(icons.battery)
elif state == "CHARGING": elif state == "CHARGING":
@@ -266,7 +233,6 @@ class Battery(Box):
else: else:
self.icon.set_markup(icons.battery) self.icon.set_markup(icons.battery)
# Apply alert styling if battery is low AND not charging
if percentage <= 15 and not charging: if percentage <= 15 and not charging:
self.icon.add_style_class("alert") self.icon.add_style_class("alert")
self.circle.add_style_class("alert") self.circle.add_style_class("alert")