From adcd6da5b4c034af8ff1da212296c533e5e6913a Mon Sep 17 00:00:00 2001 From: hyb-oyqq <1512383570@qq.com> Date: Fri, 15 Aug 2025 14:06:22 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20=E5=A2=9E=E5=BC=BA=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E6=A8=A1=E5=BC=8F=E8=8F=9C=E5=8D=95=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在主窗口和离线模式管理器中添加工作模式菜单状态同步逻辑,确保UI状态与实际工作模式一致。 - 在UI管理器中实现同步方法,提升菜单状态更新的可靠性和兼容性。 - 优化代码结构,确保在菜单创建后立即同步状态,增强用户体验。 --- source/core/managers/offline_mode_manager.py | 11 ++++-- source/core/managers/ui_manager.py | 40 ++++++++++++++++++++ source/main_window.py | 4 ++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/source/core/managers/offline_mode_manager.py b/source/core/managers/offline_mode_manager.py index d7afdc5..b1ea2e3 100644 --- a/source/core/managers/offline_mode_manager.py +++ b/source/core/managers/offline_mode_manager.py @@ -204,9 +204,14 @@ class OfflineModeManager: # 同步更新UI菜单中的模式选择状态 if hasattr(self.main_window, 'ui_manager'): ui_manager = self.main_window.ui_manager - if hasattr(ui_manager, 'online_mode_action') and hasattr(ui_manager, 'offline_mode_action'): - ui_manager.online_mode_action.setChecked(not enabled) - ui_manager.offline_mode_action.setChecked(enabled) + # 使用专门的同步方法确保菜单状态正确更新 + if hasattr(ui_manager, 'sync_work_mode_menu_state'): + ui_manager.sync_work_mode_menu_state() + elif hasattr(ui_manager, 'online_mode_action') and hasattr(ui_manager, 'offline_mode_action'): + # 兼容旧版本的直接设置方式 + if ui_manager.online_mode_action and ui_manager.offline_mode_action: + ui_manager.online_mode_action.setChecked(not enabled) + ui_manager.offline_mode_action.setChecked(enabled) # 记录离线模式状态变化 logger.debug(f"离线模式已{'启用' if enabled else '禁用'}") diff --git a/source/core/managers/ui_manager.py b/source/core/managers/ui_manager.py index 935a548..f913030 100644 --- a/source/core/managers/ui_manager.py +++ b/source/core/managers/ui_manager.py @@ -62,6 +62,13 @@ class UIManager: self.debug_action = self.menu_builder.debug_action self.disable_auto_restore_action = self.menu_builder.disable_auto_restore_action self.disable_pre_hash_action = self.menu_builder.disable_pre_hash_action + + # 保存对工作模式菜单项的引用,确保能正确同步状态 + self.online_mode_action = self.menu_builder.online_mode_action + self.offline_mode_action = self.menu_builder.offline_mode_action + + # 在菜单创建完成后,强制同步一次工作模式状态 + self.sync_work_mode_menu_state() # 为了向后兼容性,添加委托方法 def create_progress_window(self, title, initial_text="准备中..."): @@ -84,6 +91,39 @@ class UIManager: """显示菜单(委托给menu_builder)""" return self.menu_builder.show_menu(menu, button) + def sync_work_mode_menu_state(self): + """同步工作模式菜单状态,确保菜单选择状态与实际工作模式一致""" + try: + # 检查是否有离线模式管理器和菜单项 + if not hasattr(self.main_window, 'offline_mode_manager') or not self.main_window.offline_mode_manager: + return + + if not hasattr(self, 'online_mode_action') or not hasattr(self, 'offline_mode_action'): + return + + if not self.online_mode_action or not self.offline_mode_action: + return + + # 获取当前离线模式状态 + is_offline_mode = self.main_window.offline_mode_manager.is_in_offline_mode() + + # 同步菜单选择状态 + self.online_mode_action.setChecked(not is_offline_mode) + self.offline_mode_action.setChecked(is_offline_mode) + + # 记录同步操作(仅在调试模式下) + if hasattr(self.main_window, 'config') and self.main_window.config.get('debug_mode', False): + from utils.logger import setup_logger + logger = setup_logger("ui_manager") + logger.debug(f"已同步工作模式菜单状态: 离线模式={is_offline_mode}") + + except Exception as e: + # 静默处理异常,避免影响程序正常运行 + if hasattr(self.main_window, 'config') and self.main_window.config.get('debug_mode', False): + from utils.logger import setup_logger + logger = setup_logger("ui_manager") + logger.debug(f"同步工作模式菜单状态时出错: {e}") + diff --git a/source/main_window.py b/source/main_window.py index 2a46229..ca37dd3 100644 --- a/source/main_window.py +++ b/source/main_window.py @@ -207,6 +207,10 @@ class MainWindow(QMainWindow): else: self.window_manager.change_window_state(self.window_manager.STATE_ERROR) + # 确保工作模式菜单状态与实际状态同步 + if hasattr(self, 'ui_manager') and hasattr(self.ui_manager, 'sync_work_mode_menu_state'): + self.ui_manager.sync_work_mode_menu_state() + def set_start_button_enabled(self, enabled, installing=False): """[过渡方法] 设置按钮状态,将调用委托给WindowManager