feat: 增加设置页和托盘,登录后关闭窗口留在托盘

This commit is contained in:
2026-10-26 08:28:16 +08:00
parent e78b8897fc
commit 331e316fd9
16 changed files with 501 additions and 52 deletions
+50 -9
View File
@@ -13,6 +13,7 @@ import logging
import os
import re
import shutil
import threading
import time
from collections.abc import AsyncIterator
from concurrent.futures import Future
@@ -340,25 +341,65 @@ def _new_job_id() -> str:
return candidate
def load_task_limit() -> int:
_SETTINGS_LOCK = threading.Lock()
def _read_settings() -> dict:
try:
data = json.loads(SETTINGS_PATH.read_text(encoding="utf-8"))
return min(8, max(1, int(data.get("maxConcurrentJobs") or 2)))
except (OSError, json.JSONDecodeError, TypeError, ValueError):
return 2
return {}
return data if isinstance(data, dict) else {}
def _write_settings(data: dict) -> None:
SETTINGS_PATH.parent.mkdir(parents=True, exist_ok=True)
payload = json.dumps(data, ensure_ascii=False, indent=2).encode("utf-8")
write_atomic(SETTINGS_PATH, payload)
def load_task_limit() -> int:
with _SETTINGS_LOCK:
try:
return min(8, max(1, int(_read_settings().get("maxConcurrentJobs") or 2)))
except (TypeError, ValueError):
return 2
def store_task_limit(value: int) -> int:
limit = min(8, max(1, int(value)))
data = json.dumps({"maxConcurrentJobs": limit}, ensure_ascii=False, indent=2).encode("utf-8")
try:
SETTINGS_PATH.parent.mkdir(parents=True, exist_ok=True)
write_atomic(SETTINGS_PATH, data)
except OSError as exc:
raise NqiError(explain_os_error(exc) or "设置保存失败,请稍后重试") from exc
with _SETTINGS_LOCK:
data = _read_settings()
data["maxConcurrentJobs"] = limit
try:
_write_settings(data)
except OSError as exc:
raise NqiError(explain_os_error(exc) or "设置保存失败,请稍后重试") from exc
return limit
def app_settings() -> dict:
"""未写过的项用默认值:显示托盘,关闭窗口时留在托盘。"""
with _SETTINGS_LOCK:
data = _read_settings()
return {
"showTray": data.get("showTray") is not False,
"closeToTray": data.get("closeToTray") is not False,
}
def store_app_settings(show_tray: bool, close_to_tray: bool) -> dict:
with _SETTINGS_LOCK:
data = _read_settings()
data["showTray"] = bool(show_tray)
data["closeToTray"] = bool(close_to_tray)
try:
_write_settings(data)
except OSError as exc:
raise NqiError(explain_os_error(exc) or "设置保存失败,请稍后重试") from exc
return app_settings()
class JobManager:
def __init__(self) -> None:
self.jobs: dict[str, DownloadJob] = {}