|
| 1 | +import os |
1 | 2 | from typing import Optional, Callable, TYPE_CHECKING |
2 | 3 | from loguru import logger |
3 | 4 | from PySide6.QtCore import QTimer |
|
9 | 10 |
|
10 | 11 |
|
11 | 12 | app_start_time: float = 0.0 |
| 13 | +pending_uiaccess_restart_after_show: bool = False |
| 14 | +_pending_uiaccess_restart_consumed: bool = False |
12 | 15 |
|
13 | 16 |
|
14 | 17 | class WindowManager: |
@@ -160,16 +163,62 @@ def _configure_main_window_display(self) -> None: |
160 | 163 | if is_maximized: |
161 | 164 | from app.tools.variable import APP_INIT_DELAY |
162 | 165 |
|
163 | | - QTimer.singleShot(APP_INIT_DELAY, self.main_window.showMaximized) |
| 166 | + def show_main_window(): |
| 167 | + try: |
| 168 | + self.main_window.showMaximized() |
| 169 | + finally: |
| 170 | + self._schedule_main_window_shown_tasks() |
| 171 | + |
| 172 | + QTimer.singleShot(APP_INIT_DELAY, show_main_window) |
164 | 173 | else: |
165 | 174 | self.main_window.show() |
| 175 | + self._schedule_main_window_shown_tasks() |
166 | 176 |
|
167 | 177 | startup_display_float = readme_settings_async( |
168 | 178 | "floating_window_management", "startup_display_floating_window" |
169 | 179 | ) |
170 | 180 | if startup_display_float: |
171 | 181 | self.show_float_window() |
172 | 182 |
|
| 183 | + def _schedule_main_window_shown_tasks(self) -> None: |
| 184 | + try: |
| 185 | + QTimer.singleShot(0, self._handle_main_window_shown) |
| 186 | + except Exception: |
| 187 | + pass |
| 188 | + |
| 189 | + def _handle_main_window_shown(self) -> None: |
| 190 | + global pending_uiaccess_restart_after_show |
| 191 | + global _pending_uiaccess_restart_consumed |
| 192 | + |
| 193 | + if not bool(pending_uiaccess_restart_after_show): |
| 194 | + return |
| 195 | + if bool(_pending_uiaccess_restart_consumed): |
| 196 | + return |
| 197 | + _pending_uiaccess_restart_consumed = True |
| 198 | + |
| 199 | + try: |
| 200 | + import platform |
| 201 | + |
| 202 | + if platform.system() != "Windows": |
| 203 | + return |
| 204 | + except Exception: |
| 205 | + return |
| 206 | + |
| 207 | + try: |
| 208 | + from PySide6.QtWidgets import QApplication |
| 209 | + from app.tools.variable import EXIT_CODE_RESTART |
| 210 | + from app.common.windows.uiaccess import ( |
| 211 | + UIACCESS_RESTART_ENV, |
| 212 | + is_uiaccess_process, |
| 213 | + ) |
| 214 | + |
| 215 | + if bool(is_uiaccess_process()): |
| 216 | + return |
| 217 | + os.environ[str(UIACCESS_RESTART_ENV)] = "1" |
| 218 | + QApplication.exit(EXIT_CODE_RESTART) |
| 219 | + except Exception as e: |
| 220 | + logger.debug("主窗口显示后触发 UIAccess 重启失败(已忽略): {}", e) |
| 221 | + |
173 | 222 | def _connect_url_handler_signals(self) -> None: |
174 | 223 | """连接URL处理器信号""" |
175 | 224 | if not self.url_handler: |
|
0 commit comments