Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 5 additions & 27 deletions app/view/main/window.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# ==================================================
# 导入库
# ==================================================
import os
import subprocess
import sys
import loguru
from loguru import logger
from PySide6.QtWidgets import QApplication, QWidget
from PySide6.QtGui import QIcon
Expand All @@ -19,6 +15,7 @@
PRE_CLASS_RESET_INTERVAL_MS,
RESIZE_TIMER_DELAY_MS,
MAXIMIZE_RESTORE_DELAY_MS,
EXIT_CODE_RESTART,
)
from app.tools.path_utils import get_data_path
from app.tools.personalised import get_theme_icon
Expand Down Expand Up @@ -1019,29 +1016,10 @@ def restart_app(self):

self.cleanup_shortcuts()

try:
working_dir = os.getcwd()
filtered_args = [arg for arg in sys.argv if not arg.startswith("--")]
startup_info = subprocess.STARTUPINFO()
startup_info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.Popen(
[sys.executable] + filtered_args,
cwd=working_dir,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
| subprocess.DETACHED_PROCESS,
startupinfo=startup_info,
)
except Exception as e:
logger.error(f"启动新进程失败: {e}")
return

try:
loguru.logger.remove()
except Exception as e:
logger.error(f"日志系统关闭出错: {e}")

QApplication.quit()
sys.exit(0)
# 使用 EXIT_CODE_RESTART 退出码来触发重启
# main.py 中的 handle_exit() 函数会检测此退出码并执行重启逻辑
logger.info("正在退出以触发重启流程...")
QApplication.exit(EXIT_CODE_RESTART)

def close_window_secrandom(self):
"""关闭窗口
Expand Down
25 changes: 20 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import sys
import time
import gc
import subprocess
import platform

import sentry_sdk
from sentry_sdk.integrations.loguru import LoguruIntegration, LoggingLevels
Expand Down Expand Up @@ -345,18 +347,31 @@ def restart_application(program_dir):
logger.info("检测到重启信号,正在重启应用程序...")
filtered_args = [arg for arg in sys.argv if not arg.startswith("--")]

if getattr(sys, "frozen", False):
executable = sys.executable
else:
executable = sys.executable
executable = sys.executable

if not os.path.exists(executable):
logger.critical(f"重启失败:无法找到可执行文件: {executable}")
os._exit(1)

try:
os.chdir(program_dir)
os.execl(executable, executable, *filtered_args)

# Windows 平台使用 subprocess.Popen 启动新进程
if platform.system() == "Windows":
startup_info = subprocess.STARTUPINFO()
startup_info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.Popen(
[executable] + filtered_args,
cwd=program_dir,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS,
startupinfo=startup_info,
)
logger.info("Windows 平台:新进程已启动")
os._exit(0)
else:
# Linux/Unix/macOS 平台使用 os.execl 替换当前进程
logger.info("Linux/Unix/macOS 平台:使用 execl 重启应用程序")
os.execl(executable, executable, *filtered_args)
except Exception as e:
logger.exception(f"重启应用程序失败: {e}")
os._exit(1)
Expand Down
Loading