feat: restore project workspace with lazy background tabs
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
"""Validated per-project tab snapshots; background tabs contain no browsers."""
|
||||
from __future__ import annotations
|
||||
import hashlib
|
||||
import json
|
||||
from dataclasses import asdict, dataclass, field
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@dataclass
|
||||
class TabState:
|
||||
path: str
|
||||
editing: bool = False
|
||||
view: str = "split"
|
||||
sync: bool = True
|
||||
cursor: int = 0
|
||||
source_scroll: int = 0
|
||||
scroll: int = 0
|
||||
sizes: list[int] = field(default_factory=lambda: [560, 560])
|
||||
|
||||
@classmethod
|
||||
def read(cls, data, root):
|
||||
if not isinstance(data, dict) or not isinstance(data.get("path"), str):
|
||||
return None
|
||||
try:
|
||||
path = Path(data["path"]).resolve()
|
||||
if not path.is_file() or not path.is_relative_to(root.resolve()):
|
||||
return None
|
||||
state = cls(str(path), data.get("editing") is True and path.suffix.lower() in {".md", ".markdown"})
|
||||
state.view = data.get("view") if data.get("view") in ("source", "preview", "split") else "split"
|
||||
state.sync = data.get("sync", True) is not False
|
||||
for key in ("cursor", "source_scroll", "scroll"):
|
||||
setattr(state, key, max(0, int(data.get(key, 0))))
|
||||
sizes = data.get("sizes")
|
||||
if isinstance(sizes, list) and len(sizes) == 2 and all(isinstance(v, int) and 0 <= v <= 100000 for v in sizes):
|
||||
state.sizes = sizes
|
||||
return state
|
||||
except (OSError, ValueError, TypeError, OverflowError):
|
||||
return None
|
||||
|
||||
|
||||
class WorkspaceStore:
|
||||
def __init__(self, settings):
|
||||
self.settings = settings
|
||||
|
||||
@staticmethod
|
||||
def key(root: Path) -> str:
|
||||
return "workspace/" + hashlib.sha256(str(root.resolve()).casefold().encode()).hexdigest()
|
||||
|
||||
def save(self, root: Path, tabs: list[TabState], current: int) -> None:
|
||||
self.settings.setValue(self.key(root), json.dumps({"tabs": [asdict(tab) for tab in tabs], "current": current}, ensure_ascii=False))
|
||||
|
||||
def load(self, root: Path):
|
||||
try:
|
||||
payload = json.loads(str(self.settings.value(self.key(root), "{}")))
|
||||
if not isinstance(payload, dict) or not isinstance(payload.get("tabs", []), list):
|
||||
return [], 0
|
||||
tabs, active = [], 0
|
||||
selected = int(payload.get("current", 0))
|
||||
for index, item in enumerate(payload.get("tabs", [])[:100]):
|
||||
state = TabState.read(item, root)
|
||||
if state:
|
||||
if index == selected:
|
||||
active = len(tabs)
|
||||
tabs.append(state)
|
||||
return tabs, active
|
||||
except (ValueError, TypeError, AttributeError, OverflowError):
|
||||
return [], 0
|
||||
Reference in New Issue
Block a user