Fix Bluetooth and Wifi

This commit is contained in:
tr1x_em committed 2025-08-07 14:22:06 +05:30
1 parent 0255933fe7
commit 8eaac4ff30
2 files changed
+70 -205

No files matched your search

+55 -201
View File
@@ -1,48 +1,31 @@
# Standard library imports from gi.repository import Gdk, GLib
import contextlib
import gc
import weakref
from pathlib import Path
from typing import Optional, Dict, Any
# Fabric imports from fabric.utils import idle_add
from fabric.core.service import Service, Signal, Property from fabric.utils.helpers import (
from fabric.widgets.window import Window get_relative_path,
from fabric.widgets.overlay import Overlay )
from fabric.widgets.box import Box from fabric.widgets.box import Box
from fabric.widgets.centerbox import CenterBox
from fabric.widgets.button import Button from fabric.widgets.button import Button
from fabric.widgets.centerbox import CenterBox
from fabric.widgets.label import Label from fabric.widgets.label import Label
from fabric.widgets.scale import Scale from fabric.widgets.scale import Scale
from fabric.widgets.revealer import Revealer
from fabric.utils import idle_add, get_relative_path, invoke_repeater
from fabric.widgets.image import Image
from fabric.widgets.svg import Svg from fabric.widgets.svg import Svg
# Gi imports
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("GLib", "2.0")
from gi.repository import Gtk, GLib, Gdk
# Local imports
from modules.controlcenter.bluetooth import BluetoothConnections from modules.controlcenter.bluetooth import BluetoothConnections
from modules.controlcenter.wifi import WifiConnections from modules.controlcenter.wifi import WifiConnections
from services.brightness import Brightness
from utils.roam import audio_service, modus_service
from widgets.wayland import WaylandWindow as Window
from modules.controlcenter.player import PlayerBoxStack from modules.controlcenter.player import PlayerBoxStack
from modules.controlcenter.per_app_volume import PerAppVolumeControl from modules.controlcenter.per_app_volume import PerAppVolumeControl
from modules.controlcenter.expanded_player import EmbeddedExpandedPlayer, get_shared_mpris_manager from modules.controlcenter.expanded_player import EmbeddedExpandedPlayer
from services.modus import modus_service
from services.brightness import Brightness
from services.mpris import MprisPlayerManager from services.mpris import MprisPlayerManager
from utils.roam import audio_service from fabric.widgets.svg import Svg
from widgets.wayland import WaylandWindow as Window from fabric.utils.helpers import get_relative_path
from loguru import logger
brightness_service = Brightness.get_initial() brightness_service = Brightness.get_initial()
# Memory management globals # FIX: Icon not showing up in control center
_widget_cache = weakref.WeakValueDictionary() # TODO: Add Player
_cleanup_timer_id = None
class ModusControlCenter(Window): class ModusControlCenter(Window):
@@ -296,32 +279,24 @@ class ModusControlCenter(Window):
Box( Box(
orientation="horizontal", orientation="horizontal",
spacing=8, spacing=8,
v_expand=True, v_expand=False,
h_expand=True,
children=[ children=[
self.volume_scale, self.volume_scale,
Button( Button(
name="per-app-volume-button", name="per-app-volume-button",
child=Box( child=Svg(
h_align="center", svg_file=get_relative_path(
v_align="center", "../../config/assets/icons/player/audio-switcher.svg"
h_expand=True, ),
size=(32, 32), name="per-app-volume-icon",
v_expand=True,
children=[
Svg(
svg_file=get_relative_path(
"../../config/assets/icons/player/audio-switcher.svg"
),
size=34,
)
],
), ),
on_clicked=self.open_per_app_volume, on_clicked=self.open_per_app_volume,
), ),
], ],
), ),
Label(label="", name="volume-widget-icon", h_align="start"), Label(
label="", name="brightness-widget-icon", h_align="start"
),
], ],
), ),
Box( Box(
@@ -356,119 +331,39 @@ class ModusControlCenter(Window):
self.children = self.center_box self.children = self.center_box
# Set memory baseline for monitoring
# Connect to visibility changes for cleanup # Connect to visibility changes for cleanup
self.connect("notify::visible", self._on_visibility_changed) self.connect("notify::visible", self._on_visibility_changed)
# Periodic cleanup timer (every 5 minutes when visible)
# Start periodic memory cleanup (every 5 minutes)
global _cleanup_timer_id
_cleanup_timer_id = GLib.timeout_add_seconds(300, self._periodic_cleanup)
def _on_visibility_changed(self, widget, param): def _on_visibility_changed(self, widget, param):
"""Handle visibility changes for memory management""" """Handle visibility changes for memory management"""
if not self.get_visible(): if not self.get_visible():
logger.debug("Control center hidden - starting cleanup")
self._cleanup_when_hidden() self._cleanup_when_hidden()
def _cleanup_when_hidden(self): def _cleanup_when_hidden(self):
"""Clean up resources when widget is hidden to reduce memory usage""" """Clean up resources when widget is hidden"""
logger.debug("Starting hidden cleanup - freeing widget memory") # Clean up music widget content if it exists
# Clean up music widget content properly if self._music_widget_content and not self._music_initialized:
if self._music_widget_content: # Remove from the parent container
try: current_children = list(self.music_widget.children)
# Remove from parent container first if self._music_widget_content in current_children:
current_children = list(self.music_widget.children) current_children.remove(self._music_widget_content)
if self._music_widget_content in current_children: self.music_widget.children = current_children
current_children.remove(self._music_widget_content) self._music_widget_content = None
self.music_widget.children = current_children
# Destroy the content to free memory completely # Reset lazy loading flags to allow re-initialization when shown again
if hasattr(self._music_widget_content, "destroy"): # Keep initialized flags to avoid recreating heavy components unnecessarily
self._music_widget_content.destroy()
logger.debug("Destroyed music widget content to free memory")
self._music_widget_content = None
self._music_initialized = False
except Exception as e:
logger.warning(f"Failed to cleanup music widget content: {e}")
# Clean up per-app volume widget
if self._per_app_volume_widget and hasattr(
self._per_app_volume_widget, "destroy"
):
try:
self._per_app_volume_widget.destroy()
self._per_app_volume_widget = None
self._per_app_volume_initialized = False
except Exception:
pass
# Clean up expanded player widget - aggressive cleanup for reuse
if self._expanded_player_widget:
try:
# Call cleanup directly on the player content (PlayerBoxStack)
if hasattr(self._expanded_player_widget, 'player_content') and \
hasattr(self._expanded_player_widget.player_content, '_periodic_cleanup'):
self._expanded_player_widget.player_content._periodic_cleanup()
# Also clean any EmbeddedExpandedPlayer specific state
if hasattr(self._expanded_player_widget, '_periodic_cleanup'):
self._expanded_player_widget._periodic_cleanup()
logger.debug("Cleaned expanded player internal state (preserved for reuse)")
# DON'T destroy or set to None - keep for reuse!
except Exception as e:
logger.warning(f"Failed to clean expanded player state: {e}")
# Reset widget containers to free memory (except expanded player for reuse)
self.bluetooth_widgets = None
self.wifi_widgets = None
self.per_app_volume_widgets = None
# Keep expanded_player_widgets for reuse!
# self.expanded_player_widgets = None
# Reset center boxes (except expanded player for reuse)
self.bluetooth_center_box = None
self.wifi_center_box = None
self.per_app_volume_center_box = None
# Keep expanded_player_center_box for reuse!
# self.expanded_player_center_box = None
# Force garbage collection
import gc
gc.collect()
def _periodic_cleanup(self):
"""Periodic cleanup to keep memory usage low"""
if not self.get_visible():
# If not visible, do aggressive cleanup
self._cleanup_when_hidden()
return True # Continue timer
# Light cleanup when visible
import gc
gc.collect()
return True # Continue timer
def _ensure_music_widget(self): def _ensure_music_widget(self):
"""Lazy load music widget content""" """Lazy load music widget content"""
if not self._music_initialized: if not self._music_initialized:
logger.debug("Creating new PlayerBoxStack instance")
self._music_widget_content = PlayerBoxStack( self._music_widget_content = PlayerBoxStack(
get_shared_mpris_manager(), control_center=self MprisPlayerManager(), control_center=self
) )
# Add to the music widget's children list # Add to the music widget's children list
current_children = list(self.music_widget.children) current_children = list(self.music_widget.children)
current_children.append(self._music_widget_content) current_children.append(self._music_widget_content)
self.music_widget.children = current_children self.music_widget.children = current_children
self._music_initialized = True self._music_initialized = True
else:
logger.debug("Music widget already initialized")
def _ensure_bluetooth_widgets(self): def _ensure_bluetooth_widgets(self):
"""Lazy load bluetooth widgets""" """Lazy load bluetooth widgets"""
@@ -550,14 +445,10 @@ class ModusControlCenter(Window):
self.per_app_volume_center_box.set_size_request(300, -1) self.per_app_volume_center_box.set_size_request(300, -1)
def _ensure_expanded_player_widgets(self): def _ensure_expanded_player_widgets(self):
"""Lazy load expanded player widgets with reuse optimization""" """Lazy load expanded player widgets"""
if self.expanded_player_widgets is None: if self.expanded_player_widgets is None:
logger.debug("Creating new expanded player widgets")
if self._expanded_player_widget is None: if self._expanded_player_widget is None:
logger.debug("Creating new EmbeddedExpandedPlayer instance")
self._expanded_player_widget = EmbeddedExpandedPlayer(self) self._expanded_player_widget = EmbeddedExpandedPlayer(self)
else:
logger.debug("Reusing existing EmbeddedExpandedPlayer instance")
self.expanded_player_widgets = Box( self.expanded_player_widgets = Box(
orientation="vertical", orientation="vertical",
@@ -571,8 +462,6 @@ class ModusControlCenter(Window):
start_children=[self.expanded_player_widgets] start_children=[self.expanded_player_widgets]
) )
self.expanded_player_center_box.set_size_request(300, -1) self.expanded_player_center_box.set_size_request(300, -1)
else:
logger.debug("Reusing existing expanded player widgets")
def set_dont_disturb(self, *_): def set_dont_disturb(self, *_):
self.focus_mode = not self.focus_mode self.focus_mode = not self.focus_mode
@@ -776,59 +665,24 @@ class ModusControlCenter(Window):
self.set_visible(False) self.set_visible(False)
def destroy(self): def destroy(self):
"""Enhanced cleanup of resources when widget is destroyed.""" """Clean up resources when widget is destroyed"""
try: # Disconnect all signal connections
# Cancel any periodic cleanup timers for connection in self._signal_connections:
global _cleanup_timer_id try:
if _cleanup_timer_id: connection.disconnect()
GLib.source_remove(_cleanup_timer_id) except:
_cleanup_timer_id = None pass
# Disconnect all signal connections with better error handling # Clean up heavy components
for connection in self._signal_connections: if self._wifi_man:
try: self._wifi_man.destroy()
if connection and hasattr(connection, 'disconnect'): if self._bluetooth_man:
connection.disconnect() self._bluetooth_man.destroy()
except Exception as e: if self._music_widget_content:
logger.warning(f"Failed to disconnect signal: {e}") self._music_widget_content.destroy()
self._signal_connections.clear() if self._per_app_volume_widget:
self._per_app_volume_widget.destroy()
if self._expanded_player_widget:
self._expanded_player_widget.destroy()
# Clean up heavy components with memory optimization super().destroy()
components_to_cleanup = [
('wifi_man', getattr(self, 'wifi_man', None)),
('bluetooth_man', getattr(self, 'bluetooth_man', None)),
('_music_widget_content', self._music_widget_content),
('_per_app_volume_widget', self._per_app_volume_widget),
('_expanded_player_widget', self._expanded_player_widget)
]
for attr_name, component in components_to_cleanup:
if component and hasattr(component, 'destroy'):
try:
component.destroy()
setattr(self, attr_name, None)
except Exception as e:
logger.warning(f"Failed to destroy {attr_name}: {e}")
# Clean up widget containers and references
widget_containers = [
'bluetooth_widgets', 'wifi_widgets', 'per_app_volume_widgets',
'expanded_player_widgets', 'bluetooth_center_box', 'wifi_center_box',
'per_app_volume_center_box', 'expanded_player_center_box'
]
for container in widget_containers:
if hasattr(self, container):
setattr(self, container, None)
# Clear any cached widgets
global _widget_cache
_widget_cache.clear()
# Force garbage collection for immediate memory cleanup
gc.collect()
except Exception as e:
logger.error(f"Error during control center destruction: {e}")
finally:
super().destroy()
+15 -4
View File
@@ -325,11 +325,17 @@
/* Per-app volume control styles */ /* Per-app volume control styles */
#per-app-volume-control { #per-app-volume-control {
margin: 0px; margin: 0px;
background-color: alpha(#999, 0.1); background-color: alpha(#999, 0.1);
min-height: 140px; min-height: 140px;
min-width: 140px; min-width: 140px;
border: 0.5px solid alpha(#000, 0.4); border: 0.5px solid alpha(#000, 0.4);
border-radius: 8px; border-radius: 8px;
/* padding: 1rem; */ /* padding: 1rem; */
} }
@@ -478,6 +484,8 @@
#per-app-volume-button { #per-app-volume-button {
background-color: alpha(#fff, 0.15); background-color: alpha(#fff, 0.15);
border: 1px solid alpha(#999, 0.6); border: 1px solid alpha(#999, 0.6);
min-width: 33px;
min-height: 30px;
border-radius: 50%; border-radius: 50%;
} }
@@ -493,10 +501,13 @@
} }
#per-app-volume-icon { #per-app-volume-icon {
/* font-size: 14px; */ background-color: alpha(#fff, 0.15);
/* font-weight: bold; */
/* color: #007aff; */ border: 2px solid alpha(#999, 0.6);
/* text-shadow: none; */
border-radius: 50%;
min-width: 30px;
} }
/* Apple-style seek bar for expanded player */ /* Apple-style seek bar for expanded player */