refactor(view): 重构主窗口和自定义Fluent窗口实现

将CustomFluentWindow改为继承自MSFluentWindow,简化导航接口实现
更新导航项设置方式,使用objectName作为路由键
优化头像widget的添加方式,直接使用QIcon而非NavigationAvatarWidget
移除冗余代码,统一使用MSFluentWindow提供的方法
This commit is contained in:
2025-11-01 20:39:53 +08:00
parent f006729311
commit ebf784146e
2 changed files with 155 additions and 281 deletions

View File

@@ -53,55 +53,61 @@ class MainWindow(CustomFluentWindow):
def updateNavigation(self):
# 更新导航项文本
self.navigationInterface.setItemText(self.ownFiledInterface, lang("我的文件"))
# 使用MSFluentWindow的方法更新导航项文本
self.navigationInterface.setItemText(self.ownFiledInterface.objectName(), lang("我的文件"))
self.navigationInterface.setItemText(
self.storagespaceInterface, lang("存储配额")
self.storagespaceInterface.objectName(), lang("存储配额")
)
self.navigationInterface.setItemText(self.taskInterface, lang("任务管理"))
self.navigationInterface.setItemText(self.appInfoInterface, lang("应用信息"))
...
self.navigationInterface.setItemText(self.taskInterface.objectName(), lang("任务管理"))
self.navigationInterface.setItemText(self.appInfoInterface.objectName(), lang("应用信息"))
def initNavigation(self):
self.navigationInterface.setAcrylicEnabled(True)
self.navigationInterface.setExpandWidth(200)
logger.info("开始初始化导航界面")
# MSFluentWindow自带导航设置不需要直接设置navigationInterface
# 注意MSFluentWindow的addSubInterface方法支持selectedIcon参数
self.addSubInterface(
self.ownFiledInterface,
QIcon(":app/icons/Myfile.svg"),
lang("我的文件"),
NavigationItemPosition.TOP,
position=NavigationItemPosition.TOP,
)
self.addSubInterface(
self.storagespaceInterface,
QIcon(":app/icons/Storage.svg"),
lang("存储配额"),
NavigationItemPosition.TOP,
position=NavigationItemPosition.TOP,
)
self.addSubInterface(
self.taskInterface,
QIcon(":app/icons/Task.svg"),
lang("任务管理"),
NavigationItemPosition.TOP,
position=NavigationItemPosition.TOP,
)
self.addSubInterface(
self.appInfoInterface,
QIcon(":app/icons/Application.svg"),
lang("应用信息"),
NavigationItemPosition.BOTTOM,
position=NavigationItemPosition.BOTTOM,
)
# 创建默认头像widget先使用本地默认头像
self.avatarWidget = NavigationAvatarWidget(
userConfig.userName, ":app/images/logo.png"
)
self.navigationInterface.addWidget(
# 使用MSFluentWindow的navigationInterface添加头像widget
# 直接使用原始图标而不是avatarWidget.avatar
self.navigationInterface.addItem(
routeKey="settingInterface",
widget=self.avatarWidget,
position=NavigationItemPosition.BOTTOM,
icon=QIcon(":app/images/logo.png"),
text="", # 不显示文本,只显示头像
onClick=self.setPersonalInfoWidget,
selectable=False,
position=NavigationItemPosition.BOTTOM,
)
self.settingInterface = SettingInterface(self)
self.stackedWidget.addWidget(self.settingInterface)
@@ -132,16 +138,38 @@ class MainWindow(CustomFluentWindow):
self.taskInterface._changePivot("Download")
def setPersonalInfoWidget(self):
self.stackedWidget.setCurrentWidget(
self.stackedWidget.findChild(QWidget, "settingInterface")
)
self.navigationInterface.setCurrentItem("settingInterface")
# 使用MSFluentWindow的方式切换到设置界面
self.stackedWidget.setCurrentWidget(self.settingInterface)
# 由于设置项是通过addItem添加的非selectable项不需要设置currentItem
def onAvatarDownloaded(self, pixmap):
userConfig.setUserAvatarPixmap(pixmap)
self.avatarWidget.setAvatar(pixmap)
self.settingInterface.updateAvatar(pixmap)
# 更新导航项中的头像图标
if hasattr(self, 'navigationInterface'):
# 创建临时QIcon
icon = QIcon()
icon.addPixmap(pixmap)
# 使用QFluentWidgets的正确API更新导航项图标
# 由于NavigationBar没有setItemIcon方法我们重新创建导航项
# 先获取现有的设置项并移除
try:
# 移除现有的设置项
self.navigationInterface.removeItem("settingInterface")
# 重新添加设置项,使用新的头像图标
self.navigationInterface.addItem(
routeKey="settingInterface",
icon=icon,
text="", # 不显示文本,只显示头像
onClick=self.setPersonalInfoWidget,
selectable=False,
position=NavigationItemPosition.BOTTOM,
)
except Exception as e:
logger.error(f"更新导航头像失败: {str(e)}")
def initWindow(self):
logger.info("开始初始化窗口设置")