Persist closed account tabs and add scoped account removal
This commit is contained in:
+129
-11
@@ -9,6 +9,7 @@ from PySide6 import QtCore as C, QtWidgets as W
|
||||
from . import gui
|
||||
from .api import Session
|
||||
from .sessions import identity
|
||||
from .account_storage import profile_directory, remove_account_files
|
||||
|
||||
|
||||
class AccountsWindow(W.QMainWindow):
|
||||
@@ -19,6 +20,7 @@ class AccountsWindow(W.QMainWindow):
|
||||
self.root.mkdir(parents=True, exist_ok=True)
|
||||
self.index_path = self.root / "accounts.json"
|
||||
self.windows, self.locks, self.profiles = {}, {}, []
|
||||
self.pending_closes, self.pending_removals = set(), set()
|
||||
self.setWindowTitle("牧羊人之心牧场辅助 — 多账号")
|
||||
font = self.font()
|
||||
font.setPointSizeF(8.5)
|
||||
@@ -35,6 +37,8 @@ class AccountsWindow(W.QMainWindow):
|
||||
self.tabs.currentChanged.connect(self.activate_profile)
|
||||
self.tabs.setTabsClosable(True)
|
||||
self.tabs.tabCloseRequested.connect(self.close_profile)
|
||||
self.tabs.tabBar().setContextMenuPolicy(C.Qt.ContextMenuPolicy.CustomContextMenu)
|
||||
self.tabs.tabBar().customContextMenuRequested.connect(self.account_tab_menu)
|
||||
layout.addWidget(self.tabs, 1)
|
||||
try:
|
||||
self.profiles = json.loads(self.index_path.read_text(encoding="utf-8"))["profiles"]
|
||||
@@ -42,9 +46,9 @@ class AccountsWindow(W.QMainWindow):
|
||||
self.profiles = [{"id": "default", "label": "账号 1"}]
|
||||
self.profiles = [p for p in self.profiles if isinstance(p, dict) and re.fullmatch(r"default|[a-f0-9]{32}", str(p.get("id", "")))]
|
||||
for profile in list(self.profiles):
|
||||
self.open_profile(profile)
|
||||
if not self.profiles:
|
||||
self.add_profile()
|
||||
if profile.get("open", True):
|
||||
self.open_profile(profile)
|
||||
self.update_account_actions()
|
||||
gui.compact_layouts(central)
|
||||
|
||||
def setup_menus(self):
|
||||
@@ -52,6 +56,10 @@ class AccountsWindow(W.QMainWindow):
|
||||
for text, handler in (("新增账号页签", self.add_profile), ("打开已保存账号…", self.choose_profile),
|
||||
("恢复各页签的保存会话", self.resume_all)):
|
||||
accounts.addAction(text, handler)
|
||||
self.close_account_action = accounts.addAction("关闭当前账号页签", lambda: self.close_profile(self.tabs.currentIndex()))
|
||||
self.remove_account_action = accounts.addAction("移除当前账号…", self.remove_current_profile)
|
||||
self.remove_saved_action = accounts.addAction("移除已保存账号…", self.choose_remove_profile)
|
||||
accounts.aboutToShow.connect(self.update_account_actions)
|
||||
accounts.addSeparator()
|
||||
accounts.addAction("退出", self.close)
|
||||
monitoring = self.menuBar().addMenu("监控(&M)")
|
||||
@@ -73,6 +81,9 @@ class AccountsWindow(W.QMainWindow):
|
||||
window = self.tabs.currentWidget()
|
||||
for action in self.policy_actions:
|
||||
action.setEnabled(window is not None and not window.closed)
|
||||
for action in (self.close_account_action, self.remove_account_action):
|
||||
action.setEnabled(window is not None and not window.closed)
|
||||
self.remove_saved_action.setEnabled(bool(self.profiles))
|
||||
|
||||
def run_account_action(self, method):
|
||||
window = self.tabs.currentWidget()
|
||||
@@ -93,10 +104,18 @@ class AccountsWindow(W.QMainWindow):
|
||||
|
||||
def open_profile(self, profile):
|
||||
identifier = profile["id"]
|
||||
if not any(p["id"] == identifier for p in self.profiles):
|
||||
return
|
||||
if identifier in self.pending_removals:
|
||||
self.statusBar().showMessage("此账号正在移除,请等待当前请求完成", 5000)
|
||||
return
|
||||
if identifier in self.windows:
|
||||
if self.windows[identifier].closed:
|
||||
self.statusBar().showMessage("此账号正在关闭,请稍后重新打开", 5000)
|
||||
return
|
||||
self.tabs.setCurrentWidget(self.windows[identifier])
|
||||
return
|
||||
folder = self.root if identifier == "default" else self.root / "profiles" / identifier
|
||||
folder = profile_directory(self.root, identifier)
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
lock = C.QLockFile(str(folder / "workspace.lock"))
|
||||
if not lock.tryLock(0):
|
||||
@@ -109,9 +128,11 @@ class AccountsWindow(W.QMainWindow):
|
||||
self.windows[identifier], self.locks[identifier] = window, lock
|
||||
self.tabs.addTab(window, profile.get("label", "账号"))
|
||||
self.tabs.setCurrentWidget(window)
|
||||
profile["open"] = True
|
||||
self.save_index()
|
||||
|
||||
def add_profile(self):
|
||||
profile = {"id": uuid.uuid4().hex, "label": f"账号 {len(self.profiles) + 1}"}
|
||||
profile = {"id": uuid.uuid4().hex, "label": f"账号 {len(self.profiles) + 1}", "open": True}
|
||||
self.profiles.append(profile)
|
||||
self.open_profile(profile)
|
||||
self.save_index()
|
||||
@@ -125,10 +146,11 @@ class AccountsWindow(W.QMainWindow):
|
||||
def choose_profile(self):
|
||||
if not self.profiles:
|
||||
return
|
||||
labels = [p.get("label", "账号") + " [" + p["id"][:6] + "]" for p in self.profiles]
|
||||
profiles = list(self.profiles)
|
||||
labels = [p.get("label", "账号") + " [" + p["id"][:6] + "]" for p in profiles]
|
||||
label, accepted = W.QInputDialog.getItem(self, "打开账号页签", "保存的账号工作区", labels, 0, False)
|
||||
if accepted:
|
||||
self.open_profile(self.profiles[labels.index(label)])
|
||||
self.open_profile(profiles[labels.index(label)])
|
||||
|
||||
def rename_profile(self, identifier, label):
|
||||
for profile in self.profiles:
|
||||
@@ -180,20 +202,113 @@ class AccountsWindow(W.QMainWindow):
|
||||
window = self.tabs.widget(index)
|
||||
if window is None:
|
||||
return
|
||||
identifier = next(k for k, value in self.windows.items() if value is window)
|
||||
self.close_profile_id(identifier)
|
||||
|
||||
def close_profile_id(self, identifier):
|
||||
window = self.windows.get(identifier)
|
||||
if window is None:
|
||||
return
|
||||
profile = next(p for p in self.profiles if p["id"] == identifier)
|
||||
if profile.get("open", True):
|
||||
profile["open"] = False
|
||||
self.save_index()
|
||||
window.start_monitor.setChecked(False)
|
||||
window.execute.setChecked(False)
|
||||
window.timer.stop()
|
||||
window.hatch_timer.stop()
|
||||
window.engine.auto = False
|
||||
window.closed = True
|
||||
window.pending_refresh = None
|
||||
window.setEnabled(False)
|
||||
self.update_account_actions()
|
||||
if window.busy:
|
||||
C.QTimer.singleShot(250, lambda: self.close_profile(self.tabs.indexOf(window)))
|
||||
if identifier not in self.pending_closes:
|
||||
self.pending_closes.add(identifier)
|
||||
C.QTimer.singleShot(100, lambda: self.finish_close_profile(identifier))
|
||||
return
|
||||
identifier = next(k for k, value in self.windows.items() if value is window)
|
||||
window.close()
|
||||
self.tabs.removeTab(index)
|
||||
self.tabs.removeTab(self.tabs.indexOf(window))
|
||||
del self.windows[identifier]
|
||||
self.locks.pop(identifier).unlock()
|
||||
# Workspaces and saved sessions remain on disk for the next launch.
|
||||
window.deleteLater()
|
||||
if identifier in self.pending_removals:
|
||||
self.finish_remove_profile(identifier)
|
||||
|
||||
def finish_close_profile(self, identifier):
|
||||
self.pending_closes.discard(identifier)
|
||||
self.close_profile_id(identifier)
|
||||
|
||||
def account_tab_menu(self, position):
|
||||
index = self.tabs.tabBar().tabAt(position)
|
||||
window = self.tabs.widget(index)
|
||||
if window is None:
|
||||
return
|
||||
identifier = next(k for k, value in self.windows.items() if value is window)
|
||||
menu = W.QMenu(self)
|
||||
self.tab_context_menu = menu
|
||||
menu.addAction("关闭账号页签", lambda: self.close_profile_id(identifier))
|
||||
menu.addAction("移除此账号…", lambda: self.confirm_remove_profile(identifier))
|
||||
menu.popup(self.tabs.tabBar().mapToGlobal(position))
|
||||
|
||||
def remove_current_profile(self):
|
||||
window = self.tabs.currentWidget()
|
||||
if window is not None:
|
||||
self.confirm_remove_profile(next(k for k, value in self.windows.items() if value is window))
|
||||
|
||||
def choose_remove_profile(self):
|
||||
if not self.profiles:
|
||||
return
|
||||
profiles = list(self.profiles)
|
||||
labels = [p.get("label", "账号") + " [" + p["id"][:6] + "]" for p in profiles]
|
||||
label, accepted = W.QInputDialog.getItem(self, "移除已保存账号", "选择要移除的账号(含已关闭页签)", labels, 0, False)
|
||||
if accepted and label:
|
||||
self.confirm_remove_profile(profiles[labels.index(label)]["id"])
|
||||
|
||||
def confirm_remove_profile(self, identifier):
|
||||
profile = next((p for p in self.profiles if p["id"] == identifier), None)
|
||||
if profile is None or identifier in self.pending_removals:
|
||||
return
|
||||
label = profile.get("label", "账号")
|
||||
text = f"移除“{label}”?\n\n将删除该账号的本地密码、会话、策略、地图缓存和运行记录,无法恢复。\n此操作不会注销游戏账号。"
|
||||
if W.QMessageBox.question(self, "移除账号", text, W.QMessageBox.StandardButton.Yes | W.QMessageBox.StandardButton.No,
|
||||
W.QMessageBox.StandardButton.No) != W.QMessageBox.StandardButton.Yes:
|
||||
return
|
||||
self.pending_removals.add(identifier)
|
||||
if identifier in self.windows:
|
||||
self.statusBar().showMessage("正在停止该账号,当前请求完成后移除", 5000)
|
||||
self.close_profile_id(identifier)
|
||||
else:
|
||||
self.finish_remove_profile(identifier)
|
||||
|
||||
def finish_remove_profile(self, identifier):
|
||||
lock = None
|
||||
try:
|
||||
folder = profile_directory(self.root, identifier)
|
||||
if folder.exists():
|
||||
lock = C.QLockFile(str(folder / "workspace.lock"))
|
||||
if not lock.tryLock(0):
|
||||
raise ValueError("该账号正在其他窗口使用,请关闭后重试")
|
||||
remove_account_files(self.root, identifier)
|
||||
lock.unlock()
|
||||
lock = None
|
||||
if identifier != "default":
|
||||
folder.rmdir()
|
||||
old_profiles = self.profiles
|
||||
self.profiles = [p for p in self.profiles if p["id"] != identifier]
|
||||
try:
|
||||
self.save_index()
|
||||
except OSError:
|
||||
self.profiles = old_profiles
|
||||
raise
|
||||
self.statusBar().showMessage("已移除账号及其本地记录", 5000)
|
||||
except (OSError, ValueError) as error:
|
||||
self.statusBar().showMessage("账号未完全移除:" + str(error), 10000)
|
||||
finally:
|
||||
if lock is not None:
|
||||
lock.unlock()
|
||||
self.pending_removals.discard(identifier)
|
||||
self.update_account_actions()
|
||||
|
||||
def closeEvent(self, event):
|
||||
self.stop_all()
|
||||
@@ -207,6 +322,9 @@ class AccountsWindow(W.QMainWindow):
|
||||
C.QTimer.singleShot(250, self.close)
|
||||
event.ignore()
|
||||
return
|
||||
for identifier in list(self.pending_removals):
|
||||
if identifier in self.windows:
|
||||
self.close_profile_id(identifier)
|
||||
for window in self.windows.values():
|
||||
window.close()
|
||||
for lock in self.locks.values():
|
||||
|
||||
Reference in New Issue
Block a user