From 0e63a053de46c8bb2eb346cda734f558d2f77968 Mon Sep 17 00:00:00 2001 From: Altair Wei Date: Wed, 6 Oct 2021 22:05:15 +0800 Subject: [PATCH] fix examples on MacOS --- examples/mainwindow/mainwindow.cpp | 11 ++++- examples/mainwindow/mainwindow.h | 4 ++ examples/widget/widget.cpp | 13 ++++- examples/widget/widget.h | 4 ++ framelesshelper.cpp | 1 + utilities.h | 1 + utilities_macos.mm | 76 ++++++++++++++++++++++++++++++ 7 files changed, 108 insertions(+), 2 deletions(-) diff --git a/examples/mainwindow/mainwindow.cpp b/examples/mainwindow/mainwindow.cpp index 3ad1f56..5a892c4 100644 --- a/examples/mainwindow/mainwindow.cpp +++ b/examples/mainwindow/mainwindow.cpp @@ -25,6 +25,7 @@ #include "mainwindow.h" #include #include "../../framelesswindowsmanager.h" +#include "../../utilities.h" FRAMELESSHELPER_USE_NAMESPACE @@ -44,7 +45,6 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(par QMenuBar *mb = menuBar(); titleBarWidget->horizontalLayout->insertWidget(1, mb); - setMenuWidget(m_titleBar); connect(this, &MainWindow::windowIconChanged, titleBarWidget->iconButton, &QPushButton::setIcon); @@ -101,7 +101,14 @@ void MainWindow::showEvent(QShowEvent *event) m_helper->setTitleBarHeight(m_titleBar->height()); m_helper->setResizeBorderThickness(4); m_helper->install(); +#ifndef Q_OS_MAC setContentsMargins(1, 1, 1, 1); +#else // Q_OS_MAC + titleBarWidget->minimizeButton->hide(); + titleBarWidget->maximizeButton->hide(); + titleBarWidget->closeButton->hide(); + Utilities::showMacWindowButton(windowHandle()); +#endif // Q_OS_MAC inited = true; } } @@ -121,6 +128,7 @@ bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *r } #endif // Q_OS_WIN +#ifndef Q_OS_MAC void MainWindow::changeEvent(QEvent *event) { QWidget::changeEvent(event); @@ -165,3 +173,4 @@ void MainWindow::paintEvent(QPaintEvent *event) painter.restore(); } } +#endif // Q_OS_MAC \ No newline at end of file diff --git a/examples/mainwindow/mainwindow.h b/examples/mainwindow/mainwindow.h index febafc4..70ad3eb 100644 --- a/examples/mainwindow/mainwindow.h +++ b/examples/mainwindow/mainwindow.h @@ -39,8 +39,12 @@ public: protected: void showEvent(QShowEvent *event) override; + +#ifndef Q_OS_MAC void paintEvent(QPaintEvent *event) override; void changeEvent(QEvent *event) override; +#endif // Q_OS_MAC + #ifdef Q_OS_WIN bool nativeEvent(const QByteArray &eventType, void *message, long *result) override; #endif // Q_OS_WIN diff --git a/examples/widget/widget.cpp b/examples/widget/widget.cpp index be98da9..f0f925b 100644 --- a/examples/widget/widget.cpp +++ b/examples/widget/widget.cpp @@ -107,9 +107,15 @@ void Widget::showEvent(QShowEvent *event) m_helper->setResizeBorderThickness(4); m_helper->setTitleBarHeight(m_titleBarWidget->height()); m_helper->install(); - +#ifndef Q_OS_MAC const int margin = Utilities::getWindowVisibleFrameBorderThickness(winId()); setContentsMargins(margin, margin, margin, margin); +#else // Q_OS_MAC + m_minimizeButton->hide(); + m_maximizeButton->hide(); + m_closeButton->hide(); + Utilities::showMacWindowButton(windowHandle()); +#endif // Q_OS_MAC } } @@ -121,6 +127,7 @@ void Widget::timerEvent(QTimerEvent *event) } } +#ifndef Q_OS_MAC void Widget::changeEvent(QEvent *event) { QWidget::changeEvent(event); @@ -168,6 +175,7 @@ void Widget::paintEvent(QPaintEvent *event) painter.restore(); } } +#endif // Q_OS_MAC void Widget::setupUi() { @@ -221,6 +229,9 @@ void Widget::setupUi() titleBarLayout->setContentsMargins(0, 0, 0, 0); titleBarLayout->setSpacing(0); titleBarLayout->addSpacerItem(new QSpacerItem(10, 10)); +#ifdef Q_OS_MAC + titleBarLayout->addStretch(); +#endif // Q_OS_MAC titleBarLayout->addWidget(m_windowTitleLabel); titleBarLayout->addStretch(); titleBarLayout->addWidget(m_minimizeButton); diff --git a/examples/widget/widget.h b/examples/widget/widget.h index 2668eb6..bd86c10 100644 --- a/examples/widget/widget.h +++ b/examples/widget/widget.h @@ -43,8 +43,12 @@ public: protected: void showEvent(QShowEvent *event) override; void timerEvent(QTimerEvent *event) override; + +#ifndef Q_OS_MAC void changeEvent(QEvent *event) override; void paintEvent(QPaintEvent *event) override; +#endif // Q_OS_MAC + #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) bool nativeEvent(const QByteArray &eventType, void *message, qintptr *result) override; #else diff --git a/framelesshelper.cpp b/framelesshelper.cpp index 7fa9416..e042f1a 100644 --- a/framelesshelper.cpp +++ b/framelesshelper.cpp @@ -119,6 +119,7 @@ void FramelessHelper::uninstall() #ifdef Q_OS_MAC Utilities::unsetMacWindowHook(m_window); + Utilities::unsetMacWindowFrameless(m_window); #endif #ifdef Q_OS_WIN diff --git a/utilities.h b/utilities.h index 9e2b154..ccf0d12 100644 --- a/utilities.h +++ b/utilities.h @@ -72,6 +72,7 @@ FRAMELESSHELPER_API unsigned int getX11CursorForFrameSection(Qt::WindowFrameSect FRAMELESSHELPER_API bool setMacWindowHook(QWindow* w); FRAMELESSHELPER_API bool unsetMacWindowHook(QWindow* w); FRAMELESSHELPER_API bool setMacWindowFrameless(QWindow* w); +FRAMELESSHELPER_API bool unsetMacWindowFrameless(QWindow* w); FRAMELESSHELPER_API bool startMacDrag(QWindow* w, const QPoint& pos); FRAMELESSHELPER_API Qt::MouseButtons getMacMouseButtons(); FRAMELESSHELPER_API bool showMacWindowButton(QWindow *w); diff --git a/utilities_macos.mm b/utilities_macos.mm index fcbb7ac..6141aad 100644 --- a/utilities_macos.mm +++ b/utilities_macos.mm @@ -71,6 +71,55 @@ int getSystemMetric(const QWindow *window, const SystemMetric metric, const bool return 0; } + +QColor getColorizationColor() +{ + // ### TO BE IMPLEMENTED + return Qt::darkGray; +} + +int getWindowVisibleFrameBorderThickness(const WId winId) +{ + // ### TO BE IMPLEMENTED + Q_UNUSED(winId); + return 1; +} + +bool shouldAppsUseDarkMode() +{ + // ### TO BE IMPLEMENTED + return false; +} + +ColorizationArea getColorizationArea() +{ + // ### TO BE IMPLEMENTED + return ColorizationArea::NoArea; +} + +bool isThemeChanged(const void *data) +{ + // ### TO BE IMPLEMENTED + Q_UNUSED(data); + return false; +} + +bool isSystemMenuRequested(const void *data, QPointF *pos) +{ + // ### TO BE IMPLEMENTED + Q_UNUSED(data); + Q_UNUSED(pos); + return false; +} + +bool showSystemMenu(const WId winId, const QPointF &pos) +{ + // ### TO BE IMPLEMENTED + Q_UNUSED(winId); + Q_UNUSED(pos); + return false; +} + static QList gFlsWindows; static bool gNSWindowOverrode = false; @@ -272,6 +321,33 @@ bool setMacWindowFrameless(QWindow* w) return true; } +bool unsetMacWindowFrameless(QWindow* w) +{ + NSView* view = reinterpret_cast(w->winId()); + if (view == nullptr) + return false; + NSWindow* nswindow = [view window]; + if (nswindow == nullptr) + return false; + + view.wantsLayer = NO; + + nswindow.styleMask = nswindow.styleMask & ~NSWindowStyleMaskFullSizeContentView; + nswindow.titlebarAppearsTransparent = false; + nswindow.titleVisibility = NSWindowTitleVisible; + nswindow.hasShadow = true; + + nswindow.movableByWindowBackground = false; + nswindow.movable = true; + + nswindow.showsToolbarButton = true; + [nswindow standardWindowButton:NSWindowCloseButton].hidden = false; + [nswindow standardWindowButton:NSWindowMiniaturizeButton].hidden = false; + [nswindow standardWindowButton:NSWindowZoomButton].hidden = false; + + return true; +} + bool showMacWindowButton(QWindow *w) { NSView* view = reinterpret_cast(w->winId());