diff options
| author | Christian Kolset <christian.kolset@gmail.com> | 2026-06-03 12:53:35 -0600 |
|---|---|---|
| committer | Christian Kolset <christian.kolset@gmail.com> | 2026-06-03 12:53:35 -0600 |
| commit | 6edbeba5d93569cd4f8f00670399c86909047bab (patch) | |
| tree | 4af17f2f6029281bfadd676b64f17d9c1798bcd6 /core/app_settings.py | |
| parent | 6fb559b64682d6f564d134b8937a688df59ed8e5 (diff) | |
Persist settings per-computer; add Reset to Defaults in General tab
- New core/app_settings.py: load/save settings.json to platform config dir
(Windows: %APPDATA%, Linux: ~/.config, macOS: ~/Library/Application Support)
- MainWindow loads saved settings on startup, saves on every Apply & Close
- Settings survive profile switches and app restarts independently of profiles
- Settings > General: Reset to Defaults button with QMessageBox confirmation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'core/app_settings.py')
| -rw-r--r-- | core/app_settings.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/core/app_settings.py b/core/app_settings.py new file mode 100644 index 0000000..e8bc7ae --- /dev/null +++ b/core/app_settings.py @@ -0,0 +1,53 @@ +""" +core/app_settings.py + +Platform-aware persistence for application settings. + +Stored independently of profiles so they survive profile switches and +reflect per-computer preferences (theme, font, paths, etc.). + +Locations: + Windows : %APPDATA%/labUI/settings.json + macOS : ~/Library/Application Support/labUI/settings.json + Linux : $XDG_CONFIG_HOME/labUI/settings.json (default ~/.config/labUI/) +""" + +import json +import os +import sys +from pathlib import Path + + +def _settings_path() -> Path: + if sys.platform == "win32": + base = Path(os.environ.get("APPDATA", Path.home())) + elif sys.platform == "darwin": + base = Path.home() / "Library" / "Application Support" + else: + base = Path(os.environ.get("XDG_CONFIG_HOME", Path.home() / ".config")) + return base / "labUI" / "settings.json" + + +SETTINGS_PATH = _settings_path() + + +def load_settings(defaults: dict) -> dict: + """Return defaults merged with any saved settings. Never raises.""" + cfg = dict(defaults) + try: + if SETTINGS_PATH.exists(): + with open(SETTINGS_PATH) as f: + cfg.update(json.load(f)) + except Exception: + pass + return cfg + + +def save_settings(cfg: dict) -> None: + """Persist cfg to disk. Never raises.""" + try: + SETTINGS_PATH.parent.mkdir(parents=True, exist_ok=True) + with open(SETTINGS_PATH, "w") as f: + json.dump(cfg, f, indent=2) + except Exception: + pass |
