map position to frame section

This commit is contained in:
Altair Wei 2021-09-13 19:59:35 +08:00
parent 1e2598398d
commit 3784ef0e7f
2 changed files with 69 additions and 0 deletions

View File

@ -47,7 +47,15 @@ FramelessHelper::FramelessHelper(QWindow *window)
void FramelessHelper::install()
{
QRect origRect = m_window->geometry();
m_origWindowFlags = m_window->flags();
#ifdef Q_OS_MAC
m_window->setFlags(Qt::Window);
#else
m_window->setFlags(m_origWindowFlags | Qt::FramelessWindowHint);
#endif
m_window->setGeometry(origRect);
resizeWindow(origRect.size());
}
@ -56,6 +64,8 @@ void FramelessHelper::install()
*/
void FramelessHelper::uninstall()
{
m_window->setFlags(m_origWindowFlags);
m_origWindowFlags = Qt::WindowFlags();
resizeWindow(QSize());
}
@ -92,6 +102,61 @@ QRegion FramelessHelper::nonClientRegion()
return region;
}
bool FramelessHelper::isInTitlebarArea(const QPoint& pos)
{
return nonClientRegion().contains(pos);
}
Qt::WindowFrameSection FramelessHelper::mapPosToFrameSection(const QPoint& pos)
{
int border = 0;
// TODO: get system default resize border
const int sysBorder = Utilities::getSystemMetric(window(), SystemMetric::ResizeBorderThickness, false);
Qt::WindowStates states = window()->windowState();
if (!(states & Qt::WindowMaximized) && !(states & Qt::WindowFullScreen))
{
border = resizeBorderThickness();
border = qMin(border, sysBorder);
}
QRect windowRect(0, 0, windowSize().width(), windowSize().height());
if (windowRect.contains(pos))
{
QPoint mappedPos = pos - windowRect.topLeft();
if (QRect(0, 0, border, border).contains(mappedPos))
return Qt::TopLeftSection;
if (QRect(border, 0, windowRect.width() - border * 2, border).contains(mappedPos))
return Qt::TopSection;
if (QRect(windowRect.width() - border, 0, border, border).contains(mappedPos))
return Qt::TopRightSection;
if (QRect(windowRect.width() - border, border, border, windowRect.height() - border * 2).contains(mappedPos))
return Qt::RightSection;
if (QRect(windowRect.width() - border, windowRect.height() - border, border, border).contains(mappedPos))
return Qt::BottomRightSection;
if (QRect(border, windowRect.height() - border, windowRect.width() - border * 2, border).contains(mappedPos))
return Qt::BottomSection;
if (QRect(0, windowRect.height() - border, border, border).contains(mappedPos))
return Qt::BottomLeftSection;
if (QRect(0, border, border, windowRect.height() - border * 2).contains(mappedPos))
return Qt::LeftSection;
if (isInTitlebarArea(pos))
return Qt::TitleBarArea;
}
return Qt::NoSection;
}
bool FramelessHelper::eventFilter(QObject *object, QEvent *event)
{

View File

@ -67,6 +67,9 @@ public:
QRect clientRect();
QRegion nonClientRegion();
bool isInTitlebarArea(const QPoint& pos);
Qt::WindowFrameSection mapPosToFrameSection(const QPoint& pos);
protected:
bool eventFilter(QObject *object, QEvent *event) override;
@ -76,6 +79,7 @@ private:
int m_titleBarHeight;
int m_resizeBorderThickness;
bool m_resizable;
Qt::WindowFlags m_origWindowFlags;
};
FRAMELESSHELPER_END_NAMESPACE