From 89afc6f9d208f0adf67f7648221fb69129dc1856 Mon Sep 17 00:00:00 2001 From: kaidegit <2857693944@qq.com> Date: Thu, 12 Feb 2026 19:28:37 +0800 Subject: [PATCH] =?UTF-8?q?feat(main=5Fwindow):=20=E6=B7=BB=E5=8A=A0macOS?= =?UTF-8?q?=E9=A3=8E=E6=A0=BC=E6=A0=87=E9=A2=98=E6=A0=8F=E5=92=8C=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E6=8C=89=E9=92=AE=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为macOS平台添加自定义标题栏样式,将窗口控制按钮移至左侧,并调整导航栏布局以避开系统三色灯区域。同时添加系统标题栏区域计算功能,确保窗口操作区域正确。 --- app/view/main_window.py | 48 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/app/view/main_window.py b/app/view/main_window.py index 1ff76399..66defa74 100644 --- a/app/view/main_window.py +++ b/app/view/main_window.py @@ -1,14 +1,17 @@ import atexit import os import shutil +import sys import psutil -from PyQt5.QtCore import QSize, QThread, QUrl +from PyQt5.QtCore import QSize, QThread, QUrl, QRect, Qt from PyQt5.QtGui import QDesktopServices, QIcon -from PyQt5.QtWidgets import QApplication +from PyQt5.QtWidgets import QApplication, QLabel, QHBoxLayout from qfluentwidgets import FluentIcon as FIF from qfluentwidgets import ( FluentWindow, + FluentStyleSheet, + FluentTitleBar, InfoBar, InfoBarPosition, MessageBox, @@ -29,10 +32,40 @@ LOGO_PATH = ASSETS_PATH / "logo.png" +MAC_SYSTEM_BUTTON_WIDTH = 85 +MAC_TITLE_LEFT_MARGIN = 45 + +class MacTitleBar(FluentTitleBar): + """macOS style title bar with buttons on the left""" + + def __init__(self, parent): + super().__init__(parent) + self.setFixedHeight(48) + + self.hBoxLayout.removeWidget(self.minBtn) + self.hBoxLayout.removeWidget(self.maxBtn) + self.hBoxLayout.removeWidget(self.closeBtn) + + self.hBoxLayout.setContentsMargins(MAC_TITLE_LEFT_MARGIN, 0, 0, 0) + + self.vBoxLayout = QHBoxLayout() + self.vBoxLayout.setSpacing(0) + self.vBoxLayout.setContentsMargins(0, 0, 0, 0) + self.vBoxLayout.setAlignment(Qt.AlignTop) + self.vBoxLayout.addWidget(self.minBtn) + self.vBoxLayout.addWidget(self.maxBtn) + self.vBoxLayout.addWidget(self.closeBtn) + + FluentStyleSheet.FLUENT_WINDOW.apply(self) + class MainWindow(FluentWindow): def __init__(self): super().__init__() + + if sys.platform == "darwin": + self.setTitleBar(MacTitleBar(self)) + self.initWindow() # 创建子界面 @@ -87,6 +120,11 @@ def initNavigation(self): NavigationItemPosition.BOTTOM, ) + if sys.platform == "darwin": + # 隐藏返回按钮并避开三色灯 + self.navigationInterface.setReturnButtonVisible(False) + self.navigationInterface.panel.topLayout.setContentsMargins(4, 40, 4, 0) + # 设置默认界面 self.switchTo(self.homeInterface) @@ -177,6 +215,12 @@ def resizeEvent(self, e): if hasattr(self, "splashScreen"): self.splashScreen.resize(self.size()) + def systemTitleBarRect(self, size: QSize) -> QRect: + """Returns the system title bar rect for macOS""" + if sys.platform == "darwin": + return QRect(0, 0 if self.isFullScreen() else 9, MAC_SYSTEM_BUTTON_WIDTH, size.height()) + return super().systemTitleBarRect(size) + def closeEvent(self, event): # 关闭所有子界面 # self.homeInterface.close()