From 419f0698c58db3e172134359b713487e3a6b442a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 11:51:21 +0000 Subject: [PATCH 1/5] Initial plan From cd71817e751b6c959bfa28c0f28f819a7375a74e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 11:54:08 +0000 Subject: [PATCH 2/5] Fix restart functionality to work properly in both dev and packaged environments - Modified restart_app() in window.py to use QApplication.exit(EXIT_CODE_RESTART) - Updated restart_application() in main.py to handle Windows and Linux properly - Removed unused subprocess import from window.py - The restart now properly uses the EXIT_CODE_RESTART mechanism Co-authored-by: WSXYT <102407247+WSXYT@users.noreply.github.com> --- app/view/main/window.py | 29 +++++------------------------ main.py | 24 +++++++++++++++++++++++- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/app/view/main/window.py b/app/view/main/window.py index f5d04ecf..d87ec31d 100644 --- a/app/view/main/window.py +++ b/app/view/main/window.py @@ -2,7 +2,6 @@ # 导入库 # ================================================== import os -import subprocess import sys import loguru from loguru import logger @@ -1019,29 +1018,11 @@ 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() 函数会检测此退出码并执行重启逻辑 + from app.tools.variable import EXIT_CODE_RESTART + logger.info("正在退出以触发重启流程...") + QApplication.exit(EXIT_CODE_RESTART) def close_window_secrandom(self): """关闭窗口 diff --git a/main.py b/main.py index eef4bed9..78b28330 100644 --- a/main.py +++ b/main.py @@ -342,6 +342,9 @@ def restart_application(program_dir): Args: program_dir: 程序目录路径 """ + import subprocess + import platform + logger.info("检测到重启信号,正在重启应用程序...") filtered_args = [arg for arg in sys.argv if not arg.startswith("--")] @@ -356,7 +359,26 @@ def restart_application(program_dir): try: os.chdir(program_dir) - os.execl(executable, executable, *filtered_args) + + # Windows 平台使用 subprocess.Popen 启动新进程 + if platform.system() == "Windows": + try: + 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 平台:新进程已启动") + except Exception as e: + logger.exception(f"Windows 平台启动新进程失败: {e}") + os._exit(1) + else: + # Linux/Unix 平台使用 os.execl 替换当前进程 + logger.info("Linux 平台:使用 execl 重启应用程序") + os.execl(executable, executable, *filtered_args) except Exception as e: logger.exception(f"重启应用程序失败: {e}") os._exit(1) From 3f9bd8bffac3a58db048e630852e867a67856022 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 11:55:24 +0000 Subject: [PATCH 3/5] Apply ruff auto-fixes: remove unused imports and fix whitespace Co-authored-by: WSXYT <102407247+WSXYT@users.noreply.github.com> --- app/view/main/window.py | 3 --- main.py | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/app/view/main/window.py b/app/view/main/window.py index d87ec31d..e22906f9 100644 --- a/app/view/main/window.py +++ b/app/view/main/window.py @@ -1,9 +1,6 @@ # ================================================== # 导入库 # ================================================== -import os -import sys -import loguru from loguru import logger from PySide6.QtWidgets import QApplication, QWidget from PySide6.QtGui import QIcon diff --git a/main.py b/main.py index 78b28330..8382dddf 100644 --- a/main.py +++ b/main.py @@ -344,7 +344,7 @@ def restart_application(program_dir): """ import subprocess import platform - + logger.info("检测到重启信号,正在重启应用程序...") filtered_args = [arg for arg in sys.argv if not arg.startswith("--")] @@ -359,7 +359,7 @@ def restart_application(program_dir): try: os.chdir(program_dir) - + # Windows 平台使用 subprocess.Popen 启动新进程 if platform.system() == "Windows": try: From 30550453253ce4fd68d28c3047646b3c4555b2ff Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 11:57:52 +0000 Subject: [PATCH 4/5] Address code review feedback: move imports to top and fix Windows restart exit - Moved subprocess and platform imports to module level - Simplified executable assignment (removed redundant condition) - Added os._exit(0) after successful Windows restart to properly terminate current process Co-authored-by: WSXYT <102407247+WSXYT@users.noreply.github.com> --- main.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 8382dddf..4aeb6d66 100644 --- a/main.py +++ b/main.py @@ -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 @@ -342,16 +344,10 @@ def restart_application(program_dir): Args: program_dir: 程序目录路径 """ - import subprocess - import platform - 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}") @@ -372,6 +368,7 @@ def restart_application(program_dir): startupinfo=startup_info, ) logger.info("Windows 平台:新进程已启动") + os._exit(0) except Exception as e: logger.exception(f"Windows 平台启动新进程失败: {e}") os._exit(1) From b72c8826d3ba09db4ef2d8d6786a430e0068e2c9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 12:01:03 +0000 Subject: [PATCH 5/5] Address final code review feedback - Moved EXIT_CODE_RESTART import to module level in window.py - Simplified exception handling in restart_application - Updated comments to clarify macOS support in restart logic Co-authored-by: WSXYT <102407247+WSXYT@users.noreply.github.com> --- app/view/main/window.py | 2 +- main.py | 28 ++++++++++++---------------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/app/view/main/window.py b/app/view/main/window.py index e22906f9..8298c23a 100644 --- a/app/view/main/window.py +++ b/app/view/main/window.py @@ -15,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 @@ -1017,7 +1018,6 @@ def restart_app(self): # 使用 EXIT_CODE_RESTART 退出码来触发重启 # main.py 中的 handle_exit() 函数会检测此退出码并执行重启逻辑 - from app.tools.variable import EXIT_CODE_RESTART logger.info("正在退出以触发重启流程...") QApplication.exit(EXIT_CODE_RESTART) diff --git a/main.py b/main.py index 4aeb6d66..aea2f5d4 100644 --- a/main.py +++ b/main.py @@ -358,23 +358,19 @@ def restart_application(program_dir): # Windows 平台使用 subprocess.Popen 启动新进程 if platform.system() == "Windows": - try: - 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) - except Exception as e: - logger.exception(f"Windows 平台启动新进程失败: {e}") - os._exit(1) + 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 平台使用 os.execl 替换当前进程 - logger.info("Linux 平台:使用 execl 重启应用程序") + # 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}")