fix(accounts): keep first login launch in one window
This commit is contained in:
@@ -110,6 +110,9 @@ class AccountsTab(QWidget):
|
||||
self.account_rows = []
|
||||
self.login_statuses = {}
|
||||
self.threads = []
|
||||
self._launching_accounts = set()
|
||||
self._launch_guard_until = {}
|
||||
self._launch_run_ids = {}
|
||||
|
||||
self.table = QTableWidget(0, len(self.COLUMNS))
|
||||
self.table.setHorizontalHeaderLabels(self.COLUMNS)
|
||||
@@ -173,6 +176,9 @@ class AccountsTab(QWidget):
|
||||
|
||||
def _update_button_state(self):
|
||||
has_selection = self._selected_account() is not None
|
||||
launch_in_progress = bool(self._launching_accounts)
|
||||
self.table.setEnabled(not launch_in_progress)
|
||||
self.add_button.setEnabled(not launch_in_progress)
|
||||
for button in (
|
||||
self.edit_button,
|
||||
self.delete_button,
|
||||
@@ -180,7 +186,33 @@ class AccountsTab(QWidget):
|
||||
self.check_button,
|
||||
self.shortcut_button,
|
||||
):
|
||||
button.setEnabled(has_selection)
|
||||
button.setEnabled(has_selection and not launch_in_progress)
|
||||
|
||||
def _launch_guard_seconds(self):
|
||||
app = QApplication.instance()
|
||||
if app is None:
|
||||
return 0.5
|
||||
return max(0.25, app.doubleClickInterval() / 1000.0)
|
||||
|
||||
def _ignore_reentrant_launch(self, account):
|
||||
alias = account.alias
|
||||
now = time.monotonic()
|
||||
if (
|
||||
alias not in self._launching_accounts
|
||||
and now >= self._launch_guard_until.get(alias, 0.0)
|
||||
):
|
||||
return False
|
||||
_safe_add_run_log_event(
|
||||
self._launch_run_ids.get(alias),
|
||||
(
|
||||
f"step=launch_chrome result=ignored detail=账号 {alias} "
|
||||
"reason=launch_in_progress"
|
||||
),
|
||||
db_path=self.db_path,
|
||||
account=account,
|
||||
)
|
||||
self._set_status("该账号 Chrome 正在启动,请稍候", level="info")
|
||||
return True
|
||||
|
||||
def refresh_accounts(self):
|
||||
try:
|
||||
@@ -297,12 +329,27 @@ class AccountsTab(QWidget):
|
||||
account = self._selected_account()
|
||||
if account is None:
|
||||
return
|
||||
if self._ignore_reentrant_launch(account):
|
||||
return
|
||||
run_id = _safe_create_run_log(
|
||||
"chrome_launch",
|
||||
db_path=self.db_path,
|
||||
total=1,
|
||||
options={"alias": account.alias, "debug_port": account.debug_port},
|
||||
)
|
||||
self._launching_accounts.add(account.alias)
|
||||
self._launch_run_ids[account.alias] = run_id
|
||||
self._update_button_state()
|
||||
try:
|
||||
self._launch_login_once(account, run_id)
|
||||
finally:
|
||||
self._launching_accounts.discard(account.alias)
|
||||
self._launch_guard_until[account.alias] = (
|
||||
time.monotonic() + self._launch_guard_seconds()
|
||||
)
|
||||
self._update_button_state()
|
||||
|
||||
def _launch_login_once(self, account, run_id):
|
||||
started = time.monotonic()
|
||||
_safe_add_run_log_event(
|
||||
run_id,
|
||||
@@ -346,13 +393,27 @@ class AccountsTab(QWidget):
|
||||
action = (launch_result or {}).get("action") or "launched"
|
||||
pid = (launch_result or {}).get("pid")
|
||||
target_id = (launch_result or {}).get("target_id") or ""
|
||||
url = (launch_result or {}).get("url") or ""
|
||||
url = diagnostics.redact_log_text((launch_result or {}).get("url") or "")
|
||||
created_tab = bool((launch_result or {}).get("created_tab"))
|
||||
startup_target_reused = bool(
|
||||
(launch_result or {}).get("startup_target_reused")
|
||||
)
|
||||
startup_page_navigated = bool(
|
||||
(launch_result or {}).get("startup_page_navigated")
|
||||
)
|
||||
page_target_count = int((launch_result or {}).get("page_target_count") or 0)
|
||||
fallback_reason = (launch_result or {}).get("fallback_reason") or ""
|
||||
event_result = "reused" if action == "reused" else "launched"
|
||||
_safe_add_run_log_event(
|
||||
run_id,
|
||||
(
|
||||
f"step=launch_chrome result={event_result} detail=账号 {account.alias} "
|
||||
f"pid={pid or ''} target_id={target_id} url={url} elapsed_ms={elapsed_ms}"
|
||||
f"pid={pid or ''} target_id={target_id} url={url} "
|
||||
f"created_tab={int(created_tab)} "
|
||||
f"startup_target_reused={int(startup_target_reused)} "
|
||||
f"startup_page_navigated={int(startup_page_navigated)} "
|
||||
f"page_target_count={page_target_count} "
|
||||
f"fallback_reason={fallback_reason} elapsed_ms={elapsed_ms}"
|
||||
),
|
||||
db_path=self.db_path,
|
||||
account=account,
|
||||
@@ -372,6 +433,11 @@ class AccountsTab(QWidget):
|
||||
"pid": pid,
|
||||
"target_id": target_id,
|
||||
"url": url,
|
||||
"created_tab": created_tab,
|
||||
"startup_target_reused": startup_target_reused,
|
||||
"startup_page_navigated": startup_page_navigated,
|
||||
"page_target_count": page_target_count,
|
||||
"fallback_reason": fallback_reason or None,
|
||||
},
|
||||
)
|
||||
self.login_statuses[account.alias] = "已启动"
|
||||
@@ -447,4 +513,3 @@ class AccountsTab(QWidget):
|
||||
PLAINTEXT_SECRET_TITLE,
|
||||
PLAINTEXT_PASSWORD_WARNING,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user