summaryrefslogtreecommitdiff
path: root/core/app_settings.py
blob: 718a76833fd7c8c6b00866775bc32c84403e3edd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
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 app_data_dir() -> Path:
    """Per-user, per-platform base directory for anything this app needs to
    persist outside the install directory (settings, updater cache, ...) —
    deliberately outside the install dir since an update can replace/move
    everything in there."""
    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"


def _settings_path() -> Path:
    return app_data_dir() / "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


# ── Developer mode ────────────────────────────────────────────────────────
#
# In-memory cache so widgets that build device/config UI (Add Device dialog,
# per-device config panels, plugin panels) can check this without needing
# the full settings dict threaded through their constructors. MainWindow
# keeps it in sync with the persisted setting whenever settings are
# loaded/applied — see set_developer_mode() calls in ui/main_window.py.
_dev_mode = True


def is_developer_mode() -> bool:
    return _dev_mode


def set_developer_mode(value: bool) -> None:
    global _dev_mode
    _dev_mode = value