feat(src/window): migrate device monitoring to map/unmap events

Ensure monitoring starts only when the widget is shown and stops on hide, preventing timer leaks and unnecessary refreshes
This commit is contained in:
sankalp committed 2026-06-23 14:11:25 +05:45
1 parent 01fed4c52f
commit 6707e1e830
8 files changed
+95 -29

No files matched your search

+6 -2
View File
@@ -357,8 +357,8 @@ class BluetoothConnections(Box):
# Initial device update
self.update_devices()
# Start periodic device monitoring for real-time updates
self.start_device_monitoring()
self.connect("map", lambda *_: self.start_device_monitoring())
self.connect("unmap", lambda *_: self.on_hide())
def toggle_other_devices(self, *_):
"""Toggle the visibility of other devices section"""
@@ -490,6 +490,10 @@ class BluetoothConnections(Box):
finally:
self._update_in_progress = False
def on_hide(self, *_):
"""Called when the widget is hidden"""
self.stop_device_monitoring()
def start_device_monitoring(self):
"""Start periodic monitoring for device changes"""
# Monitor for device changes every 5 seconds (less aggressive)
+2 -2
View File
@@ -421,13 +421,12 @@ class BluetoothConnections(Box):
self.connect("destroy", self.on_destroy)
self.connect("unmap", self.on_hide)
self.connect("map", lambda *_: self.start_device_monitoring())
self.client.notify("scanning")
self.client.notify("enabled")
self.update_devices()
self.start_device_monitoring()
def handle_user_toggle(self, active: bool):
self._switch_lock = True
set_bluetooth_enabled_with_fallback(self.client, active)
@@ -441,6 +440,7 @@ class BluetoothConnections(Box):
def on_hide(self, *_):
"""Called when the widget is hidden (popup closed)"""
self.stop_device_monitoring()
if self.other_devices.get_visible():
self.other_devices.set_visible(False)
self.other_devices_scrolled.snap_to_size(0)
+11 -2
View File
@@ -77,8 +77,8 @@ class PerAppVolumeControl(Box):
# Initial population
self._populate_apps()
# Set up auto-refresh timer for audio streams
self._refresh_timer = GLib.timeout_add_seconds(2, self._auto_refresh)
self.connect("map", self._on_map)
self.connect("unmap", self._on_unmap)
def _auto_refresh(self):
"""Auto-refresh the application list every 2 seconds"""
@@ -88,6 +88,15 @@ class PerAppVolumeControl(Box):
self._populate_apps()
return True # Continue the timer
def _on_map(self, *_):
if self._refresh_timer is None:
self._refresh_timer = GLib.timeout_add_seconds(2, self._auto_refresh)
def _on_unmap(self, *_):
if self._refresh_timer is not None:
GLib.source_remove(self._refresh_timer)
self._refresh_timer = None
def _go_back(self, *_):
"""Return to main control center view"""
self.control_center.close_per_app_volume()
+2 -2
View File
@@ -383,11 +383,11 @@ class WifiConnections(Box):
self.connect("destroy", self.on_destroy)
self.connect("unmap", self.on_hide)
self.start_network_monitoring()
self.connect("map", lambda *_: self.start_network_monitoring())
def on_hide(self, *_):
"""Called when the widget is hidden (popup closed)"""
self.stop_network_monitoring()
if self.other_networks.get_visible():
self.other_networks.set_visible(False)
self.other_networks_scrolled.snap_to_size(0)