chore: archive tools/ui_inspector snapshot before migration removal.
Keep the last in-template copy on main so history can restore it; product continues in jiangchang-ui-recorder. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
88
tools/ui_inspector/user_prefs.py
Normal file
88
tools/ui_inspector/user_prefs.py
Normal file
@@ -0,0 +1,88 @@
|
||||
"""Persist UI Inspector prefs (pick mode + start URL + recording mode) across runs."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import target
|
||||
|
||||
_HERE = Path(__file__).resolve().parent
|
||||
_PREFS_PATH = _HERE / "user_prefs.json"
|
||||
|
||||
DEFAULT_MODE = target.SURFACE_BROWSER
|
||||
# 起始 URL:默认空;仅记住用户主动填写过的值
|
||||
DEFAULT_START_URL = ""
|
||||
DEFAULT_RECORDING_MODE = "auto"
|
||||
|
||||
_VALID_MODES = {
|
||||
target.SURFACE_BROWSER,
|
||||
target.SURFACE_WINDOWS,
|
||||
target.SURFACE_ANDROID, # UI label「手机」
|
||||
}
|
||||
|
||||
_VALID_RECORDING = {"auto", "guided", "checkpoint", "legacy_interval"}
|
||||
|
||||
|
||||
def prefs_path() -> Path:
|
||||
return _PREFS_PATH
|
||||
|
||||
|
||||
def load_prefs() -> dict[str, Any]:
|
||||
raw: dict[str, Any] = {}
|
||||
try:
|
||||
if _PREFS_PATH.is_file():
|
||||
data = json.loads(_PREFS_PATH.read_text(encoding="utf-8"))
|
||||
if isinstance(data, dict):
|
||||
raw = data
|
||||
except Exception:
|
||||
raw = {}
|
||||
mode = str(raw.get("mode") or DEFAULT_MODE).strip().lower()
|
||||
if mode in {"mobile", "phone", "ios"}:
|
||||
mode = target.SURFACE_ANDROID
|
||||
if mode not in _VALID_MODES:
|
||||
mode = DEFAULT_MODE
|
||||
# 允许空字符串;未写过 start_url 键时也视为空
|
||||
if "start_url" in raw:
|
||||
start_url = str(raw.get("start_url") or "").strip()
|
||||
else:
|
||||
start_url = DEFAULT_START_URL
|
||||
# v0.14.3 standardizes the public UI on automatic action recording.
|
||||
# Older values remain accepted by lower-level capture APIs, but must not
|
||||
# silently put the simplified UI into a hidden legacy mode.
|
||||
recording_mode = DEFAULT_RECORDING_MODE
|
||||
return {
|
||||
"mode": mode,
|
||||
"start_url": start_url,
|
||||
"recording_mode": recording_mode,
|
||||
}
|
||||
|
||||
|
||||
def save_prefs(
|
||||
*,
|
||||
mode: str,
|
||||
start_url: str,
|
||||
recording_mode: str = DEFAULT_RECORDING_MODE,
|
||||
) -> None:
|
||||
mode_n = (mode or DEFAULT_MODE).strip().lower()
|
||||
if mode_n in {"mobile", "phone", "ios"}:
|
||||
mode_n = target.SURFACE_ANDROID
|
||||
if mode_n not in _VALID_MODES:
|
||||
mode_n = DEFAULT_MODE
|
||||
url = (start_url or "").strip()
|
||||
rec = (recording_mode or DEFAULT_RECORDING_MODE).strip().lower()
|
||||
if rec not in _VALID_RECORDING:
|
||||
rec = DEFAULT_RECORDING_MODE
|
||||
payload = {
|
||||
"mode": mode_n,
|
||||
"start_url": url,
|
||||
"recording_mode": rec,
|
||||
}
|
||||
try:
|
||||
_PREFS_PATH.write_text(
|
||||
json.dumps(payload, ensure_ascii=False, indent=2) + "\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
Reference in New Issue
Block a user