fix examples on MacOS

This commit is contained in:
Altair Wei 2021-10-06 22:05:15 +08:00
parent 420d942762
commit 0e63a053de
7 changed files with 108 additions and 2 deletions

View File

@ -25,6 +25,7 @@
#include "mainwindow.h"
#include <QtGui/qpainter.h>
#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

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -119,6 +119,7 @@ void FramelessHelper::uninstall()
#ifdef Q_OS_MAC
Utilities::unsetMacWindowHook(m_window);
Utilities::unsetMacWindowFrameless(m_window);
#endif
#ifdef Q_OS_WIN

View File

@ -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);

View File

@ -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<NSWindow*> gFlsWindows;
static bool gNSWindowOverrode = false;
@ -272,6 +321,33 @@ bool setMacWindowFrameless(QWindow* w)
return true;
}
bool unsetMacWindowFrameless(QWindow* w)
{
NSView* view = reinterpret_cast<NSView *>(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<NSView *>(w->winId());