refactor(src/window/controlcenter): refactor UI layout and battery icon handling in BluetoothDeviceSlot

This commit is contained in:
sankalp committed 2026-07-04 05:00:36 +05:45
1 parent c1e24f7cb4
commit 901731711f
1 file changed
+59 -53
+59 -53
View File
@@ -63,7 +63,7 @@ class BTState(Enum):
FAILED = auto() FAILED = auto()
class BluetoothDeviceSlot(CenterBox): class BluetoothDeviceSlot(Box):
def __init__(self, device: BluetoothDevice, **kwargs): def __init__(self, device: BluetoothDevice, **kwargs):
super().__init__(h_expand=True, name="device-button", **kwargs) super().__init__(h_expand=True, name="device-button", **kwargs)
self.device = device self.device = device
@@ -97,38 +97,42 @@ class BluetoothDeviceSlot(CenterBox):
) )
self.status_icon.set_visible(False) self.status_icon.set_visible(False)
self.battery_icon = None
self.battery_label = None
if hasattr(device, "battery_percentage") and device.battery_percentage > 0:
self.battery_icon = svg_file(
get_battery_icon_file(
device.battery_percentage,
False,
),
size=24,
)
self.battery_label = Label(
label=f"{device.battery_percentage:.0f}%", name="battery-label"
)
button_children = [
self.dimage,
Label(label=device.name),
Box(h_expand=True), # Spacer
self.status_icon,
]
if self.battery_icon and self.battery_label:
button_children.insert(-1, self.battery_icon)
button_children.insert(-1, self.battery_label)
self.device_button = Button( self.device_button = Button(
on_clicked=lambda *_: self.toggle_connecting(), on_clicked=lambda *_: self.toggle_connecting(),
h_expand=True,
h_align="fill",
child=Box( child=Box(
orientation="h", orientation="h",
h_expand=True, h_expand=True,
spacing=8, spacing=8,
children=[ children=button_children,
self.dimage,
Label(label=device.name),
Box(h_expand=True), # Spacer
self.status_icon,
],
), ),
) )
self.start_children = [self.device_button] self.children = [self.device_button]
if hasattr(device, "battery_percentage") and device.battery_percentage > 0:
battery_box = Box(orientation="h", spacing=4)
battery_icon = svg_file(
get_battery_icon_file(
device.battery_percentage,
False,
)
)
battery_label = Label(
label=f"{device.battery_percentage:.0f}%", name="battery-label"
)
battery_box.children = [battery_icon, battery_label]
self.end_children = [battery_box]
self.device_button.connect("enter-notify-event", self.on_button_enter) self.device_button.connect("enter-notify-event", self.on_button_enter)
self.device_button.connect("leave-notify-event", self.on_button_leave) self.device_button.connect("leave-notify-event", self.on_button_leave)
@@ -209,39 +213,41 @@ class BluetoothDeviceSlot(CenterBox):
hasattr(self.device, "battery_percentage") hasattr(self.device, "battery_percentage")
and self.device.battery_percentage > 0 and self.device.battery_percentage > 0
): ):
if not self.end_children: # Add battery display if not already present if self.battery_icon and self.battery_label:
battery_box = Box(orientation="h", spacing=4) self.battery_icon.set_visible(True)
self.battery_label.set_visible(True)
# Create battery icon self.battery_icon.dynamic_file(
battery_icon = svg_file(
get_battery_icon_file( get_battery_icon_file(
self.device.battery_percentage, self.device.battery_percentage,
False, # Not charging for bluetooth devices False,
) )
) )
self.battery_label.set_label(f"{self.device.battery_percentage:.0f}%")
battery_label = Label(
label=f"{self.device.battery_percentage:.0f}%", name="battery-label"
)
battery_box.children = [battery_icon, battery_label]
self.end_children = [battery_box]
else: else:
battery_box = self.end_children[0] self.battery_icon = svg_file(
if hasattr(battery_box, "children") and len(battery_box.children) >= 2: get_battery_icon_file(
battery_icon = battery_box.children[0] self.device.battery_percentage,
battery_label = battery_box.children[1] False,
),
battery_icon.dynamic_file( size=12,
get_battery_icon_file( )
self.device.battery_percentage, self.battery_label = Label(
False, # Not charging for bluetooth devices label=f"{self.device.battery_percentage:.0f}%",
) name="battery-label",
) )
btn_box = self.device_button.get_child()
battery_label.set_label(f"{self.device.battery_percentage:.0f}%") if btn_box:
elif self.end_children: btn_box.children = [
self.end_children = [] *btn_box.children[:-1],
self.battery_icon,
self.battery_label,
btn_box.children[-1],
]
elif self.battery_icon or self.battery_label:
if self.battery_icon:
self.battery_icon.set_visible(False)
if self.battery_label:
self.battery_label.set_visible(False)
def _refresh_state(self): def _refresh_state(self):
self._set_state( self._set_state(