mirror of
https://github.com/darkzoul5/YoutubePlaylistSync.git
synced 2026-09-18 20:43:54 +03:00
refactor: centralize tray config handling
This commit is contained in:
@@ -52,6 +52,30 @@ def normalize_config(data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
return out
|
||||
|
||||
|
||||
def get_tray_config(data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Return the tray config as a safe dict copy."""
|
||||
ui = data.get("ui")
|
||||
ui = ui if isinstance(ui, dict) else {}
|
||||
tray = ui.get("tray")
|
||||
tray = tray if isinstance(tray, dict) else {}
|
||||
return dict(tray)
|
||||
|
||||
|
||||
def ensure_tray_config(data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Ensure the nested ui.tray dict exists and return it for mutation."""
|
||||
ui = data.get("ui")
|
||||
if not isinstance(ui, dict):
|
||||
ui = {}
|
||||
data["ui"] = ui
|
||||
|
||||
tray = ui.get("tray")
|
||||
if not isinstance(tray, dict):
|
||||
tray = {}
|
||||
ui["tray"] = tray
|
||||
|
||||
return tray
|
||||
|
||||
|
||||
class Settings:
|
||||
"""Unified configuration loader that combines file I/O and playlist merging."""
|
||||
|
||||
|
||||
+2
-6
@@ -5,7 +5,7 @@ import threading
|
||||
|
||||
from PySide6 import QtCore, QtGui, QtWidgets
|
||||
|
||||
from ..config.settings import Settings, load_config
|
||||
from ..config.settings import Settings, get_tray_config, load_config
|
||||
from ..core.events.event_bus import EventBus
|
||||
from .bus_bridge import BusBridge
|
||||
from .app_icon import load_app_icon
|
||||
@@ -132,11 +132,7 @@ class MainWindow(QtWidgets.QMainWindow):
|
||||
if cfg_path is None:
|
||||
return {}
|
||||
raw = load_config(cfg_path)
|
||||
ui = raw.get("ui")
|
||||
ui = ui if isinstance(ui, dict) else {}
|
||||
tray = ui.get("tray")
|
||||
tray = tray if isinstance(tray, dict) else {}
|
||||
return dict(tray)
|
||||
return get_tray_config(raw)
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ from typing import Any
|
||||
|
||||
from PySide6 import QtCore, QtWidgets
|
||||
|
||||
from ...config.settings import load_config, save_config
|
||||
from ...config.settings import ensure_tray_config, get_tray_config, load_config, save_config
|
||||
from ..autosave import DebouncedAutosave
|
||||
|
||||
|
||||
@@ -114,10 +114,7 @@ class SettingsPage(QtWidgets.QWidget):
|
||||
self._retry_delay.setValue(float(self._config.get("retry_delay_seconds") or 1.5))
|
||||
self._download_delay.setValue(float(self._config.get("delay_between_downloads_seconds") or 0.0))
|
||||
|
||||
ui = self._config.get("ui")
|
||||
ui = ui if isinstance(ui, dict) else {}
|
||||
tray = ui.get("tray")
|
||||
tray = tray if isinstance(tray, dict) else {}
|
||||
tray = get_tray_config(self._config)
|
||||
self._close_to_tray.setChecked(bool(tray.get("close_to_tray", False)))
|
||||
self._minimize_to_tray.setChecked(bool(tray.get("minimize_to_tray", False)))
|
||||
self._start_minimized_to_tray.setChecked(bool(tray.get("start_minimized_to_tray", False)))
|
||||
@@ -142,15 +139,10 @@ class SettingsPage(QtWidgets.QWidget):
|
||||
data["retry_delay_seconds"] = float(self._retry_delay.value())
|
||||
data["delay_between_downloads_seconds"] = float(self._download_delay.value())
|
||||
|
||||
ui = data.get("ui")
|
||||
ui = ui if isinstance(ui, dict) else {}
|
||||
tray = ui.get("tray")
|
||||
tray = tray if isinstance(tray, dict) else {}
|
||||
tray = ensure_tray_config(data)
|
||||
tray["close_to_tray"] = bool(self._close_to_tray.isChecked())
|
||||
tray["minimize_to_tray"] = bool(self._minimize_to_tray.isChecked())
|
||||
tray["start_minimized_to_tray"] = bool(self._start_minimized_to_tray.isChecked())
|
||||
ui["tray"] = tray
|
||||
data["ui"] = ui
|
||||
|
||||
save_config(self._config_path, data)
|
||||
self._status.setText(f"Saved settings to {self._config_path}.")
|
||||
|
||||
Reference in New Issue
Block a user