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