the_goth_cardgame_collection/games/solitaire/base/solitaire_tap_prefs.py

75 lines
2.3 KiB
Python

# -*- coding: utf-8 -*-
# games/solitaire/base/solitaire_tap_prefs.py
from __future__ import annotations
import sqlite3
from models.database import Database
TAP_TO_MOVE_SETTING_KEY = "sol_single_tap_to_move"
SINGLE_TAP_MAX_HOLD_SEC = 0.28
_single_tap_cache: bool | None = None
def _get_user_db_path() -> str:
for attr in ("USER_DB_PATH", "DB_PATH", "_user_db_path", "db_path"):
val = getattr(Database, attr, None)
if isinstance(val, str) and val.strip():
return val
return "user_data.db"
def _load_persisted_flag(default: bool = False) -> bool:
try:
with sqlite3.connect(_get_user_db_path()) as conn:
conn.execute(
"CREATE TABLE IF NOT EXISTS app_settings ("
"key TEXT PRIMARY KEY, "
"val TEXT NOT NULL"
")"
)
row = conn.execute(
"SELECT val FROM app_settings WHERE key = ?",
(TAP_TO_MOVE_SETTING_KEY,),
).fetchone()
if row is not None:
return str(row[0]).strip().lower() in ("1", "true", "yes", "on")
except Exception:
pass
return default
def _save_persisted_flag(enabled: bool) -> None:
try:
with sqlite3.connect(_get_user_db_path()) as conn:
conn.execute(
"CREATE TABLE IF NOT EXISTS app_settings ("
"key TEXT PRIMARY KEY, "
"val TEXT NOT NULL"
")"
)
conn.execute(
"INSERT OR REPLACE INTO app_settings (key, val) VALUES (?, ?)",
(TAP_TO_MOVE_SETTING_KEY, "1" if enabled else "0"),
)
conn.commit()
except Exception:
pass
def is_single_tap_to_move_enabled() -> bool:
"""Gibt zurück, ob 'Einfacher Klick zum Schieben' aktiv ist (mit In-Memory-Cache)."""
global _single_tap_cache
if _single_tap_cache is None:
_single_tap_cache = _load_persisted_flag(default=False)
return _single_tap_cache
def set_single_tap_to_move_enabled(enabled: bool, persist: bool = True) -> None:
"""Aktiviert/deaktiviert 'Einfacher Klick zum Schieben' und speichert es optional in user_data.db."""
global _single_tap_cache
state = bool(enabled)
_single_tap_cache = state
if persist:
_save_persisted_flag(state)